102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
mesh_nodes.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
16#include "pcdogs/math/globals.h"
17#include "pcdogs/math/types.h"
19#include "pcdogs/scene/types.h"
20#include "billboard.h"
21#include <math.h>
22#include <string.h>
23
24static int32_t transform_translation(const int16_t *row, const struct Math_Vec3I32 *position)
25{
26 int64_t dot = (int64_t)row[0] * position->x + (int64_t)row[1] * position->y + (int64_t)row[2] * position->z;
27 return (int32_t)(dot >> MATH_Q12_SHIFT);
28}
29
30static int32_t project_offset(const int16_t *row, struct Math_Vec3I16 offset)
31{
32 uint32_t dot = (uint32_t)(row[0] * offset.x) + (uint32_t)(row[1] * offset.y) + (uint32_t)(row[2] * offset.z);
33 return (int32_t)dot >> 6;
34}
35
36void Graphics_ProjectCorner(struct Graphics_ProjectedCorner *corner, int32_t x, int32_t y, int32_t z)
37{
39 corner->screen.z = z;
40 if (z <= render_list_state->projection_near_fp)
41 return;
42
43 int32_t scale = ((int64_t)render_list_state->focal_distance * MATH_Q12_ONE) / z;
44 /* The native SHRD narrows to 32 bits before the second shift. */
45 int32_t sx = ((int32_t)(((int64_t)scale * x) >> MATH_Q12_SHIFT) >> MATH_Q12_SHIFT) + (Graphics_WindowWidth >> 1);
46 int32_t sy = ((int32_t)(((int64_t)scale * y) >> MATH_Q12_SHIFT) >> MATH_Q12_SHIFT) + (Graphics_WindowHeight >> 1);
47 if ((int16_t)sx != sx || (int16_t)sy != sy)
48 corner->screen.z = render_list_state->projection_near_fp;
49 else
50 {
51 corner->screen.screen.x = sx;
52 corner->screen.screen.y = sy;
53 }
54}
55
57{
58 const struct Math_Matrix3x3I16 *view = &render_list_state->node_view_matrix;
59 const struct Math_Vec3I32 *origin = &render_list_state->node_view_translation;
60 g_nodeViewTranslationX = origin->x + transform_translation(&view->elements[0], world);
61 g_nodeViewTranslationY = origin->y + transform_translation(&view->elements[3], world);
62 g_nodeViewTranslationZ = origin->z + transform_translation(&view->elements[6], world);
63}
64
65static bool build_billboard_corners(struct Graphics_ProjectedCorner *corners, const struct Math_Matrix3x3I16 *offset_rotation,
66 struct Math_Vec3I16 offset, struct Math_Vec2I16 extents, bool yaw_aligned)
67{
68 const struct Math_Matrix3x3I16 *view = &render_list_state->node_view_matrix;
69 const struct Math_Vec3I32 *world = &g_renderWorldTransformPtr->translationQ6;
71
72 int16_t width = extents.x;
73 int16_t height = extents.y;
74 if (offset.x || offset.y || offset.z)
75 {
76 Math_MulMat3x3Q12(&g_cameraTransformMatrix, offset_rotation, view);
77 g_nodeViewTranslationX += (int32_t)((uint32_t)width * project_offset(&g_cameraTransformMatrix.elements[0], offset)) >> 6;
78 g_nodeViewTranslationY += (int32_t)((uint32_t)height * project_offset(&g_cameraTransformMatrix.elements[3], offset)) >> 6;
80 }
81
82 if (yaw_aligned)
83 {
84 int32_t yaw = Math_CalculateAtan2FP12(world->z - render_list_state->eye_pos.y, world->x - render_list_state->eye_pos.x);
86 }
87 else if (render_list_state->view_roll)
89 else
90 {
92 return false;
95 /* A and C combine the packed words from the projected opposite corners.
96 A rejected projection can supply an incoming stack word here. */
97 for (unsigned i = 0; i != 4; i += 2)
98 {
99 const struct Graphics_ProjectedCorner *horizontal = &corners[i ? 1 : 3];
100 const struct Graphics_ProjectedCorner *vertical = &corners[i ? 3 : 1];
101 corners[i].screen.screen.x = horizontal->screen.screen.x;
102 corners[i].screen.screen.y = vertical->screen.screen.y;
103 corners[i].screen.z = g_nodeViewTranslationZ;
104 corners[i].view = (struct Math_Vec3F){horizontal->view.x, vertical->view.y, horizontal->view.z};
105 }
106 return true;
107 }
108
109 for (unsigned i = 0; i < 4; ++i)
110 {
111 struct Math_Vec3I16 local = {
112 (i == 0 || i == 3) ? width : (int16_t)-width,
113 i < 2 ? (int16_t)-height : height, 0};
114 Graphics_ProjectCorner(&corners[i],
118 }
119 return true;
120}
121
123{
124 long double a = corners[0].view.z, b = corners[1].view.z, c = corners[2].view.z;
125 /* The second FCOMP compares the integer result of the first test to depth C. */
126 long double first_equal = a == b || isunordered(a, b);
127 long double depth = first_equal == c || isunordered(first_equal, c)
129 : (c + b + a + corners[3].view.z) * g_levelRenderDistanceQuarter;
130 int32_t key = __ftol_msvc(depth);
131 if (key > 0x3fff)
132 key = 0x3fff;
133 int32_t bucket = (int32_t)(0x3fffu - (uint32_t)key);
134 return bucket < 1 ? 1 : bucket > 0x3fff ? 0x3fff
135 : bucket;
136}
137
138uint32_t Graphics_FadeVertexColor(uint32_t color, uint32_t weight)
139{
140 uint32_t red = ((color >> 12) & 0xff0) * weight;
141 uint32_t green = ((color >> 8) & 0xff) * weight;
142 uint32_t blue = (color & 0xff) * weight;
143 return (red & 0xff0000u) | (((green & 0xff000u) | (blue >> 8)) >> 4);
144}
145
147 const struct Graphics_Render_SpriteNodeData *attachment, uint32_t xy0, uint32_t xy1, uint32_t xy2, uint32_t xy3)
148{
149 struct Graphics_ProjectedCorner corners[4] = {
150 {.screen.packed_screen = xy0}, {.screen.packed_screen = xy1},
151 {.screen.packed_screen = xy2}, {.screen.packed_screen = xy3}};
152 const struct Math_Matrix3x3I16 *rotation = attachment ? &g_renderWorldTransformPtr->rotationQ12 : &node->world_rot_matrix;
153 struct Math_Vec3I16 offset = attachment ? attachment->offset : mesh->offset;
154 struct Math_Vec2I16 extents = attachment ? attachment->bound_extent : node->billboard_scale;
155 unsigned flags = attachment ? attachment->sprite_flags : (uint8_t)node->render_entry_index_for_type_6;
156 if (!build_billboard_corners(corners, rotation, offset, extents, flags & ANIMATION_SPRITE_YAW_ALIGNED))
157 return;
158
159 struct Material_TableEntry *material = attachment ? attachment->material_ptr : mesh->material_ptr;
160 struct Material_TableEntry *second = NULL;
161 if (material->next_material_entry && (material->flags & MATERIAL_LINKED_PASS))
162 {
163 second = material;
164 material = material->next_material_entry;
165 }
166 g_currentRenderMaterial = material;
168 int bucket = 0;
169 if ((material->flags & MATERIAL_DEPTH_SORT) || (packet->flags & MATERIAL_BLEND_ENABLED))
170 bucket = Graphics_QuadDepthBucket(corners);
171
172 for (;;)
173 {
174 packet->face_normal_dot = 0;
175 if (mesh)
176 packet->depth_bias = 0.0f;
177 Graphics_SetPolygonUVs(NULL, packet, g_currentRenderMaterial, -1, NULL);
180 {
181 if (bucket < 1)
182 bucket = 1;
183 head = &render_list_state->polygon_queue_heads[bucket];
184 }
186 head = &render_list_state->sorted_list_buckets[0x3ffe];
187 else
188 head = &render_list_state->sorted_list_head;
189 packet->next_in_bucket = *head;
190 *head = packet;
191
192 for (unsigned i = 0; i < 4; ++i)
193 {
194 const struct Graphics_ProjectedCorner *corner = &corners[(i + 1) & 3];
195 packet->screen_vertices[i] = corner->screen;
196 packet->view_vertices[i] = corner->view;
197 }
199 packet->depth_bias = 100.0f;
200
201 uint32_t capabilities = g_graphicsCapabilityFlags;
202 if (!(capabilities & GRAPHICS_RENDER_UNIFORM_COLOR))
203 {
204 if (attachment)
206 else
207 for (unsigned i = 0; i < 4; ++i)
208 packet->vertex_colors[i] = 0x808080;
209 }
210 else
211 {
212 for (unsigned i = 0; i < 4; ++i)
213 packet->vertex_colors[i] = capabilities;
214 unsigned blend = capabilities >> 29;
215 if (blend)
217 ((blend - 1) << 5) | MATERIAL_BLEND_ENABLED;
218 }
220 {
221 uint32_t fade;
222 memcpy(&fade, &g_renderingStateFlag, sizeof(fade));
223 for (unsigned i = 0; i < 4; ++i)
224 packet->vertex_colors[i] = Graphics_FadeVertexColor(packet->vertex_colors[i], MATH_Q12_ONE - fade);
225 }
226
228 if (!second)
229 return;
231 second = NULL;
233 }
234}
235
236/* The x86 entry supplies the packed XY values retained when projection fails. */
238 uint32_t xy0, uint32_t xy1, uint32_t xy2, uint32_t xy3)
239{
240 Graphics_RenderBillboard(node, mesh, NULL, xy0, xy1, xy2, xy3);
241}
#define ANIMATION_SPRITE_YAW_ALIGNED
Definition constants.h:50
uint32_t Graphics_FadeVertexColor(uint32_t color, uint32_t weight)
Definition mesh_nodes.c:138
#define g_cameraTransformMatrix
Definition globals.h:81
#define GAME_TRANSITION_COLOR_FADE
Definition constants.h:30
@ GAME_MODE_SPRITE_MESH
Definition enums.h:44
@ GAME_MODE_GAMEPLAY_3D
Definition enums.h:38
#define Game_FrameTransitionFlags
Definition globals.h:47
#define g_currentGameMode
Definition globals.h:74
#define GRAPHICS_RENDER_UNIFORM_COLOR
Definition constants.h:23
void Graphics_AdjustColorQuadRGB(uint32_t const *input_colors, uint32_t *output_colors)
Definition effects.c:453
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
#define g_graphicsCapabilityFlags
Definition globals.h:158
#define g_polygonBatchRecords
Definition globals.h:290
#define Graphics_WindowWidth
Definition globals.h:140
#define render_list_state
Definition globals.h:152
#define Graphics_WindowHeight
Definition globals.h:143
#define g_levelRenderDistanceQuarter
Definition globals.h:121
#define g_levelRenderDistance
Definition globals.h:115
#define MATERIAL_BLEND_MODE_HIGH
Definition constants.h:28
#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_BLEND_MODE_LOW
Definition constants.h:27
#define g_currentRenderMaterial
Definition globals.h:15
#define MATH_Q12_SHIFT
Definition constants.h:8
#define MATH_Q6_SCALE_LONG_DOUBLE
Definition constants.h:6
#define MATH_Q12_ONE
Definition constants.h:9
#define MATH_ANGLE_HALF_TURN
Definition constants.h:14
void Math_BuildMatrixRotationY(struct Math_Matrix3x3I16 *out_matrix, struct Math_Matrix3x3I16 const *left_factor, int32_t angle)
Definition matrices.c:76
void Math_BuildMatrixRotationZ(struct Math_Matrix3x3I16 *out_matrix, struct Math_Matrix3x3I16 const *left_factor, int32_t angle)
Definition matrices.c:98
void Math_MulMat3x3Q12(struct Math_Matrix3x3I16 *out_matrix, struct Math_Matrix3x3I16 const *right_factor, struct Math_Matrix3x3I16 const *left_factor)
Definition matrices.c:11
int16_t Math_CalculateAtan2FP12(int32_t y, int32_t x)
#define g_renderWorldTransformPtr
Definition globals.h:27
void Graphics_RenderBillboard(struct Scene_Node *node, const struct Graphics_Render_MeshBillboardRuntimeView *mesh, const struct Graphics_Render_SpriteNodeData *attachment, uint32_t xy0, uint32_t xy1, uint32_t xy2, uint32_t xy3)
Definition mesh_nodes.c:146
static bool build_billboard_corners(struct Graphics_ProjectedCorner *corners, const struct Math_Matrix3x3I16 *offset_rotation, struct Math_Vec3I16 offset, struct Math_Vec2I16 extents, bool yaw_aligned)
Definition mesh_nodes.c:65
void Graphics_RenderBillboardCorners(struct Scene_Node *node, struct Graphics_Render_MeshBillboardRuntimeView *mesh, uint32_t xy0, uint32_t xy1, uint32_t xy2, uint32_t xy3)
Definition mesh_nodes.c:237
void Graphics_TransformViewCenter(const struct Math_Vec3I32 *world)
Definition mesh_nodes.c:56
void Graphics_ProjectCorner(struct Graphics_ProjectedCorner *corner, int32_t x, int32_t y, int32_t z)
Definition mesh_nodes.c:36
static int32_t transform_translation(const int16_t *row, const struct Math_Vec3I32 *position)
Definition mesh_nodes.c:24
int Graphics_QuadDepthBucket(const struct Graphics_ProjectedCorner *corners)
Definition mesh_nodes.c:122
static int32_t project_offset(const int16_t *row, struct Math_Vec3I16 offset)
Definition mesh_nodes.c:30
uint32_t Graphics_FadeVertexColor(uint32_t color, uint32_t weight)
Definition mesh_nodes.c:138
int64_t __ftol_msvc(long double value)
struct Math_Vec3F view
Definition billboard.h:10
struct Graphics_Render_ScreenVertexI16Z screen
Definition billboard.h:9
struct Material_TableEntry * material_ptr
Definition types.h:471
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 Graphics_Render_ScreenVertexI16Z::@276010272006271151340046314217100172252315123166::@167176104245357063215260323150214177117323053225 screen
struct Math_Vec3I16 offset
Definition types.h:590
struct Material_TableEntry * material_ptr
Definition types.h:589
struct Math_Vec2I16 bound_extent
Definition types.h:592
struct Material_TableEntry * next_material_entry
Definition types.h:377
uint32_t flags
Definition types.h:342
int16_t elements[9]
Definition types.h:173
int16_t x
Definition types.h:402
int16_t y
Definition types.h:403
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
int32_t y
Definition types.h:471
int32_t x
Definition types.h:470
int32_t z
Definition types.h:472
struct Math_Vec2I16 billboard_scale
Definition types.h:137
void * model_relocated_ptr_84
Definition types.h:169
struct Math_Matrix3x3I16 world_rot_matrix
Definition types.h:118
char render_entry_index_for_type_6
Definition types.h:131
#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