102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
sprites.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
17#include "pcdogs/math/globals.h"
18#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: 0x4027a0
27// Culls a sprite node, processes its mesh commands and renders it using the node's world transform.
29{
30 uint16_t render_flag_bits = node->render_flags;
31
33 (int32_t)node->bound_extent.y + (int32_t)node->bound_extent.x, render_flag_bits & (FRUSTUM_CULL_NODE_RENDER_MODE | FRUSTUM_CULL_SKIP_DISTANCE)))
34 return;
35
38 Graphics_RenderMeshNode((struct Scene_Node *)(node), &node->payload);
39}
40
41// Original pcdogs.exe: 0x4154d0
42// Draws a textured or untextured sprite with inferred dimensions, blending, gradient colors and optional rotation or mirroring. A null sprite selects the shared glyph context.
43void Graphics_RenderTexturedSprite(uint32_t packed_xy, uint32_t packed_wh,
44 struct Graphics_Render_SpriteContext *sprite, uint32_t render_flags,
45 uint32_t rotation_angle, uint32_t rotation_pivot_xy)
46{
47 /* Native 0x4154d0: two packed vertex formats, with UVs only for textures. */
48 if (!sprite)
50
51 struct Material_RuntimeDescriptor *texture = sprite->textureDescriptor;
52 int32_t x = (int16_t)packed_xy, y = (int16_t)(packed_xy >> 16);
53 int32_t width = (int16_t)packed_wh, height = (int16_t)(packed_wh >> 16);
54
55 if (width <= 0)
56 width = texture ? texture->dimensions_minus_1.width + 1 : 1;
57
58 if (height <= 0)
59 height = texture ? texture->dimensions_minus_1.height + 1 : 1;
60
62
63 if (!blend_mode)
64 {
65 if (sprite->flags & MATERIAL_BLEND_ENABLED)
67 }
68 else if (blend_mode >= GRAPHICS_BLEND_ADDITIVE_DIM)
69 blend_mode = 0;
70
72
73 uint32_t alpha = D3D_SetBlendMode(blend_mode);
74
76 IDirect3DDevice7 *device = g_d3dDevice7;
77
79 device->lpVtbl->SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
80
81 /* D3DFVF_XYZRHW | D3DFVF_DIFFUSE, optionally D3DFVF_TEX1. */
82 struct
83 {
84 float x, y, z, rhw;
85 uint32_t color;
86 float u, v;
87 } textured[4];
88
89 struct
90 {
91 float x, y, z, rhw;
92 uint32_t color;
93 } plain[4];
94
95 uint8_t *vertices = texture ? (uint8_t *)textured : (uint8_t *)plain;
96 size_t stride = texture ? sizeof(textured[0]) : sizeof(plain[0]);
97
98 if (texture)
99 {
100 D3D_SetTextureColorOperation(D3DTOP_MODULATE);
101
102 int32_t texture_width = texture->dimensions_minus_1.width + 1;
103 int32_t texture_height = texture->dimensions_minus_1.height + 1;
104 int32_t max_u = texture_width - 1, max_v = texture_height - 1, start_u = 0, start_v = 0;
105 uint32_t flags = sprite->flags;
106
108 {
109 start_u = sprite->subrect_uv.u;
110 max_u >>= 1;
111 }
112 else if (flags & MATERIAL_TILE_U_SECOND)
113 {
114 start_u = (texture_width >> 1) - sprite->subrect_uv.u - 1;
115 max_u >>= 1;
116 }
117
119 {
120 start_v = sprite->subrect_uv.v;
121 max_v >>= 1;
122 }
123 else if (flags & MATERIAL_TILE_V_SECOND)
124 {
125 start_v = (texture_height >> 1) - sprite->subrect_uv.v - 1;
126 max_v >>= 1;
127 }
128
129 float du = (float)((long double)(max_u + 1) / (texture_width * (max_u + 1)));
130 float dv = (float)((long double)(max_v + 1) / (texture_height * (max_v + 1)));
131 float uv[4][2] = {
132 {(float)((long double)start_u * du), (float)((long double)start_v * dv)},
133 {(float)((long double)(start_u + max_u + 1) * du), (float)((long double)start_v * dv)},
134 {(float)((long double)(start_u + max_u + 1) * du), (float)((long double)(start_v + max_v + 1) * dv)},
135 {(float)((long double)start_u * du), (float)((long double)(start_v + max_v + 1) * dv)}};
136
137 static const uint8_t uv_order[4][4] = {{0, 1, 3, 2}, {1, 0, 2, 3}, {3, 2, 0, 1}, {2, 3, 1, 0}};
138
140
141 for (unsigned i = 0; i < 4; i++)
142 {
143 textured[i].u = uv[uv_order[rotation][i]][0];
144 textured[i].v = uv[uv_order[rotation][i]][1];
145 }
146
148
149 if (g_currentBoundTextureSurface != surface)
150 {
152 device->lpVtbl->SetTexture(device, 0, surface);
153 }
154 }
155 else
156 D3D_SetTextureColorOperation(D3DTOP_DISABLE);
157
158 uint32_t top_color, bottom_color;
159
160 if (render_flags & SPRITE_RENDER_COLOR_OVERRIDE)
161 Graphics_ComputeColorGradient(&top_color, &bottom_color, render_flags);
162 else
163 top_color = bottom_color = (render_flags & SPRITE_RENDER_EXPLICIT_COLOR) ? render_flags : GRAPHICS_COLOR_WHITE_RGB;
164
165 top_color = (alpha << 24) | ((top_color & 0xff) << 16) | (top_color & 0xff00) | ((top_color >> 16) & 0xff);
166 bottom_color = (alpha << 24) | ((bottom_color & 0xff) << 16) | (bottom_color & 0xff00) | ((bottom_color >> 16) & 0xff);
167
168 float positions[4][2] = {{x, y}, {x + width, y}, {x, y + height}, {x + width, y + height}};
169
170 if (rotation_angle & 0xfff)
171 {
172 int32_t pivot_x = (int16_t)rotation_pivot_xy, pivot_y = (int16_t)(rotation_pivot_xy >> 16);
173 int32_t sine = -(Math_SinQ14(rotation_angle) >> 2);
174 int32_t cosine = Math_SinQ14(0x400 - rotation_angle) >> 2;
175
176 for (unsigned i = 0; i < 4; i++)
177 {
178 int32_t dx = __ftol_msvc((long double)positions[i][0] - pivot_x);
179 int32_t dy = __ftol_msvc((long double)positions[i][1] - pivot_y);
180
181 positions[i][0] = ((dx * cosine - dy * sine) >> 12) + pivot_x;
182 positions[i][1] = ((dy * cosine + dx * sine) >> 12) + pivot_y;
183 }
184 }
185
186 for (unsigned i = 0; i < 4; i++)
187 {
188 float *vertex_components = (float *)(vertices + i * stride);
189
190 vertex_components[0] = positions[i][0];
191 vertex_components[1] = positions[i][1];
192
193 uint32_t overlay_depth_bits = GRAPHICS_SCREEN_QUAD_DEPTH_BITS;
194
195 memcpy(&vertex_components[2], &overlay_depth_bits, 4);
196 memcpy(&vertex_components[3], &overlay_depth_bits, 4);
197
198 uint32_t color = i < 2 ? top_color : bottom_color;
199
200 memcpy(&vertex_components[4], &color, 4);
201 }
202
203 device->lpVtbl->DrawPrimitive(device, D3DPT_TRIANGLESTRIP, (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | (texture ? D3DFVF_TEX1 : 0)), vertices, 4, 0);
204
206 device->lpVtbl->SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
207}
208
209// Original pcdogs.exe: 0x423f90
210// Transforms and queues sprite triangles or quads with material chaining, sidedness, color and fade handling. The count loop and shared record allocation retain their unchecked native behavior.
211void Graphics_RenderSpritePolygons(struct Scene_Node *node, struct Graphics_Render_SpriteVertexData *transformed_vertices, struct Graphics_Render_ProjectedVertex *projected_vertices)
212{
213 float view_xyz[12];
214 int32_t *projected_slots[4];
215 struct Graphics_Render_PolygonRenderRef *variant_payload_ptr = node->variant_payload_ptr;
216 // SPRITE POLYGON RENDERER: Renders sprite quads/triangles with transformed vertices. Projects to screen
217 // space, performs backface culling, applies vertex lighting. Handles double-sided materials (flag 0x100000).
218 // Queues to depth-sorted render list. Used for 2D sprite sheets and particle effects.
219 int32_t polygons_remaining = (int32_t)(int16_t)node->variant_transform_tail;
220 struct Graphics_Render_PolygonRenderRef *polygon_ref = variant_payload_ptr;
221
222 do
223 {
224 int32_t vertex_slot = 3;
225 int16_t *fourth_projected_vertex;
226 int32_t **projected_slot_cursor = &projected_slots[3];
227 float *view_y_cursor = &view_xyz[10];
228 uint16_t *vertex_index_cursor = &variant_payload_ptr->vertex_indices[3];
229
230 while (true)
231 {
232 int32_t vertex_index = (uint16_t)(*(uint16_t *)vertex_index_cursor);
233 char cached_vertex_state = transformed_vertices[vertex_index].vertex_state;
234 struct Graphics_Render_SpriteVertexData *source_vertex = &transformed_vertices[vertex_index];
235
236 if (cached_vertex_state == GRAPHICS_SPRITE_VERTEX_INVALID)
237 break;
238
239 struct Graphics_Render_ProjectedVertex *projected_buffer;
240 int32_t projected_vertex_index;
241
242 if (cached_vertex_state != GRAPHICS_SPRITE_VERTEX_NEEDS_PROJECTION)
243 {
244 int32_t cached_local_z = (int32_t)source_vertex->pos.z;
245 int32_t cached_local_y = (int32_t)source_vertex->pos.y;
246 int32_t cached_local_x = (int32_t)source_vertex->pos.x;
247 int32_t cached_view_y_q6 = (((int32_t)g_cameraTransformMatrix.m10 * cached_local_x + (int32_t)g_cameraTransformMatrix.m11 * cached_local_y + (int32_t)g_cameraTransformMatrix.m12 * cached_local_z) >> 0xa) + g_nodeViewTranslationY;
248
249 projected_buffer = projected_vertices;
250
251 int32_t cached_depth_x_product = (int32_t)g_cameraTransformMatrix.m20 * cached_local_x;
252 int32_t cached_depth_y_product = (int32_t)g_cameraTransformMatrix.m21 * cached_local_y;
253 int32_t cached_depth_z_product = (int32_t)g_cameraTransformMatrix.m22 * cached_local_z;
254
255 view_y_cursor[-1] = (float)((long double)((((int32_t)g_cameraTransformMatrix.m00 * cached_local_x + (int32_t)g_cameraTransformMatrix.m01 * cached_local_y + (int32_t)g_cameraTransformMatrix.m02 * cached_local_z) >> 0xa) + g_nodeViewTranslationX) * (long double)0.015625f);
256
257 int32_t cached_view_z_q6 = ((cached_depth_x_product + cached_depth_y_product + cached_depth_z_product) >> 0xa) + g_nodeViewTranslationZ;
258
259 *view_y_cursor = (float)((long double)cached_view_y_q6 * (long double)0.015625f);
260 projected_vertex_index = vertex_index;
261 view_y_cursor[1] = (float)((long double)cached_view_z_q6 * (long double)0.015625f);
262 }
263 else
264 {
265 int32_t local_z = (int32_t)source_vertex->pos.z;
266 int32_t local_y = (int32_t)source_vertex->pos.y;
267 int32_t local_x = (int32_t)source_vertex->pos.x;
268
270
271 int32_t view_z_q6 = (((int32_t)g_cameraTransformMatrix.m20 * local_x + (int32_t)g_cameraTransformMatrix.m21 * local_y + (int32_t)g_cameraTransformMatrix.m22 * local_z) >> 0xa) + g_nodeViewTranslationZ;
272 int32_t view_x_q6 = (((int32_t)g_cameraTransformMatrix.m00 * local_x + (int32_t)g_cameraTransformMatrix.m01 * local_y + (int32_t)g_cameraTransformMatrix.m02 * local_z) >> 0xa) + g_nodeViewTranslationX;
273 int32_t view_y_q6 = (((int32_t)g_cameraTransformMatrix.m10 * local_x + (int32_t)g_cameraTransformMatrix.m11 * local_y + (int32_t)g_cameraTransformMatrix.m12 * local_z) >> 0xa) + g_nodeViewTranslationY;
274
275 projected_buffer = projected_vertices;
276 view_y_cursor[-1] = (float)((long double)view_x_q6 * (long double)0.015625f);
277 projected_vertex_index = vertex_index;
278 projected_buffer[projected_vertex_index].screen_z = view_z_q6;
279
280 struct Graphics_Render_ListState *list_state = render_list_state;
281 bool behind_near_plane = view_z_q6 <= list_state->projection_near_fp;
282
283 *view_y_cursor = (float)((long double)view_y_q6 * (long double)0.015625f);
284 view_y_cursor[1] = (float)((long double)view_z_q6 * (long double)0.015625f);
285
286 if (!behind_near_plane)
287 {
288 int32_t focal_distance = list_state->focal_distance;
289 int32_t perspective_scale =
290 ((int64_t)focal_distance * MATH_Q12_ONE) / view_z_q6;
291 int64_t screen_x_product = (int64_t)perspective_scale * view_x_q6;
292 int32_t screen_x =
293 (((int32_t)(uint32_t)((uint64_t)screen_x_product >> 12)) >> 0xc) + (Graphics_WindowWidth >> 1);
294 int64_t screen_y_product = (int64_t)perspective_scale * view_y_q6;
295 int32_t screen_y =
296 (((int32_t)(uint32_t)((uint64_t)screen_y_product >> 12)) >> 0xc) + (Graphics_WindowHeight >> 1);
297
298 if (screen_x != (int32_t)(int16_t)screen_x || screen_y != (int32_t)(int16_t)screen_y)
299 projected_buffer[projected_vertex_index].screen_z =
300 render_list_state->projection_near_fp;
301 else
302 {
303 projected_buffer[projected_vertex_index].screen.x = (uint16_t)screen_x;
304 projected_buffer[projected_vertex_index].screen.y = (uint16_t)screen_y;
305 }
306 }
307 }
308
309 view_y_cursor -= 3;
310 *projected_slot_cursor = (int32_t *)&projected_buffer[projected_vertex_index];
311
312 int32_t previous_vertex_slot = vertex_slot;
313
314 vertex_slot -= 1;
315 vertex_index_cursor -= 1;
316 projected_slot_cursor -= 1;
317
318 if (!previous_vertex_slot)
319 {
320 int32_t *second_projected_words = projected_slots[1];
321
322 fourth_projected_vertex = (int16_t *)projected_slots[3];
323
324 int16_t *second_projected_vertex = (int16_t *)second_projected_words;
325 int32_t projection_near_fp = render_list_state->projection_near_fp;
326 int32_t *first_projected_words = projected_slots[0];
327 int32_t *third_projected_words = projected_slots[2];
328 int32_t winding_area;
329 struct Graphics_Render_PolygonRenderRef *winding_polygon;
330
331 if (((struct Graphics_Render_ProjectedVertex *)second_projected_vertex)->screen_z <= projection_near_fp)
332 {
333 winding_area = 0;
334 winding_polygon = polygon_ref;
335 }
336 else
337 {
338 int16_t *third_projected_vertex = (int16_t *)third_projected_words;
339
340 if (((struct Graphics_Render_ProjectedVertex *)third_projected_vertex)->screen_z <= projection_near_fp)
341 {
342 winding_area = 0;
343 winding_polygon = polygon_ref;
344 }
345 else
346 {
347 int16_t *quad_fourth_vertex = fourth_projected_vertex;
348
349 if (((struct Graphics_Render_ProjectedVertex *)quad_fourth_vertex)->screen_z <= projection_near_fp)
350 {
351 winding_area = 0;
352 winding_polygon = polygon_ref;
353 }
354 else
355 {
356 int32_t third_screen_y = (int32_t)third_projected_vertex[1];
357 int32_t third_screen_x = (int32_t)*(int16_t *)third_projected_vertex;
358 int32_t second_screen_x = (int32_t)*(int16_t *)second_projected_vertex;
359 int32_t second_screen_y = (int32_t)second_projected_vertex[1];
360 int32_t fourth_screen_y = (int32_t)fourth_projected_vertex[1];
361
362 winding_area = (third_screen_x - second_screen_x) * (third_screen_y - fourth_screen_y) - (third_screen_x - (int32_t)*(int16_t *)quad_fourth_vertex) * (third_screen_y - second_screen_y);
363 winding_polygon = polygon_ref;
364
365 if (winding_area < 0 && !(*(uint8_t *)&winding_polygon->material_ref->flags & MATERIAL_TWO_SIDED || g_graphicsCapabilities & GRAPHICS_CAP_DISABLE_BACKFACE_CULL))
366 {
367 if (!(*(uint32_t *)&winding_polygon->flags & POLYGON_TRIANGLE))
368 {
369 if (first_projected_words[1] <= projection_near_fp)
370 winding_area = 0;
371 else
372 {
373 int32_t alternate_winding_product = (second_screen_y - (int32_t)((struct Graphics_Render_ProjectedVertex *)first_projected_words)->screen.y) * (second_screen_x - (int32_t)*(int16_t *)fourth_projected_vertex);
374
375 winding_polygon = polygon_ref;
376
377 int32_t primary_winding_product = (second_screen_x - (int32_t)*(int16_t *)first_projected_words) * (second_screen_y - fourth_screen_y);
378
379 winding_area = primary_winding_product - alternate_winding_product;
380
381 if (primary_winding_product - alternate_winding_product < 0)
382 {
383 if (first_projected_words[1] <= projection_near_fp)
384 winding_area = 1;
385 else
386 break;
387 }
388 }
389 }
390 else
391 {
392 if (first_projected_words[1] <= projection_near_fp)
393 winding_area = 1;
394 else
395 break;
396 }
397 }
398 }
399 }
400 }
401 {
402 if (*(uint32_t *)&winding_polygon->flags & POLYGON_TRIANGLE)
403 fourth_projected_vertex = NULL;
404
405 char *material_bytes = (char *)winding_polygon->material_ref;
406
407 g_currentRenderMaterial = (struct Material_TableEntry *)material_bytes;
408
409 int32_t saved_face_dot;
410 int32_t face_dot;
411
412 if (winding_area >= 0)
413 {
414 int32_t front_face_dot = (int32_t)winding_polygon->depth_bias_q12 << 3;
415
416 saved_face_dot = front_face_dot;
417 face_dot = front_face_dot;
418 }
419 else
420 {
421 face_dot = -((int32_t)winding_polygon->depth_bias_q12) << 3;
422 saved_face_dot = face_dot;
423 }
424
425 char *alternate_material = (char *)((struct Material_TableEntry *)material_bytes)->next_material_entry;
426 char *pending_material;
427
428 if (!alternate_material || !(*(uint32_t *)material_bytes & MATERIAL_LINKED_PASS))
429 pending_material = NULL;
430 else
431 {
432 char *base_material = material_bytes;
433
434 material_bytes = alternate_material;
435 g_currentRenderMaterial = (struct Material_TableEntry *)material_bytes;
436 pending_material = base_material;
437 }
438
439 int32_t depth_bucket = 0;
440 int32_t batch_index;
441
442 if ((*(uint32_t *)material_bytes & MATERIAL_DEPTH_SORT) ||
443 ((batch_index = g_currentPolygonBatchIndex),
445 {
446 int32_t first_depths_equal = 1;
447 long double three_vertex_depth_sum =
448 (long double)view_xyz[8] + (long double)view_xyz[5] + (long double)view_xyz[2];
449 long double first_view_depth = (long double)view_xyz[2];
450 long double second_view_depth = (long double)view_xyz[5];
451
452 if (!(((uint8_t)((((first_view_depth < second_view_depth) ? X87_STATUS_C0 : 0) | ((isunordered(first_view_depth, second_view_depth)) ? X87_STATUS_C2 : 0) | ((first_view_depth == second_view_depth) ? X87_STATUS_C3 : 0) | X87_STATUS_TOP_MASK) >> 8)) & (X87_STATUS_C3 >> 8)))
453 first_depths_equal = 0;
454
455 long double depth_equality_as_float = (long double)first_depths_equal;
456 long double third_view_depth = (long double)view_xyz[8];
457
458 long double scaled_sort_depth;
459
460 if (((uint8_t)((((depth_equality_as_float < third_view_depth) ? X87_STATUS_C0 : 0) | ((isunordered(depth_equality_as_float, third_view_depth)) ? X87_STATUS_C2 : 0) | ((depth_equality_as_float == third_view_depth) ? X87_STATUS_C3 : 0) | X87_STATUS_TOP_MASK) >> 8)) & (X87_STATUS_C3 >> 8))
461 scaled_sort_depth = (long double)view_xyz[2] * (long double)g_levelRenderDistance;
462 else if (!fourth_projected_vertex)
463 scaled_sort_depth = three_vertex_depth_sum * (long double)g_levelRenderDistanceThird;
464 else
465 scaled_sort_depth = (three_vertex_depth_sum + (long double)view_xyz[11]) * (long double)g_levelRenderDistanceQuarter;
466
467 int32_t sort_depth = __ftol_msvc(scaled_sort_depth);
468
469 if (sort_depth > 0x3fff)
470 sort_depth = 0x3fff;
471
472 depth_bucket = 0x3fff - sort_depth;
473
474 if (0x3fff - sort_depth < 1)
475 depth_bucket = 1;
476 else if (0x3fff - sort_depth > 0x3fff)
477 depth_bucket = 0x3fff;
478
479 face_dot = saved_face_dot;
480 batch_index = g_currentPolygonBatchIndex;
481 }
482
483 while (true)
484 {
485 (&g_polygonBatchRecords)[batch_index].face_normal_dot =
486 face_dot;
487
488 int32_t second_screen_depth = (&g_polygonBatchRecords)[batch_index].screen_vertices[1].z;
489
490 (&g_polygonBatchRecords)[batch_index].view_vertices[0].x =
491 view_xyz[0];
492
493 (&(g_polygonBatchRecords.view_vertices)[0].y)[batch_index * 0x22] = view_xyz[1];
494 (&(g_polygonBatchRecords.view_vertices)[0].z)[batch_index * 0x22] = view_xyz[2];
495
496 (&g_polygonBatchRecords)[batch_index].view_vertices[1] =
497 (struct Math_Vec3F){
498 .x = view_xyz[3],
499 .y = view_xyz[4],
500 .z = view_xyz[5]};
501
502 (&g_polygonBatchRecords)[batch_index].view_vertices[2].x =
503 view_xyz[6];
504
505 (&g_polygonBatchRecords)[batch_index].view_vertices[2].y = view_xyz[7];
506 (&g_polygonBatchRecords)[batch_index].view_vertices[2].z = view_xyz[8];
507
508 (&g_polygonBatchRecords)[batch_index].view_vertices[3].x =
509 view_xyz[9];
510 (&g_polygonBatchRecords)[batch_index].view_vertices[3].y = view_xyz[10];
511 (&g_polygonBatchRecords)[batch_index].view_vertices[3].z = view_xyz[11];
512 (&g_polygonBatchRecords)[batch_index].screen_vertices[0].z -= 0xc00;
513
514 int32_t third_screen_depth = (&g_polygonBatchRecords)[batch_index].screen_vertices[2].z;
515
516 (&g_polygonBatchRecords)[batch_index].screen_vertices[1].z =
517 second_screen_depth - 0xc00;
518
519 bool is_triangle = !fourth_projected_vertex;
520
521 (&g_polygonBatchRecords)[batch_index].screen_vertices[2].z =
522 third_screen_depth - 0xc00;
523
524 if (!is_triangle)
525 (&g_polygonBatchRecords)[batch_index].screen_vertices[3].z -= 0xc00;
526
527 (&g_polygonBatchRecords)[batch_index].depth_bias = 0.0f;
528
529 struct Mesh_RuntimeVertex *polygon_vertices[4];
530
531 for (int k = 0; k < 3; ++k)
532 polygon_vertices[k] = (struct Mesh_RuntimeVertex *)&transformed_vertices[polygon_ref->vertex_indices[k]];
533
534 polygon_vertices[3] = (polygon_ref->flags & POLYGON_TRIANGLE) ? NULL
535 : (struct Mesh_RuntimeVertex *)&transformed_vertices[polygon_ref->vertex_indices[3]];
537 &(&g_polygonBatchRecords)[batch_index],
538 g_currentRenderMaterial, polygon_ref->uv_index_or_sentinel, polygon_vertices);
539
540 uint32_t flags = (&g_polygonBatchRecords)[batch_index].flags;
541
542 if (flags & MATERIAL_DEPTH_SORT || (uint8_t)flags & MATERIAL_BLEND_ENABLED)
543 {
544 int32_t list_bucket = depth_bucket;
545
546 if (list_bucket < 1)
547 {
548 depth_bucket = 1;
549 list_bucket = 1;
550 }
551
552 (&g_polygonBatchRecords)[batch_index].next_in_bucket =
553 render_list_state->polygon_queue_heads[list_bucket];
554 render_list_state->polygon_queue_heads[list_bucket] =
555 &(&g_polygonBatchRecords)[batch_index];
556 }
557 else if ((&g_polygonBatchRecords)[batch_index].texture_desc || g_currentGameMode != GAME_MODE_GAMEPLAY_3D)
558 {
559 (&g_polygonBatchRecords)[batch_index].next_in_bucket =
560 render_list_state->sorted_list_head;
561 render_list_state->sorted_list_head =
562 &(&g_polygonBatchRecords)[batch_index];
563 }
564 else
565 {
566 (&g_polygonBatchRecords)[batch_index].next_in_bucket =
567 render_list_state->sorted_list_buckets[0x3ffe];
568 render_list_state->sorted_list_buckets[0x3ffe] =
569 &(&g_polygonBatchRecords)[batch_index];
570 }
571
572 int32_t first_packed_screen = *(uint32_t *)first_projected_words;
573
574 (&g_polygonBatchRecords)[batch_index].screen_vertices[0].screen.x = (uint16_t)first_packed_screen;
575 (&g_polygonBatchRecords)[batch_index].screen_vertices[0].screen.y = (uint16_t)((uint32_t)first_packed_screen >> 16);
576 (&g_polygonBatchRecords)[batch_index].screen_vertices[0].z =
577 first_projected_words[1];
578
579 int32_t second_packed_screen = *(uint32_t *)second_projected_words;
580
581 (&g_polygonBatchRecords)[batch_index].screen_vertices[1].screen.x = (uint16_t)second_packed_screen;
582 (&g_polygonBatchRecords)[batch_index].screen_vertices[1].screen.y = (uint16_t)((uint32_t)second_packed_screen >> 16);
583 (&g_polygonBatchRecords)[batch_index].screen_vertices[1].z =
584 second_projected_words[1];
585
586 int32_t third_packed_screen = *(uint32_t *)third_projected_words;
587
588 (&g_polygonBatchRecords)[batch_index].screen_vertices[2].screen.x = (uint16_t)third_packed_screen;
589 (&g_polygonBatchRecords)[batch_index].screen_vertices[2].screen.y = (uint16_t)((uint32_t)third_packed_screen >> 16);
590
591 int32_t third_projected_depth = third_projected_words[1];
592
593 (&g_polygonBatchRecords)[batch_index].view_vertices[0].x =
594 view_xyz[0];
595 (&g_polygonBatchRecords)[batch_index].screen_vertices[2].z =
596 third_projected_depth;
597 (&(g_polygonBatchRecords.view_vertices)[0].y)[batch_index * 0x22] = view_xyz[1];
598 (&g_polygonBatchRecords)[batch_index].view_vertices[1].x = view_xyz[3];
599 (&(g_polygonBatchRecords.view_vertices)[0].z)[batch_index * 0x22] = view_xyz[2];
600 (&g_polygonBatchRecords)[batch_index].view_vertices[1].y = view_xyz[4];
601 (&g_polygonBatchRecords)[batch_index].view_vertices[2].x =
602 view_xyz[6];
603
604 int16_t *fourth_screen_source = fourth_projected_vertex;
605
606 (&g_polygonBatchRecords)[batch_index].view_vertices[1].z = view_xyz[5];
607 (&g_polygonBatchRecords)[batch_index].view_vertices[2].y =
608 view_xyz[7];
609 (&g_polygonBatchRecords)[batch_index].view_vertices[2].z =
610 view_xyz[8];
611
612 if (fourth_screen_source)
613 {
614 int32_t fourth_packed_screen = *(uint32_t *)fourth_screen_source;
615
616 (&g_polygonBatchRecords)[batch_index].screen_vertices[3].screen.x = (uint16_t)fourth_packed_screen;
617 (&g_polygonBatchRecords)[batch_index].screen_vertices[3].screen.y = (uint16_t)((uint32_t)fourth_packed_screen >> 16);
618
619 float fourth_view_y = view_xyz[10];
620
621 (&g_polygonBatchRecords)[batch_index].screen_vertices[3].z = (uint32_t)((struct Graphics_Render_ProjectedVertex *)fourth_screen_source)->screen_z;
622 (&g_polygonBatchRecords)[batch_index].view_vertices[3].x = view_xyz[9];
623 (&g_polygonBatchRecords)[batch_index].view_vertices[3].y = fourth_view_y;
624 (&g_polygonBatchRecords)[batch_index].view_vertices[3].z = view_xyz[11];
625 }
626 else
627 {
628 (&g_polygonBatchRecords)[batch_index].screen_vertices[3].screen.x = 0x4000;
629 (&g_polygonBatchRecords)[batch_index].screen_vertices[3].screen.y = 0x4000;
630 }
631
632 uint32_t color_override_flags = g_graphicsCapabilityFlags;
633 uint32_t *out_colors;
634
635 if (!(color_override_flags & GRAPHICS_RENDER_UNIFORM_COLOR))
636 {
637 out_colors =
638 (&g_polygonBatchRecords)[batch_index].vertex_colors;
639 Graphics_ComputeVertexColors((struct Mesh_RuntimeVertex *)transformed_vertices, polygon_ref,
640 out_colors);
641 }
642 else
643 {
644 (&g_polygonBatchRecords)[batch_index].vertex_colors[3] =
645 color_override_flags;
646 (&g_polygonBatchRecords)[batch_index].vertex_colors[2] =
648 (&g_polygonBatchRecords)[batch_index].vertex_colors[1] =
650 out_colors =
651 (&g_polygonBatchRecords)[batch_index].vertex_colors;
652 *(uint32_t *)out_colors = g_graphicsCapabilityFlags;
653
654 uint32_t override_blend_mode = g_graphicsCapabilityFlags >> 0x1d;
655
656 if (override_blend_mode)
657 {
658 int32_t override_batch_flags = (override_blend_mode + 0x7ffffff) << 5 | ((&g_polygonBatchRecords)[batch_index].flags & ~(uint32_t)(MATERIAL_BLEND_MODE_LOW | MATERIAL_BLEND_MODE_HIGH));
659
660 override_batch_flags |= MATERIAL_BLEND_ENABLED;
661 (&g_polygonBatchRecords)[batch_index].flags = override_batch_flags;
662 }
663 }
664
666 {
667 int32_t fade_scale_q12 = 0x1000 - (*(uint32_t *)&g_renderingStateFlag);
668 uint32_t first_color = *(uint32_t *)out_colors;
669 int32_t first_green = (uint8_t)((uint8_t)((uint32_t)first_color >> 8));
670 uint32_t second_color = (&g_polygonBatchRecords)[batch_index].vertex_colors[1];
671
672 *(uint32_t *)out_colors = (((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);
673
674 int32_t second_green = (uint8_t)((uint8_t)((uint32_t)second_color >> 8));
675 uint32_t third_color = (&g_polygonBatchRecords)[batch_index].vertex_colors[2];
676
677 (&g_polygonBatchRecords)[batch_index].vertex_colors[1] =
678 (((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);
679
680 int32_t third_green = (uint8_t)((uint8_t)((uint32_t)third_color >> 8));
681 uint32_t fourth_color = (&g_polygonBatchRecords)[batch_index].vertex_colors[3];
682
683 (&g_polygonBatchRecords)[batch_index].vertex_colors[2] =
684 (((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);
685
686 int32_t fourth_green = (uint8_t)((uint8_t)((uint32_t)fourth_color >> 8));
687
688 (&g_polygonBatchRecords)[batch_index].vertex_colors[3] =
689 (((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);
690 }
691
693
694 if (!pending_material)
695 break;
696
697 g_currentRenderMaterial = (struct Material_TableEntry *)pending_material;
698 pending_material = NULL;
699 face_dot = saved_face_dot;
700 batch_index = g_currentPolygonBatchIndex;
701 }
702 }
703
704 break;
705 }
706 }
707
708 variant_payload_ptr = &polygon_ref[1];
709
710 polygon_ref = variant_payload_ptr;
711 polygons_remaining -= 1;
712 } while (polygons_remaining);
713}
#define g_cameraTransformMatrix
Definition globals.h:81
#define GAME_TRANSITION_COLOR_FADE
Definition constants.h:30
@ GAME_MODE_GAMEPLAY_3D
Definition enums.h:38
#define Game_FrameTransitionFlags
Definition globals.h:47
#define g_currentGameMode
Definition globals.h:74
#define SPRITE_RENDER_ROTATION_MASK
Definition constants.h:89
#define GRAPHICS_CAP_DEPTH_BUFFER
Definition constants.h:16
#define GRAPHICS_COLOR_WHITE_RGB
Definition constants.h:39
#define FRUSTUM_CULL_NODE_RENDER_MODE
Definition constants.h:69
#define SPRITE_RENDER_COLOR_OVERRIDE
Definition constants.h:32
#define GRAPHICS_SCREEN_QUAD_DEPTH_BITS
Definition constants.h:77
@ GRAPHICS_SPRITE_VERTEX_PROJECTED
Definition constants.h:100
@ GRAPHICS_SPRITE_VERTEX_NEEDS_PROJECTION
Definition constants.h:99
@ GRAPHICS_SPRITE_VERTEX_INVALID
Definition constants.h:98
#define SPRITE_RENDER_EXPLICIT_COLOR
Definition constants.h:33
#define SPRITE_RENDER_BLEND_MODE_SHIFT
Definition constants.h:36
@ GRAPHICS_BLEND_ADDITIVE_DIM
Definition constants.h:47
#define GRAPHICS_CAP_DISABLE_BACKFACE_CULL
Definition constants.h:21
#define POLYGON_TRIANGLE
Definition constants.h:7
#define SPRITE_RENDER_BLEND_MODE_MASK
Definition constants.h:37
#define FRUSTUM_CULL_SKIP_DISTANCE
Definition constants.h:68
#define GRAPHICS_RENDER_UNIFORM_COLOR
Definition constants.h:23
#define GRAPHICS_CAP_DEPTH_BUFFER_SHIFT
Definition constants.h:17
#define SPRITE_RENDER_ROTATION_SHIFT
Definition constants.h:88
void Graphics_RenderMeshNode(struct Scene_Node *node, struct Graphics_Render_MeshBillboardRuntimeView *sprite_ctx)
void Graphics_ComputeColorGradient(uint32_t *out_top_color, uint32_t *out_bottom_color, uint32_t text_flags)
Definition effects.c:385
void Graphics_ProcessMeshCommands(struct Mesh_CmdList *cmd_list, struct Material_Node **material_nodes, void *owner_context)
Definition meshes.c:70
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
int32_t Graphics_CheckFrustumCull(struct Math_Vec3I32 *position, int32_t cull_radius, uint32_t cull_flags)
Definition clipping.c:772
#define g_graphicsCapabilityFlags
Definition globals.h:158
#define g_polygonBatchRecords
Definition globals.h:290
#define Graphics_WindowWidth
Definition globals.h:140
#define g_fontGlyphRenderState
Definition globals.h:41
#define g_graphicsCapabilities
Definition globals.h:284
#define render_list_state
Definition globals.h:152
#define Graphics_WindowHeight
Definition globals.h:143
#define graphics_text_rendering_mode
Definition globals.h:293
void Graphics_RenderSpriteObjectNode(struct Scene_BillboardNodeView *node)
Definition sprites.c:28
void Graphics_RenderSpritePolygons(struct Scene_Node *node, struct Graphics_Render_SpriteVertexData *transformed_vertices, struct Graphics_Render_ProjectedVertex *projected_vertices)
Definition sprites.c:211
void Graphics_RenderTexturedSprite(uint32_t packed_xy, uint32_t packed_wh, struct Graphics_Render_SpriteContext *sprite, uint32_t render_flags, uint32_t rotation_angle, uint32_t rotation_pivot_xy)
Definition sprites.c:43
#define g_levelRenderDistanceQuarter
Definition globals.h:121
#define g_levelRenderDistance
Definition globals.h:115
#define g_levelRenderDistanceThird
Definition globals.h:124
#define MATERIAL_BLEND_MODE_HIGH
Definition constants.h:28
#define MATERIAL_BLEND_MODE_SHIFT
Definition constants.h:50
#define MATERIAL_BLEND_MODE_MASK
Definition constants.h:51
#define MATERIAL_DEPTH_SORT
Definition constants.h:39
#define MATERIAL_TILE_V_FIRST
Definition constants.h:35
#define MATERIAL_BLEND_ENABLED
Definition constants.h:29
#define MATERIAL_TILE_U_SECOND
Definition constants.h:34
#define MATERIAL_TILE_V_SECOND
Definition constants.h:36
#define MATERIAL_LINKED_PASS
Definition constants.h:38
#define MATERIAL_TILE_U_FIRST
Definition constants.h:33
#define MATERIAL_BLEND_MODE_LOW
Definition constants.h:27
#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_SinQ14(int32_t angle)
#define g_renderWorldTransformPtr
Definition globals.h:27
uint8_t D3D_SetBlendMode(int32_t blend_mode)
void D3D_SetTextureColorOperation(DWORD color_operation)
#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)
struct Material_Node ** controller_slots
Definition types.h:470
struct Mesh_CmdList * command_list
Definition types.h:472
struct Material_TableEntry * material_ref
Definition types.h:204
struct Math_ScreenPointI16 screen
Definition types.h:528
struct Material_RuntimeDescriptor * textureDescriptor
Definition types.h:570
struct Math_UV8 subrect_uv
Definition types.h:573
struct Math_Vec3I16 pos
Definition types.h:602
IDirectDrawSurface7 * texture_handles[0x19]
Definition types.h:268
struct Math_SizeU8 dimensions_minus_1
Definition types.h:264
uint32_t flags
Definition types.h:342
uint8_t width
Definition types.h:347
uint8_t height
Definition types.h:348
struct Math_Vec3I32 translationQ6
Definition types.h:562
uint8_t v
Definition types.h:370
uint8_t u
Definition types.h:369
int16_t x
Definition types.h:402
int16_t y
Definition types.h:403
int16_t z
Definition types.h:460
int16_t x
Definition types.h:458
int16_t y
Definition types.h:459
struct Math_Vec2I16 bound_extent
Definition types.h:287
uint16_t render_flags
Definition types.h:286
struct Math_TransformQ12Q6 world_transform
Definition types.h:282
struct Graphics_Render_MeshBillboardRuntimeView payload
Definition types.h:288
uint32_t variant_transform_tail
Definition types.h:183
void * variant_payload_ptr
Definition types.h:140
#define g_d3dDevice7
Definition values.h:792
#define g_nodeViewTranslationY
Definition values.h:774
#define g_renderingStateFlag
Definition values.h:627
#define g_currentPolygonBatchIndex
Definition values.h:768
#define g_nodeViewTranslationZ
Definition values.h:777
#define g_nodeViewTranslationX
Definition values.h:771
#define g_currentBoundTextureSurface
Definition values.h:294