102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
shadows.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
11#include "pcdogs/math/types.h"
13#include "pcdogs/scene/types.h"
15#include <math.h>
16#include <stdarg.h>
17#include <string.h>
18
19// Original pcdogs.exe: 0x43ae80
20// Clears the pending shadow render-list head.
25
26// Original pcdogs.exe: 0x43ae90
27// Builds projected actor shadows from visible occluders and ground geometry, then filters the pending shadow list through ground-visibility tests.
29{
30 // The root shadow link contains actor-shaped occluders rather than scene nodes.
31 struct Shadow_OccluderView *source_shadow_actor = (struct Shadow_OccluderView *)(g_objectNodeRoot->shadow_list_link);
32 struct Shadow_OccluderView *current_shadow_actor = source_shadow_actor;
33
34 if (source_shadow_actor)
35 {
36 while (true)
37 {
38 if (Graphics_CheckActorVisibilityAndFrustum(&source_shadow_actor->position,
39 source_shadow_actor->cull_radius, 0))
40 {
41 struct Graphics_Render_ListState *camera_list_state = render_list_state;
42 int32_t actor_y = source_shadow_actor->position.y;
43 int32_t actor_z = source_shadow_actor->position.z;
44 int32_t camera_delta[3];
45
46 camera_delta[0] = camera_list_state->eye_pos.x - source_shadow_actor->position.x;
47
48 int32_t camera_delta_y = camera_list_state->eye_pos.z - actor_y;
49
50 camera_delta[1] = camera_delta_y;
51
52 struct Mesh_RuntimeVertex *runtime_vertices = source_shadow_actor->runtime_vertices;
53 int32_t camera_delta_z = camera_list_state->eye_pos.y - actor_z;
54
55 camera_delta[2] = camera_delta_z;
56 source_shadow_actor->exclusion_flags &= ~(uint32_t)SHADOW_OCCLUDER_EXCLUDED;
57
58 struct Math_Vec3I16 first_vertex_xy;
59
60 first_vertex_xy.x = runtime_vertices->pos.x;
61 first_vertex_xy.y = runtime_vertices->pos.y;
62
63 struct Mesh_RuntimeVertex first_vertex_z_group;
64
65 first_vertex_z_group.pos.z = runtime_vertices->pos.z;
66 first_vertex_z_group.normal_group_index = runtime_vertices->normal_group_index;
67
68 struct Collision_ClipVertexQ6 clip_input_vertices[0x4];
69
70 clip_input_vertices[0].position_q6.x = first_vertex_xy.x;
71 clip_input_vertices[0].position_q6.y = first_vertex_xy.y;
72
73 int32_t second_vertex_xy_bits = *(int32_t *)&runtime_vertices[1].pos.x;
74
75 clip_input_vertices[0].position_q6.z = first_vertex_z_group.pos.z;
76 clip_input_vertices[0].padding_06 = first_vertex_z_group.normal_group_index;
77
78 int32_t second_vertex_z_group_bits = *(int32_t *)&runtime_vertices[1].pos.z;
79
80 *(uint32_t *)&clip_input_vertices[1].position_q6.x = second_vertex_xy_bits;
81
82 int32_t third_vertex_xy_bits = *(int32_t *)&runtime_vertices[2].pos.x;
83
84 *(uint32_t *)&clip_input_vertices[1].position_q6.z = second_vertex_z_group_bits;
85
86 int32_t third_vertex_z_group_bits = *(int32_t *)&runtime_vertices[2].pos.z;
87 struct Mesh_RuntimeVertex *edge_vertex_cursor = runtime_vertices;
88
89 *(uint32_t *)&clip_input_vertices[2].position_q6.x = third_vertex_xy_bits;
90 *(uint32_t *)&clip_input_vertices[2].position_q6.z = third_vertex_z_group_bits;
91
92 int32_t fourth_vertex_z_group_bits = *(int32_t *)&runtime_vertices[3].pos.z;
93
94 *(uint32_t *)&clip_input_vertices[3].position_q6.x =
95 *(int32_t *)&runtime_vertices[3].pos.x;
96 *(uint32_t *)&clip_input_vertices[3].position_q6.z = fourth_vertex_z_group_bits;
97
98 struct Mesh_Polygon *clip_source_polygon = source_shadow_actor->source_polygon;
99
100 clip_input_vertices[0].next = &clip_input_vertices[1];
101 clip_input_vertices[1].next = &clip_input_vertices[2];
102
103 if (!(clip_source_polygon->flags & POLYGON_TRIANGLE))
104 {
105 clip_input_vertices[2].next = &clip_input_vertices[3];
106 clip_input_vertices[3].next = clip_input_vertices;
107 }
108 else
109 clip_input_vertices[2].next = clip_input_vertices;
110
111 struct Collision_ClipVertexQ6 *clipped_ring = clip_input_vertices;
112 struct Collision_ClipVertexQ6 clip_intersections[0x10];
113 struct Collision_ClipVertexQ6 *intersection_cursor = clip_intersections;
114 int32_t planes_remaining = 4;
115 int32_t *camera_delta_components = &camera_delta[0];
116 int32_t plane_byte_offset = 0x30;
117 int32_t minimum_plane_distance;
118 int32_t maximum_plane_distance;
119 bool has_more_clip_planes;
120
121 do
122 {
123 struct Graphics_Render_ListState *clip_list_state = render_list_state;
124 int32_t plane_xy_bits =
125 *(uint32_t *)&clip_list_state->frustum_planes[plane_byte_offset / sizeof(struct Graphics_Render_FrustumClipPlane)].normal;
126 int16_t plane_normal_z = (int16_t)*(uint32_t *)&clip_list_state->frustum_planes[plane_byte_offset / sizeof(struct Graphics_Render_FrustumClipPlane)].normal.z;
127 struct Collision_ClipVertexQ6 *clip_vertex = clipped_ring;
128 struct Collision_ClipVertexQ6 *first_intersection = NULL;
129 int32_t plane_camera_offset =
130 -(((int32_t)((((uint64_t)((int64_t)(int32_t)clip_list_state->frustum_planes[plane_byte_offset / sizeof(struct Graphics_Render_FrustumClipPlane)].normal.x *
131 *(int32_t *)camera_delta_components) +
132 (uint64_t)((int64_t)(int32_t)clip_list_state->frustum_planes[plane_byte_offset / sizeof(struct Graphics_Render_FrustumClipPlane)].normal.y * camera_delta_components[1]) + (uint64_t)((int64_t)(int32_t)clip_list_state->frustum_planes[plane_byte_offset / sizeof(struct Graphics_Render_FrustumClipPlane)].normal.z * camera_delta_components[2])) >>
133 12))))
134 << 4;
135 struct Collision_ClipVertexQ6 *clip_ring_start = clip_vertex;
136 int32_t current_distance_or_fraction_q12 = (((int32_t)clip_vertex->position_q6.y * (int32_t)*(int16_t *)((char *)&plane_xy_bits + 2) + (int32_t)clip_vertex->position_q6.z * (int32_t)plane_normal_z + (int32_t)clip_vertex->position_q6.x * (int32_t)(int16_t)plane_xy_bits) >> 2) + plane_camera_offset;
137 int32_t current_distance_snapshot = current_distance_or_fraction_q12;
138
139 maximum_plane_distance = current_distance_or_fraction_q12;
140 minimum_plane_distance = current_distance_or_fraction_q12;
141
142 struct Collision_ClipVertexQ6 *intersection_or_cursor;
143 struct Collision_ClipVertexQ6 *next_clip_vertex;
144
145 do
146 {
147 next_clip_vertex = clip_vertex->next;
148
149 int32_t next_plane_distance = ((
150 (int32_t)next_clip_vertex->position_q6.y * (int32_t)*(int16_t *)((char *)&plane_xy_bits + 2) + (int32_t)next_clip_vertex->position_q6.z * (int32_t)plane_normal_z + (int32_t)next_clip_vertex->position_q6.x * (int32_t)(int16_t)plane_xy_bits) >>
151 2) +
152 plane_camera_offset;
153
154 if (next_plane_distance < minimum_plane_distance)
155 minimum_plane_distance = next_plane_distance;
156 else if (next_plane_distance > maximum_plane_distance)
157 maximum_plane_distance = next_plane_distance;
158
159 if ((next_plane_distance ^ current_distance_or_fraction_q12) >= 0)
160 intersection_or_cursor = intersection_cursor;
161 else if (next_plane_distance >= 0)
162 {
163 current_distance_or_fraction_q12 = ((int64_t)next_plane_distance * MATH_Q12_ONE) / (next_plane_distance - current_distance_or_fraction_q12);
164 intersection_cursor->position_q6.x = (int16_t)((
165 (int32_t)next_clip_vertex->position_q6.x * (0x1000 - current_distance_or_fraction_q12) + (int32_t)clip_vertex->position_q6.x * current_distance_or_fraction_q12) >>
166 0xc);
167 intersection_cursor->position_q6.y = (int16_t)((
168 (int32_t)clip_vertex->position_q6.y * current_distance_or_fraction_q12 + (int32_t)next_clip_vertex->position_q6.y * (0x1000 - current_distance_or_fraction_q12)) >>
169 0xc);
170 intersection_cursor->position_q6.z = (int16_t)((
171 (int32_t)clip_vertex->position_q6.z * current_distance_or_fraction_q12 + (int32_t)next_clip_vertex->position_q6.z * (0x1000 - current_distance_or_fraction_q12)) >>
172 0xc);
173 intersection_cursor->next = next_clip_vertex;
174
175 if (first_intersection)
176 {
177 struct Collision_ClipVertexQ6 *closing_intersection = intersection_cursor;
178
179 first_intersection->next = closing_intersection;
180 intersection_or_cursor = closing_intersection;
181
182 break;
183 }
184
185 first_intersection = intersection_cursor;
186 intersection_or_cursor = intersection_cursor + 1;
187 intersection_cursor = intersection_or_cursor;
188 }
189 else
190 {
191 current_distance_or_fraction_q12 = ((int64_t)current_distance_snapshot * MATH_Q12_ONE) / (current_distance_or_fraction_q12 - next_plane_distance);
192 intersection_cursor->position_q6.x = (int16_t)((
193 (int32_t)clip_vertex->position_q6.x * (0x1000 - current_distance_or_fraction_q12) + (int32_t)next_clip_vertex->position_q6.x * current_distance_or_fraction_q12) >>
194 0xc);
195 intersection_cursor->position_q6.y = (int16_t)((
196 (int32_t)clip_vertex->position_q6.y * (0x1000 - current_distance_or_fraction_q12) + (int32_t)next_clip_vertex->position_q6.y * current_distance_or_fraction_q12) >>
197 0xc);
198 intersection_or_cursor = intersection_cursor;
199 intersection_or_cursor->position_q6.z = (int16_t)((
200 (int32_t)clip_vertex->position_q6.z * (0x1000 - current_distance_or_fraction_q12) + (int32_t)next_clip_vertex->position_q6.z * current_distance_or_fraction_q12) >>
201 0xc);
202 clip_vertex->next = intersection_or_cursor;
203
204 if (first_intersection)
205 {
206 intersection_or_cursor->next = first_intersection;
207
208 break;
209 }
210
211 first_intersection = intersection_or_cursor;
212 intersection_or_cursor = &intersection_or_cursor[1];
213 intersection_cursor = intersection_or_cursor;
214 }
215
216 current_distance_or_fraction_q12 = next_plane_distance;
217 current_distance_snapshot = current_distance_or_fraction_q12;
218 clip_vertex = next_clip_vertex;
219 } while (next_clip_vertex != clip_ring_start);
220
221 if (!first_intersection)
222 {
223 if (current_distance_or_fraction_q12 < 0)
224 break;
225
226 clipped_ring = clip_vertex;
227 }
228 else
229 {
230 clipped_ring = intersection_or_cursor;
231 intersection_cursor = &intersection_or_cursor[1];
232 }
233
234 planes_remaining -= 1;
235 has_more_clip_planes = plane_byte_offset;
236 plane_byte_offset -= 0xc;
237 } while (has_more_clip_planes);
238
239 if (planes_remaining < 0)
240 {
241 struct Graphics_Render_ListState *depth_list_state = render_list_state;
242 int32_t distance_culling_disabled = g_graphicsCapabilities & GRAPHICS_CAP_DISABLE_DISTANCE_CULL;
243
244 if (distance_culling_disabled || minimum_plane_distance >> 4 <= depth_list_state->dynamic_level_scale)
245 {
246 uint32_t exclusion_flags = current_shadow_actor->exclusion_flags;
247
248 if (!((uint8_t)((uint32_t)exclusion_flags >> 8) & (SHADOW_OCCLUDER_TEST_FAR_EDGE >> 8)))
249 {
250 exclusion_flags |= SHADOW_OCCLUDER_EXCLUDED;
251 current_shadow_actor->exclusion_flags = exclusion_flags;
252 }
253 else if (!distance_culling_disabled && maximum_plane_distance >> 4 > depth_list_state->dynamic_level_scale)
254 {
255 exclusion_flags |= SHADOW_OCCLUDER_EXCLUDED;
256 current_shadow_actor->exclusion_flags = exclusion_flags;
257 }
258
259 struct Mesh_Polygon *source_polygon = current_shadow_actor->source_polygon;
260 int32_t camera_relative_or_edge_x = ((int32_t)edge_vertex_cursor->pos.x << 6) - camera_delta[0];
261 struct Math_Vec3I16 *face_normal =
262 &((struct Mesh_FaceNormal *)(uintptr_t)source_polygon->face_normal_ptr)->normal;
263 int32_t oriented_face_dot =
264 (int32_t)((((uint64_t)((int64_t)(int32_t)face_normal->x * camera_relative_or_edge_x) + (uint64_t)((int64_t)(int32_t)face_normal->y * (((int32_t)edge_vertex_cursor->pos.y << 6) - camera_delta_y)) + (uint64_t)((int64_t)(int32_t)face_normal->z * (((int32_t)edge_vertex_cursor->pos.z << 6) - camera_delta_z))) >> 12));
265 int32_t saved_face_dot = oriented_face_dot;
266
267 if (oriented_face_dot)
268 {
269 struct Shadow_OccluderView *edge_plane_actor =
270 current_shadow_actor;
271 struct Mesh_Polygon *oriented_source_polygon =
272 edge_plane_actor->source_polygon;
273
274 if (oriented_face_dot >= 0)
275 {
276 if (oriented_source_polygon->flags_high & (POLYGON_BACK_FACING >> 8))
277 {
278 oriented_face_dot = -(oriented_face_dot);
279 saved_face_dot = oriented_face_dot;
280 }
281
282 edge_plane_actor = current_shadow_actor;
283 }
284 else
285 {
286 uint32_t face_normal_ptr = oriented_source_polygon->face_normal_ptr;
287
288 *(uint16_t *)&((struct Mesh_FaceNormal *)(uintptr_t)face_normal_ptr)->normal.x =
289 -(*(uint16_t *)&((struct Mesh_FaceNormal *)(uintptr_t)face_normal_ptr)->normal.x);
290
291 uint32_t normal_y_address =
292 edge_plane_actor->source_polygon->face_normal_ptr;
293
294 *(uint16_t *)&((struct Mesh_FaceNormal *)(uintptr_t)normal_y_address)->normal.y =
295 -(*(uint16_t *)&((struct Mesh_FaceNormal *)(uintptr_t)normal_y_address)->normal.y);
296
297 uint32_t normal_z_address =
298 edge_plane_actor->source_polygon->face_normal_ptr;
299
300 *(uint16_t *)&((struct Mesh_FaceNormal *)(uintptr_t)normal_z_address)->normal.z =
301 -(*(uint16_t *)&((struct Mesh_FaceNormal *)(uintptr_t)normal_z_address)->normal.z);
302
303 uint32_t normal_distance_address =
304 edge_plane_actor->source_polygon->face_normal_ptr;
305
306 *(uint32_t *)normal_distance_address = -(*(uint32_t *)normal_distance_address);
307
308 struct Mesh_Polygon *reversed_source_polygon =
309 edge_plane_actor->source_polygon;
310
311 reversed_source_polygon->flags ^= POLYGON_BACK_FACING;
312
313 if (!(edge_plane_actor->source_polygon->flags & POLYGON_BACK_FACING))
314 {
315 oriented_face_dot = -(oriented_face_dot);
316 saved_face_dot = oriented_face_dot;
317 }
318 }
319
320 struct Shadow_EdgePlane *edge_plane_write_cursor =
321 edge_plane_actor->edge_planes;
322 int32_t outer_edges_remaining = 4;
323 int32_t camera_delta_y_q6 = camera_delta_y >> 6;
324 int32_t camera_delta_z_q6 = camera_delta_z >> 6;
325 struct Mesh_RuntimeVertex *previous_outer_vertex =
326 &runtime_vertices[3];
327 int32_t outer_plane_cross[3];
328 int32_t *outer_cross_right = &outer_plane_cross[0];
329
330 camera_delta[0] >>= 6;
331
332 int32_t *outer_cross_left = &outer_plane_cross[0];
333
334 while (true)
335 {
336 struct Mesh_RuntimeVertex *current_outer_vertex;
337 int32_t outer_edge_z;
338 int32_t outer_edge_x;
339 int32_t outer_edge_y;
340
341 if (oriented_face_dot <= 0)
342 {
343 outer_edge_x = (int32_t)edge_vertex_cursor->pos.x - (int32_t)previous_outer_vertex->pos.x;
344 camera_relative_or_edge_x = outer_edge_x;
345 outer_edge_y = (int32_t)edge_vertex_cursor->pos.y - (int32_t)previous_outer_vertex->pos.y;
346 current_outer_vertex = edge_vertex_cursor;
347
348 outer_edge_z = (int32_t)edge_vertex_cursor->pos.z - (int32_t)previous_outer_vertex->pos.z;
349 }
350 else
351 {
352 current_outer_vertex = edge_vertex_cursor;
353 outer_edge_x =
354 (int32_t)previous_outer_vertex->pos.x - (int32_t)current_outer_vertex->pos.x;
355 camera_relative_or_edge_x = outer_edge_x;
356 outer_edge_y =
357 (int32_t)previous_outer_vertex->pos.y - (int32_t)current_outer_vertex->pos.y;
358
359 outer_edge_z =
360 (int32_t)previous_outer_vertex->pos.z - (int32_t)current_outer_vertex->pos.z;
361 }
362
363 int32_t outer_relative_x_q10 = ((int32_t)current_outer_vertex->pos.x - camera_delta[0]) << 4;
364 int32_t outer_relative_y_q10 = ((int32_t)current_outer_vertex->pos.y - camera_delta_y_q6) << 4;
365 int32_t outer_relative_z_q10 = ((int32_t)current_outer_vertex->pos.z - camera_delta_z_q6) << 4;
366 int32_t outer_edge_x_q10 = outer_edge_x << 4;
367 int32_t outer_edge_y_q10 = outer_edge_y << 4;
368 int32_t outer_edge_z_q10 = outer_edge_z << 4;
369 int64_t outer_cross_zy_product = (int64_t)outer_relative_z_q10 * outer_edge_y_q10;
370 int64_t outer_cross_yz_product = (int64_t)outer_edge_z_q10 * outer_relative_y_q10;
371
372 outer_plane_cross[0] = ((int32_t)(uint32_t)((uint64_t)outer_cross_zy_product >> 12)) - ((int32_t)(uint32_t)((uint64_t)outer_cross_yz_product >> 12));
373
374 int64_t outer_cross_xz_product = (int64_t)outer_relative_x_q10 * outer_edge_z_q10;
375 int64_t outer_cross_zx_product = (int64_t)outer_edge_x_q10 * outer_relative_z_q10;
376
377 outer_plane_cross[1] = ((int32_t)(uint32_t)((uint64_t)outer_cross_xz_product >> 12)) - ((int32_t)(uint32_t)((uint64_t)outer_cross_zx_product >> 12));
378
379 int64_t outer_cross_yx_product = (int64_t)outer_relative_y_q10 * outer_edge_x_q10;
380 int64_t outer_cross_xy_product = (int64_t)outer_edge_y_q10 * outer_relative_x_q10;
381
382 outer_plane_cross[2] = ((int32_t)(uint32_t)((uint64_t)outer_cross_yx_product >> 12)) - ((int32_t)(uint32_t)((uint64_t)outer_cross_xy_product >> 12));
383
384 int32_t outer_plane_length = Math_SqrtFP12((int32_t)((((uint64_t)((int64_t)*(int32_t *)outer_cross_left * *(int32_t *)outer_cross_right) + (uint64_t)((int64_t)outer_cross_left[1] * outer_cross_right[1]) + (uint64_t)((int64_t)outer_cross_left[2] * outer_cross_right[2])) >> 12)));
385 struct Shadow_EdgePlane *edge_plane_write_cursor_next;
386
387 if (outer_plane_length)
388 {
389 int32_t outer_inverse_length = INT64_C(0x100000000) / outer_plane_length;
390 int64_t outer_normalize_x_product = (int64_t)outer_inverse_length * outer_plane_cross[0];
391
392 edge_plane_write_cursor->nx =
393 (int16_t)(((int32_t)(uint32_t)((uint64_t)outer_normalize_x_product >> 12)) >> 8);
394
395 int64_t outer_normalize_y_product = (int64_t)outer_inverse_length * outer_plane_cross[1];
396
397 edge_plane_write_cursor->ny =
398 (int16_t)(((int32_t)(uint32_t)((uint64_t)outer_normalize_y_product >> 12)) >> 8);
399
400 int64_t outer_normalize_z_product = (int64_t)outer_inverse_length * outer_plane_cross[2];
401
402 edge_plane_write_cursor->nz =
403 (int16_t)(((int32_t)(uint32_t)((uint64_t)outer_normalize_z_product >> 12)) >> 8);
404 edge_plane_write_cursor_next = edge_plane_write_cursor;
405 }
406 else
407 {
408 edge_plane_write_cursor_next = edge_plane_write_cursor;
409 edge_plane_write_cursor_next->nz = 0;
410 edge_plane_write_cursor_next->ny = 0;
411 edge_plane_write_cursor_next->nx = 0;
412 }
413
414 int32_t outer_normal_y = (int32_t)edge_plane_write_cursor_next->ny;
415 int32_t outer_normal_z = (int32_t)edge_plane_write_cursor_next->nz;
416 int32_t outer_normal_x = (int32_t)edge_plane_write_cursor_next->nx;
417
418 if ((outer_normal_y * outer_normal_y + outer_normal_z * outer_normal_z + outer_normal_x * outer_normal_x) >> 0xc > 0x1004)
419 {
420 int32_t *outer_retry_cross_right = &outer_plane_cross[0];
421 int32_t *outer_retry_cross_left = &outer_plane_cross[0];
422 int32_t outer_retry_normal_x;
423 int32_t outer_retry_normal_z;
424 int32_t outer_retry_normal_y;
425
426 do
427 {
428 outer_plane_cross[0] >>= 1;
429 outer_plane_cross[1] >>= 1;
430 outer_plane_cross[2] >>= 1;
431
432 int32_t *outer_retry_right_components = outer_retry_cross_right;
433 int32_t outer_retry_length = Math_SqrtFP12((int32_t)((((uint64_t)((int64_t)*(int32_t *)outer_retry_cross_left * *(int32_t *)outer_retry_right_components) + (uint64_t)((int64_t)outer_retry_cross_left[1] * outer_retry_right_components[1]) + (uint64_t)((int64_t)outer_retry_cross_left[2] * outer_retry_right_components[2])) >> 12)));
434 struct Shadow_EdgePlane *outer_retry_plane;
435
436 if (outer_retry_length)
437 {
438 int32_t outer_retry_inverse_length = INT64_C(0x100000000) / outer_retry_length;
439 int64_t outer_retry_normalize_x_product = (int64_t)outer_retry_inverse_length * outer_plane_cross[0];
440
441 edge_plane_write_cursor->nx =
442 (int16_t)(((int32_t)(uint32_t)((uint64_t)outer_retry_normalize_x_product >> 12)) >> 8);
443
444 int64_t outer_retry_normalize_y_product = (int64_t)outer_retry_inverse_length * outer_plane_cross[1];
445
446 edge_plane_write_cursor->ny =
447 (int16_t)(((int32_t)(uint32_t)((uint64_t)outer_retry_normalize_y_product >> 12)) >> 8);
448
449 int64_t outer_retry_normalize_z_product = (int64_t)outer_retry_inverse_length * outer_plane_cross[2];
450
451 edge_plane_write_cursor->nz =
452 (int16_t)(((int32_t)(uint32_t)((uint64_t)outer_retry_normalize_z_product >> 12)) >> 8);
453 outer_retry_plane = edge_plane_write_cursor;
454 }
455 else
456 {
457 outer_retry_plane = edge_plane_write_cursor;
458 outer_retry_plane->nz = 0;
459 outer_retry_plane->ny = 0;
460 outer_retry_plane->nx = 0;
461 }
462
463 outer_retry_normal_y = (int32_t)outer_retry_plane->ny;
464 outer_retry_normal_z = (int32_t)outer_retry_plane->nz;
465 outer_retry_normal_x = (int32_t)outer_retry_plane->nx;
466 } while ((outer_retry_normal_y * outer_retry_normal_y + outer_retry_normal_z * outer_retry_normal_z + outer_retry_normal_x * outer_retry_normal_x) >> 0xc > 0x1004);
467
468 edge_plane_write_cursor_next = edge_plane_write_cursor;
469 }
470
471 edge_plane_write_cursor = &edge_plane_write_cursor_next[1];
472 previous_outer_vertex = edge_vertex_cursor;
473
474 edge_vertex_cursor = &edge_vertex_cursor[1];
475 outer_edges_remaining -= 1;
476
477 if (!outer_edges_remaining)
478 break;
479
480 oriented_face_dot = saved_face_dot;
481 }
482
483 struct Shadow_OccluderView *linked_shadow_actor =
484 current_shadow_actor;
485 int32_t plane_group_count =
486 (int32_t)linked_shadow_actor->plane_group_count;
487 int32_t inner_groups_remaining = plane_group_count - 1;
488
489 if (plane_group_count != 1)
490 {
491 int32_t inner_plane_cross[3];
492 int32_t *inner_cross_right = &inner_plane_cross[0];
493 int32_t *inner_cross_left = &inner_plane_cross[0];
494
495 do
496 {
497 struct Mesh_RuntimeVertex *current_inner_vertex =
498 edge_vertex_cursor;
499 int32_t inner_edges_remaining = 4;
500 struct Mesh_RuntimeVertex *previous_inner_vertex =
501 &current_inner_vertex[3];
502
503 while (true)
504 {
505 int32_t inner_edge_start_z;
506 int32_t inner_edge_end_z;
507 int32_t inner_edge_x;
508 int32_t inner_edge_y;
509
510 if (saved_face_dot <= 0)
511 {
512 inner_edge_x = (int32_t)current_inner_vertex->pos.x - (int32_t)previous_inner_vertex->pos.x;
513 camera_relative_or_edge_x = inner_edge_x;
514 inner_edge_y = (int32_t)current_inner_vertex->pos.y - (int32_t)previous_inner_vertex->pos.y;
515
516 inner_edge_start_z = (int32_t)previous_inner_vertex->pos.z;
517 inner_edge_end_z = (int32_t)current_inner_vertex->pos.z;
518 }
519 else
520 {
521 inner_edge_x = (int32_t)previous_inner_vertex->pos.x - (int32_t)current_inner_vertex->pos.x;
522 camera_relative_or_edge_x = inner_edge_x;
523 inner_edge_y = (int32_t)previous_inner_vertex->pos.y - (int32_t)current_inner_vertex->pos.y;
524
525 inner_edge_end_z = (int32_t)previous_inner_vertex->pos.z;
526 inner_edge_start_z = (int32_t)current_inner_vertex->pos.z;
527 }
528
529 int32_t inner_edge_z = inner_edge_end_z - inner_edge_start_z;
530 int32_t inner_edge_x_q10 = inner_edge_x << 4;
531 int32_t inner_edge_y_q10 = inner_edge_y << 4;
532 int32_t inner_edge_z_q10 = inner_edge_z << 4;
533 int32_t inner_relative_x_q10 =
534 ((int32_t)current_inner_vertex->pos.x - camera_delta[0]) << 4;
535 int32_t inner_relative_y_q10 =
536 ((int32_t)current_inner_vertex->pos.y - camera_delta_y_q6) << 4;
537 int32_t inner_relative_z_q10 =
538 ((int32_t)current_inner_vertex->pos.z - camera_delta_z_q6) << 4;
539 int64_t inner_cross_yz_product = (int64_t)inner_edge_z_q10 * inner_relative_y_q10;
540 int64_t inner_cross_zy_product = (int64_t)inner_relative_z_q10 * inner_edge_y_q10;
541
542 inner_plane_cross[0] = ((int32_t)(uint32_t)((uint64_t)inner_cross_yz_product >> 12)) - ((int32_t)(uint32_t)((uint64_t)inner_cross_zy_product >> 12));
543
544 int64_t inner_cross_zx_product = (int64_t)inner_edge_x_q10 * inner_relative_z_q10;
545 int64_t inner_cross_xz_product = (int64_t)inner_relative_x_q10 * inner_edge_z_q10;
546
547 inner_plane_cross[1] = ((int32_t)(uint32_t)((uint64_t)inner_cross_zx_product >> 12)) - ((int32_t)(uint32_t)((uint64_t)inner_cross_xz_product >> 12));
548
549 int64_t inner_cross_xy_product = (int64_t)inner_edge_y_q10 * inner_relative_x_q10;
550 int64_t inner_cross_yx_product = (int64_t)inner_relative_y_q10 * inner_edge_x_q10;
551
552 inner_plane_cross[2] = ((int32_t)(uint32_t)((uint64_t)inner_cross_xy_product >> 12)) - ((int32_t)(uint32_t)((uint64_t)inner_cross_yx_product >> 12));
553
554 int32_t inner_plane_length = Math_SqrtFP12((int32_t)((((uint64_t)((int64_t)*(int32_t *)inner_cross_left * *(int32_t *)inner_cross_right) + (uint64_t)((int64_t)inner_cross_left[1] * inner_cross_right[1]) + (uint64_t)((int64_t)inner_cross_left[2] * inner_cross_right[2])) >> 12)));
555 struct Shadow_EdgePlane *inner_plane;
556
557 if (inner_plane_length)
558 {
559 int32_t inner_inverse_length = INT64_C(0x100000000) / inner_plane_length;
560 int64_t inner_normalize_x_product = (int64_t)inner_inverse_length * inner_plane_cross[0];
561
562 inner_plane = edge_plane_write_cursor;
563 inner_plane->nx =
564 (int16_t)(((int32_t)(uint32_t)((uint64_t)inner_normalize_x_product >> 12)) >> 8);
565
566 int64_t inner_normalize_y_product = (int64_t)inner_inverse_length * inner_plane_cross[1];
567
568 inner_plane->ny =
569 (int16_t)(((int32_t)(uint32_t)((uint64_t)inner_normalize_y_product >> 12)) >> 8);
570
571 int64_t inner_normalize_z_product = (int64_t)inner_inverse_length * inner_plane_cross[2];
572
573 inner_plane->nz =
574 (int16_t)(((int32_t)(uint32_t)((uint64_t)inner_normalize_z_product >> 12)) >> 8);
575 }
576 else
577 {
578 inner_plane = edge_plane_write_cursor;
579 inner_plane->nz = 0;
580 inner_plane->ny = 0;
581 inner_plane->nx = 0;
582 }
583
584 int32_t inner_normal_x = (int32_t)inner_plane->nx;
585 int32_t inner_normal_y = (int32_t)inner_plane->ny;
586 int32_t inner_normal_z = (int32_t)inner_plane->nz;
587
588 if ((inner_normal_x * inner_normal_x + inner_normal_y * inner_normal_y + inner_normal_z * inner_normal_z) >> 0xc > 0x1004)
589 {
590 int32_t *inner_retry_cross_right = &inner_plane_cross[0];
591 int32_t *inner_retry_cross_left = &inner_plane_cross[0];
592 int32_t inner_retry_normal_x;
593 int32_t inner_retry_normal_z;
594 int32_t inner_retry_normal_y;
595
596 do
597 {
598 inner_plane_cross[0] >>= 1;
599 inner_plane_cross[1] >>= 1;
600 inner_plane_cross[2] >>= 1;
601
602 int32_t inner_retry_length = Math_SqrtFP12(
603 (int32_t)((((uint64_t)((int64_t)*(int32_t *)inner_retry_cross_left * *(int32_t *)inner_retry_cross_right) + (uint64_t)((int64_t)inner_retry_cross_left[1] * inner_retry_cross_right[1]) + (uint64_t)((int64_t)inner_retry_cross_left[2] * inner_retry_cross_right[2])) >> 12)));
604 struct Shadow_EdgePlane *inner_retry_plane;
605
606 if (inner_retry_length)
607 {
608 int32_t inner_retry_inverse_length = INT64_C(0x100000000) / inner_retry_length;
609 int64_t inner_retry_normalize_x_product = (int64_t)inner_retry_inverse_length * inner_plane_cross[0];
610
611 edge_plane_write_cursor->nx = (int16_t)((
612 (int32_t)(uint32_t)((uint64_t)inner_retry_normalize_x_product >> 12)) >>
613 8);
614
615 int64_t inner_retry_normalize_y_product = (int64_t)inner_retry_inverse_length * inner_plane_cross[1];
616
617 edge_plane_write_cursor->ny = (int16_t)((
618 (int32_t)(uint32_t)((uint64_t)inner_retry_normalize_y_product >> 12)) >>
619 8);
620
621 int64_t inner_retry_normalize_z_product = (int64_t)inner_retry_inverse_length * inner_plane_cross[2];
622
623 edge_plane_write_cursor->nz = (int16_t)((
624 (int32_t)(uint32_t)((uint64_t)inner_retry_normalize_z_product >> 12)) >>
625 8);
626 inner_retry_plane =
627 edge_plane_write_cursor;
628 }
629 else
630 {
631 inner_retry_plane =
632 edge_plane_write_cursor;
633 inner_retry_plane->nz = 0;
634 inner_retry_plane->ny = 0;
635 inner_retry_plane->nx = 0;
636 }
637
638 inner_retry_normal_z = (int32_t)inner_retry_plane->nz;
639 inner_retry_normal_y = (int32_t)inner_retry_plane->ny;
640 inner_retry_normal_x = (int32_t)inner_retry_plane->nx;
641 } while (
642 (inner_retry_normal_x * inner_retry_normal_x + inner_retry_normal_y * inner_retry_normal_y + inner_retry_normal_z * inner_retry_normal_z) >> 0xc > 0x1004);
643
644 inner_plane = edge_plane_write_cursor;
645 }
646
647 edge_plane_write_cursor = &inner_plane[1];
648 previous_inner_vertex = edge_vertex_cursor;
649
650 edge_vertex_cursor = &edge_vertex_cursor[1];
651 inner_edges_remaining -= 1;
652
653 if (!inner_edges_remaining)
654 break;
655
656 current_inner_vertex = edge_vertex_cursor;
657 }
658
659 inner_groups_remaining -= 1;
660 } while (inner_groups_remaining);
661
662 linked_shadow_actor = current_shadow_actor;
663 }
664
665 linked_shadow_actor->next_occluder = (struct Shadow_OccluderView *)(graphics_shadow_render_list);
667 }
668 }
669 }
670
671 source_shadow_actor = current_shadow_actor;
672 }
673
674 struct Shadow_OccluderView *list_next = source_shadow_actor->source_next;
675
676 current_shadow_actor = list_next;
677
678 if (!list_next)
679 break;
680
681 source_shadow_actor = current_shadow_actor;
682 }
683 }
684
685 struct Shadow_OccluderView *ground_test_actor = (struct Shadow_OccluderView *)(graphics_shadow_render_list);
686
687 if (!ground_test_actor)
688 return;
689
691
692 struct Shadow_OccluderView *next_actor;
693
694 do
695 {
696 next_actor = ground_test_actor->next_occluder;
697
698 if (!Collision_CheckActorGround(&ground_test_actor->position,
699 ground_test_actor->cull_radius, 0))
700 {
701 ground_test_actor->next_occluder = (struct Shadow_OccluderView *)(graphics_shadow_render_list);
703 }
704
705 ground_test_actor = next_actor;
706 } while (next_actor);
707}
uint8_t Collision_CheckActorGround(struct Math_Vec3I32 *position, int32_t cull_radius, uint32_t cull_flags)
Definition ground.c:1146
#define SHADOW_OCCLUDER_EXCLUDED
Definition constants.h:67
#define POLYGON_BACK_FACING
Definition constants.h:9
#define POLYGON_TRIANGLE
Definition constants.h:7
#define GRAPHICS_CAP_DISABLE_DISTANCE_CULL
Definition constants.h:52
#define SHADOW_OCCLUDER_TEST_FAR_EDGE
Definition constants.h:93
int32_t Graphics_CheckActorVisibilityAndFrustum(struct Math_Vec3I32 *position, int32_t cull_radius, uint32_t cull_flags)
Definition clipping.c:709
struct Actor * global_type_graphics_shadow_render_list
Definition globals.h:286
#define g_graphicsCapabilities
Definition globals.h:284
#define render_list_state
Definition globals.h:152
#define graphics_shadow_render_list
Definition globals.h:287
#define MATH_Q12_ONE
Definition constants.h:9
int32_t Math_SqrtFP12(uint32_t value)
static struct Math_Vec3I16 face_normal(struct Math_Vec3I16 a, struct Math_Vec3I16 b)
Definition normals.c:39
#define g_objectNodeRoot
Definition globals.h:12
void Shadow_RenderAll()
Definition shadows.c:28
void Shadow_ClearList()
Definition shadows.c:21
struct Math_Vec3I16 position_q6
Definition types.h:144
struct Collision_ClipVertexQ6 * next
Definition types.h:146
uint16_t padding_06
Definition types.h:145
struct Graphics_Render_FrustumClipPlane::@002324077244057217040013003070153264014256137317 normal
struct Graphics_Render_FrustumClipPlane frustum_planes[0x5]
Definition types.h:676
int32_t dynamic_level_scale
Definition types.h:681
struct Math_Vec3I32XZY eye_pos
Definition types.h:672
int16_t z
Definition types.h:460
int16_t x
Definition types.h:458
int16_t y
Definition types.h:459
int32_t x
Definition types.h:482
int32_t z
Definition types.h:483
int32_t y
Definition types.h:484
int32_t y
Definition types.h:471
int32_t x
Definition types.h:470
int32_t z
Definition types.h:472
struct Math_Vec3I16 normal
Definition types.h:963
uint8_t flags_high
Definition types.h:596
uint32_t face_normal_ptr
Definition types.h:587
uint16_t flags
Definition types.h:591
struct Math_Vec3I16 pos
Definition types.h:1115
int16_t normal_group_index
Definition types.h:1116
int16_t ny
Definition types.h:311
int16_t nx
Definition types.h:310
int16_t nz
Definition types.h:312
int32_t cull_radius
Definition types.h:641
struct Math_Vec3I32 position
Definition types.h:632
struct Shadow_EdgePlane * edge_planes
Definition types.h:638
uint32_t exclusion_flags
Definition types.h:640
struct Mesh_Polygon * source_polygon
Definition types.h:636
struct Shadow_OccluderView * source_next
Definition types.h:629
struct Shadow_OccluderView * next_occluder
Definition types.h:630
struct Mesh_RuntimeVertex * runtime_vertices
Definition types.h:637
int16_t plane_group_count
Definition types.h:634