102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
clipping.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
14#include "pcdogs/math/types.h"
17#include <math.h>
18#include <stdarg.h>
19#include <string.h>
20
21// Original pcdogs.exe: 0x415b20
22// Clips a polygon and its attributes against one plane and reports whether at least three vertices remain. New intersections leave the reserved vertex float unchanged, and output capacity is not checked.
23BOOL Graphics_ClipPolygonByPlane(struct Graphics_Render_ClipVertex *input_vertices, struct Graphics_Render_ClipAttribute *input_attributes, struct Graphics_Render_ClipPlane *clip_plane, struct Graphics_Render_ClipVertex *output_vertices, struct Graphics_Render_ClipAttribute *output_attributes, int32_t *in_out_vertex_count)
24{
25 int input_count = *in_out_vertex_count, output_count = 0;
26
27 if (input_count <= 0)
28 {
29 *in_out_vertex_count = 0;
30
31 return 0;
32 }
33
34 const struct Graphics_Render_ClipVertex *current = input_vertices;
35 long double distance = (long double)current->pos.y * clip_plane->normal.y + (long double)current->pos.z * clip_plane->normal.z + (long double)current->pos.x * clip_plane->normal.x;
36 float previous_distance = (float)distance;
37 int inside = distance >= (long double)clip_plane->distance;
38
39 for (int i = 0; i < input_count; ++i)
40 {
41 int next_vertex_index = (i + 1) % input_count;
42
43 current = &input_vertices[i];
44
45 const struct Graphics_Render_ClipVertex *next = &input_vertices[next_vertex_index];
46
47 if (inside)
48 {
49 output_vertices[output_count] = *current;
50 output_attributes[output_count++] = input_attributes[i];
51 }
52
53 distance = (long double)next->pos.y * clip_plane->normal.y + (long double)next->pos.x * clip_plane->normal.x + (long double)next->pos.z * clip_plane->normal.z;
54
55 int next_inside = distance >= (long double)clip_plane->distance;
56
57 if (inside != next_inside)
58 {
59 long double delta = distance - (long double)previous_distance;
60
61 if (delta != 0.0L && !isnan(delta))
62 {
63 long double intersection_fraction = ((long double)clip_plane->distance - previous_distance) / delta;
64 /* The original writes position, reciprocal W and UV, leaving the
65 reserved fifth float unchanged on newly inserted vertices. */
66 float *intersection_components = (float *)&output_vertices[output_count];
67 const float *current_components = (const float *)current, *next_components = (const float *)next;
68
69 for (int k = 0; k < 7; ++k)
70 if (k != 4)
71 intersection_components[k] = (float)(((long double)next_components[k] - current_components[k]) * intersection_fraction + current_components[k]);
72
73 for (int k = 0; k < 3; ++k)
74 output_attributes[output_count].components[k] = (float)(((long double)input_attributes[next_vertex_index].components[k] - input_attributes[i].components[k]) * intersection_fraction + input_attributes[i].components[k]);
75
76 ++output_count;
77 }
78 }
79
80 previous_distance = (float)distance;
81 inside = next_inside;
82 }
83
84 *in_out_vertex_count = output_count;
85
86 return output_count >= 3;
87}
88
89// Original pcdogs.exe: 0x415d40
90// Copies the input polygon to scratch storage and clips it against four camera side planes, alternating scratch banks. The separate z-near plane is not visited, and scratch capacity is not checked.
91BOOL Graphics_ClipPolygonByCameraPyramid(struct Graphics_Render_ClipVertex *input_vertices, struct Graphics_Render_ClipAttribute *input_attributes, struct Graphics_Render_ClipVertex *output_vertices, struct Graphics_Render_ClipAttribute *output_attributes, int32_t *in_out_vertex_count)
92{
93 /* Copy 28-byte vertices and 12-byte attributes. Each destination pair uses
94 the same bank before toggling it for the next side-plane clip. */
95 struct Graphics_Render_ClipVertex *vertices = (void *)&g_clipTempVertexBuffer;
96 struct Graphics_Render_ClipAttribute *attributes = (void *)&g_clipTempUVBuffer;
97
98 for (int32_t i = 0; i < *in_out_vertex_count; ++i)
99 {
100 memcpy(&vertices[i], &input_vertices[i], 28);
101 memcpy(&attributes[i], &input_attributes[i], 12);
102 }
103
104 unsigned bank = 0;
105
106 for (unsigned plane = 0; plane < 3; ++plane)
107 {
110
111 if (!Graphics_ClipPolygonByPlane(vertices, attributes,
112 &camera_clip_planes[plane],
113 next_vertices, next_attributes, in_out_vertex_count))
114 return 0;
115
116 vertices = next_vertices;
117 attributes = next_attributes;
118 bank ^= 1;
119 }
120
121 return Graphics_ClipPolygonByPlane(vertices, attributes, &camera_near_clip_plane,
122 output_vertices, output_attributes, in_out_vertex_count);
123}
124
125// Original pcdogs.exe: 0x4160a0
126// Snaps to the first screen vertex with each difference below two pixels; native unordered comparisons also match.
127void Graphics_SnapPointToFirstNearbyVertex(float *x, float *y, struct Graphics_Render_ProjectedVertex *screen_vertices, int32_t vertex_count)
128{
129 for (int i = 0; i < vertex_count; ++i)
130 {
131 int32_t screen_x = screen_vertices[i].screen.x;
132 int32_t screen_y = screen_vertices[i].screen.y;
133 float dy;
134 const float zero = 0.0f, threshold = 2.0f;
135 uint16_t status;
136
137 /* Native FCOM tests C0 alone, accepting unordered as well as less.
138 Keep its comparison order, float32 Y store and signaling behavior;
139 fabs/isnan would change the x87 status for exceptional inputs. */
140 __asm__ volatile(
141 "fildl %[sx]\n\t"
142 "fsubrs %[x]\n\t"
143 "fildl %[sy]\n\t"
144 "fsubrs %[y]\n\t"
145 "fstps %[dy]\n\t"
146 "fcoms %[zero]\n\t"
147 "fnstsw %%ax\n\t"
148 "testb $1, %%ah\n\t"
149 "jz 1f\n\t"
150 "fchs\n"
151 "1:\n\t"
152 "flds %[dy]\n\t"
153 "fcomps %[zero]\n\t"
154 "fnstsw %%ax\n\t"
155 "testb $1, %%ah\n\t"
156 "jz 2f\n\t"
157 "flds %[dy]\n\t"
158 "fchs\n\t"
159 "fstps %[dy]\n"
160 "2:\n\t"
161 "fcomps %[threshold]\n\t"
162 "fnstsw %%ax\n\t"
163 "testb $1, %%ah\n\t"
164 "jz 3f\n\t"
165 "flds %[dy]\n\t"
166 "fcomps %[threshold]\n\t"
167 "fnstsw %%ax\n"
168 "3:"
169 : "=a"(status), [dy] "=m"(dy)
170 : [sx] "m"(screen_x), [sy] "m"(screen_y),
171 [x] "m"(*x), [y] "m"(*y), [zero] "m"(zero),
172 [threshold] "m"(threshold)
173 : "st", "st(1)", "cc");
174
175 if (status & X87_STATUS_C0)
176 {
177 *x = screen_vertices[i].screen.x;
178 *y = screen_vertices[i].screen.y;
179
180 return;
181 }
182 }
183}
184
185// Original pcdogs.exe: 0x416160
186// Clips and projects a triangle or quad, applies color and texture attributes, and submits surviving triangle fans to Direct3D. Shared scratch-buffer capacity is not checked.
187void Graphics_ClipAndDrawPolygon(struct Graphics_Render_PolygonBatchRecord *batch, struct D3D_TransformedVertex const *prepared_vertices, uint8_t alpha_byte, int32_t unused_batch_alias, int32_t brighten_or_clipped_count)
188{
189 (void)unused_batch_alias;
190 float signed_depth_bias = -9.99999975e-05f;
191
192 // Check if quad has 4 vertices by testing quad_data+0x2c field
193 int32_t has_fourth_vertex = (uint8_t)(batch->screen_vertices[3].packed_screen != GRAPHICS_TRIANGLE_FOURTH_VERTEX);
194 int32_t input_count_snapshot = has_fourth_vertex + 3;
195 int32_t input_vertex_count = input_count_snapshot;
196
197 // Determine depth bias direction based on quad_data+0x68 sign
198 if (batch->face_normal_dot >= 0)
199 signed_depth_bias = 9.99999975e-05f;
200
201 // Start frustum culling test: Check all vertices against near plane (g_nearPlaneDistance)
202 long double first_zero_test_depth = (long double)batch->view_vertices[0].z;
203 long double first_zero_threshold = (long double)0.0f;
204
205 if (!(first_zero_test_depth >= first_zero_threshold))
206 {
207 long double second_zero_test_depth = (long double)batch->view_vertices[1].z;
208 long double second_zero_threshold = (long double)0.0f;
209
210 if (!(second_zero_test_depth >= second_zero_threshold))
211 {
212 long double third_zero_test_depth = (long double)batch->view_vertices[2].z;
213 long double third_zero_threshold = (long double)0.0f;
214
215 if (!(third_zero_test_depth >= third_zero_threshold))
216 {
217 if (input_count_snapshot == 3)
218 return;
219
220 long double fourth_zero_test_depth = (long double)batch->view_vertices[3].z;
221 long double fourth_zero_threshold = (long double)0.0f;
222
223 if (!(fourth_zero_test_depth >= fourth_zero_threshold))
224 return;
225 }
226 }
227 }
228
229 // Check all vertices against far plane (g_farPlaneDistance)
230 long double first_clip_test_depth = (long double)batch->view_vertices[0].z;
231 long double first_clip_distance = (long double)*(float *)&Camera_SetupClipPlanes_NearClipDistanceSource;
232
233 if (first_clip_test_depth >= first_clip_distance)
234 {
235 long double second_clip_test_depth = (long double)batch->view_vertices[1].z;
236 long double second_clip_distance = (long double)*(float *)&Camera_SetupClipPlanes_NearClipDistanceSource;
237
238 if (second_clip_test_depth >= second_clip_distance)
239 {
240 long double third_clip_test_depth = (long double)batch->view_vertices[2].z;
241 long double third_clip_distance = (long double)*(float *)&Camera_SetupClipPlanes_NearClipDistanceSource;
242
243 if (third_clip_test_depth >= third_clip_distance)
244 {
245 int16_t first_screen_axis = batch->screen_vertices[0].screen.x;
246
247 if (!(first_screen_axis >= 0 || batch->screen_vertices[1].screen.x >= 0 || batch->screen_vertices[2].screen.x >= 0))
248 {
249 if (input_count_snapshot == 3)
250 return;
251
252 if (!(batch->screen_vertices[3].screen.x >= 0))
253 {
254 long double left_test_depth = (long double)batch->view_vertices[3].z;
255 long double left_clip_distance = (long double)*(float *)&Camera_SetupClipPlanes_NearClipDistanceSource;
256
257 if (left_test_depth >= left_clip_distance)
258 return;
259 }
260 }
261
262 if (!(first_screen_axis <= 0x27f || batch->screen_vertices[1].screen.x <= 0x27f || batch->screen_vertices[2].screen.x <= 0x27f))
263 {
264 if (input_count_snapshot == 3)
265 return;
266
267 if (!(batch->screen_vertices[3].screen.x <= 0x27f))
268 {
269 long double right_test_depth = (long double)batch->view_vertices[3].z;
270 long double right_clip_distance = (long double)*(float *)&Camera_SetupClipPlanes_NearClipDistanceSource;
271
272 if (right_test_depth >= right_clip_distance)
273 return;
274 }
275 }
276
277 first_screen_axis = batch->screen_vertices[0].screen.y;
278
279 if (!(first_screen_axis >= 0 || batch->screen_vertices[1].screen.y >= 0 || batch->screen_vertices[2].screen.y >= 0))
280 {
281 if (input_count_snapshot == 3)
282 return;
283
284 if (!(batch->screen_vertices[3].screen.y >= 0))
285 {
286 long double top_test_depth = (long double)batch->view_vertices[3].z;
287 long double top_clip_distance =
288 (long double)*(float *)&Camera_SetupClipPlanes_NearClipDistanceSource;
289
290 if (top_test_depth >= top_clip_distance)
291 return;
292 }
293 }
294
295 if (!(first_screen_axis <= 0x1df || batch->screen_vertices[1].screen.y <= 0x1df || batch->screen_vertices[2].screen.y <= 0x1df))
296 {
297 if (input_count_snapshot == 3)
298 return;
299
300 if (!(batch->screen_vertices[3].screen.y <= 0x1df))
301 {
302 long double bottom_test_depth = (long double)batch->view_vertices[3].z;
303 long double bottom_clip_distance =
304 (long double)*(float *)&Camera_SetupClipPlanes_NearClipDistanceSource;
305
306 if (bottom_test_depth >= bottom_clip_distance)
307 return;
308 }
309 }
310 }
311 }
312 }
313
314 int32_t input_vertex_index = 0;
315 // Quad passes basic culling - begin vertex processing. Determine D3DFVF format based on g_currentVertexFormat
316 DWORD vertex_format_flags = ((g_currentVertexFormat != GRAPHICS_VERTEX_LAYOUT_COLOR) ? (DWORD)(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1) : (DWORD)(D3DFVF_XYZRHW | D3DFVF_DIFFUSE));
317
318 if (input_count_snapshot > 0)
319 {
320 float *color_green_cursor = (float *)&g_vertexColorBuffer_g;
321
322 do
323 {
324 struct Graphics_Render_ClipVertex *vertex = &g_clipInputVertices[input_vertex_index];
325 vertex->pos.x = batch->view_vertices[input_vertex_index].x;
326 vertex->pos.y = batch->view_vertices[input_vertex_index].y;
327 vertex->pos.z = batch->view_vertices[input_vertex_index].z;
328 vertex->uv.u = prepared_vertices[input_vertex_index].uv.u;
329 vertex->uv.v = prepared_vertices[input_vertex_index].uv.v;
330 g_clipSavedInputVertices[input_vertex_index] = *vertex;
331
332 uint32_t source_red = g_quadVertexColorR[input_vertex_index];
333 long double output_blue;
334
335 // Process vertex colors: If use_texcoords flag set, double the color values (for brightness)
336 if (!(uint8_t)brighten_or_clipped_count)
337 {
338 // use_texcoords=0 path: Use default vertex colors from g_defaultVertexColors arrays
339 uint32_t source_green = g_quadVertexColorG[input_vertex_index];
340 uint32_t source_blue = g_quadVertexColorB[input_vertex_index];
341
342 color_green_cursor[-1] = (float)(long double)source_red;
343 *color_green_cursor = (float)(long double)source_green;
344 output_blue = (long double)source_blue;
345 }
346 else
347 {
348 uint32_t bright_red = source_red << 1; // Double color channel and clamp to 0xFF max
349
350 if (bright_red >= 0xff)
351 bright_red = 0xff;
352
353 uint32_t bright_green = g_quadVertexColorG[input_vertex_index] << 1;
354
355 color_green_cursor[-1] = (float)(long double)bright_red;
356
357 if (bright_green >= 0xff)
358 bright_green = 0xff;
359
360 uint32_t bright_blue = g_quadVertexColorB[input_vertex_index] << 1;
361
362 *color_green_cursor = (float)(long double)bright_green;
363
364 if (bright_blue >= 0xff)
365 bright_blue = 0xff;
366
367 output_blue = (long double)bright_blue;
368 }
369
370 input_count_snapshot = input_vertex_count;
371 color_green_cursor[1] = (float)output_blue;
372 input_vertex_index += 1;
373 color_green_cursor = &color_green_cursor[3];
374 } while (input_vertex_index < input_count_snapshot);
375 }
376
377 int32_t triangles_remaining = 1;
378
379 // Quad triangulation setup: If 4 vertices, split into 2 triangles with proper winding order
380 if (input_count_snapshot == 4)
381 {
382 // Reorder vertices for second triangle of quad (swap vertex 2 and 3)
383 float u2 = *(float *)&g_renderClipMinX;
384 uint32_t u3_bits = (uint32_t)g_clipInputBuffer_vertex2_w;
385 float v2 = *(float *)&g_renderClipMinY;
386 float v3 = *(float *)&g_renderClipMaxX;
387
388 triangles_remaining = 2;
389 *(float *)&g_clipInputBuffer_vertex2_w = u2;
390 g_renderClipMinX = u3_bits;
391 *(float *)&g_renderClipMinY = v3;
392 g_clipQuadSrcU = u3_bits;
393 *(float *)&g_renderClipMaxX = v2;
394 *(float *)&g_clipQuadSrcV = v3;
395 *(float *)&g_clipQuadDstU = u2;
396 *(float *)&g_clipQuadDstV = v2;
397 }
398
399 float first_green = (*(float *)&g_vertexColorBuffer_g);
400 float first_blue = (*(float *)&g_vertexColorBuffer_b1);
401 float second_red = (*(float *)&g_vertexColorBuffer_b0);
402 float third_blue = (*(float *)&g_vertexColorBuffer_b2);
403
404 g_clipInputAttributes[0].components[0] = (*(float *)&g_vertexColorBuffer);
405
406 float third_red = (*(float *)&g_vertexColorBuffer_r1);
407
408 ((float *)(g_clipInputAttributes[0].components))[1] = first_green;
409
410 float second_green = (*(float *)&g_vertexColorBuffer_r0);
411
412 g_clipInputAttributes[2].components[0] = third_red;
413
414 float third_green = (*(float *)&g_vertexColorBuffer_b);
415
416 ((float *)(g_clipInputAttributes[0].components))[2] = first_blue;
417
418 float second_blue = (*(float *)&g_vertexColorBuffer_g1);
419
420 ((float *)(g_clipInputAttributes[2].components))[1] = third_green;
421 g_clipInputAttributes[1].components[0] = second_red;
422 ((float *)(g_clipInputAttributes[1].components))[1] = second_green;
423 ((float *)(g_clipInputAttributes[1].components))[2] = second_blue;
424 ((float *)(g_clipInputAttributes[2].components))[2] = third_blue;
425
426 float depth_bias = batch->depth_bias;
427
428 while (true)
429 {
430 brighten_or_clipped_count = 3;
431
432 // Prepare vertices for clipping based on which triangle we're processing
433 // Triangle rendering loop starts here - process each triangle (1 for tri, 2 for quad)
434 if (input_vertex_count != 4)
435 {
436 ((float *)(g_clipInputAttributes[1].components))[1] = second_green;
437 __builtin_memcpy(&g_clipInputVertices[0], &g_clipSavedInputVertices[0], 0x1c);
438
439 float triangle_third_red = (*(float *)&g_vertexColorBuffer_r1);
440
441 __builtin_memcpy(&g_clipInputVertices[1], &g_clipSavedInputVertices[1], 0x1c);
442 ((float *)(g_clipInputAttributes[1].components))[2] = second_blue;
443 __builtin_memcpy(&g_clipInputVertices[2], &g_clipSavedInputVertices[2], 0x1c);
444
445 float triangle_third_green = (*(float *)&g_vertexColorBuffer_b);
446
447 g_clipInputAttributes[0].components[0] = (*(float *)&g_vertexColorBuffer);
448 ((float *)(g_clipInputAttributes[0].components))[1] = (*(float *)&g_vertexColorBuffer_g);
449 ((float *)(g_clipInputAttributes[0].components))[2] = (*(float *)&g_vertexColorBuffer_b1);
450 g_clipInputAttributes[1].components[0] = second_red;
451 g_clipInputAttributes[2].components[0] = triangle_third_red;
452 ((float *)(g_clipInputAttributes[2].components))[1] = triangle_third_green;
453 ((float *)(g_clipInputAttributes[2].components))[2] = third_blue;
454 }
455 else if (triangles_remaining != 2)
456 {
457 __builtin_memcpy(&g_clipInputVertices[0], &g_clipSavedInputVertices[1], 0x1c);
458 ((float *)(g_clipInputAttributes[0].components))[1] = second_green;
459
460 float second_triangle_middle_red = (*(float *)&g_vertexColorBuffer_r1);
461
462 ((float *)(g_clipInputAttributes[0].components))[2] = second_blue;
463
464 float second_triangle_middle_green = (*(float *)&g_vertexColorBuffer_b);
465
466 g_clipInputAttributes[1].components[0] = second_triangle_middle_red;
467
468 float second_triangle_last_green = (*(float *)&g_vertexColorBuffer_r2);
469
470 ((float *)(g_clipInputAttributes[1].components))[1] = second_triangle_middle_green;
471
472 float second_triangle_last_blue = (*(float *)&g_vertexColorBuffer_g3);
473
474 g_clipInputAttributes[0].components[0] = second_red;
475 __builtin_memcpy(&g_clipInputVertices[1], &g_clipSavedInputVertices[2], 0x1c);
476 ((float *)(g_clipInputAttributes[1].components))[2] = third_blue;
477 __builtin_memcpy(&g_clipInputVertices[2], &g_clipSavedInputVertices[3], 0x1c);
478
479 float second_triangle_last_red = (*(float *)&g_vertexColorBuffer_g2);
480
481 ((float *)(g_clipInputAttributes[2].components))[1] = second_triangle_last_green;
482 g_clipInputAttributes[2].components[0] = second_triangle_last_red;
483 ((float *)(g_clipInputAttributes[2].components))[2] = second_triangle_last_blue;
484 }
485 else
486 {
487 __builtin_memcpy(&g_clipInputVertices[0], &g_clipSavedInputVertices[0], 0x1c);
488 ((float *)(g_clipInputAttributes[1].components))[1] = second_green;
489 __builtin_memcpy(&g_clipInputVertices[1], &g_clipSavedInputVertices[1], 0x1c);
490
491 float first_triangle_last_red = (*(float *)&g_vertexColorBuffer_g2);
492
493 __builtin_memcpy(&g_clipInputVertices[2], &g_clipSavedInputVertices[3], 0x1c);
494
495 float first_triangle_first_red = (*(float *)&g_vertexColorBuffer);
496
497 ((float *)(g_clipInputAttributes[1].components))[2] = second_blue;
498
499 float first_triangle_last_green = (*(float *)&g_vertexColorBuffer_r2);
500
501 g_clipInputAttributes[0].components[0] = first_triangle_first_red;
502
503 float first_triangle_first_green = (*(float *)&g_vertexColorBuffer_g);
504
505 g_clipInputAttributes[1].components[0] = second_red;
506 ((float *)(g_clipInputAttributes[0].components))[1] = first_triangle_first_green;
507 ((float *)(g_clipInputAttributes[0].components))[2] = (*(float *)&g_vertexColorBuffer_b1);
508
509 float first_triangle_last_blue = (*(float *)&g_vertexColorBuffer_g3);
510
511 g_clipInputAttributes[2].components[0] = first_triangle_last_red;
512 ((float *)(g_clipInputAttributes[2].components))[1] = first_triangle_last_green;
513 ((float *)(g_clipInputAttributes[2].components))[2] = first_triangle_last_blue;
514 }
515
517 // Call ClipPolygonByCameraPyramid to perform frustum clipping
518 g_clipOutputVertices, (struct Graphics_Render_ClipAttribute *)(&g_clipTempBuffer), &brighten_or_clipped_count))
519 {
520 int32_t clipped_count_snapshot = brighten_or_clipped_count;
521
522 if (clipped_count_snapshot >= 3 && clipped_count_snapshot <= 0xf)
523 {
524 BOOL clipped_vertex_index = 0;
525
526 if (clipped_count_snapshot > 0)
527 {
528 struct Graphics_Render_ClipAttribute *clipped_color_cursor = (struct Graphics_Render_ClipAttribute *)&g_clipTempBuffer;
529
530 while (true) // Vertex transformation loop: Convert clipped vertices to screen space
531 {
532 long double clipped_view_depth = (long double)g_clipOutputVertices[clipped_vertex_index].pos.z;
533 long double clipped_zero_depth = (long double)0.0f; // Skip vertices behind near plane (Z <= 0)
534
535 if (!(clipped_view_depth > clipped_zero_depth))
536 break;
537
538 // Calculate RHW (reciprocal homogeneous W) = g_perspectiveDivisor / Z
539 /* Native 0x416783 stores float32, then reloads for both projections. */
540 volatile float reciprocal_depth = (float)((long double)*(float *)&g_reciprocalZ / (long double)g_clipOutputVertices[clipped_vertex_index].pos.z);
541
542 g_clipScreenVertices[clipped_vertex_index].pos.x = (float)(long double)
543 // Project X coordinate to default screen resolution and convert to integer
544 (int32_t)__ftol_msvc((long double)reciprocal_depth * (long double)g_clipOutputVertices[clipped_vertex_index].pos.x + (long double)320.0f);
545 g_clipScreenVertices[clipped_vertex_index].pos.y = (float)(long double)
546 // Project Y coordinate: screen_y = (y * rhw) + screen_offset_y, then convert to integer
547 (int32_t)__ftol_msvc((long double)reciprocal_depth * (long double)g_clipOutputVertices[clipped_vertex_index].pos.y + (long double)240.0f);
548 // Call SnapVertexToNearestPoint to adjust projected coords to nearest input vertex
549 // position
551 &g_clipScreenVertices[clipped_vertex_index].pos.x,
552 &g_clipScreenVertices[clipped_vertex_index].pos.y, (struct Graphics_Render_ProjectedVertex *)(&batch->screen_vertices),
553 input_vertex_count);
554
555 // Calculate Z depth value: z = (z_offset + vertex_z) * g_zDepthScale + depth_bias
556 long double normalized_depth = ((long double)depth_bias + (long double)g_clipOutputVertices[clipped_vertex_index].pos.z) * (long double)1.52587891e-05f + (long double)signed_depth_bias;
557 // Clamp Z depth to maximum depth value (g_maxDepthValue)
558 long double maximum_normalized_depth = (long double)0.99000001f;
559
560 if (!(maximum_normalized_depth >= normalized_depth))
561 normalized_depth = (long double)0.99000001f;
562
563 // Copy UV texture coordinates from clip output buffer
564 int32_t clipped_u_bits = *(uint32_t *)&g_clipOutputVertices[clipped_vertex_index].uv.u;
565
566 g_clipScreenVertices[clipped_vertex_index].pos.z = (float)normalized_depth;
567
568 int32_t clipped_v_bits = *(uint32_t *)&g_clipOutputVertices[clipped_vertex_index].uv.v;
569
570 g_clipScreenVertices[clipped_vertex_index].pos.rhw = reciprocal_depth;
571
572 long double clipped_red =
573 (long double)clipped_color_cursor->components[0];
574
575 *(uint32_t *)&g_clipScreenVertices[clipped_vertex_index].uv.u = clipped_u_bits;
576 *(uint32_t *)&g_clipScreenVertices[clipped_vertex_index].uv.v = clipped_v_bits;
577
578 // Pack RGBA color into D3DCOLOR format: (alpha << 24) | (r << 16) | (g << 8) | b
579 int32_t packed_alpha_red = (uint8_t)(__ftol_msvc(clipped_red));
580
581 *(uint8_t *)((char *)&packed_alpha_red + 1) = alpha_byte;
582 *(uint32_t *)&g_clipScreenVertices[clipped_vertex_index].color_or_data = (packed_alpha_red << 8 | (uint32_t)__ftol_msvc((long double)clipped_color_cursor->components[1])) << 8 | (uint32_t)__ftol_msvc((long double)clipped_color_cursor->components[2]);
583
584 long double screen_x_before_lower_clamp = // Clamp screen X coordinate to valid range [0.0, 639.0]
585 (long double)g_clipScreenVertices[clipped_vertex_index].pos.x;
586 long double minimum_screen_x = (long double)0.0f;
587
588 if (!(screen_x_before_lower_clamp >= minimum_screen_x))
589 *(uint32_t *)&g_clipScreenVertices[clipped_vertex_index].pos.x = 0;
590
591 long double screen_x_before_upper_clamp =
592 (long double)g_clipScreenVertices[clipped_vertex_index].pos.x;
593 long double maximum_screen_x = (long double)639.0f;
594
595 if (screen_x_before_upper_clamp > maximum_screen_x)
596 *(uint32_t *)&g_clipScreenVertices[clipped_vertex_index].pos.x = 0x441fc000;
597
598 long double screen_y_before_lower_clamp = // Clamp screen Y coordinate to valid range [0.0, 479.0]
599 (long double)g_clipScreenVertices[clipped_vertex_index].pos.y;
600 long double minimum_screen_y = (long double)0.0f;
601
602 if (!(screen_y_before_lower_clamp >= minimum_screen_y))
603 *(uint32_t *)&g_clipScreenVertices[clipped_vertex_index].pos.y = 0;
604
605 long double screen_y_before_upper_clamp =
606 (long double)g_clipScreenVertices[clipped_vertex_index].pos.y;
607 long double maximum_screen_y = (long double)479.0f;
608
609 if (screen_y_before_upper_clamp > maximum_screen_y)
610 *(uint32_t *)&g_clipScreenVertices[clipped_vertex_index].pos.y = 0x43ef8000;
611
612 clipped_count_snapshot = brighten_or_clipped_count;
613
614 clipped_vertex_index += 1;
615 clipped_color_cursor++;
616
617 if (clipped_vertex_index >= clipped_count_snapshot)
618 break;
619 }
620 }
621
622 if (clipped_vertex_index == clipped_count_snapshot)
623 {
624 void *draw_vertices;
625 int32_t draw_vertex_count;
626
627 IDirect3DDevice7 *device;
628 IDirect3DDevice7Vtbl *device_vtable;
629
630 // Check if vertex format conversion needed for D3D rendering
632 {
633 if (clipped_count_snapshot > 0)
634 {
635 int32_t *untextured_y_cursor = (int32_t *)(&graphics_d3d_vertex_buffer_y);
636 struct Graphics_Render_ClipVertex *textured_vertex = g_clipScreenVertices;
637 int32_t vertices_remaining = clipped_count_snapshot;
638 int32_t remaining_before_copy;
639
640 do
641 {
642 // Copy transformed vertices to D3D vertex buffer (5 dwords per vertex:
643 // x,y,z,rhw,color)
644 int32_t screen_x_bits = *(uint32_t *)&textured_vertex->pos.x;
645
646 textured_vertex++;
647 untextured_y_cursor[-1] = screen_x_bits;
648 *(uint32_t *)untextured_y_cursor = *(uint32_t *)&textured_vertex[-1].pos.y;
649 untextured_y_cursor[1] = *(uint32_t *)&textured_vertex[-1].pos.z;
650 untextured_y_cursor[2] = *(uint32_t *)&textured_vertex[-1].pos.rhw;
651 untextured_y_cursor[3] = *(uint32_t *)&textured_vertex[-1].color_or_data;
652 untextured_y_cursor = &untextured_y_cursor[5];
653 remaining_before_copy = vertices_remaining;
654 vertices_remaining -= 1;
655 } while (remaining_before_copy != 1);
656 }
657
658 // Check if we can submit another primitive batch (respect g_maxPrimitivesPerBatch limit)
660 {
661 device = g_d3dDevice7;
662
663 draw_vertex_count = clipped_count_snapshot;
664 draw_vertices = &graphics_d3d_vertex_buffer;
665 device_vtable = device->lpVtbl;
666
667 device_vtable->DrawPrimitive(device, D3DPT_TRIANGLEFAN, vertex_format_flags, draw_vertices, draw_vertex_count, 0);
668
669 int32_t previous_triangle_count = g_renderBatchTriangleCount;
670
672 g_renderBatchTriangleCount = previous_triangle_count + brighten_or_clipped_count - 2;
673 }
674 }
676 {
677 device = g_d3dDevice7;
678
679 draw_vertex_count = clipped_count_snapshot;
680 draw_vertices = &g_transformedVertices;
681 device_vtable = device->lpVtbl;
682
683 // Call IDirect3DDevice7::DrawPrimitive with D3DPT_TRIANGLEFAN (6) and vertex buffer
684 device_vtable->DrawPrimitive(device, D3DPT_TRIANGLEFAN, vertex_format_flags, draw_vertices, draw_vertex_count, 0);
685
686 int32_t previous_triangle_count = g_renderBatchTriangleCount;
687
689 g_renderBatchTriangleCount = previous_triangle_count + brighten_or_clipped_count - 2;
690 }
691 }
692 }
693 }
694
695 triangles_remaining -= 1;
696
697 if (!triangles_remaining)
698 break;
699
700 third_blue = (*(float *)&g_vertexColorBuffer_b2);
701 second_blue = (*(float *)&g_vertexColorBuffer_g1);
702 second_green = (*(float *)&g_vertexColorBuffer_r0);
703 second_red = (*(float *)&g_vertexColorBuffer_b0);
704 }
705}
706
707// Original pcdogs.exe: 0x43f0b0
708// Tests an actor sphere against five fixed-point frustum planes and the ground-visibility test. Returns zero when rejected or the near-plane value with bit zero set, and updates the partial-clipping flag.
709int32_t Graphics_CheckActorVisibilityAndFrustum(struct Math_Vec3I32 *position, int32_t cull_radius, uint32_t cull_flags)
710{
711 // Package file not found - show error message box
712 // local label: pos_x_q8 = position.x >> 8, sign-extended to EBX
713 // Extract position Y component
714 int32_t position_x = (int32_t)(int16_t)(position->x >> 8);
715 struct Graphics_Render_ListState *list_state = render_list_state;
716 // local label: pos_y_q8 = position.y >> 8, sign-extended to EBP
717 int32_t position_y = (int32_t)(int16_t)(position->y >> 8);
718 // local label: pos_z_q8 = position.z >> 8, sign-extended to EDX
719 int32_t position_z = (int32_t)(int16_t)(position->z >> 8);
720 int32_t near_plane_distance = (int32_t)list_state->frustum_planes[0].normal.x * position_x + (int32_t)list_state->frustum_planes[0].normal.y * position_y + (int32_t)list_state->frustum_planes[0].normal.z * position_z + list_state->frustum_planes[0].distance;
721 // local label: neg_cull_radius_q10 = -(cull_radius << 10); reject threshold for all five planes
722 int32_t negative_radius_q10 = -((cull_radius << 0xa));
723
724 if (near_plane_distance <= negative_radius_q10 || (!(g_graphicsCapabilities & GRAPHICS_CAP_DISABLE_DISTANCE_CULL) && !((uint8_t)cull_flags & FRUSTUM_CULL_SKIP_DISTANCE) && (negative_radius_q10 + near_plane_distance) >> 4 >= list_state->dynamic_level_scale))
725 return 0;
726
727 int32_t first_side_distance = (int32_t)list_state->frustum_planes[1].normal.x * position_x + (int32_t)list_state->frustum_planes[1].normal.y * position_y + (int32_t)list_state->frustum_planes[1].normal.z * position_z + list_state->frustum_planes[1].distance;
728
729 // Test against LEFT PLANE (camera+0x64): dot(normal, position) + distance
730 if (first_side_distance <= negative_radius_q10)
731 return 0;
732
733 int32_t second_side_distance = (int32_t)list_state->frustum_planes[2].normal.x * position_x + (int32_t)list_state->frustum_planes[2].normal.y * position_y + (int32_t)list_state->frustum_planes[2].normal.z * position_z + list_state->frustum_planes[2].distance;
734
735 if (second_side_distance <= negative_radius_q10)
736 return 0;
737
738 int32_t third_side_distance = (int32_t)list_state->frustum_planes[3].normal.x * position_x + (int32_t)list_state->frustum_planes[3].normal.y * position_y + (int32_t)list_state->frustum_planes[3].normal.z * position_z + list_state->frustum_planes[3].distance;
739
740 if (third_side_distance <= negative_radius_q10)
741 return 0;
742
743 int32_t fourth_side_distance = (int32_t)list_state->frustum_planes[4].normal.x * position_x + (int32_t)list_state->frustum_planes[4].normal.y * position_y + (int32_t)list_state->frustum_planes[4].normal.z * position_z + list_state->frustum_planes[4].distance;
744
745 if (fourth_side_distance <= negative_radius_q10 || Collision_CheckActorGround(position, cull_radius, cull_flags))
746 return 0;
747
748 int32_t positive_radius_q10 = -(negative_radius_q10);
749
750 if (first_side_distance >= positive_radius_q10 && second_side_distance >= positive_radius_q10 && third_side_distance >= positive_radius_q10 && fourth_side_distance >= positive_radius_q10)
751 {
753
754 int32_t fully_inside_visibility = near_plane_distance;
755
756 fully_inside_visibility |= GRAPHICS_VISIBILITY_PRESENT;
757
758 return fully_inside_visibility;
759 }
760
762
763 int32_t partially_inside_visibility = near_plane_distance;
764
765 partially_inside_visibility |= GRAPHICS_VISIBILITY_PRESENT;
766
767 return partially_inside_visibility;
768}
769
770// Original pcdogs.exe: 0x43f270
771// Runs actor visibility testing and updates the visible and near-plane relation flags, returning whether the actor remains visible.
772int32_t Graphics_CheckFrustumCull(struct Math_Vec3I32 *position, int32_t cull_radius, uint32_t cull_flags)
773{
774 // Call primary frustum culling test. Returns 0 if outside frustum, distance if visible.
775 int32_t visibility_distance_flags = Graphics_CheckActorVisibilityAndFrustum(position, cull_radius, cull_flags);
776
777 if (!visibility_distance_flags)
778 {
780
781 return 0;
782 }
783
784 uint32_t transition_flags;
785
786 if (cull_radius < 0 || visibility_distance_flags <= cull_radius << 0xa)
787 transition_flags = Game_FrameTransitionFlags & ~(uint32_t)GAME_TRANSITION_BEYOND_NEAR_PLANE;
788 else
790
791 Game_FrameTransitionFlags = transition_flags; // Mark object as visible (set bit 20) for rendering pipeline
793
794 return 1;
795}
#define camera_near_clip_plane
Definition globals.h:87
#define camera_clip_planes
Definition globals.h:84
#define Camera_SetupClipPlanes_NearClipDistanceSource
Definition globals.h:90
BOOL Graphics_ClipPolygonByPlane(struct Graphics_Render_ClipVertex *input_vertices, struct Graphics_Render_ClipAttribute *input_attributes, struct Graphics_Render_ClipPlane *clip_plane, struct Graphics_Render_ClipVertex *output_vertices, struct Graphics_Render_ClipAttribute *output_attributes, int32_t *in_out_vertex_count)
Definition clipping.c:23
BOOL Graphics_ClipPolygonByCameraPyramid(struct Graphics_Render_ClipVertex *input_vertices, struct Graphics_Render_ClipAttribute *input_attributes, struct Graphics_Render_ClipVertex *output_vertices, struct Graphics_Render_ClipAttribute *output_attributes, int32_t *in_out_vertex_count)
Definition clipping.c:91
int32_t Graphics_CheckActorVisibilityAndFrustum(struct Math_Vec3I32 *position, int32_t cull_radius, uint32_t cull_flags)
Definition clipping.c:709
void Graphics_ClipAndDrawPolygon(struct Graphics_Render_PolygonBatchRecord *batch, struct D3D_TransformedVertex const *prepared_vertices, uint8_t alpha_byte, int32_t unused_batch_alias, int32_t brighten_or_clipped_count)
Definition clipping.c:187
int32_t Graphics_CheckFrustumCull(struct Math_Vec3I32 *position, int32_t cull_radius, uint32_t cull_flags)
Definition clipping.c:772
void Graphics_SnapPointToFirstNearbyVertex(float *x, float *y, struct Graphics_Render_ProjectedVertex *screen_vertices, int32_t vertex_count)
Definition clipping.c:127
uint8_t Collision_CheckActorGround(struct Math_Vec3I32 *position, int32_t cull_radius, uint32_t cull_flags)
Definition ground.c:1146
#define GAME_TRANSITION_FRUSTUM_PARTIAL
Definition constants.h:13
#define GAME_TRANSITION_BEYOND_NEAR_PLANE
Definition constants.h:32
#define GAME_TRANSITION_OBJECT_VISIBLE
Definition constants.h:29
#define Game_FrameTransitionFlags
Definition globals.h:47
#define GRAPHICS_TRIANGLE_FOURTH_VERTEX
Definition constants.h:30
@ GRAPHICS_VERTEX_LAYOUT_COLOR
Definition constants.h:81
#define GRAPHICS_VISIBILITY_PRESENT
Definition constants.h:70
#define GRAPHICS_CAP_DISABLE_DISTANCE_CULL
Definition constants.h:52
#define FRUSTUM_CULL_SKIP_DISTANCE
Definition constants.h:68
#define g_vertexColorBuffer_b2
Definition globals.h:185
#define g_clipOutputVertices
Definition globals.h:331
#define graphics_d3d_vertex_buffer_y
Definition globals.h:233
#define g_vertexColorBuffer_r2
Definition globals.h:191
#define g_clipScreenVertices
Definition globals.h:332
#define g_vertexColorBuffer_b1
Definition globals.h:167
#define g_vertexColorBuffer_g2
Definition globals.h:188
#define g_vertexColorBuffer_r0
Definition globals.h:173
#define g_clipPingPongVertices1
Definition globals.h:203
#define g_quadVertexColorB
Definition globals.h:296
#define g_vertexColorBuffer
Definition globals.h:161
#define g_quadVertexColorR
Definition globals.h:302
#define g_vertexColorBuffer_r1
Definition globals.h:179
#define g_vertexColorBuffer_g3
Definition globals.h:194
#define g_clipPingPongAttributes0
Definition globals.h:200
#define g_graphicsCapabilities
Definition globals.h:284
#define g_vertexColorBuffer_g
Definition globals.h:164
#define g_vertexColorBuffer_g1
Definition globals.h:176
#define graphics_d3d_vertex_buffer
Definition globals.h:230
#define render_list_state
Definition globals.h:152
#define g_quadVertexColorG
Definition globals.h:299
#define g_clipInputVertices
Definition globals.h:329
#define g_vertexColorBuffer_b
Definition globals.h:182
#define g_vertexColorBuffer_b0
Definition globals.h:170
#define g_clipSavedInputVertices
Definition globals.h:330
#define g_clipPingPongVertices0
Definition globals.h:197
#define g_clipPingPongAttributes1
Definition globals.h:206
#define g_clipInputAttributes
Definition globals.h:41
#define g_clipInputBuffer_vertex2_w
Definition globals.h:38
#define X87_STATUS_C0
Definition constants.h:76
int64_t __ftol_msvc(long double value)
struct D3D_TransformedVertex::@022204046230112111334327023223004057003241046046 uv
struct Math_Vec3F normal
Definition types.h:431
struct Graphics_Render_ClipVertex::@004041224161122311126136335344051354147207176224 pos
struct Graphics_Render_ClipVertex::@065344116142077061363306200302256134000125147124 uv
struct Graphics_Render_FrustumClipPlane::@002324077244057217040013003070153264014256137317 normal
struct Graphics_Render_FrustumClipPlane frustum_planes[0x5]
Definition types.h:676
int32_t dynamic_level_scale
Definition types.h:681
struct Graphics_Render_ScreenVertexI16Z screen_vertices[0x4]
Definition types.h:512
struct Math_Vec3F view_vertices[0x4]
Definition types.h:513
struct Math_ScreenPointI16 screen
Definition types.h:528
struct Graphics_Render_ScreenVertexI16Z::@276010272006271151340046314217100172252315123166::@167176104245357063215260323150214177117323053225 screen
float z
Definition types.h:448
float y
Definition types.h:447
float x
Definition types.h:446
int32_t y
Definition types.h:471
int32_t x
Definition types.h:470
int32_t z
Definition types.h:472
#define g_d3dDevice7
Definition values.h:792
#define g_clipTempBuffer
Definition values.h:225
#define g_transformedVertices
Definition values.h:282
#define g_clipTempUVBuffer
Definition values.h:291
#define g_clipQuadDstV
Definition values.h:222
#define g_clipQuadSrcV
Definition values.h:216
#define g_renderClipMinY
Definition values.h:249
#define g_currentVertexFormat
Definition values.h:795
#define g_renderClipMaxX
Definition values.h:252
#define g_reciprocalZ
Definition values.h:813
#define g_clipQuadSrcU
Definition values.h:213
#define g_clipQuadDstU
Definition values.h:219
#define g_renderBatchTriangleCount
Definition values.h:537
#define g_renderBatchPrimitiveCount
Definition values.h:543
#define g_maxPrimitivesPerBatch
Definition values.h:117
#define g_clipTempVertexBuffer
Definition values.h:288
#define g_renderClipMinX
Definition values.h:246