Debian Codegraph @

renderer/HardwareDraw.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "3d.h"
20 #include "HardwareInternal.h"
21 #include "renderer.h"
22
23 #define round(v) ((int)(((v) + 0.5f)))
24
25 // draws a line. takes two points. returns true if drew
26 void g3_DrawLine(ddgr_color color, g3Point *p0, g3Point *p1) {
27 uint8_t codes_or;
28 bool was_clipped = 0;
29
30 if (p0->p3_codes & p1->p3_codes)
31 return;
32
33 codes_or = p0->p3_codes | p1->p3_codes;
34
35 if (codes_or) {
36 ClipLine(&p0, &p1, codes_or);
37 was_clipped = 1;
38 }
39
40 if (!(p0->p3_flags & PF_PROJECTED)) {
41 g3_ProjectPoint(p0);
42 }
43
44 if (!(p1->p3_flags & PF_PROJECTED)) {
45 g3_ProjectPoint(p1);
46 }
47
48 rend_SetFlatColor(color);
49 rend_DrawLine(round(p0->p3_sx), round(p0->p3_sy), round(p1->p3_sx), round(p1->p3_sy));
50
51 // If was clipped, free temp points
52 if (was_clipped) {
53 if (p0->p3_flags & PF_TEMP_POINT) {
54 FreeTempPoint(p0);
55 }
56
57 if (p1->p3_flags & PF_TEMP_POINT) {
58 FreeTempPoint(p1);
59 }
60
61 // Make sure all temp points have been freed
62 CheckTempPoints();
63 }
64 }
65
66 // draws a line based on the current setting of render states. takes two points. returns true if drew
67 void g3_DrawSpecialLine(g3Point *p0, g3Point *p1) {
68 uint8_t codes_or;
69 bool was_clipped = 0;
70
71 if (p0->p3_codes & p1->p3_codes)
72 return;
73
74 codes_or = p0->p3_codes | p1->p3_codes;
75
76 if (codes_or) {
77
78 ClipLine(&p0, &p1, codes_or);
79
80 was_clipped = 1;
81 }
82
83 if (!(p0->p3_flags & PF_PROJECTED))
84 g3_ProjectPoint(p0);
85
86 if (!(p1->p3_flags & PF_PROJECTED))
87 g3_ProjectPoint(p1);
88
89 rend_DrawSpecialLine(p0, p1);
90
91 // If was clipped, free temp points
92 if (was_clipped) {
93 if (p0->p3_flags & PF_TEMP_POINT)
94 FreeTempPoint(p0);
95
96 if (p1->p3_flags & PF_TEMP_POINT)
97 FreeTempPoint(p1);
98
99 // Make sure all temp points have been freed
100 CheckTempPoints();
101 }
102 }
103
104 // returns true if a plane is facing the viewer. takes the unrotated surface
105 // normal of the plane, and a point on it. The normal need not be normalized
106 bool g3_CheckNormalFacing(vector *v, vector *norm) {
107 vector tempv;
108
109 tempv = View_position - *v;
110
111 return ((tempv * *norm) > 0);
112 }
113
114 bool DoFacingCheck(vector *norm, g3Point **vertlist, vector *p) {
115 if (norm) {
116 // have normal
117 ASSERT(norm->x || norm->y || norm->z);
118 return g3_CheckNormalFacing(p, norm);
119 } else {
120 // normal not specified, so must compute
121 vector tempv;
122 // get three points (rotated) and compute normal
123 vm_GetPerp(&tempv, &vertlist[0]->p3_vec, &vertlist[1]->p3_vec, &vertlist[2]->p3_vec);
124 return ((tempv * vertlist[1]->p3_vec) < 0);
125 }
126 }
127
128 // like g3_DrawPoly(), but checks to see if facing. If surface normal is
129 // NULL, this routine must compute it, which will be slow. It is better to
130 // pre-compute the normal, and pass it to this function. When the normal
131 // is passed, this function works like g3_CheckNormalFacing() plus
132 // g3_DrawPoly().
133 void g3_CheckAndDrawPoly(int nv, g3Point **pointlist, int bm, vector *norm, vector *pnt) {
134 if (DoFacingCheck(norm, pointlist, pnt))
135 g3_DrawPoly(nv, pointlist, bm);
136 }
137
138 int Triangulate_test = 0;
139
140 // draw a polygon
141 // Parameters: nv - the number of verts in the poly
142 // pointlist - a pointer to a list of pointers to points
143 // bm - the bitmap handle if texturing. ignored if flat shading
144 // Returns 0 if clipped away
145 int g3_DrawPoly(int nv, g3Point **pointlist, int bm, int map_type, g3Codes *clip_codes) {
146 rend_DrawPolygon3D(bm, pointlist, nv, map_type);
147 return 1;
148
149 /*
150 int i;
151 g3Codes cc;
152 bool was_clipped=0;
153
154 if( Triangulate_test && (nv > 3) )
155 {
156 g3Point *tripoints[3];
157 int sum=0;
158
159 for (i=0;i<nv-2;i++)
160 {
161 tripoints[0] = pointlist[0];
162 tripoints[1] = pointlist[i+1];
163 tripoints[2] = pointlist[i+2];
164 sum += g3_DrawPoly( 3, tripoints, bm, map_type );
165 }
166
167 return sum;
168 }
169
170 //Initialize or just used the ones passed in
171 if( clip_codes )
172 {
173 cc = *clip_codes;
174 }
175 else
176 {
177 cc.cc_or = 0;
178 cc.cc_and = 0xff;
179
180 //Get codes for this polygon, and copy uvls into points
181 for( i = 0; i < nv; ++i )
182 {
183 uint8_t c = pointlist[i]->p3_codes;
184 cc.cc_and &= c;
185 cc.cc_or |= c;
186 }
187 }
188
189 //All points off screen?
190 if( cc.cc_and )
191 return 0;
192
193 //One or more point off screen, so clip
194 if( cc.cc_or )
195 {
196 //Clip the polygon, getting pointer to new buffer
197 pointlist = g3_ClipPolygon( pointlist, &nv, &cc );
198
199 //Flag as clipped so temp points will be freed
200 was_clipped = 1;
201
202 //Check for polygon clipped away, or clip otherwise failed
203 if( (nv==0) || (cc.cc_or&CC_BEHIND) || cc.cc_and )
204 goto free_points;
205 }
206
207 //Make list of 2d coords (& check for overflow)
208 for( i = 0; i < nv; ++i )
209 {
210 g3Point *p = pointlist[i];
211
212 //Project if needed
213 if( !(p->p3_flags&PF_PROJECTED) )
214 {
215 g3_ProjectPoint(p);
216 }
217 }
218
219 //Draw!
220 rend_DrawPolygon3D( bm, pointlist, nv, map_type );
221
222 free_points:;
223
224 //If was clipped, free temp points
225 if( was_clipped )
226 {
227 g3_FreeTempPoints( pointlist, nv );
228 }
229
230 return 1;
231 */
232 }
233
234 // draw a sortof sphere - i.e., the 2d radius is proportional to the 3d
235 // radius, but not to the distance from the eye
236 void g3_DrawSphere(ddgr_color color, g3Point *pnt, float rad) {
237 if (!(pnt->p3_codes & CC_BEHIND)) {
238 if (!(pnt->p3_flags & PF_PROJECTED))
239 g3_ProjectPoint(pnt);
240
241 rend_FillCircle(color, pnt->p3_sx, pnt->p3_sy, (rad * Matrix_scale.x * Window_w2 / pnt->p3_z));
242 }
243 }
244
245 // draws a bitmap with the specified 3d width & height
246 // If offsets are not -1, then the blitter draws not from the upper left hand
247 // corner of the bitmap, but from size*offsetx,size*offsety
248 // See Jason for explaination
249 void g3_DrawBitmap(vector *pos, float width, float height, int bm, int color) {
250 // get the view orientation
251 matrix viewOrient;
252 g3_GetUnscaledMatrix(&viewOrient);
253
254 // break down the color into components
255 float r, g, b;
256 if (color != -1) {
257 float scale = 1.0f / 255.0f;
258 r = GR_COLOR_RED(color) * scale;
259 g = GR_COLOR_GREEN(color) * scale;
260 b = GR_COLOR_BLUE(color) * scale;
261 }
262
263 // calculate the four corners
264 g3Point corners[4], *pts[4];
265 int i;
266 for (i = 0; i < 4; ++i) {
267 pts[i] = &corners[i];
268
269 // calculate the offset for this corner
270 float cornerScaleU = ((i & 1) ^ ((i & 2) >> 1)) ? 1.0f : -1.0f;
271 float cornerScaleV = (i & 2) ? 1.0f : -1.0f;
272
273 // find the point (parallel to the view frame)
274 vector cornerPos = *pos + (viewOrient.uvec * (height * -cornerScaleV)) + (viewOrient.rvec * (width * cornerScaleU));
275 corners[i].p3_codes = 0;
276 g3_RotatePoint(pts[i], &cornerPos);
277
278 // setup the flags, UVs and colors
279 corners[i].p3_flags |= PF_UV;
280 corners[i].p3_uvl.u = (cornerScaleU * 0.5f) + 0.5f;
281 corners[i].p3_uvl.v = (cornerScaleV * 0.5f) + 0.5f;
282 if (color == -1) {
283 corners[i].p3_flags |= PF_L;
284 corners[i].p3_uvl.l = 1.0f;
285 } else {
286 corners[i].p3_flags |= PF_RGBA;
287 corners[i].p3_uvl.r = r;
288 corners[i].p3_uvl.g = g;
289 corners[i].p3_uvl.b = b;
290 }
291 corners[i].p3_uvl.a = 1.0f;
292 }
293
294 rend_SetTextureType(TT_LINEAR);
295 rend_DrawPolygon3D(bm, pts, 4);
296 }
297
298 // Draws a bitmap that has been rotated about its center. Angle of rotation is passed as 'rot_angle'
299 void g3_DrawRotatedBitmap(vector *pos, angle rot_angle, float width, float height, int bm, int color) {
300 // get the view orientation
301 matrix viewOrient;
302 g3_GetUnscaledMatrix(&viewOrient);
303
304 matrix rot_matrix;
305 vm_AnglesToMatrix(&rot_matrix, 0, 0, rot_angle);
306
307 float w = width;
308 float h = height;
309
310 vector rot_vectors[4];
311 rot_vectors[0].x = -w;
312 rot_vectors[0].y = h;
313
314 rot_vectors[1].x = w;
315 rot_vectors[1].y = h;
316
317 rot_vectors[2].x = w;
318 rot_vectors[2].y = -h;
319
320 rot_vectors[3].x = -w;
321 rot_vectors[3].y = -h;
322
323 g3Point rot_points[8], *pntlist[8];
324 int i;
325 for (i = 0; i < 4; ++i) {
326 vector offset;
327 rot_vectors[i].z = 0.0f;
328 vm_MatrixMulVector(&offset, &rot_vectors[i], &rot_matrix);
329
330 vector cornerPos = *pos + (viewOrient.uvec * offset.y) + (viewOrient.rvec * offset.x);
331 rot_points[i].p3_codes = 0;
332 g3_RotatePoint(&rot_points[i], &cornerPos);
333
334 rot_points[i].p3_flags |= PF_UV | PF_RGBA;
335 rot_points[i].p3_l = 1.0f;
336 rot_points[i].p3_uvl.u = ((i & 1) ^ ((i & 2) >> 1)) ? 1.0f : 0.0f;
337 rot_points[i].p3_uvl.v = (i & 2) ? 1.0f : 0.0f;
338
339 pntlist[i] = &rot_points[i];
340 }
341
342 // And draw!!
343 rend_SetTextureType(TT_LINEAR);
344
345 if (color != -1) {
346 rend_SetLighting(LS_FLAT_GOURAUD);
347 rend_SetFlatColor(color);
348 }
349
350 g3_DrawPoly(4, pntlist, bm);
351 }
352
353 // Draws a bitmap on a specific plane. Also does rotation. Angle of rotation is passed as 'rot_angle'
354 void g3_DrawPlanarRotatedBitmap(vector *pos, vector *norm, angle rot_angle, float width, float height, int bm) {
355 matrix rot_matrix;
356 vm_VectorToMatrix(&rot_matrix, norm, NULL, NULL);
357 vm_TransposeMatrix(&rot_matrix);
358
359 matrix twist_matrix;
360 vm_AnglesToMatrix(&twist_matrix, 0, 0, rot_angle);
361
362 float w = width;
363 float h = height;
364
365 vector rot_vectors[4];
366 rot_vectors[0] = (twist_matrix.rvec * -w);
367 rot_vectors[0] += (twist_matrix.uvec * h);
368
369 rot_vectors[1] = (twist_matrix.rvec * w);
370 rot_vectors[1] += (twist_matrix.uvec * h);
371
372 rot_vectors[2] = (twist_matrix.rvec * w);
373 rot_vectors[2] -= (twist_matrix.uvec * h);
374
375 rot_vectors[3] = (twist_matrix.rvec * -w);
376 rot_vectors[3] -= (twist_matrix.uvec * h);
377
378 int i;
379 for (i = 0; i < 4; ++i) {
380 vector temp_vec = rot_vectors[i];
381 vm_MatrixMulVector(&rot_vectors[i], &temp_vec, &rot_matrix);
382 }
383
384 g3Point rot_points[8], *pntlist[8];
385 for (i = 0; i < 4; ++i) {
386 rot_vectors[i] += *pos;
387
388 g3_RotatePoint(&rot_points[i], &rot_vectors[i]);
389 rot_points[i].p3_flags |= PF_UV | PF_L;
390 rot_points[i].p3_l = 1.0f;
391
392 pntlist[i] = &rot_points[i];
393 }
394
395 rot_points[0].p3_u = 0.0f;
396 rot_points[0].p3_v = 0.0f;
397
398 rot_points[1].p3_u = 1.0f;
399 rot_points[1].p3_v = 0.0f;
400
401 rot_points[2].p3_u = 1.0f;
402 rot_points[2].p3_v = 1.0f;
403
404 rot_points[3].p3_u = 0.0f;
405 rot_points[3].p3_v = 1.0f;
406
407 // And draw!!
408 rend_SetTextureType(TT_LINEAR);
409 g3_DrawPoly(4, pntlist, bm);
410 }
411
412 // Draw a wireframe box aligned with the screen. Used for the editor.
413 // Parameters: color - the color to draw the lines
414 // pnt - the center point
415 // rad - specifies the width/2 & height/2 of the box
416 void g3_DrawBox(ddgr_color color, g3Point *pnt, float rad) {
417 if (!(pnt->p3_codes & CC_BEHIND)) {
418 if (!(pnt->p3_flags & PF_PROJECTED))
419 g3_ProjectPoint(pnt);
420
421 float w, h;
422
423 w = rad * Matrix_scale.x * Window_w2 / pnt->p3_z;
424 h = rad * Matrix_scale.y * Window_h2 / pnt->p3_z;
425
426 rend_DrawLine(round(pnt->p3_sx - w), round(pnt->p3_sy - h), round(pnt->p3_sx + w), round(pnt->p3_sy - h));
427 rend_DrawLine(round(pnt->p3_sx + w), round(pnt->p3_sy - h), round(pnt->p3_sx + w), round(pnt->p3_sy + h));
428 rend_DrawLine(round(pnt->p3_sx + w), round(pnt->p3_sy + h), round(pnt->p3_sx - w), round(pnt->p3_sy + h));
429 rend_DrawLine(round(pnt->p3_sx - w), round(pnt->p3_sy + h), round(pnt->p3_sx - w), round(pnt->p3_sy - h));
430 }
431 }
432
433 // Sets the triangulation test to on or off
434 void g3_SetTriangulationTest(int state) { Triangulate_test = state; }