102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
polygons.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
10#include "pcdogs/game/globals.h"
19#include "pcdogs/math/types.h"
21#include "pcdogs/scene/types.h"
22#include <math.h>
23#include <stdarg.h>
24#include <string.h>
25
26// Original pcdogs.exe: 0x41dcf0
27// Culls, transforms and projects a polygon batch, applies materials and queues records for sorted rendering. The count loop and shared record allocation retain their unchecked native behavior.
28void Graphics_RenderPolygonBatch(struct Scene_Node *node, struct Graphics_Render_PolygonRenderRef *polygon_refs, int32_t count)
29{
30 // Get vertex buffer base from materialNode+0x70. Each MeshVertex is 12 bytes. Access pattern:
31 // vertex_buffer + vertex_index * 0xC
32 //
33 // DALMATIAN VERTEX ACCESS: Get vertex buffer pointer from material_node+0x70. Each vertex is 12 bytes:
34 // [X:2][Y:2][Z:2][normalIdx:2][unused:4]
35 struct Mesh_RuntimeVertex *runtime_vertices = node->runtime_vertices;
36 int32_t batch_index = g_currentPolygonBatchIndex;
37 struct Mesh_RuntimeVertex *saved_runtime_vertices = runtime_vertices;
38
39 do
40 {
41 struct Graphics_Render_PolygonRenderRef *polygon_ref = polygon_refs;
42 struct Mesh_FaceNormal *face_plane = polygon_ref->face_normal;
43 int32_t view_normal_dot = (int32_t)face_plane->normal.z * (int32_t)g_viewDirectionZ + (int32_t)face_plane->normal.y * (int32_t)g_viewDirectionY
44 // Calculate fog distance for this polygon using face normal dot product with view direction.
45 // fogDistance = normal.x*viewDir.x + normal.y*viewDir.y + normal.z*viewDir.z + normal.d
46 // Face normal data at polygon_data->face_normal_ptr: [0]=d (int), [4]=nx, [6]=ny, [8]=nz (shorts)
47 + (int32_t)polygon_ref->face_normal->normal.x * (int32_t)g_viewDirectionX;
48 struct Mesh_FaceNormal *face_plane_reload = polygon_ref->face_normal;
49 int32_t view_plane_distance = view_normal_dot + face_plane_reload->plane_dist;
50
51 // faceFlags bit 5 (0x20): DOUBLE_SIDED -- if clear and face is back-facing, skip polygon
52 if (view_normal_dot + face_plane_reload->plane_dist >= 0 || polygon_ref->flags & POLYGON_TWO_SIDED || g_graphicsCapabilities & GRAPHICS_CAP_DISABLE_BACKFACE_CULL)
53 {
54 // faceFlags: test DOUBLE_SIDED(0x20), FLIP_WINDING(0x400), and material tiling -- controls
55 // recalculated face normal for double-sided polygons
56 uint16_t source_flags = polygon_ref->flags;
57
58 if (!(!((uint8_t)source_flags & POLYGON_TWO_SIDED) || (uint8_t)((uint32_t)source_flags >> 8) & (POLYGON_BACK_FACING >> 8) || polygon_ref->material_ref->flags & MATERIAL_TWO_SIDED))
59 {
60 int32_t outer_first_index = (uint16_t)(polygon_ref->vertex_indices[0]);
61
62 // Load vertex positions for backface culling calculation.
63 // g_backfaceVertex1Ptr = vertex_buffer + polygon->vertex_index_0 * 12
64 // g_backfaceVertex2Ptr = vertex_buffer + polygon->vertex_index_1 * 12
65 // Third vertex at polygon->vertex_index_3 * 12
66 // VERTEX INDEX LOOKUP: g_backfaceVertex1Ptr = vertex_buffer + polygon->vertex_index_0 * 12.
67 // Polygon structure at 0x18 bytes contains vertex indices at +0x04, +0x06, +0x08, +0x0A
68 int16_t *outer_first_position = (int16_t *)&runtime_vertices[outer_first_index];
69
70 g_backfaceVertex1Ptr = outer_first_position;
71
72 int32_t outer_second_index = (uint16_t)(polygon_ref->vertex_indices[1]);
73 int16_t *outer_second_position = (int16_t *)&runtime_vertices[outer_second_index];
74
75 g_backfaceVertex2Ptr = outer_second_position;
76
77 int32_t outer_fourth_index = (uint16_t)(polygon_ref->vertex_indices[3]);
78
79 // Backface culling: compute cross product of two edges to get face normal.
80 // edge1 = vertex3 - vertex1, edge2 = vertex3 - vertex2
81 // cross = edge1 x edge2 (perpendicular to face)
82 // If dot(cross, view_direction) < 0, face is back-facing and can be culled.
83 int16_t *outer_fourth_position = (int16_t *)&runtime_vertices[outer_fourth_index];
84
85 *(int16_t **)&g_polyBatchTextureState = outer_fourth_position;
86 *(int16_t **)&g_polyBatchRenderFlags = outer_fourth_position;
87
88 int16_t outer_fourth_y = outer_fourth_position[1];
89 int16_t outer_fourth_x_or_edge_b_x = *outer_fourth_position;
90 int16_t outer_fourth_z = outer_fourth_position[2];
91 int16_t outer_edge_a_x_short = (int16_t)(outer_fourth_x_or_edge_b_x - *outer_first_position);
92 int16_t outer_edge_a_y_short = (int16_t)(outer_fourth_y - outer_first_position[1]);
93 int16_t *outer_second_position_reload = g_backfaceVertex2Ptr;
94
95 outer_fourth_x_or_edge_b_x = (int16_t)(outer_fourth_x_or_edge_b_x - *outer_second_position_reload);
96
97 int32_t outer_edge_b_y = (int32_t)(int16_t)(outer_fourth_y - outer_second_position_reload[1]);
98 int32_t outer_edge_a_z = (int32_t)(int16_t)(outer_fourth_z - outer_first_position[2]);
99 int32_t outer_edge_b_z = (int32_t)(int16_t)(outer_fourth_z - outer_second_position_reload[2]);
100 int16_t outer_edge_b_x_short = outer_fourth_x_or_edge_b_x;
101 int32_t outer_cross_x = (int32_t)outer_edge_a_y_short * outer_edge_b_z - outer_edge_a_z * outer_edge_b_y;
102 int32_t outer_edge_a_x = (int32_t)outer_edge_a_x_short;
103 int32_t outer_normal_x = outer_cross_x;
104 int32_t outer_edge_b_x = (int32_t)outer_edge_b_x_short;
105 int32_t outer_cross_y = outer_edge_b_x * outer_edge_a_z - outer_edge_a_x * outer_edge_b_z;
106 int32_t outer_normal_y = outer_cross_y;
107 int32_t outer_cross_z = outer_edge_a_x * outer_edge_b_y - outer_edge_b_x * (int32_t)outer_edge_a_y_short;
108 int32_t outer_normal_z = outer_cross_z;
109 int64_t outer_square_x_product = (int64_t)outer_normal_x * outer_normal_x;
110 int64_t outer_square_y_product = (int64_t)outer_normal_y * outer_normal_y;
111 int64_t outer_square_z_product = (int64_t)outer_normal_z * outer_normal_z;
112 int32_t outer_length_squared = ((int32_t)((int64_t)((uint64_t)outer_square_z_product + (uint64_t)outer_square_x_product + (uint64_t)outer_square_y_product) >> 12));
113
114 if ((uint32_t)outer_length_squared < 0x1000u)
115 {
116 // Scale small vectors (*0x100 or *0x40) before normalization to maintain precision in 12-bit fixed point
117 // (>>12 = /4096)
118 int32_t outer_scaled_cross_z;
119
120 if ((uint32_t)outer_length_squared >= 0x100u)
121 {
122 outer_normal_x = outer_cross_x << 6;
123 outer_normal_y = outer_cross_y << 6;
124 outer_scaled_cross_z = outer_cross_z << 6;
125 }
126 else
127 {
128 outer_normal_x = outer_cross_x << 8;
129 outer_normal_y = outer_cross_y << 8;
130 outer_scaled_cross_z = outer_cross_z << 8;
131 }
132
133 outer_normal_z = outer_scaled_cross_z;
134 }
135
136 int64_t outer_scaled_square_x_product = (int64_t)outer_normal_x * outer_normal_x;
137 int64_t outer_scaled_square_y_product = (int64_t)outer_normal_y * outer_normal_y;
138 int64_t outer_scaled_square_z_product = (int64_t)outer_normal_z * outer_normal_z;
139 int32_t outer_normal_length =
140 Math_SqrtFP12(((int32_t)((int64_t)((uint64_t)outer_scaled_square_z_product + (uint64_t)outer_scaled_square_x_product + (uint64_t)outer_scaled_square_y_product) >> 12)));
141
142 if (outer_normal_length)
143 {
144 int32_t outer_inverse_length = INT64_C(0x100000000) / outer_normal_length;
145 int64_t outer_normalize_x_product = (int64_t)outer_inverse_length * outer_normal_x;
146
148 (int16_t)(((int32_t)(uint32_t)((uint64_t)outer_normalize_x_product >> 12)) >> 8);
149
150 int64_t outer_normalize_y_product = (int64_t)outer_inverse_length * outer_normal_y;
151
153 (int16_t)(((int32_t)(uint32_t)((uint64_t)outer_normalize_y_product >> 12)) >> 8);
154
155 int64_t outer_normalize_z_product = (int64_t)outer_inverse_length * outer_normal_z;
156
158 (int16_t)(((int32_t)(uint32_t)((uint64_t)outer_normalize_z_product >> 12)) >> 8);
159 }
160 else
161 {
165 }
166
167 int32_t inner_second_index = (uint16_t)(polygon_refs->vertex_indices[1]);
168 int16_t *inner_second_position = (int16_t *)&saved_runtime_vertices[inner_second_index];
169
170 g_backfaceVertex2Ptr = inner_second_position;
171
172 int32_t inner_third_index = (uint16_t)(polygon_refs->vertex_indices[2]);
173 int16_t *inner_third_position = (int16_t *)&saved_runtime_vertices[inner_third_index];
174
175 *(int16_t **)&g_polyBatchTextureState = inner_third_position;
176
177 int32_t inner_fourth_index = (uint16_t)(polygon_refs->vertex_indices[3]);
178 int16_t *inner_fourth_position = (int16_t *)&saved_runtime_vertices[inner_fourth_index];
179
180 *(int16_t **)&g_polyBatchRenderFlags = inner_fourth_position;
181 g_backfaceVertex1Ptr = inner_fourth_position;
182
183 int16_t inner_fourth_x_or_edge_b_x = *(uint16_t *)inner_fourth_position;
184 int16_t inner_edge_a_x_short = (int16_t)(*inner_third_position - inner_fourth_x_or_edge_b_x);
185
186 inner_fourth_x_or_edge_b_x -= *(uint16_t *)inner_second_position;
187
188 int16_t saved_inner_edge_a_x_short = inner_edge_a_x_short;
189 int16_t inner_fourth_y = inner_fourth_position[1];
190 int16_t inner_fourth_z = inner_fourth_position[2];
191 int32_t inner_edge_b_y = (int32_t)(int16_t)(inner_fourth_y - inner_second_position[1]);
192 int32_t inner_edge_a_z = (int32_t)(int16_t)(inner_third_position[2] - inner_fourth_z);
193 int32_t inner_edge_b_z = (int32_t)(int16_t)(inner_fourth_z - inner_second_position[2]);
194 int32_t inner_edge_a_y = (int32_t)(int16_t)(inner_third_position[1] - inner_fourth_y);
195 int32_t inner_cross_x = inner_edge_a_y * inner_edge_b_z - inner_edge_a_z * inner_edge_b_y;
196 int32_t inner_normal_x = inner_cross_x;
197 int32_t inner_edge_b_x = (int32_t)inner_fourth_x_or_edge_b_x;
198 int32_t inner_edge_a_x = (int32_t)saved_inner_edge_a_x_short;
199 int32_t inner_cross_y = inner_edge_b_x * inner_edge_a_z - inner_edge_a_x * inner_edge_b_z;
200 int32_t inner_cross_z = inner_edge_a_x * inner_edge_b_y - inner_edge_b_x * inner_edge_a_y;
201 int32_t inner_normal_y = inner_cross_y;
202 int32_t inner_normal_z = inner_cross_z;
203 int64_t inner_square_x_product = (int64_t)inner_normal_x * inner_normal_x;
204 int64_t inner_square_y_product = (int64_t)inner_normal_y * inner_normal_y;
205 int64_t inner_square_z_product = (int64_t)inner_normal_z * inner_normal_z;
206 int32_t inner_length_squared = ((int32_t)((int64_t)((uint64_t)inner_square_z_product + (uint64_t)inner_square_x_product + (uint64_t)inner_square_y_product) >> 12));
207
208 if ((uint32_t)inner_length_squared < 0x1000u)
209 {
210 int32_t inner_scaled_cross_z;
211
212 if ((uint32_t)inner_length_squared >= 0x100u)
213 {
214 inner_normal_x = inner_cross_x << 6;
215 inner_normal_y = inner_cross_y << 6;
216 inner_scaled_cross_z = inner_cross_z << 6;
217 }
218 else
219 {
220 inner_normal_x = inner_cross_x << 8;
221 inner_normal_y = inner_cross_y << 8;
222 inner_scaled_cross_z = inner_cross_z << 8;
223 }
224
225 inner_normal_z = inner_scaled_cross_z;
226 }
227
228 int64_t inner_scaled_square_x_product = (int64_t)inner_normal_x * inner_normal_x;
229 int64_t inner_scaled_square_y_product = (int64_t)inner_normal_y * inner_normal_y;
230
231 // Transform vertices using camera matrix DAT_00634dc0-dd0 (3x3 rotation) + DAT_00634dd4-ddc (translation).
232 // Shift amount varies by material flags: >>10 (type 3), >>6 (default), >>4 (flag 4)
233 // Converts 12-bit fixed point world coords to view space
234 int64_t inner_scaled_square_z_product = (int64_t)inner_normal_z * inner_normal_z;
235 int32_t inner_normal_length_or_zero =
236 Math_SqrtFP12(((int32_t)((int64_t)((uint64_t)inner_scaled_square_z_product + (uint64_t)inner_scaled_square_x_product + (uint64_t)inner_scaled_square_y_product) >> 12)));
237
238 if (inner_normal_length_or_zero)
239 {
240 int32_t inner_inverse_length = INT64_C(0x100000000) / inner_normal_length_or_zero;
241 int64_t inner_normalize_x_product = (int64_t)inner_inverse_length * inner_normal_x;
242
243 // Convert view space to screen coordinates if Z > near plane:
244 // Scale = camera_focal_length / Z (perspective divide)
245 // X_screen = (X_view * Scale >> 12) + screen_center_x
246 // Y_screen = (Y_view * Scale >> 12) + screen_center_y
247 g_polygonRenderFlags = (int16_t)(((int32_t)(uint32_t)((uint64_t)inner_normalize_x_product >> 12)) >> 8);
248
249 int64_t inner_normalize_y_product = (int64_t)inner_inverse_length * inner_normal_y;
250
251 data_49482a = (int16_t)(((int32_t)(uint32_t)((uint64_t)inner_normalize_y_product >> 12)) >> 8);
252
253 int64_t inner_normalize_z_product = (int64_t)inner_inverse_length * inner_normal_z;
254
256 (int16_t)(((int32_t)(uint32_t)((uint64_t)inner_normalize_z_product >> 12)) >> 8);
257 inner_normal_length_or_zero = 0;
258 }
259 else
260 {
261 graphics_max_primitives_per_batch_d3d = (uint16_t)inner_normal_length_or_zero;
262 data_49482a = (uint16_t)inner_normal_length_or_zero;
263 g_polygonRenderFlags = (uint16_t)inner_normal_length_or_zero;
264 }
265
266 int32_t outer_normal_xy_bits =
268 int32_t outer_normal_z_padding_bits =
270 int16_t *outer_plane_anchor = g_backfaceVertex1Ptr;
271
272 g_renderBatchVertexBase = inner_normal_length_or_zero;
273 g_polyBatchVertexCount = outer_normal_xy_bits;
274
275 int32_t outer_unit_x =
277
278 g_polyBatchTriCount = outer_normal_z_padding_bits;
279
280 int32_t outer_unit_z =
282 // Check if screen coordinates fit in short (overflow detection)
283 // If overflow, clamp Z to near plane distance to prevent projection errors
284 int32_t outer_anchor_y = (int32_t)outer_plane_anchor[1];
285 int32_t outer_unit_y =
287 int32_t outer_plane_offset;
288
290 {
291 outer_plane_offset = -(((outer_anchor_y * outer_unit_y + (int32_t)outer_plane_anchor[2] * outer_unit_z + (int32_t)*(int16_t *)outer_plane_anchor * outer_unit_x) >> 2));
292 g_renderBatchVertexBase = outer_plane_offset;
293
295 {
296 outer_plane_offset >>= 4;
297 g_renderBatchVertexBase = outer_plane_offset;
298 }
299 }
300 else
301 {
302 outer_plane_offset = -((outer_anchor_y * outer_unit_y + (int32_t)outer_plane_anchor[2] * outer_unit_z + (int32_t)*(int16_t *)outer_plane_anchor * outer_unit_x));
303 g_renderBatchVertexBase = outer_plane_offset;
304 }
305
306 int32_t inner_plane_offset;
307 int32_t inner_unit_y;
308 int32_t inner_unit_z;
309 int32_t inner_unit_x;
310
311 if (outer_unit_z * (int32_t)g_viewDirectionZ + outer_unit_y * (int32_t)g_viewDirectionY + outer_plane_offset + outer_unit_x * (int32_t)g_viewDirectionX < 0)
312 {
313 int32_t inner_normal_z_padding_bits =
315 int16_t *inner_plane_anchor = g_backfaceVertex2Ptr;
316
317 inner_unit_x = (int32_t)g_polygonRenderFlags;
319 g_polyBatchTriCount = inner_normal_z_padding_bits;
320 inner_unit_z =
323
324 int32_t inner_anchor_y = (int32_t)inner_plane_anchor[1];
325
326 inner_unit_y = (int32_t)data_49482a;
327
329 {
330 inner_plane_offset = -(((inner_anchor_y * inner_unit_y + (int32_t)inner_plane_anchor[2] * inner_unit_z + (int32_t)*(int16_t *)inner_plane_anchor * inner_unit_x) >> 2));
331 g_renderBatchVertexBase = inner_plane_offset;
332
334 {
335 inner_plane_offset >>= 4;
336 g_renderBatchVertexBase = inner_plane_offset;
337 }
338 }
339 else
340 {
341 inner_plane_offset = -((inner_anchor_y * inner_unit_y + (int32_t)inner_plane_anchor[2] * inner_unit_z + (int32_t)*(int16_t *)inner_plane_anchor * inner_unit_x));
342 g_renderBatchVertexBase = inner_plane_offset;
343 }
344 }
345
346 if (outer_unit_z * (int32_t)g_viewDirectionZ + outer_unit_y * (int32_t)g_viewDirectionY + outer_plane_offset + outer_unit_x * (int32_t)g_viewDirectionX < 0 && inner_unit_z * (int32_t)g_viewDirectionZ + inner_unit_y * (int32_t)g_viewDirectionY + inner_plane_offset + inner_unit_x * (int32_t)g_viewDirectionX < 0)
347 {
348 batch_index = g_currentPolygonBatchIndex;
349 runtime_vertices = saved_runtime_vertices;
350 polygon_ref = polygon_refs;
351
352 polygon_refs = &polygon_ref[1];
353 count -= 1;
354
355 continue;
356 }
357
358 batch_index = g_currentPolygonBatchIndex;
359 runtime_vertices = saved_runtime_vertices;
360 polygon_ref = polygon_refs;
361 }
362 {
363 struct Graphics_Render_PolygonBatchRecord *out_batch =
364 &(&g_polygonBatchRecords)[batch_index];
365 struct Graphics_Render_ScreenVertexI16Z *fourth_projected_vertex;
366 int32_t projected_axis_or_polygon_flags;
367 int32_t fourth_screen_y_q12;
368 int32_t fourth_screen_x;
369
371 {
372 int32_t vertex0_index_shift4 = (uint16_t)(polygon_ref->vertex_indices[0]);
373 int32_t vertex0_local_z_shift4 = (int32_t)runtime_vertices[vertex0_index_shift4].pos.z;
374 struct Math_Vec3I16 *vertex0_position_shift4 = &runtime_vertices[vertex0_index_shift4].pos;
375 int32_t vertex0_local_y_shift4 = (int32_t)vertex0_position_shift4->y;
376 int32_t vertex0_local_x_shift4 = (int32_t)vertex0_position_shift4->x;
377 int32_t vertex0_view_z_shift4 = (((int32_t)g_cameraTransformMatrix.m20 * vertex0_local_x_shift4 + (int32_t)g_cameraTransformMatrix.m21 * vertex0_local_y_shift4 + (int32_t)g_cameraTransformMatrix.m22 * vertex0_local_z_shift4) >> 4) + g_nodeViewTranslationZ;
378 int32_t vertex0_view_x_shift4 = (((int32_t)g_cameraTransformMatrix.m00 * vertex0_local_x_shift4 + (int32_t)g_cameraTransformMatrix.m01 * vertex0_local_y_shift4 + (int32_t)g_cameraTransformMatrix.m02 * vertex0_local_z_shift4) >> 4) + g_nodeViewTranslationX;
379 int32_t vertex0_view_y_precise_shift4 = (((int32_t)g_cameraTransformMatrix.m10 * vertex0_local_x_shift4 + (int32_t)g_cameraTransformMatrix.m11 * vertex0_local_y_shift4 + (int32_t)g_cameraTransformMatrix.m12 * vertex0_local_z_shift4) >> 4) + g_nodeViewTranslationY;
380
381 out_batch->view_vertices[0].x =
382 (float)((long double)vertex0_view_x_shift4 * (long double)0.015625f);
383 out_batch->view_vertices[0].y =
384 (float)((long double)vertex0_view_y_precise_shift4 * (long double)0.015625f);
385 out_batch->view_vertices[0].z =
386 (float)((long double)vertex0_view_z_shift4 * (long double)0.015625f);
387
388 struct Graphics_Render_ListState *vertex0_list_state_shift4 = render_list_state;
389
390 g_transformedVertex1ViewZ = vertex0_view_z_shift4;
391
392 if (vertex0_view_z_shift4 > vertex0_list_state_shift4->projection_near_fp)
393 {
394 int32_t vertex0_focal_distance_shift4 = vertex0_list_state_shift4->focal_distance;
395 int32_t vertex0_perspective_scale_shift4 =
396 ((int64_t)vertex0_focal_distance_shift4 * MATH_Q12_ONE) / vertex0_view_z_shift4;
397 int64_t vertex0_screen_x_product_shift4 = (int64_t)vertex0_perspective_scale_shift4 * vertex0_view_x_shift4;
398 int32_t vertex0_screen_x_shift4 = (((int32_t)(uint32_t)((uint64_t)vertex0_screen_x_product_shift4 >> 12)) >> 0xc) + (Graphics_WindowWidth >> 1);
399 int64_t vertex0_screen_y_product_shift4 = (int64_t)vertex0_perspective_scale_shift4 * vertex0_view_y_precise_shift4;
400 int32_t vertex0_screen_y_shift4 = (((int32_t)(uint32_t)((uint64_t)vertex0_screen_y_product_shift4 >> 12)) >> 0xc) + (Graphics_WindowHeight >> 1);
401
402 if (vertex0_screen_x_shift4 != (int32_t)(int16_t)vertex0_screen_x_shift4 || vertex0_screen_y_shift4 != (int32_t)(int16_t)vertex0_screen_y_shift4)
403 g_transformedVertex1ViewZ = render_list_state->projection_near_fp;
404 else
405 {
406 g_transformedVertex1ScreenX = (uint16_t)vertex0_screen_x_shift4;
407 g_transformedVertex1ScreenY = (uint16_t)vertex0_screen_y_shift4;
408 }
409 }
410
411 int32_t vertex1_index_shift4 = (uint16_t)(polygon_ref->vertex_indices[1]);
412 struct Math_Vec3I16 *vertex1_position_shift4 = &runtime_vertices[vertex1_index_shift4].pos;
413 int32_t vertex1_z_scale4 = (int32_t)runtime_vertices[vertex1_index_shift4].pos.z;
414 int32_t vertex1_local_y_shift4 = (int32_t)vertex1_position_shift4->y;
415 int32_t vertex1_local_x_shift4 = (int32_t)vertex1_position_shift4->x;
416 int32_t vertex1_view_z_shift4 = (((int32_t)g_cameraTransformMatrix.m20 * vertex1_local_x_shift4 + (int32_t)g_cameraTransformMatrix.m21 * vertex1_local_y_shift4 + (int32_t)g_cameraTransformMatrix.m22 * vertex1_z_scale4) >> 4) + g_nodeViewTranslationZ;
417 int32_t vertex1_view_x_shift4 = (((int32_t)g_cameraTransformMatrix.m00 * vertex1_local_x_shift4 + (int32_t)g_cameraTransformMatrix.m01 * vertex1_local_y_shift4 + (int32_t)g_cameraTransformMatrix.m02 * vertex1_z_scale4) >> 4) + g_nodeViewTranslationX;
418 int32_t vertex1_view_y_precise_shift4 = (((int32_t)g_cameraTransformMatrix.m10 * vertex1_local_x_shift4 + (int32_t)g_cameraTransformMatrix.m11 * vertex1_local_y_shift4 + (int32_t)g_cameraTransformMatrix.m12 * vertex1_z_scale4) >> 4) + g_nodeViewTranslationY;
419
420 out_batch->view_vertices[1].x =
421 (float)((long double)vertex1_view_x_shift4 * (long double)0.015625f);
422
423 int32_t vertex1_view_y_scale4 = vertex1_view_y_precise_shift4;
424
425 out_batch->view_vertices[1].y =
426 (float)((long double)vertex1_view_y_scale4 * (long double)0.015625f);
427 out_batch->view_vertices[1].z =
428 (float)((long double)vertex1_view_z_shift4 * (long double)0.015625f);
429
430 struct Graphics_Render_ListState *vertex1_list_state_shift4 = render_list_state;
431
432 g_transformedVertex2ViewZ = vertex1_view_z_shift4;
433
434 if (vertex1_view_z_shift4 > vertex1_list_state_shift4->projection_near_fp)
435 {
436 int32_t vertex1_focal_distance_shift4 = vertex1_list_state_shift4->focal_distance;
437 int32_t vertex1_perspective_scale_shift4 =
438 ((int64_t)vertex1_focal_distance_shift4 * MATH_Q12_ONE) / vertex1_view_z_shift4;
439 int64_t vertex1_screen_x_product_shift4 = (int64_t)vertex1_perspective_scale_shift4 * vertex1_view_x_shift4;
440 int32_t vertex1_screen_x_shift4 = (((int32_t)(uint32_t)((uint64_t)vertex1_screen_x_product_shift4 >> 12)) >> 0xc) + (Graphics_WindowWidth >> 1);
441 int64_t vertex1_screen_y_product_shift4 = (int64_t)vertex1_perspective_scale_shift4 * vertex1_view_y_scale4;
442 int32_t vertex1_screen_y_shift4 = (((int32_t)(uint32_t)((uint64_t)vertex1_screen_y_product_shift4 >> 12)) >> 0xc) + (Graphics_WindowHeight >> 1);
443
444 if (vertex1_screen_x_shift4 != (int32_t)(int16_t)vertex1_screen_x_shift4 || vertex1_screen_y_shift4 != (int32_t)(int16_t)vertex1_screen_y_shift4)
445 g_transformedVertex2ViewZ = render_list_state->projection_near_fp;
446 else
447 {
448 g_transformedVertex2ScreenX = (uint16_t)vertex1_screen_x_shift4;
449 g_transformedVertex2ScreenY = (uint16_t)vertex1_screen_y_shift4;
450 }
451 }
452
453 int32_t vertex2_index_shift4 = (uint16_t)(polygon_ref->vertex_indices[2]);
454 struct Math_Vec3I16 *vertex2_position_shift4 = &runtime_vertices[vertex2_index_shift4].pos;
455 int32_t vertex2_z_scale4 = (int32_t)runtime_vertices[vertex2_index_shift4].pos.z;
456 int32_t vertex2_local_y_shift4 = (int32_t)vertex2_position_shift4->y;
457 int32_t vertex2_local_x_shift4 = (int32_t)vertex2_position_shift4->x;
458 int32_t vertex2_view_z_shift4 = (((int32_t)g_cameraTransformMatrix.m20 * vertex2_local_x_shift4 + (int32_t)g_cameraTransformMatrix.m21 * vertex2_local_y_shift4 + (int32_t)g_cameraTransformMatrix.m22 * vertex2_z_scale4) >> 4) + g_nodeViewTranslationZ;
459 int32_t vertex2_view_x_shift4 = (((int32_t)g_cameraTransformMatrix.m00 * vertex2_local_x_shift4 + (int32_t)g_cameraTransformMatrix.m01 * vertex2_local_y_shift4 + (int32_t)g_cameraTransformMatrix.m02 * vertex2_z_scale4) >> 4) + g_nodeViewTranslationX;
460 int32_t vertex2_view_y_precise_shift4 = (((int32_t)g_cameraTransformMatrix.m10 * vertex2_local_x_shift4 + (int32_t)g_cameraTransformMatrix.m11 * vertex2_local_y_shift4 + (int32_t)g_cameraTransformMatrix.m12 * vertex2_z_scale4) >> 4) + g_nodeViewTranslationY;
461
462 projected_axis_or_polygon_flags = vertex2_view_z_shift4;
463 out_batch->view_vertices[2].x =
464 (float)((long double)vertex2_view_x_shift4 * (long double)0.015625f);
465
466 int32_t vertex2_view_y_scale4 = vertex2_view_y_precise_shift4;
467
468 out_batch->view_vertices[2].y =
469 (float)((long double)vertex2_view_y_scale4 * (long double)0.015625f);
470 out_batch->view_vertices[2].z =
471 (float)((long double)vertex2_view_z_shift4 * (long double)0.015625f);
472
473 struct Graphics_Render_ListState *vertex2_list_state_shift4 = render_list_state;
474
475 g_transformedVertex3ViewZ = projected_axis_or_polygon_flags;
476
477 if (projected_axis_or_polygon_flags > vertex2_list_state_shift4->projection_near_fp)
478 {
479 int32_t vertex2_focal_distance_shift4 = vertex2_list_state_shift4->focal_distance;
480 int32_t vertex2_perspective_scale_shift4 = ((int64_t)vertex2_focal_distance_shift4 * MATH_Q12_ONE) / vertex2_view_z_shift4;
481 int64_t vertex2_screen_x_product_shift4 = (int64_t)vertex2_perspective_scale_shift4 * vertex2_view_x_shift4;
482 int32_t vertex2_screen_x_shift4 = (((int32_t)(uint32_t)((uint64_t)vertex2_screen_x_product_shift4 >> 12)) >> 0xc) + (Graphics_WindowWidth >> 1);
483 int64_t vertex2_screen_y_product_shift4 = (int64_t)vertex2_perspective_scale_shift4 * vertex2_view_y_scale4;
484
485 projected_axis_or_polygon_flags = (((int32_t)(uint32_t)((uint64_t)vertex2_screen_y_product_shift4 >> 12)) >> 0xc) + (Graphics_WindowHeight >> 1);
486
487 if (vertex2_screen_x_shift4 != (int32_t)(int16_t)vertex2_screen_x_shift4 || projected_axis_or_polygon_flags != (int32_t)(int16_t)projected_axis_or_polygon_flags)
488 {
489 g_transformedVertex3ViewZ = render_list_state->projection_near_fp;
490 }
491 else
492 {
493 g_transformedVertex3ScreenX = (uint16_t)vertex2_screen_x_shift4;
494 g_transformedVertex3ScreenY = (uint16_t)projected_axis_or_polygon_flags;
495 }
496 }
497
498 if (!(polygon_ref->flags & POLYGON_TRIANGLE))
499 {
500 fourth_projected_vertex = (struct Graphics_Render_ScreenVertexI16Z *)&g_transformedVertex4ScreenX;
501
502 int32_t vertex3_index_shift4 = (uint16_t)(polygon_ref->vertex_indices[3]);
503 struct Math_Vec3I16 *vertex3_position_shift4 = &runtime_vertices[vertex3_index_shift4].pos;
504 int32_t vertex3_z_scale4 = (int32_t)runtime_vertices[vertex3_index_shift4].pos.z;
505 int32_t vertex3_local_y_shift4 = (int32_t)vertex3_position_shift4->y;
506 int32_t vertex3_local_x_shift4 = (int32_t)vertex3_position_shift4->x;
507 int32_t vertex3_view_z_shift4 = ((
508 (int32_t)g_cameraTransformMatrix.m20 * vertex3_local_x_shift4 + (int32_t)g_cameraTransformMatrix.m21 * vertex3_local_y_shift4 + (int32_t)g_cameraTransformMatrix.m22 * vertex3_z_scale4) >>
509 4) +
511 int32_t vertex3_view_x_shift4 = (((int32_t)g_cameraTransformMatrix.m00 * vertex3_local_x_shift4 + (int32_t)g_cameraTransformMatrix.m01 * vertex3_local_y_shift4 + (int32_t)g_cameraTransformMatrix.m02 * vertex3_z_scale4) >> 4) + g_nodeViewTranslationX;
512 int32_t vertex3_view_y_precise_shift4 = (((int32_t)g_cameraTransformMatrix.m10 * vertex3_local_x_shift4 + (int32_t)g_cameraTransformMatrix.m11 * vertex3_local_y_shift4 + (int32_t)g_cameraTransformMatrix.m12 * vertex3_z_scale4) >> 4) + g_nodeViewTranslationY;
513
514 projected_axis_or_polygon_flags = vertex3_view_z_shift4;
515 out_batch->view_vertices[3].x =
516 (float)((long double)vertex3_view_x_shift4 * (long double)0.015625f);
517
518 int32_t vertex3_view_y_scale4 = vertex3_view_y_precise_shift4;
519
520 out_batch->view_vertices[3].y =
521 (float)((long double)vertex3_view_y_scale4 * (long double)0.015625f);
522 out_batch->view_vertices[3].z =
523 (float)((long double)vertex3_view_z_shift4 * (long double)0.015625f);
524
525 struct Graphics_Render_ListState *vertex3_list_state_shift4 = render_list_state;
526
527 g_transformedVertex4ViewZ = projected_axis_or_polygon_flags;
528
529 if (projected_axis_or_polygon_flags > vertex3_list_state_shift4->projection_near_fp)
530 {
531 int32_t vertex3_focal_distance_shift4 = vertex3_list_state_shift4->focal_distance;
532 int32_t vertex3_perspective_scale_shift4 =
533 ((int64_t)vertex3_focal_distance_shift4 * MATH_Q12_ONE) / vertex3_view_z_shift4;
534 int64_t vertex3_screen_x_product_shift4 = (int64_t)vertex3_perspective_scale_shift4 * vertex3_view_x_shift4;
535
536 fourth_screen_x = (((int32_t)(uint32_t)((uint64_t)vertex3_screen_x_product_shift4 >> 12)) >> 0xc) + (Graphics_WindowWidth >> 1);
537
538 int64_t vertex3_screen_y_product_shift4 = (int64_t)vertex3_perspective_scale_shift4 * vertex3_view_y_scale4;
539
540 fourth_screen_y_q12 = (int32_t)(uint32_t)((uint64_t)vertex3_screen_y_product_shift4 >> 12);
541
542 projected_axis_or_polygon_flags = (fourth_screen_y_q12 >> 0xc) + (Graphics_WindowHeight >> 1);
543
544 if (fourth_screen_x != (int32_t)(int16_t)fourth_screen_x || projected_axis_or_polygon_flags != (int32_t)(int16_t)projected_axis_or_polygon_flags)
545 {
546 g_transformedVertex4ViewZ = render_list_state->projection_near_fp;
547 }
548 else
549 {
550 g_transformedVertex4ScreenX = (uint16_t)fourth_screen_x;
551 g_transformedVertex4ScreenY = (uint16_t)projected_axis_or_polygon_flags;
552 }
553 }
554 }
555 else
556 memset(&fourth_projected_vertex, 0, 4);
557 }
558 else if (node->node_type != SCENE_NODE_SCALED_GEOMETRY)
559 {
560 int32_t vertex0_index_shift6 = (uint16_t)(polygon_ref->vertex_indices[0]);
561 struct Math_Vec3I16 *vertex0_position_shift6 = &runtime_vertices[vertex0_index_shift6].pos;
562 int32_t vertex0_z_scale6 = (int32_t)runtime_vertices[vertex0_index_shift6].pos.z;
563 int32_t vertex0_local_y_shift6 = (int32_t)vertex0_position_shift6->y;
564 int32_t vertex0_local_x_shift6 = (int32_t)vertex0_position_shift6->x;
565 int32_t vertex0_view_z_shift6 = (((int32_t)g_cameraTransformMatrix.m20 * vertex0_local_x_shift6 + (int32_t)g_cameraTransformMatrix.m21 * vertex0_local_y_shift6 + (int32_t)g_cameraTransformMatrix.m22 * vertex0_z_scale6) >> 6) + g_nodeViewTranslationZ;
566 int32_t vertex0_view_x_shift6 = (((int32_t)g_cameraTransformMatrix.m00 * vertex0_local_x_shift6 + (int32_t)g_cameraTransformMatrix.m01 * vertex0_local_y_shift6 + (int32_t)g_cameraTransformMatrix.m02 * vertex0_z_scale6) >> 6) + g_nodeViewTranslationX;
567 int32_t vertex0_view_y_precise_shift6 = (((int32_t)g_cameraTransformMatrix.m10 * vertex0_local_x_shift6 + (int32_t)g_cameraTransformMatrix.m11 * vertex0_local_y_shift6 + (int32_t)g_cameraTransformMatrix.m12 * vertex0_z_scale6) >> 6) + g_nodeViewTranslationY;
568
569 out_batch->view_vertices[0].x =
570 (float)((long double)vertex0_view_x_shift6 * (long double)0.015625f);
571
572 int32_t vertex0_view_y_scale6 = vertex0_view_y_precise_shift6;
573
574 out_batch->view_vertices[0].y =
575 (float)((long double)vertex0_view_y_scale6 * (long double)0.015625f);
576 out_batch->view_vertices[0].z =
577 (float)((long double)vertex0_view_z_shift6 * (long double)0.015625f);
578
579 struct Graphics_Render_ListState *vertex0_list_state_shift6 = render_list_state;
580
581 g_transformedVertex1ViewZ = vertex0_view_z_shift6;
582
583 if (vertex0_view_z_shift6 > vertex0_list_state_shift6->projection_near_fp)
584 {
585 int32_t vertex0_focal_distance_shift6 = vertex0_list_state_shift6->focal_distance;
586 int32_t vertex0_perspective_scale_shift6 =
587 ((int64_t)vertex0_focal_distance_shift6 * MATH_Q12_ONE) / vertex0_view_z_shift6;
588 int64_t vertex0_screen_x_product_shift6 = (int64_t)vertex0_perspective_scale_shift6 * vertex0_view_x_shift6;
589 int32_t vertex0_screen_x_shift6 = (((int32_t)(uint32_t)((uint64_t)vertex0_screen_x_product_shift6 >> 12)) >> 0xc) + (Graphics_WindowWidth >> 1);
590 int64_t vertex0_screen_y_product_shift6 = (int64_t)vertex0_perspective_scale_shift6 * vertex0_view_y_scale6;
591 int32_t vertex0_screen_y_shift6 = (((int32_t)(uint32_t)((uint64_t)vertex0_screen_y_product_shift6 >> 12)) >> 0xc) + (Graphics_WindowHeight >> 1);
592
593 if (vertex0_screen_x_shift6 != (int32_t)(int16_t)vertex0_screen_x_shift6 || vertex0_screen_y_shift6 != (int32_t)(int16_t)vertex0_screen_y_shift6)
594 g_transformedVertex1ViewZ = render_list_state->projection_near_fp;
595 else
596 {
597 g_transformedVertex1ScreenX = (uint16_t)vertex0_screen_x_shift6;
598 g_transformedVertex1ScreenY = (uint16_t)vertex0_screen_y_shift6;
599 }
600 }
601
602 int32_t vertex1_index_shift6 = (uint16_t)(polygon_ref->vertex_indices[1]);
603 struct Math_Vec3I16 *vertex1_position_shift6 = &runtime_vertices[vertex1_index_shift6].pos;
604 int32_t vertex1_z_scale6 = (int32_t)runtime_vertices[vertex1_index_shift6].pos.z;
605 int32_t vertex1_local_y_shift6 = (int32_t)vertex1_position_shift6->y;
606 int32_t vertex1_local_x_shift6 = (int32_t)vertex1_position_shift6->x;
607 int32_t vertex1_view_z_shift6 = (((int32_t)g_cameraTransformMatrix.m20 * vertex1_local_x_shift6 + (int32_t)g_cameraTransformMatrix.m21 * vertex1_local_y_shift6 + (int32_t)g_cameraTransformMatrix.m22 * vertex1_z_scale6) >> 6) + g_nodeViewTranslationZ;
608 int32_t vertex1_view_x_shift6 = (((int32_t)g_cameraTransformMatrix.m00 * vertex1_local_x_shift6 + (int32_t)g_cameraTransformMatrix.m01 * vertex1_local_y_shift6 + (int32_t)g_cameraTransformMatrix.m02 * vertex1_z_scale6) >> 6) + g_nodeViewTranslationX;
609 int32_t vertex1_view_y_precise_shift6 = (((int32_t)g_cameraTransformMatrix.m10 * vertex1_local_x_shift6 + (int32_t)g_cameraTransformMatrix.m11 * vertex1_local_y_shift6 + (int32_t)g_cameraTransformMatrix.m12 * vertex1_z_scale6) >> 6) + g_nodeViewTranslationY;
610
611 out_batch->view_vertices[1].x =
612 (float)((long double)vertex1_view_x_shift6 * (long double)0.015625f);
613
614 int32_t vertex1_view_y_scale6 = vertex1_view_y_precise_shift6;
615
616 out_batch->view_vertices[1].y =
617 (float)((long double)vertex1_view_y_scale6 * (long double)0.015625f);
618 out_batch->view_vertices[1].z =
619 (float)((long double)vertex1_view_z_shift6 * (long double)0.015625f);
620
621 struct Graphics_Render_ListState *vertex1_list_state_shift6 = render_list_state;
622
623 g_transformedVertex2ViewZ = vertex1_view_z_shift6;
624
625 if (vertex1_view_z_shift6 > vertex1_list_state_shift6->projection_near_fp)
626 {
627 int32_t vertex1_focal_distance_shift6 = vertex1_list_state_shift6->focal_distance;
628 int32_t vertex1_perspective_scale_shift6 =
629 ((int64_t)vertex1_focal_distance_shift6 * MATH_Q12_ONE) / vertex1_view_z_shift6;
630 int64_t vertex1_screen_x_product_shift6 = (int64_t)vertex1_perspective_scale_shift6 * vertex1_view_x_shift6;
631 int32_t vertex1_screen_x_shift6 = (((int32_t)(uint32_t)((uint64_t)vertex1_screen_x_product_shift6 >> 12)) >> 0xc) + (Graphics_WindowWidth >> 1);
632 int64_t vertex1_screen_y_product_shift6 = (int64_t)vertex1_perspective_scale_shift6 * vertex1_view_y_scale6;
633 int32_t vertex1_screen_y_shift6 = (((int32_t)(uint32_t)((uint64_t)vertex1_screen_y_product_shift6 >> 12)) >> 0xc) + (Graphics_WindowHeight >> 1);
634
635 if (vertex1_screen_x_shift6 != (int32_t)(int16_t)vertex1_screen_x_shift6 || vertex1_screen_y_shift6 != (int32_t)(int16_t)vertex1_screen_y_shift6)
636 g_transformedVertex2ViewZ = render_list_state->projection_near_fp;
637 else
638 {
639 g_transformedVertex2ScreenX = (uint16_t)vertex1_screen_x_shift6;
640 g_transformedVertex2ScreenY = (uint16_t)vertex1_screen_y_shift6;
641 }
642 }
643
644 int32_t vertex2_index_shift6 = (uint16_t)(polygon_ref->vertex_indices[2]);
645 struct Math_Vec3I16 *vertex2_position_shift6 = &runtime_vertices[vertex2_index_shift6].pos;
646 int32_t vertex2_z_scale6 = (int32_t)runtime_vertices[vertex2_index_shift6].pos.z;
647 int32_t vertex2_local_y_shift6 = (int32_t)vertex2_position_shift6->y;
648 int32_t vertex2_local_x_shift6 = (int32_t)vertex2_position_shift6->x;
649 int32_t vertex2_view_z_shift6 = (((int32_t)g_cameraTransformMatrix.m20 * vertex2_local_x_shift6 + (int32_t)g_cameraTransformMatrix.m21 * vertex2_local_y_shift6 + (int32_t)g_cameraTransformMatrix.m22 * vertex2_z_scale6) >> 6) + g_nodeViewTranslationZ;
650 int32_t vertex2_view_x_shift6 = (((int32_t)g_cameraTransformMatrix.m00 * vertex2_local_x_shift6 + (int32_t)g_cameraTransformMatrix.m01 * vertex2_local_y_shift6 + (int32_t)g_cameraTransformMatrix.m02 * vertex2_z_scale6) >> 6) + g_nodeViewTranslationX;
651 int32_t vertex2_view_y_precise_shift6 = (((int32_t)g_cameraTransformMatrix.m10 * vertex2_local_x_shift6 + (int32_t)g_cameraTransformMatrix.m11 * vertex2_local_y_shift6 + (int32_t)g_cameraTransformMatrix.m12 * vertex2_z_scale6) >> 6) + g_nodeViewTranslationY;
652
653 out_batch->view_vertices[2].x =
654 (float)((long double)vertex2_view_x_shift6 * (long double)0.015625f);
655
656 int32_t vertex2_view_y_scale6 = vertex2_view_y_precise_shift6;
657
658 out_batch->view_vertices[2].y =
659 (float)((long double)vertex2_view_y_scale6 * (long double)0.015625f);
660 out_batch->view_vertices[2].z =
661 (float)((long double)vertex2_view_z_shift6 * (long double)0.015625f);
662
663 struct Graphics_Render_ListState *vertex2_list_state_shift6 = render_list_state;
664
665 g_transformedVertex3ViewZ = vertex2_view_z_shift6;
666
667 if (vertex2_view_z_shift6 > vertex2_list_state_shift6->projection_near_fp)
668 {
669 int32_t vertex2_focal_distance_shift6 = vertex2_list_state_shift6->focal_distance;
670 int32_t vertex2_perspective_scale_shift6 =
671 ((int64_t)vertex2_focal_distance_shift6 * MATH_Q12_ONE) / vertex2_view_z_shift6;
672 int64_t vertex2_screen_x_product_shift6 = (int64_t)vertex2_perspective_scale_shift6 * vertex2_view_x_shift6;
673 int32_t vertex2_screen_x_shift6 = (((int32_t)(uint32_t)((uint64_t)vertex2_screen_x_product_shift6 >> 12)) >> 0xc) + (Graphics_WindowWidth >> 1);
674 int64_t vertex2_screen_y_product_shift6 = (int64_t)vertex2_perspective_scale_shift6 * vertex2_view_y_scale6;
675 int32_t vertex2_screen_y_shift6 = (((int32_t)(uint32_t)((uint64_t)vertex2_screen_y_product_shift6 >> 12)) >> 0xc) + (Graphics_WindowHeight >> 1);
676
677 if (vertex2_screen_x_shift6 != (int32_t)(int16_t)vertex2_screen_x_shift6 || vertex2_screen_y_shift6 != (int32_t)(int16_t)vertex2_screen_y_shift6)
678 g_transformedVertex3ViewZ = render_list_state->projection_near_fp;
679 else
680 {
681 g_transformedVertex3ScreenX = (uint16_t)vertex2_screen_x_shift6;
682 g_transformedVertex3ScreenY = (uint16_t)vertex2_screen_y_shift6;
683 }
684 }
685
686 if (!(polygon_ref->flags & POLYGON_TRIANGLE))
687 {
688 fourth_projected_vertex = (struct Graphics_Render_ScreenVertexI16Z *)&g_transformedVertex4ScreenX;
689
690 int32_t vertex3_index_shift6 = (uint16_t)(polygon_ref->vertex_indices[3]);
691 struct Math_Vec3I16 *vertex3_position_shift6 = &runtime_vertices[vertex3_index_shift6].pos;
692 int32_t vertex3_z_scale6 = (int32_t)runtime_vertices[vertex3_index_shift6].pos.z;
693 int32_t vertex3_local_y_shift6 = (int32_t)vertex3_position_shift6->y;
694 int32_t vertex3_local_x_shift6 = (int32_t)vertex3_position_shift6->x;
695 int32_t vertex3_view_z_shift6 = ((
696 (int32_t)g_cameraTransformMatrix.m20 * vertex3_local_x_shift6 + (int32_t)g_cameraTransformMatrix.m21 * vertex3_local_y_shift6 + (int32_t)g_cameraTransformMatrix.m22 * vertex3_z_scale6) >>
697 6) +
699 int32_t vertex3_view_x_shift6 = (((int32_t)g_cameraTransformMatrix.m00 * vertex3_local_x_shift6 + (int32_t)g_cameraTransformMatrix.m01 * vertex3_local_y_shift6 + (int32_t)g_cameraTransformMatrix.m02 * vertex3_z_scale6) >> 6) + g_nodeViewTranslationX;
700 int32_t vertex3_view_y_precise_shift6 = (((int32_t)g_cameraTransformMatrix.m10 * vertex3_local_x_shift6 + (int32_t)g_cameraTransformMatrix.m11 * vertex3_local_y_shift6 + (int32_t)g_cameraTransformMatrix.m12 * vertex3_z_scale6) >> 6) + g_nodeViewTranslationY;
701
702 projected_axis_or_polygon_flags = vertex3_view_z_shift6;
703 out_batch->view_vertices[3].x =
704 (float)((long double)vertex3_view_x_shift6 * (long double)0.015625f);
705
706 int32_t vertex3_view_y_scale6 = vertex3_view_y_precise_shift6;
707
708 out_batch->view_vertices[3].y =
709 (float)((long double)vertex3_view_y_scale6 * (long double)0.015625f);
710 out_batch->view_vertices[3].z =
711 (float)((long double)vertex3_view_z_shift6 * (long double)0.015625f);
712
713 struct Graphics_Render_ListState *vertex3_list_state_shift6 = render_list_state;
714
715 g_transformedVertex4ViewZ = projected_axis_or_polygon_flags;
716
717 if (projected_axis_or_polygon_flags > vertex3_list_state_shift6->projection_near_fp)
718 {
719 int32_t vertex3_focal_distance_shift6 = vertex3_list_state_shift6->focal_distance;
720 int32_t vertex3_perspective_scale_shift6 =
721 ((int64_t)vertex3_focal_distance_shift6 * MATH_Q12_ONE) / vertex3_view_z_shift6;
722 int64_t vertex3_screen_x_product_shift6 = (int64_t)vertex3_perspective_scale_shift6 * vertex3_view_x_shift6;
723 int32_t vertex3_screen_x_shift6 = (((int32_t)(uint32_t)((uint64_t)vertex3_screen_x_product_shift6 >> 12)) >> 0xc) + (Graphics_WindowWidth >> 1);
724 int64_t vertex3_screen_y_product_shift6 = (int64_t)vertex3_perspective_scale_shift6 * vertex3_view_y_scale6;
725
726 projected_axis_or_polygon_flags = (((int32_t)(uint32_t)((uint64_t)vertex3_screen_y_product_shift6 >> 12)) >> 0xc) + (Graphics_WindowHeight >> 1);
727
728 if (vertex3_screen_x_shift6 != (int32_t)(int16_t)vertex3_screen_x_shift6 || projected_axis_or_polygon_flags != (int32_t)(int16_t)projected_axis_or_polygon_flags)
729 {
730 g_transformedVertex4ViewZ = render_list_state->projection_near_fp;
731 }
732 else
733 {
734 g_transformedVertex4ScreenX = (uint16_t)vertex3_screen_x_shift6;
735 g_transformedVertex4ScreenY = (uint16_t)projected_axis_or_polygon_flags;
736 }
737 }
738 }
739 else
740 {
741 projected_axis_or_polygon_flags = 0;
742 memset(&fourth_projected_vertex, 0, 4);
743 out_batch->view_vertices[3].z = 0.0f;
744 out_batch->view_vertices[3].y = 0.0f;
745 out_batch->view_vertices[3].x = 0;
746 }
747 }
748 else
749 {
750 int32_t vertex0_index_shift10 = (uint16_t)(polygon_ref->vertex_indices[0]);
751 struct Math_Vec3I16 *vertex0_position_shift10 = &runtime_vertices[vertex0_index_shift10].pos;
752 int32_t vertex0_z_scale10 = (int32_t)runtime_vertices[vertex0_index_shift10].pos.z;
753 int32_t vertex0_local_y_shift10 = (int32_t)vertex0_position_shift10->y;
754 int32_t vertex0_local_x_shift10 = (int32_t)vertex0_position_shift10->x;
755 int32_t vertex0_view_z_shift10 = (((int32_t)g_cameraTransformMatrix.m20 * vertex0_local_x_shift10 + (int32_t)g_cameraTransformMatrix.m21 * vertex0_local_y_shift10 + (int32_t)g_cameraTransformMatrix.m22 * vertex0_z_scale10) >> 0xa) + g_nodeViewTranslationZ;
756 int32_t vertex0_view_x_shift10 = (((int32_t)g_cameraTransformMatrix.m00 * vertex0_local_x_shift10 + (int32_t)g_cameraTransformMatrix.m01 * vertex0_local_y_shift10 + (int32_t)g_cameraTransformMatrix.m02 * vertex0_z_scale10) >> 0xa) + g_nodeViewTranslationX;
757 int32_t vertex0_view_y_precise_shift10 = (((int32_t)g_cameraTransformMatrix.m10 * vertex0_local_x_shift10 + (int32_t)g_cameraTransformMatrix.m11 * vertex0_local_y_shift10 + (int32_t)g_cameraTransformMatrix.m12 * vertex0_z_scale10) >> 0xa) + g_nodeViewTranslationY;
758
759 out_batch->view_vertices[0].x =
760 (float)((long double)vertex0_view_x_shift10 * (long double)0.015625f);
761
762 int32_t vertex0_view_y_scale10 = vertex0_view_y_precise_shift10;
763
764 out_batch->view_vertices[0].y =
765 (float)((long double)vertex0_view_y_scale10 * (long double)0.015625f);
766 out_batch->view_vertices[0].z =
767 (float)((long double)vertex0_view_z_shift10 * (long double)0.015625f);
768
769 struct Graphics_Render_ListState *vertex0_list_state_shift10 = render_list_state;
770
771 g_transformedVertex1ViewZ = vertex0_view_z_shift10;
772
773 if (vertex0_view_z_shift10 > vertex0_list_state_shift10->projection_near_fp)
774 {
775 int32_t vertex0_focal_distance_shift10 = vertex0_list_state_shift10->focal_distance;
776 int32_t vertex0_perspective_scale_shift10 =
777 ((int64_t)vertex0_focal_distance_shift10 * MATH_Q12_ONE) / vertex0_view_z_shift10;
778 int64_t vertex0_screen_x_product_shift10 = (int64_t)vertex0_perspective_scale_shift10 * vertex0_view_x_shift10;
779 int32_t vertex0_screen_x_shift10 = (((int32_t)(uint32_t)((uint64_t)vertex0_screen_x_product_shift10 >> 12)) >> 0xc) + (Graphics_WindowWidth >> 1);
780 int64_t vertex0_screen_y_product_shift10 = (int64_t)vertex0_perspective_scale_shift10 * vertex0_view_y_scale10;
781 int32_t vertex0_screen_y_shift10 = (((int32_t)(uint32_t)((uint64_t)vertex0_screen_y_product_shift10 >> 12)) >> 0xc) + (Graphics_WindowHeight >> 1);
782
783 if (vertex0_screen_x_shift10 != (int32_t)(int16_t)vertex0_screen_x_shift10 || vertex0_screen_y_shift10 != (int32_t)(int16_t)vertex0_screen_y_shift10)
784 g_transformedVertex1ViewZ = render_list_state->projection_near_fp;
785 else
786 {
787 g_transformedVertex1ScreenX = (uint16_t)vertex0_screen_x_shift10;
788 g_transformedVertex1ScreenY = (uint16_t)vertex0_screen_y_shift10;
789 }
790 }
791
792 int32_t vertex1_index_shift10 = (uint16_t)(polygon_ref->vertex_indices[1]);
793 struct Math_Vec3I16 *vertex1_position_shift10 = &runtime_vertices[vertex1_index_shift10].pos;
794 int32_t vertex1_z_scale10 = (int32_t)runtime_vertices[vertex1_index_shift10].pos.z;
795 int32_t vertex1_local_y_shift10 = (int32_t)vertex1_position_shift10->y;
796 int32_t vertex1_local_x_shift10 = (int32_t)vertex1_position_shift10->x;
797 int32_t vertex1_view_z_shift10 = (((int32_t)g_cameraTransformMatrix.m20 * vertex1_local_x_shift10 + (int32_t)g_cameraTransformMatrix.m21 * vertex1_local_y_shift10 + (int32_t)g_cameraTransformMatrix.m22 * vertex1_z_scale10) >> 0xa) + g_nodeViewTranslationZ;
798 int32_t vertex1_view_x_shift10 = (((int32_t)g_cameraTransformMatrix.m00 * vertex1_local_x_shift10 + (int32_t)g_cameraTransformMatrix.m01 * vertex1_local_y_shift10 + (int32_t)g_cameraTransformMatrix.m02 * vertex1_z_scale10) >> 0xa) + g_nodeViewTranslationX;
799 int32_t vertex1_view_y_precise_shift10 = (((int32_t)g_cameraTransformMatrix.m10 * vertex1_local_x_shift10 + (int32_t)g_cameraTransformMatrix.m11 * vertex1_local_y_shift10 + (int32_t)g_cameraTransformMatrix.m12 * vertex1_z_scale10) >> 0xa) + g_nodeViewTranslationY;
800
801 out_batch->view_vertices[1].x =
802 (float)((long double)vertex1_view_x_shift10 * (long double)0.015625f);
803
804 int32_t vertex1_view_y_scale10 = vertex1_view_y_precise_shift10;
805
806 out_batch->view_vertices[1].y =
807 (float)((long double)vertex1_view_y_scale10 * (long double)0.015625f);
808 out_batch->view_vertices[1].z =
809 (float)((long double)vertex1_view_z_shift10 * (long double)0.015625f);
810
811 struct Graphics_Render_ListState *vertex1_list_state_shift10 = render_list_state;
812
813 g_transformedVertex2ViewZ = vertex1_view_z_shift10;
814
815 if (vertex1_view_z_shift10 > vertex1_list_state_shift10->projection_near_fp)
816 {
817 int32_t vertex1_focal_distance_shift10 = vertex1_list_state_shift10->focal_distance;
818 int32_t vertex1_perspective_scale_shift10 =
819 ((int64_t)vertex1_focal_distance_shift10 * MATH_Q12_ONE) / vertex1_view_z_shift10;
820 int64_t vertex1_screen_x_product_shift10 = (int64_t)vertex1_perspective_scale_shift10 * vertex1_view_x_shift10;
821 int32_t vertex1_screen_x_shift10 = (((int32_t)(uint32_t)((uint64_t)vertex1_screen_x_product_shift10 >> 12)) >> 0xc) + (Graphics_WindowWidth >> 1);
822 int64_t vertex1_screen_y_product_shift10 = (int64_t)vertex1_perspective_scale_shift10 * vertex1_view_y_scale10;
823 int32_t vertex1_screen_y_shift10 = (((int32_t)(uint32_t)((uint64_t)vertex1_screen_y_product_shift10 >> 12)) >> 0xc) + (Graphics_WindowHeight >> 1);
824
825 if (vertex1_screen_x_shift10 != (int32_t)(int16_t)vertex1_screen_x_shift10 || vertex1_screen_y_shift10 != (int32_t)(int16_t)vertex1_screen_y_shift10)
826 g_transformedVertex2ViewZ = render_list_state->projection_near_fp;
827 else
828 {
829 g_transformedVertex2ScreenX = (uint16_t)vertex1_screen_x_shift10;
830 g_transformedVertex2ScreenY = (uint16_t)vertex1_screen_y_shift10;
831 }
832 }
833
834 int32_t vertex2_index_shift10 = (uint16_t)(polygon_ref->vertex_indices[2]);
835 struct Math_Vec3I16 *vertex2_position_shift10 = &runtime_vertices[vertex2_index_shift10].pos;
836 int32_t vertex2_z_scale10 = (int32_t)runtime_vertices[vertex2_index_shift10].pos.z;
837 int32_t vertex2_local_y_shift10 = (int32_t)vertex2_position_shift10->y;
838 int32_t vertex2_local_x_shift10 = (int32_t)vertex2_position_shift10->x;
839 int32_t vertex2_view_z_shift10 = (((int32_t)g_cameraTransformMatrix.m20 * vertex2_local_x_shift10 + (int32_t)g_cameraTransformMatrix.m21 * vertex2_local_y_shift10 + (int32_t)g_cameraTransformMatrix.m22 * vertex2_z_scale10) >> 0xa) + g_nodeViewTranslationZ;
840 int32_t vertex2_view_x_shift10 = (((int32_t)g_cameraTransformMatrix.m00 * vertex2_local_x_shift10 + (int32_t)g_cameraTransformMatrix.m01 * vertex2_local_y_shift10 + (int32_t)g_cameraTransformMatrix.m02 * vertex2_z_scale10) >> 0xa) + g_nodeViewTranslationX;
841 int32_t vertex2_view_y_precise_shift10 = (((int32_t)g_cameraTransformMatrix.m10 * vertex2_local_x_shift10 + (int32_t)g_cameraTransformMatrix.m11 * vertex2_local_y_shift10 + (int32_t)g_cameraTransformMatrix.m12 * vertex2_z_scale10) >> 0xa) + g_nodeViewTranslationY;
842
843 out_batch->view_vertices[2].x =
844 (float)((long double)vertex2_view_x_shift10 * (long double)0.015625f);
845
846 int32_t vertex2_view_y_scale10 = vertex2_view_y_precise_shift10;
847
848 out_batch->view_vertices[2].y =
849 (float)((long double)vertex2_view_y_scale10 * (long double)0.015625f);
850 out_batch->view_vertices[2].z =
851 (float)((long double)vertex2_view_z_shift10 * (long double)0.015625f);
852
853 struct Graphics_Render_ListState *vertex2_list_state_shift10 = render_list_state;
854
855 g_transformedVertex3ViewZ = vertex2_view_z_shift10;
856
857 if (vertex2_view_z_shift10 > vertex2_list_state_shift10->projection_near_fp)
858 {
859 int32_t vertex2_focal_distance_shift10 = vertex2_list_state_shift10->focal_distance;
860 int32_t vertex2_perspective_scale_shift10 =
861 ((int64_t)vertex2_focal_distance_shift10 * MATH_Q12_ONE) / vertex2_view_z_shift10;
862 int64_t vertex2_screen_x_product_shift10 = (int64_t)vertex2_perspective_scale_shift10 * vertex2_view_x_shift10;
863 int32_t vertex2_screen_x_shift10 = (((int32_t)(uint32_t)((uint64_t)vertex2_screen_x_product_shift10 >> 12)) >> 0xc) + (Graphics_WindowWidth >> 1);
864 int64_t vertex2_screen_y_product_shift10 = (int64_t)vertex2_perspective_scale_shift10 * vertex2_view_y_scale10;
865 int32_t vertex2_screen_y_shift10 = (((int32_t)(uint32_t)((uint64_t)vertex2_screen_y_product_shift10 >> 12)) >> 0xc) + (Graphics_WindowHeight >> 1);
866
867 if (vertex2_screen_x_shift10 != (int32_t)(int16_t)vertex2_screen_x_shift10 || vertex2_screen_y_shift10 != (int32_t)(int16_t)vertex2_screen_y_shift10)
868 g_transformedVertex3ViewZ = render_list_state->projection_near_fp;
869 else
870 {
871 g_transformedVertex3ScreenX = (uint16_t)vertex2_screen_x_shift10;
872 g_transformedVertex3ScreenY = (uint16_t)vertex2_screen_y_shift10;
873 }
874 }
875
876 if (!(polygon_ref->flags & POLYGON_TRIANGLE))
877 {
878 fourth_projected_vertex = (struct Graphics_Render_ScreenVertexI16Z *)&g_transformedVertex4ScreenX;
879
880 int32_t vertex3_index_shift10 = (uint16_t)(polygon_ref->vertex_indices[3]);
881 struct Math_Vec3I16 *vertex3_position_shift10 = &runtime_vertices[vertex3_index_shift10].pos;
882 int32_t vertex3_z_scale10 =
883 (int32_t)runtime_vertices[vertex3_index_shift10].pos.z;
884 int32_t vertex3_local_y_shift10 = (int32_t)vertex3_position_shift10->y;
885 int32_t vertex3_local_x_shift10 = (int32_t)vertex3_position_shift10->x;
886 int32_t vertex3_view_z_shift10 = ((
887 (int32_t)g_cameraTransformMatrix.m20 * vertex3_local_x_shift10 + (int32_t)g_cameraTransformMatrix.m21 * vertex3_local_y_shift10 + (int32_t)g_cameraTransformMatrix.m22 * vertex3_z_scale10) >>
888 0xa) +
890 int32_t vertex3_view_x_shift10 = (((int32_t)g_cameraTransformMatrix.m00 * vertex3_local_x_shift10 + (int32_t)g_cameraTransformMatrix.m01 * vertex3_local_y_shift10 + (int32_t)g_cameraTransformMatrix.m02 * vertex3_z_scale10) >> 0xa) + g_nodeViewTranslationX;
891 int32_t vertex3_view_y_precise_shift10 = (((int32_t)g_cameraTransformMatrix.m10 * vertex3_local_x_shift10 + (int32_t)g_cameraTransformMatrix.m11 * vertex3_local_y_shift10 + (int32_t)g_cameraTransformMatrix.m12 * vertex3_z_scale10) >> 0xa) + g_nodeViewTranslationY;
892
893 projected_axis_or_polygon_flags = vertex3_view_z_shift10;
894 out_batch->view_vertices[3].x =
895 (float)((long double)vertex3_view_x_shift10 * (long double)0.015625f);
896
897 int32_t vertex3_view_y_scale10 = vertex3_view_y_precise_shift10;
898
899 out_batch->view_vertices[3].y =
900 (float)((long double)vertex3_view_y_scale10 * (long double)0.015625f);
901 out_batch->view_vertices[3].z =
902 (float)((long double)vertex3_view_z_shift10 * (long double)0.015625f);
903
904 struct Graphics_Render_ListState *vertex3_list_state_shift10 = render_list_state;
905
906 g_transformedVertex4ViewZ = projected_axis_or_polygon_flags;
907
908 if (projected_axis_or_polygon_flags > vertex3_list_state_shift10->projection_near_fp)
909 {
910 int32_t vertex3_focal_distance_shift10 = vertex3_list_state_shift10->focal_distance;
911 int32_t vertex3_perspective_scale_shift10 =
912 ((int64_t)vertex3_focal_distance_shift10 * MATH_Q12_ONE) / vertex3_view_z_shift10;
913 int64_t vertex3_screen_x_product_shift10 = (int64_t)vertex3_perspective_scale_shift10 * vertex3_view_x_shift10;
914
915 fourth_screen_x = (((int32_t)(uint32_t)((uint64_t)vertex3_screen_x_product_shift10 >> 12)) >> 0xc) + (Graphics_WindowWidth >> 1);
916
917 int64_t vertex3_screen_y_product_shift10 = (int64_t)vertex3_perspective_scale_shift10 * vertex3_view_y_scale10;
918
919 fourth_screen_y_q12 = (int32_t)(uint32_t)((uint64_t)vertex3_screen_y_product_shift10 >> 12);
920
921 projected_axis_or_polygon_flags = (fourth_screen_y_q12 >> 0xc) + (Graphics_WindowHeight >> 1);
922
923 if (fourth_screen_x != (int32_t)(int16_t)fourth_screen_x || projected_axis_or_polygon_flags != (int32_t)(int16_t)projected_axis_or_polygon_flags)
924 {
925 g_transformedVertex4ViewZ = render_list_state->projection_near_fp;
926 }
927 else
928 {
929 g_transformedVertex4ScreenX = (uint16_t)fourth_screen_x;
930 g_transformedVertex4ScreenY = (uint16_t)projected_axis_or_polygon_flags;
931 }
932 }
933 }
934 else
935 {
936 projected_axis_or_polygon_flags = 0;
937 memset(&fourth_projected_vertex, 0, 4);
938 out_batch->view_vertices[3].z = 0.0f;
939 out_batch->view_vertices[3].y = 0.0f;
940 out_batch->view_vertices[3].x = 0;
941 }
942 }
943
944 struct Material_TableEntry *material_ref = polygon_ref->material_ref;
945
946 g_currentRenderMaterial = material_ref;
947
948 uint16_t polygon_flags = polygon_ref->flags;
949
950 // faceFlags winding fixup: if DOUBLE_SIDED(0x20) and material has tiling, toggle
951 // FLIP_WINDING(0x400) based on dot product sign
952 if ((uint8_t)polygon_flags & POLYGON_TWO_SIDED && material_ref->flags & MATERIAL_TWO_SIDED)
953 {
954 if (view_plane_distance >= 0) // Runtime 0x0400 toggle (RPB path): same logic as RSG
955 {
956 if ((uint8_t)((uint32_t)polygon_flags >> 8) & (POLYGON_BACK_FACING >> 8))
957 polygon_ref->flags = (polygon_flags & (uint16_t)~POLYGON_BACK_FACING);
958 }
959 else if (!((uint8_t)((uint32_t)polygon_flags >> 8) & (POLYGON_BACK_FACING >> 8)))
960 {
961 polygon_flags |= POLYGON_BACK_FACING;
962 polygon_ref->flags = (uint16_t)polygon_flags;
963 }
964 }
965
966 int32_t initial_uv_first_index = (uint16_t)(polygon_ref->vertex_indices[0]);
967 struct Mesh_RuntimeVertex *initial_uv_vertices[0x4];
968
969 initial_uv_vertices[0] = &runtime_vertices[initial_uv_first_index];
970
971 int32_t initial_uv_second_index = (uint16_t)(polygon_ref->vertex_indices[1]);
972 int32_t initial_uv_third_index = (uint16_t)(polygon_ref->vertex_indices[2]);
973 void *initial_uv_second_vertex = &runtime_vertices[initial_uv_second_index];
974
975 initial_uv_vertices[1] = initial_uv_second_vertex;
976
977 bool initial_uv_has_fourth_vertex = !(polygon_ref->flags & POLYGON_TRIANGLE);
978
979 initial_uv_vertices[2] = &runtime_vertices[initial_uv_third_index];
980
981 if (initial_uv_has_fourth_vertex)
982 {
983 int32_t initial_uv_fourth_index = (uint16_t)(polygon_ref->vertex_indices[3]);
984
985 initial_uv_vertices[3] = &runtime_vertices[initial_uv_fourth_index];
986 }
987 else
988 initial_uv_vertices[3] = 0;
989
991 initial_uv_vertices);
992 out_batch->depth_bias = 0.0f;
993
994 float saved_first_view_x = out_batch->view_vertices[0].x;
995 float saved_first_view_y = out_batch->view_vertices[0].y;
996 float saved_first_view_z = out_batch->view_vertices[0].z;
997 float saved_second_view_x = out_batch->view_vertices[1].x;
998 float saved_second_view_y = out_batch->view_vertices[1].y;
999 float saved_second_view_z = out_batch->view_vertices[1].z;
1000 float saved_third_view_x = out_batch->view_vertices[2].x;
1001 float saved_third_view_y = out_batch->view_vertices[2].y;
1002 float saved_third_view_z = out_batch->view_vertices[2].z;
1003 struct Math_Vec3F *fourth_view_vertex = &out_batch->view_vertices[3];
1004 float saved_fourth_view_x = out_batch->view_vertices[3].x;
1005 float saved_fourth_view_y = out_batch->view_vertices[3].y;
1006 float saved_fourth_view_z = out_batch->view_vertices[3].z;
1007 int32_t face_depth_bias_q15 = (int32_t)polygon_ref->depth_bias_q12 << 3;
1008
1010 out_batch->depth_bias = 10000.0f;
1011 else if (face_depth_bias_q15)
1012 {
1013 int32_t game_mode = g_currentGameMode;
1014
1015 if (game_mode < 0 || game_mode == GAME_MODE_RENDER_BATCH || game_mode == GAME_MODE_FPS_DEBUG)
1016 {
1017 long double scaled_depth_bias = (long double)face_depth_bias_q15 * (long double)0.015625f;
1018
1019 out_batch->depth_bias = (float)scaled_depth_bias;
1020
1021 long double minimum_depth_bias = (long double)256.0f;
1022
1023 if (scaled_depth_bias < minimum_depth_bias)
1024 out_batch->depth_bias = 0.0f;
1025 else
1026 {
1027 long double first_ignored_depth_bias = (long double)320.0f;
1028
1029 if (((uint8_t)((((scaled_depth_bias < first_ignored_depth_bias) ? X87_STATUS_C0 : 0) | ((isunordered(scaled_depth_bias, first_ignored_depth_bias)) ? X87_STATUS_C2 : 0) | ((scaled_depth_bias == first_ignored_depth_bias) ? X87_STATUS_C3 : 0) | X87_STATUS_TOP_MASK) >> 8)) & (X87_STATUS_C3 >> 8))
1030 out_batch->depth_bias = 0.0f;
1031 else
1032 {
1033 long double second_ignored_depth_bias = (long double)640.0f;
1034
1035 if (((uint8_t)((((scaled_depth_bias < second_ignored_depth_bias) ? X87_STATUS_C0 : 0) | ((isunordered(scaled_depth_bias, second_ignored_depth_bias)) ? X87_STATUS_C2 : 0) | ((scaled_depth_bias == second_ignored_depth_bias) ? X87_STATUS_C3 : 0)) >> 8)) & (X87_STATUS_C3 >> 8))
1036 out_batch->depth_bias = 0.0f;
1037 }
1038 }
1039 }
1040 }
1041
1042 struct Material_TableEntry *base_material = g_currentRenderMaterial;
1044 base_material->next_material_entry;
1045 struct Material_TableEntry *pending_base_material;
1046
1047 if (!next_material_entry || !(((uint8_t *)&base_material->flags)[2] & (MATERIAL_LINKED_PASS >> 16)))
1048 pending_base_material = NULL;
1049 else
1050 {
1051 pending_base_material = base_material;
1052
1054
1055 int32_t linked_uv_first_index = (uint16_t)(polygon_ref->vertex_indices[0]);
1056 struct Mesh_RuntimeVertex *linked_uv_vertices[0x4];
1057
1058 linked_uv_vertices[0] = &runtime_vertices[linked_uv_first_index];
1059
1060 int32_t linked_uv_second_index = (uint16_t)(polygon_ref->vertex_indices[1]);
1061
1062 linked_uv_vertices[1] = &runtime_vertices[linked_uv_second_index];
1063
1064 int32_t linked_uv_third_index = (uint16_t)(polygon_ref->vertex_indices[2]);
1065 void *linked_uv_later_vertex = &runtime_vertices[linked_uv_third_index];
1066
1067 linked_uv_vertices[2] = linked_uv_later_vertex;
1068
1069 uint8_t linked_face_flags = polygon_ref->flags;
1070
1071 if (!(linked_face_flags & POLYGON_TRIANGLE))
1072 {
1073 int32_t linked_uv_fourth_index = (uint16_t)(polygon_ref->vertex_indices[3]);
1074
1075 linked_uv_later_vertex = &runtime_vertices[linked_uv_fourth_index];
1076 linked_uv_vertices[3] = linked_uv_later_vertex;
1077 }
1078 else
1079 linked_uv_vertices[3] = 0;
1080
1081 Graphics_SetPolygonUVs(node, out_batch, next_material_entry, polygon_ref->uv_index_or_sentinel,
1082 linked_uv_vertices);
1083 }
1084
1085 uint32_t flags = out_batch->flags;
1086 int32_t depth_bucket_index = 0;
1087
1088 // Render cmd flags 0x400000|0x80: HAS_TRANSPARENCY/SORT -- determines sort bucket for depth
1089 // sorting
1090 if (flags & MATERIAL_DEPTH_SORT || (uint8_t)flags & MATERIAL_BLEND_ENABLED)
1091 {
1092 int32_t depth_comparison_flag = 1;
1093 long double three_vertex_depth_sum = (long double)saved_third_view_z + (long double)saved_second_view_z + (long double)saved_first_view_z;
1094 long double first_sort_depth = (long double)saved_first_view_z;
1095 long double second_sort_depth = (long double)saved_second_view_z;
1096
1097 if (!(((uint8_t)((((first_sort_depth < second_sort_depth) ? X87_STATUS_C0 : 0) | ((isunordered(first_sort_depth, second_sort_depth)) ? X87_STATUS_C2 : 0) | ((first_sort_depth == second_sort_depth) ? X87_STATUS_C3 : 0) | X87_STATUS_TOP_MASK) >> 8)) & (X87_STATUS_C3 >> 8)))
1098 depth_comparison_flag = 0;
1099
1100 long double depth_equality_as_float = (long double)depth_comparison_flag;
1101 long double third_sort_depth = (long double)saved_third_view_z;
1102
1103 long double scaled_sort_depth;
1104
1105 if (((uint8_t)((((depth_equality_as_float < third_sort_depth) ? X87_STATUS_C0 : 0) | ((isunordered(depth_equality_as_float, third_sort_depth)) ? X87_STATUS_C2 : 0) | ((depth_equality_as_float == third_sort_depth) ? X87_STATUS_C3 : 0) | X87_STATUS_TOP_MASK) >> 8)) & (X87_STATUS_C3 >> 8))
1106 scaled_sort_depth = (long double)saved_first_view_z * (long double)g_levelRenderDistance;
1107 else if (!fourth_projected_vertex)
1108 scaled_sort_depth = three_vertex_depth_sum * (long double)g_levelRenderDistanceThird;
1109 else
1110 scaled_sort_depth = (three_vertex_depth_sum + (long double)saved_fourth_view_z) * (long double)g_levelRenderDistanceQuarter;
1111
1112 int32_t integer_sort_depth = __ftol_msvc(scaled_sort_depth);
1113
1114 if (integer_sort_depth > 0x3fff)
1115 integer_sort_depth = 0x3fff;
1116
1117 depth_bucket_index = 0x3fff - integer_sort_depth;
1118
1119 if (0x3fff - integer_sort_depth < 1)
1120 depth_bucket_index = 1;
1121 else if (0x3fff - integer_sort_depth > 0x3fff)
1122 depth_bucket_index = 0x3fff;
1123 }
1124
1125 while (true)
1126 {
1127 out_batch->face_normal_dot = face_depth_bias_q15;
1128 // Calculate fog depth from material depth value:
1129 // fogDepth = depth << 3 (multiply by 8)
1130 // Convert to float and store in vertex structure
1131 Graphics_ComputeVertexColors(runtime_vertices, polygon_ref,
1132 out_batch->vertex_colors);
1133
1135 {
1136 int32_t fade_scale_q12 = 0x1000 - (*(uint32_t *)&g_renderingStateFlag);
1137 uint32_t first_color = out_batch->vertex_colors[0];
1138 int32_t first_green = (uint8_t)((uint8_t)((uint32_t)first_color >> 8));
1139 uint32_t second_color = out_batch->vertex_colors[1];
1140
1141 out_batch->vertex_colors[0] = (((first_green * fade_scale_q12) & 0xff000) | ((uint32_t)(uint8_t)first_color * fade_scale_q12) >> 8) >> 4 | (((first_color >> 0xc & 0xff0) * fade_scale_q12) & 0xff0000);
1142
1143 int32_t second_green = (uint8_t)((uint8_t)((uint32_t)second_color >> 8));
1144 uint32_t third_color = out_batch->vertex_colors[2];
1145
1146 out_batch->vertex_colors[1] = (((second_green * fade_scale_q12) & 0xff000) | ((uint32_t)(uint8_t)second_color * fade_scale_q12) >> 8) >> 4 | (((second_color >> 0xc & 0xff0) * fade_scale_q12) & 0xff0000);
1147
1148 // Handle double-sided polygons: process backface if backFacePtr != NULL
1149 // Reuses same vertices but with flipped normal for lighting
1150 int32_t third_green = (uint8_t)((uint8_t)((uint32_t)third_color >> 8));
1151 uint32_t fourth_color = out_batch->vertex_colors[3];
1152
1153 out_batch->vertex_colors[2] = (((third_green * fade_scale_q12) & 0xff000) | ((uint32_t)(uint8_t)third_color * fade_scale_q12) >> 8) >> 4 | (((third_color >> 0xc & 0xff0) * fade_scale_q12) & 0xff0000);
1154
1155 int32_t fourth_green = (uint8_t)((uint8_t)((uint32_t)fourth_color >> 8));
1156
1157 runtime_vertices = saved_runtime_vertices;
1158 out_batch->vertex_colors[3] = (((fourth_green * fade_scale_q12) & 0xff000) | ((uint32_t)(uint8_t)fourth_color * fade_scale_q12) >> 8) >> 4 | (((fourth_color >> 0xc & 0xff0) * fade_scale_q12) & 0xff0000);
1159 }
1160
1162 {
1163 if ((const void *)polygon_ref == (const void *)g_renderingDepthMode)
1164 {
1165 // Calculate alpha sort depth from fog distance if material has transparency/blend flags (0x400000 or 0x80).
1166 // Depth = 0x3fff - fogDistance, clamped [1..0x3fff]
1167 out_batch->vertex_colors[0] = 0xff000000;
1168 out_batch->vertex_colors[1] = 0xff0000;
1169 out_batch->vertex_colors[2] = 0xff00;
1170 out_batch->vertex_colors[3] = 0xffffff00;
1171 }
1172 else if (Graphics_IsPolygonInDebugList(polygon_ref))
1173 {
1174 out_batch->vertex_colors[0] = 0xff0000;
1175 out_batch->vertex_colors[1] = 0xff00;
1176 out_batch->vertex_colors[2] = 0xff;
1177 out_batch->vertex_colors[3] = 0xffffff;
1178 }
1179 }
1180
1181 uint32_t insertion_flags = out_batch->flags;
1182
1183 // Apply Color_AdjustVertexColors: modulates vertex colors with material color, lighting, and global
1184 // brightness (DAT_00634b12 fade factor)
1185 if (insertion_flags & MATERIAL_DEPTH_SORT || (uint8_t)insertion_flags & MATERIAL_BLEND_ENABLED)
1186 {
1187 int32_t insertion_bucket_index = depth_bucket_index;
1188
1189 if (insertion_bucket_index < 1)
1190 {
1191 insertion_bucket_index = 1;
1192 depth_bucket_index = 1;
1193 }
1194
1195 out_batch->next_in_bucket =
1196 render_list_state->polygon_queue_heads[insertion_bucket_index];
1197 render_list_state->polygon_queue_heads[insertion_bucket_index] = out_batch;
1198 }
1199 else if (out_batch->texture_desc || g_currentGameMode != GAME_MODE_GAMEPLAY_3D)
1200 {
1201 out_batch->next_in_bucket = render_list_state->sorted_list_head;
1202 render_list_state->sorted_list_head = out_batch;
1203 }
1204 else
1205 {
1206 out_batch->next_in_bucket = render_list_state->sorted_list_buckets[0x3ffe];
1207 render_list_state->sorted_list_buckets[0x3ffe] = out_batch;
1208 }
1209
1210 int32_t first_packed_screen = (*(uint32_t *)&g_transformedVertex1ScreenX);
1211
1212 out_batch->screen_vertices[0].screen.x = (uint16_t)first_packed_screen;
1213 out_batch->screen_vertices[0].screen.y =
1214 (uint16_t)((uint32_t)first_packed_screen >> 16);
1216
1217 int32_t second_packed_screen = (*(uint32_t *)&g_transformedVertex2ScreenX);
1218
1219 out_batch->screen_vertices[1].screen.x = (uint16_t)second_packed_screen;
1220 out_batch->screen_vertices[1].screen.y =
1221 (uint16_t)((uint32_t)second_packed_screen >> 16);
1222 // Queue polygon to appropriate render list based on blend mode:
1223 // - Opaque: g_activeCameraPtr+0xcc (no sort)
1224 // - Transparent: g_activeCameraPtr+0xcc+depth*4 (depth sorted, 0x3fff buckets)
1226
1227 int32_t third_packed_screen = (*(uint32_t *)&g_transformedVertex3ScreenX);
1228
1229 out_batch->screen_vertices[2].screen.x = (uint16_t)third_packed_screen;
1230 out_batch->screen_vertices[2].screen.y =
1231 (uint16_t)((uint32_t)third_packed_screen >> 16);
1232
1233 int32_t third_projected_depth = g_transformedVertex3ViewZ;
1234
1235 out_batch->view_vertices[0].x = saved_first_view_x;
1236 out_batch->view_vertices[0].y = saved_first_view_y;
1237 out_batch->screen_vertices[2].z = third_projected_depth;
1238 out_batch->view_vertices[1].x = saved_second_view_x;
1239 out_batch->view_vertices[1].y = saved_second_view_y;
1240 out_batch->view_vertices[0].z = saved_first_view_z;
1241 out_batch->view_vertices[2].x = saved_third_view_x;
1242 out_batch->view_vertices[2].y = saved_third_view_y;
1243
1244 struct Graphics_Render_ScreenVertexI16Z *fourth_screen_source =
1245 fourth_projected_vertex;
1246
1247 out_batch->view_vertices[1].z = saved_second_view_z;
1248 out_batch->view_vertices[2].z = saved_third_view_z;
1249
1250 if (fourth_screen_source)
1251 {
1252 struct Math_ScreenPointI16 screen;
1253
1254 memcpy(&screen, &fourth_screen_source->screen, sizeof screen);
1255 out_batch->screen_vertices[3].screen.x = screen.x;
1256 out_batch->screen_vertices[3].screen.y = screen.y;
1257 out_batch->screen_vertices[3].z = fourth_screen_source->z;
1258 *(struct Math_Vec3F *)fourth_view_vertex = (struct Math_Vec3F){
1259 .x = saved_fourth_view_x,
1260 .y = saved_fourth_view_y,
1261 .z = saved_fourth_view_z};
1262 }
1263 else
1264 {
1265 out_batch->screen_vertices[3].screen.x = 0x4000;
1266 out_batch->screen_vertices[3].screen.y = 0x4000;
1267 }
1268
1269 struct Material_TableEntry *second_pass_material = pending_base_material;
1270
1271 batch_index = g_currentPolygonBatchIndex + 1;
1272 // Secondary materials can emit more records than the input count; batch capacity is unchecked.
1273 g_currentPolygonBatchIndex = batch_index;
1274
1275 if (!second_pass_material)
1276 break;
1277
1278 g_currentRenderMaterial = second_pass_material;
1279
1280 int32_t second_pass_first_index = (uint16_t)(polygon_ref->vertex_indices[0]);
1281
1282 pending_base_material = NULL;
1283 out_batch = &(&g_polygonBatchRecords)[batch_index];
1284
1285 struct Mesh_RuntimeVertex *second_pass_uv_vertices[0x4];
1286
1287 second_pass_uv_vertices[0] = &runtime_vertices[second_pass_first_index];
1288
1289 int32_t second_pass_second_index = (uint16_t)(polygon_ref->vertex_indices[1]);
1290
1291 second_pass_uv_vertices[1] = &runtime_vertices[second_pass_second_index];
1292
1293 int32_t second_pass_third_index = (uint16_t)(polygon_ref->vertex_indices[2]);
1294 void *second_pass_later_vertex = &runtime_vertices[second_pass_third_index];
1295
1296 second_pass_uv_vertices[2] = second_pass_later_vertex;
1297
1298 uint8_t second_pass_face_flags = polygon_ref->flags;
1299
1300 if (!(second_pass_face_flags & POLYGON_TRIANGLE))
1301 {
1302 int32_t second_pass_fourth_index = (uint16_t)(polygon_ref->vertex_indices[3]);
1303
1304 second_pass_later_vertex = &runtime_vertices[second_pass_fourth_index];
1305 second_pass_uv_vertices[3] = second_pass_later_vertex;
1306 }
1307 else
1308 second_pass_uv_vertices[3] = 0;
1309
1310 Graphics_SetPolygonUVs(node, out_batch, second_pass_material, polygon_ref->uv_index_or_sentinel,
1311 second_pass_uv_vertices);
1312 out_batch->depth_bias = 0.0f;
1313 out_batch->view_vertices[0].x = saved_first_view_x;
1314 out_batch->view_vertices[0].y = saved_first_view_y;
1315 out_batch->view_vertices[0].z = saved_first_view_z;
1316 out_batch->view_vertices[1].x = saved_second_view_x;
1317 out_batch->view_vertices[1].y = saved_second_view_y;
1318 out_batch->view_vertices[1].z = saved_second_view_z;
1319 out_batch->view_vertices[2].x = saved_third_view_x;
1320 out_batch->view_vertices[2].y = saved_third_view_y;
1321 out_batch->view_vertices[2].z = saved_third_view_z;
1322 fourth_view_vertex = &out_batch->view_vertices[3];
1323 out_batch->view_vertices[3] = (struct Math_Vec3F){
1324 .x = saved_fourth_view_x,
1325 .y = saved_fourth_view_y,
1326 .z = saved_fourth_view_z};
1327 }
1328 }
1329 }
1330
1331 polygon_refs = &polygon_ref[1];
1332 count -= 1;
1333 } while (count);
1334}
#define g_cameraTransformMatrix
Definition globals.h:81
#define g_renderingDepthMode
Definition globals.h:87
#define GAME_TRANSITION_COLOR_FADE
Definition constants.h:30
@ GAME_MODE_FPS_DEBUG
Definition enums.h:32
@ GAME_MODE_GAMEPLAY_3D
Definition enums.h:38
@ GAME_MODE_RENDER_BATCH
Definition enums.h:26
#define Game_FrameTransitionFlags
Definition globals.h:47
#define g_currentGameMode
Definition globals.h:74
#define MODEL_RUNTIME_USE_VERTEX_NORMALS
Definition constants.h:10
#define POLYGON_BACK_FACING
Definition constants.h:9
#define GRAPHICS_CAP_DISABLE_BACKFACE_CULL
Definition constants.h:21
#define POLYGON_TRIANGLE
Definition constants.h:7
#define POLYGON_TWO_SIDED
Definition constants.h:8
BOOL Graphics_IsPolygonInDebugList(struct Graphics_Render_PolygonRenderRef *polygon_ref)
Definition meshes.c:515
void Graphics_SetPolygonUVs(struct Scene_Node *node, struct Graphics_Render_PolygonBatchRecord *out_batch, struct Material_TableEntry *polygon_data, int16_t uv_index_or_sentinel, struct Mesh_RuntimeVertex **polygon_vertices)
Definition meshes.c:335
void Graphics_ComputeVertexColors(struct Mesh_RuntimeVertex *runtime_vertices, struct Graphics_Render_PolygonRenderRef *polygon_ref, uint32_t *out_colors)
Definition meshes.c:557
#define g_polygonBatchRecords
Definition globals.h:290
#define Graphics_WindowWidth
Definition globals.h:140
#define Graphics_RenderPolygonBatch_ProjectionScratch3
Definition globals.h:251
#define g_graphicsCapabilities
Definition globals.h:284
#define render_list_state
Definition globals.h:152
#define Graphics_RenderPolygonBatch_ProjectionScratch2
Definition globals.h:248
#define Graphics_WindowHeight
Definition globals.h:143
#define Graphics_RenderPolygonBatch_ProjectionScratch1
Definition globals.h:245
#define graphics_max_primitives_per_batch_d3d
Definition globals.h:242
#define g_levelRenderDistanceQuarter
Definition globals.h:121
#define g_levelRenderDistance
Definition globals.h:115
#define g_levelRenderDistanceThird
Definition globals.h:124
#define MATERIAL_DEPTH_SORT
Definition constants.h:39
#define MATERIAL_BLEND_ENABLED
Definition constants.h:29
#define MATERIAL_LINKED_PASS
Definition constants.h:38
#define MATERIAL_TWO_SIDED
Definition constants.h:24
#define g_currentRenderMaterial
Definition globals.h:15
#define MATH_Q12_ONE
Definition constants.h:9
int32_t Math_SqrtFP12(uint32_t value)
#define data_49482a
Definition opaque_data.h:39
void Graphics_RenderPolygonBatch(struct Scene_Node *node, struct Graphics_Render_PolygonRenderRef *polygon_refs, int32_t count)
Definition polygons.c:28
#define X87_STATUS_C2
Definition constants.h:77
#define X87_STATUS_TOP_MASK
Definition constants.h:79
#define X87_STATUS_C0
Definition constants.h:76
#define X87_STATUS_C3
Definition constants.h:78
int64_t __ftol_msvc(long double value)
@ SCENE_NODE_SCALED_GEOMETRY
Definition constants.h:9
struct Graphics_Render_ScreenVertexI16Z screen_vertices[0x4]
Definition types.h:512
struct Material_RuntimeDescriptor * texture_desc
Definition types.h:496
struct Graphics_Render_PolygonBatchRecord * next_in_bucket
Definition types.h:511
struct Math_Vec3F view_vertices[0x4]
Definition types.h:513
struct Material_TableEntry * material_ref
Definition types.h:204
struct Mesh_FaceNormal * face_normal
Definition types.h:206
struct Graphics_Render_ScreenVertexI16Z::@276010272006271151340046314217100172252315123166::@167176104245357063215260323150214177117323053225 screen
struct Material_TableEntry * next_material_entry
Definition types.h:377
uint32_t flags
Definition types.h:342
float z
Definition types.h:448
float y
Definition types.h:447
float x
Definition types.h:446
int16_t z
Definition types.h:460
int16_t x
Definition types.h:458
int16_t y
Definition types.h:459
struct Math_Vec3I16 normal
Definition types.h:963
int32_t plane_dist
Definition types.h:962
struct Math_Vec3I16 pos
Definition types.h:1115
struct Mesh_RuntimeVertex * runtime_vertices
Definition types.h:144
uint8_t node_type
Definition types.h:129
uint32_t model_runtime_flags
Definition types.h:173
#define g_backfaceVertex2Ptr
Definition values.h:348
#define g_nodeViewTranslationY
Definition values.h:774
#define g_renderingStateFlag
Definition values.h:627
#define g_transformedVertex2ScreenY
Definition values.h:363
#define g_transformedVertex2ScreenX
Definition values.h:360
#define g_backfaceVertex1Ptr
Definition values.h:345
#define g_transformedVertex4ViewZ
Definition values.h:384
#define g_polyBatchVertexCount
Definition values.h:324
#define g_transformedVertex3ViewZ
Definition values.h:375
#define g_polyBatchTriCount
Definition values.h:327
#define g_transformedVertex4ScreenX
Definition values.h:378
#define g_currentPolygonBatchIndex
Definition values.h:768
#define g_viewDirectionZ
Definition values.h:765
#define g_transformedVertex1ScreenY
Definition values.h:354
#define g_transformedVertex3ScreenY
Definition values.h:372
#define g_transformedVertex1ScreenX
Definition values.h:351
#define g_nodeViewTranslationZ
Definition values.h:777
#define g_viewDirectionX
Definition values.h:759
#define g_renderBatchVertexBase
Definition values.h:321
#define g_transformedVertex3ScreenX
Definition values.h:369
#define g_transformedVertex4ScreenY
Definition values.h:381
#define g_nodeViewTranslationX
Definition values.h:771
#define g_transformedVertex1ViewZ
Definition values.h:357
#define g_polygonRenderFlags
Definition values.h:318
#define g_polyBatchTextureState
Definition values.h:342
#define g_transformedVertex2ViewZ
Definition values.h:366
#define g_polyBatchRenderFlags
Definition values.h:387
#define g_polygonHighlightMode
Definition values.h:756
#define g_viewDirectionY
Definition values.h:762