102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
skinning.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
11#include "pcdogs/game/globals.h"
16#include "pcdogs/math/globals.h"
17#include "pcdogs/math/types.h"
18#include "pcdogs/scene/types.h"
19
20#include "spline.h"
21
22static int16_t scale_skin_coordinate(int16_t value, int32_t weight)
23{
24 /* IMUL keeps the low dword before SAR 12. */
25 return (int16_t)((int32_t)((uint32_t)value * (uint32_t)weight) >> MATH_Q12_SHIFT);
26}
27
28static void copy_morph_base(struct Actor_State *actor)
29{
30 const int16_t *table = actor->morph_table_list[0];
31 const int16_t *indices = table + 2;
32 const struct Animation_FrameVertex *source =
33 (const struct Animation_FrameVertex *)(indices + table[0] + table[1]);
34
35 for (uint32_t i = (int32_t)table[0]; i; --i, ++indices, ++source)
36 {
37 struct Animation_MorphNormal *normals = actor->morph_normal_buffer;
38 normals[*indices].normal_xy = source->position_xy;
39 normals[*indices].normal_z_padding = source->position_z_padding;
40 }
41 for (uint32_t i = (int32_t)table[1]; i; --i, ++indices, ++source)
42 {
43 struct Mesh_RuntimeVertex *v = &actor->runtime_vertex_array[*indices];
44 v->position_xy = source->position_xy;
46 }
47}
48
50{
51 int target;
52 int32_t value;
53};
54
55/* The final channel identifies the base shape. Each earlier channel supplies
56 one optional weight, in track order. The native work array has ten entries. */
57static int collect_skin_weights(uint32_t time, const struct Animation_SplineChannelMorph *track,
58 struct SkinWeight *weights)
59{
61 uint32_t remaining = track->nested_scalar_channel_count - 1;
62 int count = 0;
63 do
64 {
65 int32_t value = sample_scalar_keys(time, channel, true);
66 if (value)
67 weights[count++] = (struct SkinWeight){channel->target_index, value};
68 ++channel;
69 } while (--remaining);
70 return count;
71}
72
73static void add_morph_delta(struct Math_Vec3I16 *out, const struct Animation_MorphTargetVertex *delta, int32_t weight)
74{
75 int16_t x = scale_skin_coordinate(delta->delta.x, weight);
76 int16_t y = scale_skin_coordinate(delta->delta.y, weight);
77 int16_t z = scale_skin_coordinate(delta->delta.z, weight);
78 out->x += x;
79 out->y += y;
80 out->z += z;
81}
82
83// Original pcdogs.exe: 0x40d500
84void Bone_TransformVerticesMorphed(uint32_t sample_time, struct Animation_SplineChannelMorph *morph_channels,
85 struct Actor_State *actor)
86{
87 if ((uintptr_t)morph_channels->nested_scalar_channels_or_minus_one_sentinel == UINT32_MAX)
88 {
89 copy_morph_base(actor);
90 return;
91 }
92
93 struct SkinWeight weights[10];
94 int count = collect_skin_weights(sample_time, morph_channels, weights);
95 copy_morph_base(actor);
96 const int16_t *table = actor->morph_table_list[morph_channels->target_index];
97 const int16_t *indices = table + 2;
98 int32_t normal_count = table[0], vertex_count = table[1];
99 const struct Animation_MorphTargetVertex *records =
100 (const struct Animation_MorphTargetVertex *)(indices + normal_count + vertex_count);
101 for (int layer = 0; layer < count; ++layer)
102 {
103 const struct Animation_MorphTargetVertex *delta = records + weights[layer].target;
104 const int16_t *index = indices;
105 for (uint32_t i = normal_count; i; --i, ++index, ++delta)
106 {
107 struct Animation_MorphNormal *normals = actor->morph_normal_buffer;
108 add_morph_delta(&normals[*index].normal, delta, weights[layer].value);
109 }
110 for (uint32_t i = vertex_count; i; --i, ++index, ++delta)
111 add_morph_delta(&actor->runtime_vertex_array[*index].pos, delta, weights[layer].value);
112 }
113}
114
115static void add_skin_vertices(struct Mesh_RuntimeVertex *dst, const struct Animation_FrameVertex *src,
116 int32_t weight, uint32_t count)
117{
118 do
119 {
120 int16_t x = scale_skin_coordinate(src->pos.x, weight);
121 int16_t y = scale_skin_coordinate(src->pos.y, weight);
122 int16_t z = scale_skin_coordinate(src->pos.z, weight);
123 dst->pos.x += x;
124 dst->pos.y += y;
125 dst->pos.z += z;
126 ++dst;
127 ++src;
128 } while (--count);
129}
130
131// Original pcdogs.exe: 0x40d930
132void Bone_TransformVerticesWeighted(uint32_t sample_time, struct Animation_SplineChannelMorph *bone_channels,
133 struct Bone_SkinningOwnerView *actor, struct Submesh_Entry *mesh_piece)
134{
136 return;
137
139 if ((uintptr_t)channels == UINT32_MAX)
140 {
141 int32_t target = bone_channels->packed_timing_or_direct_target_index;
142 if (target >= 0)
143 {
144 struct Animation_FrameVertex *table = actor->skin_frame_tables[target];
145 if ((uintptr_t)table != 1)
146 {
147 int32_t weights[2] = {MATH_Q12_ONE, 0};
148 Bone_BlendVerticesMultiWeight(mesh_piece, actor, &table, weights);
149 }
150 }
151 return;
152 }
153
154 int base_index = bone_channels->nested_scalar_channel_count - 1;
155 if ((uintptr_t)actor->skin_frame_tables[channels[base_index].target_index] == 1)
156 return;
157
158 struct SkinWeight weights[10];
159 int count = collect_skin_weights(sample_time, bone_channels, weights);
160 struct Animation_FrameVertex *base = actor->skin_frame_tables[channels[base_index].target_index];
161 Bone_InitVertexBufferFromSkin(actor, &actor->runtime_vertices[(int16_t)mesh_piece->vert_start_index],
162 base, (int16_t)mesh_piece->vert_count);
163 while (count)
164 {
165 const struct SkinWeight *layer = &weights[--count];
166 struct Animation_FrameVertex *table = actor->skin_frame_tables[layer->target];
167 if ((uintptr_t)table != 1)
168 add_skin_vertices(&actor->runtime_vertices[(int16_t)mesh_piece->vert_start_index], table,
169 layer->value, (int16_t)mesh_piece->vert_count);
170 }
171 Bone_ComputeNormalsPostTransform(actor, mesh_piece);
172}
173
175{
176 if (v->normal_group_index >= 0)
177 {
181 }
182}
183
184// Original pcdogs.exe: 0x40e190
186 struct Animation_FrameVertex *skin_vertices, int32_t vertex_count)
187{
188 uint32_t count = vertex_count;
189 do
190 {
191 clear_skin_normal(actor, dst_vertices);
192 dst_vertices->pos.x = skin_vertices->pos.x;
193 dst_vertices->pos.y = skin_vertices->pos.y;
194 dst_vertices->pos.z = skin_vertices->pos.z;
195 ++dst_vertices;
196 ++skin_vertices;
197 } while (--count);
198}
199
200// Original pcdogs.exe: 0x40e200
202 struct Animation_FrameVertex **skin_tables, int32_t *weights)
203{
204 if (!weights[1])
205 {
206 Bone_InitVertexBufferFromSkin(actor, &actor->runtime_vertices[(int16_t)mesh_piece->vert_start_index],
207 skin_tables[0], (int16_t)mesh_piece->vert_count);
208 }
209 else
210 {
211 for (int i = 0; i < 4 && weights[i]; ++i)
212 {
213 struct Mesh_RuntimeVertex *dst = &actor->runtime_vertices[(int16_t)mesh_piece->vert_start_index];
214 struct Animation_FrameVertex *src = skin_tables[i];
215 uint32_t count = (int16_t)mesh_piece->vert_count;
216 int32_t weight = weights[i];
217 if (i)
218 {
219 add_skin_vertices(dst, src, weight, count);
220 continue;
221 }
222
223 do
224 {
225 clear_skin_normal(actor, dst);
226 dst->pos.x = scale_skin_coordinate(src->pos.x, weight);
227 dst->pos.y = scale_skin_coordinate(src->pos.y, weight);
228 dst->pos.z = scale_skin_coordinate(src->pos.z, weight);
229 ++dst;
230 ++src;
231 } while (--count);
232 }
233 }
234 Bone_ComputeNormalsPostTransform(actor, mesh_piece);
235}
236
237static int32_t rotate_long_coordinate(const int16_t *row, struct Math_Vec3I32 v)
238{
239 int64_t dot = (int64_t)row[0] * v.x + (int64_t)row[1] * v.y + (int64_t)row[2] * v.z;
240 return (int32_t)(dot >> MATH_Q12_SHIFT);
241}
242
243static void prepare_weighted_view(void)
244{
246 const struct Math_Matrix3x3I16 *rotation = &world->rotationQ12;
247 if ((const void *)rotation != (const void *)g_renderNodeInvRotMatrix)
248 {
249 for (int row = 0; row < 3; ++row)
250 for (int col = 0; col < 3; ++col)
251 g_renderNodeInvRotMatrix[3 * row + col] = rotation->elements[3 * col + row];
252 }
253 else
254 {
255 for (int row = 0; row < 3; ++row)
256 for (int col = row + 1; col < 3; ++col)
257 {
258 int16_t temp = g_renderNodeInvRotMatrix[3 * row + col];
259 g_renderNodeInvRotMatrix[3 * row + col] = g_renderNodeInvRotMatrix[3 * col + row];
260 g_renderNodeInvRotMatrix[3 * col + row] = temp;
261 }
262 }
263 const struct Graphics_Render_ListState *state = render_list_state;
264 struct Math_Vec3I32 eye = {
265 state->eye_pos.x - world->translationQ6.x,
266 state->eye_pos.z - world->translationQ6.y,
267 state->eye_pos.y - world->translationQ6.z};
271
272 const int16_t *view = state->node_view_matrix.elements;
277}
278
279static int32_t rotate_skin_coordinate(const int16_t *row, struct Math_Vec3I16 v)
280{
281 return (int32_t)((uint32_t)(row[0] * v.x) + (uint32_t)(row[1] * v.y) + (uint32_t)(row[2] * v.z));
282}
283
284static void project_skin_vertex(int index, struct Math_Vec3I16 local)
285{
287 const int16_t *view = g_cameraTransformMatrix.elements;
288 int32_t z = (rotate_skin_coordinate(&view[6], local) >> 10) + g_nodeViewTranslationZ;
289 screen->screen_z = z;
290 if (z <= render_list_state->projection_near_fp)
291 return;
292
293 int32_t x = (rotate_skin_coordinate(&view[0], local) >> 10) + g_nodeViewTranslationX;
294 int32_t y = (rotate_skin_coordinate(&view[3], local) >> 10) + g_nodeViewTranslationY;
295 int32_t scale = (int64_t)render_list_state->focal_distance * MATH_Q12_ONE / z;
296 int32_t sx = (Graphics_WindowWidth >> 1) + (spline_mul_q12(scale, x) >> MATH_Q12_SHIFT);
297 int32_t sy = (Graphics_WindowHeight >> 1) + (spline_mul_q12(scale, y) >> MATH_Q12_SHIFT);
298 if (sx != (int16_t)sx || sy != (int16_t)sy)
299 screen->screen_z = render_list_state->projection_near_fp;
300 else
301 {
302 screen->screen.x = sx;
303 screen->screen.y = sy;
304 }
305}
306
307// Original pcdogs.exe: 0x4235a0
309 const struct Math_TransformQ12Q6 *world)
310{
311 if (span->polygon_ref_count <= 0)
313
315 {
320 }
321 struct Math_Vec3I16 offset = {
322 (int16_t)((world->translationQ6.x - g_spriteLastPositionX) >> 2),
323 (int16_t)((world->translationQ6.y - g_spriteLastPositionY) >> 2),
324 (int16_t)((world->translationQ6.z - g_spriteLastPositionZ) >> 2)};
325 const int16_t *rot = world->rotationQ12.elements;
326 const struct Bone_WeightedVertex *vertex = node->weighted_vertices + span->weighted_vertex_start_index;
327 for (int i = 0; i < span->weighted_vertex_count; ++i, ++vertex)
328 {
329 struct Math_Vec3I16 local = vertex->local_pos;
330 struct Math_Vec3I16 pos = {
331 (int16_t)((rotate_skin_coordinate(&rot[0], local) >> MATH_Q12_SHIFT) + offset.x),
332 (int16_t)((rotate_skin_coordinate(&rot[3], local) >> MATH_Q12_SHIFT) + offset.y),
333 (int16_t)((rotate_skin_coordinate(&rot[6], local) >> MATH_Q12_SHIFT) + offset.z)};
334 int index = vertex->vertex_index;
335 int32_t weight = vertex->weight_q12;
336 struct Graphics_Render_SpriteVertexData *dst = &data_4967c0[index];
337 if (weight == MATH_Q12_ONE)
338 {
339 project_skin_vertex(index, local);
340 dst->color = node->runtime_vertices[index].color;
341 dst->pos = pos;
342 dst->vertex_state = 2;
343 }
344 else
345 {
346 pos.x = scale_skin_coordinate(pos.x, weight);
347 pos.y = scale_skin_coordinate(pos.y, weight);
348 pos.z = scale_skin_coordinate(pos.z, weight);
349 if (dst->vertex_state == 1)
350 {
351 dst->pos.x += pos.x;
352 dst->pos.y += pos.y;
353 dst->pos.z += pos.z;
354 }
355 else
356 {
357 dst->pos = pos;
358 dst->vertex_state = 1;
359 dst->color = node->runtime_vertices[index].color;
360 }
361 }
362 }
363}
#define ANIMATION_SPLINE_NESTED_CHANNEL_ARRAY
Definition enums.h:49
void Bone_ComputeNormalsPostTransform(struct Bone_SkinningOwnerView *actor, struct Submesh_Entry *mesh_piece)
Definition normals.c:125
#define g_cameraTransformMatrix
Definition globals.h:81
#define GAME_TRANSITION_SPRITE_PENDING
Definition constants.h:22
#define Game_FrameTransitionFlags
Definition globals.h:47
#define g_spriteLastPositionZ
Definition globals.h:263
#define g_projectedSpriteVertices
Definition globals.h:266
#define g_spriteLastPositionX
Definition globals.h:257
#define Graphics_WindowWidth
Definition globals.h:140
#define render_list_state
Definition globals.h:152
#define Graphics_WindowHeight
Definition globals.h:143
#define g_spriteLastPositionY
Definition globals.h:260
#define data_4967c0
Definition globals.h:254
#define MATH_Q12_SHIFT
Definition constants.h:8
#define MATH_Q12_ONE
Definition constants.h:9
void Math_MulMat3x3Q12(struct Math_Matrix3x3I16 *out_matrix, struct Math_Matrix3x3I16 const *right_factor, struct Math_Matrix3x3I16 const *left_factor)
Definition matrices.c:11
#define g_renderWorldTransformPtr
Definition globals.h:27
static void add_morph_delta(struct Math_Vec3I16 *out, const struct Animation_MorphTargetVertex *delta, int32_t weight)
Definition skinning.c:73
void Bone_InitVertexBufferFromSkin(struct Bone_SkinningOwnerView *actor, struct Mesh_RuntimeVertex *dst_vertices, struct Animation_FrameVertex *skin_vertices, int32_t vertex_count)
Definition skinning.c:185
static void prepare_weighted_view(void)
Definition skinning.c:243
static int32_t rotate_long_coordinate(const int16_t *row, struct Math_Vec3I32 v)
Definition skinning.c:237
static int collect_skin_weights(uint32_t time, const struct Animation_SplineChannelMorph *track, struct SkinWeight *weights)
Definition skinning.c:57
static void add_skin_vertices(struct Mesh_RuntimeVertex *dst, const struct Animation_FrameVertex *src, int32_t weight, uint32_t count)
Definition skinning.c:115
void Bone_TransformVerticesMorphed(uint32_t sample_time, struct Animation_SplineChannelMorph *morph_channels, struct Actor_State *actor)
Definition skinning.c:84
void Bone_TransformVerticesWeighted(uint32_t sample_time, struct Animation_SplineChannelMorph *bone_channels, struct Bone_SkinningOwnerView *actor, struct Submesh_Entry *mesh_piece)
Definition skinning.c:132
static void project_skin_vertex(int index, struct Math_Vec3I16 local)
Definition skinning.c:284
void Bone_BlendVerticesMultiWeight(struct Submesh_Entry *mesh_piece, struct Bone_SkinningOwnerView *actor, struct Animation_FrameVertex **skin_tables, int32_t *weights)
Definition skinning.c:201
static void clear_skin_normal(struct Bone_SkinningOwnerView *actor, struct Mesh_RuntimeVertex *v)
Definition skinning.c:174
static void copy_morph_base(struct Actor_State *actor)
Definition skinning.c:28
static int32_t rotate_skin_coordinate(const int16_t *row, struct Math_Vec3I16 v)
Definition skinning.c:279
void Bone_TransformWeightedVerticesForRender(struct Scene_Node *node, struct Submesh_RenderSpan *span, const struct Math_TransformQ12Q6 *world)
Definition skinning.c:308
static int16_t scale_skin_coordinate(int16_t value, int32_t weight)
Definition skinning.c:22
static int32_t sample_scalar_keys(uint32_t frame_time, struct Animation_SplineChannelScalar *channel, bool signed_time)
Definition spline.h:40
static int32_t spline_mul_q12(int32_t a, int32_t b)
Definition spline.h:20
void * morph_normal_buffer
Definition types.h:418
int16_t ** morph_table_list
Definition types.h:486
struct Mesh_RuntimeVertex * runtime_vertex_array
Definition types.h:397
uint32_t position_xy
Definition types.h:183
struct Animation_FrameVertex::@311120214253062041274205007045313305221045115020::@125226036063040207110103344344057165001114352331::@262040241030104153031226016256062266243312142350 pos
uint32_t position_z_padding
Definition types.h:184
uint32_t normal_z_padding
Definition types.h:1234
struct Math_Vec3I16 normal
Definition types.h:1227
struct Animation_MorphTargetVertex::@201261164316150365332005325104317215313175057013 delta
int32_t packed_timing_or_direct_target_index
Definition types.h:309
struct Animation_SplineChannelScalar * nested_scalar_channels_or_minus_one_sentinel
Definition types.h:308
Animation_SplineChannelFlags channel_flags
Definition types.h:313
uint8_t nested_scalar_channel_count
Definition types.h:310
struct Mesh_AccumulatedNormal * normal_accumulators
Definition types.h:481
struct Mesh_RuntimeVertex * runtime_vertices
Definition types.h:480
struct Animation_FrameVertex ** skin_frame_tables
Definition types.h:485
struct Math_Vec3I16 local_pos
Definition types.h:938
int16_t weight_q12
Definition types.h:941
int16_t vertex_index
Definition types.h:940
struct Math_Vec3I32 node_view_translation
Definition types.h:679
struct Math_Matrix3x3I16 node_view_matrix
Definition types.h:677
struct Math_Vec3I32XZY eye_pos
Definition types.h:672
struct Math_ScreenPointI16 screen
Definition types.h:528
struct Math_ColorRGB8 color
Definition types.h:604
struct Math_Vec3I16 pos
Definition types.h:602
int16_t elements[9]
Definition types.h:173
struct Math_Vec3I32 translationQ6
Definition types.h:562
struct Math_Matrix3x3I16 rotationQ12
Definition types.h:560
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:951
struct Math_Vec3I16 pos
Definition types.h:1115
uint32_t position_z_normal_group
Definition types.h:1122
int16_t normal_group_index
Definition types.h:1116
struct Math_ColorRGB8 color
Definition types.h:1126
uint32_t position_xy
Definition types.h:1121
struct Mesh_RuntimeVertex * runtime_vertices
Definition types.h:144
struct Bone_WeightedVertex * weighted_vertices
Definition types.h:184
int32_t value
Definition skinning.c:52
int target
Definition skinning.c:51
Definition types.h:322
uint16_t vert_start_index
Definition types.h:327
uint16_t vert_count
Definition types.h:328
int16_t weighted_vertex_count
Definition types.h:355
int16_t weighted_vertex_start_index
Definition types.h:354
int16_t polygon_ref_count
Definition types.h:353
#define g_nodeViewTranslationY
Definition values.h:774
#define g_viewDirectionZ
Definition values.h:765
#define g_nodeViewTranslationZ
Definition values.h:777
#define g_viewDirectionX
Definition values.h:759
#define g_nodeViewTranslationX
Definition values.h:771
#define g_renderNodeInvRotMatrix
Definition values.h:753
#define g_viewDirectionY
Definition values.h:762