102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
normals.c
Go to the documentation of this file.
11
12static struct Math_Vec3I16 edge_between(struct Math_Vec3I16 end, struct Math_Vec3I16 start)
13{
14 return (struct Math_Vec3I16){
15 (int16_t)(end.x - start.x),
16 (int16_t)(end.y - start.y),
17 (int16_t)(end.z - start.z)};
18}
19
20static uint32_t cross_length_squared_q12(struct Math_Vec3I32 cross)
21{
22 uint64_t sum = (uint64_t)((int64_t)cross.x * cross.x) + (uint64_t)((int64_t)cross.y * cross.y) + (uint64_t)((int64_t)cross.z * cross.z);
23 return (uint32_t)(sum >> MATH_Q12_SHIFT);
24}
25
26static struct Math_Vec3I16 unit_vector(struct Math_Vec3I32 vector, uint32_t length_squared_q12)
27{
28 int32_t length = Math_SqrtFP12(length_squared_q12);
29 if (length == 0)
30 return (struct Math_Vec3I16){0};
31
32 int32_t scale = (int32_t)(INT64_C(0x100000000) / length);
33 return (struct Math_Vec3I16){
34 (int16_t)(((int64_t)vector.x * scale) >> 20),
35 (int16_t)(((int64_t)vector.y * scale) >> 20),
36 (int16_t)(((int64_t)vector.z * scale) >> 20)};
37}
38
39static struct Math_Vec3I16 face_normal(struct Math_Vec3I16 a, struct Math_Vec3I16 b)
40{
41 struct Math_Vec3I32 cross = {
42 a.y * b.z - a.z * b.y,
43 a.z * b.x - a.x * b.z,
44 a.x * b.y - a.y * b.x};
45 uint32_t squared = cross_length_squared_q12(cross);
46 if (squared < MATH_Q12_ONE)
47 {
48 /* Preserve precision for small faces before the integer square root. */
49 int scale = squared < 0x100 ? 256 : 64;
50 cross.x *= scale;
51 cross.y *= scale;
52 cross.z *= scale;
53 squared = cross_length_squared_q12(cross);
54 }
55 return unit_vector(cross, squared);
56}
57
58static struct Math_Vec3I16 normalize_packed(struct Math_Vec3I16 vector)
59{
60 /* The packed-vector path sums squares in 32 bits before the signed shift. */
61 int32_t squared = vector.x * vector.x + vector.y * vector.y + vector.z * vector.z;
62 struct Math_Vec3I32 wide = {vector.x, vector.y, vector.z};
63 return unit_vector(wide, squared >> MATH_Q12_SHIFT);
64}
65
66static struct Math_Vec3I16 triangle_anchor(void)
67{
68 /* The triangle branch reads the absent fourth vertex at addresses 0, 2,
69 and 4. Keep the original word reads even when page zero is unmapped. */
70 struct Math_Vec3I16 anchor;
71 __asm__ volatile("movw 0, %0\n\tmovw 2, %1\n\tmovw 4, %2"
72 : "=r"(anchor.x), "=r"(anchor.y), "=r"(anchor.z) : : "memory");
73 return anchor;
74}
75
76// Original pcdogs.exe: 0x4247d0
77void Mesh_CalculateVertexNormals(struct Graphics_Render_PolygonRenderRef *polygon_refs, int32_t polygon_count, struct Graphics_Render_SpriteVertexData *transformed_vertices, int32_t vertex_count)
78{
79 struct Mesh_VertexNormal *accumulators = (void *)&g_normalAccumulatorX;
80 do
81 {
82 const uint16_t *indices = polygon_refs->vertex_indices;
83 struct Math_Vec3I16 a = transformed_vertices[indices[0]].pos;
84 struct Math_Vec3I16 b = transformed_vertices[indices[1]].pos;
85 struct Math_Vec3I16 c = transformed_vertices[indices[2]].pos;
86 const struct Graphics_Render_SpriteVertexData *fourth = NULL;
87 if (!(polygon_refs->flags & POLYGON_TRIANGLE))
88 fourth = &transformed_vertices[indices[3]];
89
90 struct Math_Vec3I16 normal;
91 if (fourth)
92 {
93 struct Math_Vec3I16 d = fourth->pos;
94 struct Math_Vec3I16 diagonal = edge_between(d, b);
95 struct Math_Vec3I16 first = face_normal(edge_between(d, a), diagonal);
96 struct Math_Vec3I16 second = face_normal(edge_between(c, d), diagonal);
97 struct Math_Vec3I16 sum = {
98 (int16_t)(first.x + second.x),
99 (int16_t)(first.y + second.y),
100 (int16_t)(first.z + second.z)};
101 normal = normalize_packed(sum);
102 }
103 else
105
106 for (unsigned i = 0; i < (fourth ? 4u : 3u); ++i)
107 {
108 accumulators[indices[i]].normal_count = 1;
109 accumulators[indices[i]].normal.x += normal.x;
110 accumulators[indices[i]].normal.y += normal.y;
111 accumulators[indices[i]].normal.z += normal.z;
112 }
113 ++polygon_refs;
114 } while (--polygon_count != 0);
115
116 /* Native 424eb1..424f7d repeatedly normalizes entry zero. */
117 do
118 {
119 if (accumulators[0].normal_count)
120 accumulators[0].normal = normalize_packed(accumulators[0].normal);
121 } while (--vertex_count != 0);
122}
123
124// Original pcdogs.exe: 0x40dc40
126{
128 return;
129
130 struct Mesh_RuntimeVertex *vertices = actor->runtime_vertices;
131 struct Graphics_Render_PolygonRenderRef *face = actor->polygon_refs + mesh_piece->poly_start_index;
132 for (uint32_t i = (int32_t)mesh_piece->poly_count; i; --i, ++face)
133 {
134 const uint16_t *indices = face->vertex_indices;
135 struct Math_Vec3I16 a = vertices[indices[0]].pos;
136 struct Math_Vec3I16 b = vertices[indices[1]].pos;
137 struct Math_Vec3I16 c = vertices[indices[2]].pos;
138 /* The fourth vertex participates in the diagonal even on triangle faces. */
139 struct Math_Vec3I16 d = vertices[indices[3]].pos;
140 struct Math_Vec3I16 normal = face_normal(edge_between(c, a), edge_between(d, b));
141 unsigned corners = face->flags & POLYGON_TRIANGLE ? 3 : 4;
142
143 if (face->face_normal)
144 {
145 face->face_normal->normal = normal;
146 int32_t dot = (int32_t)((uint32_t)(normal.x * a.x) + (uint32_t)(normal.y * a.y) + (uint32_t)(normal.z * a.z));
147 int32_t distance;
149 distance = (int32_t)(0u - (uint32_t)dot);
150 else
151 {
152 distance = -(dot >> 2);
154 distance >>= 4;
155 }
156 face->face_normal->plane_dist = distance;
157 face->flags &= (uint16_t)~ANIMATION_SKIN_FACE_REBUILD_MASK;
158 }
159 if (face->material_ref && (face->material_ref->flags & MATERIAL_TWO_SIDED))
160 face->flags |= POLYGON_TWO_SIDED;
161
163 {
164 for (unsigned j = 0; j < corners; ++j)
165 {
166 int16_t group = vertices[indices[j]].normal_group_index;
167 if (group >= 0)
168 {
169 struct Math_Vec3I16 *acc = &actor->normal_accumulators[group].normal;
170 acc->x += normal.x;
171 acc->y += normal.y;
172 acc->z += normal.z;
173 }
174 }
175 }
176 }
177
179 {
180 const struct Mesh_RuntimeVertex *v = vertices + (int16_t)mesh_piece->vert_start_index;
181 for (uint32_t i = (int16_t)mesh_piece->vert_count; i; --i, ++v)
182 {
183 if (v->normal_group_index >= 0)
184 {
186 *acc = normalize_packed(*acc);
187 }
188 }
189 }
190}
#define ANIMATION_SKIN_ACCUMULATE_NORMALS
Definition constants.h:45
#define ANIMATION_SKIN_FULL_SCALE_PLANE
Definition constants.h:49
#define ANIMATION_SKIN_FACE_REBUILD_MASK
Definition constants.h:48
#define POLYGON_TRIANGLE
Definition constants.h:7
#define POLYGON_TWO_SIDED
Definition constants.h:8
#define MATERIAL_TWO_SIDED
Definition constants.h:24
#define MATH_Q12_SHIFT
Definition constants.h:8
#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
void Bone_ComputeNormalsPostTransform(struct Bone_SkinningOwnerView *actor, struct Submesh_Entry *mesh_piece)
Definition normals.c:125
static struct Math_Vec3I16 edge_between(struct Math_Vec3I16 end, struct Math_Vec3I16 start)
Definition normals.c:12
static struct Math_Vec3I16 triangle_anchor(void)
Definition normals.c:66
void Mesh_CalculateVertexNormals(struct Graphics_Render_PolygonRenderRef *polygon_refs, int32_t polygon_count, struct Graphics_Render_SpriteVertexData *transformed_vertices, int32_t vertex_count)
Definition normals.c:77
static uint32_t cross_length_squared_q12(struct Math_Vec3I32 cross)
Definition normals.c:20
static struct Math_Vec3I16 normalize_packed(struct Math_Vec3I16 vector)
Definition normals.c:58
static struct Math_Vec3I16 unit_vector(struct Math_Vec3I32 vector, uint32_t length_squared_q12)
Definition normals.c:26
@ SCENE_NODE_SCALED_GEOMETRY
Definition constants.h:9
struct Mesh_AccumulatedNormal * normal_accumulators
Definition types.h:481
struct Mesh_RuntimeVertex * runtime_vertices
Definition types.h:480
uint8_t behavior_flags_low
Definition types.h:483
struct Graphics_Render_PolygonRenderRef * polygon_refs
Definition types.h:479
struct Material_TableEntry * material_ref
Definition types.h:204
struct Mesh_FaceNormal * face_normal
Definition types.h:206
struct Math_Vec3I16 pos
Definition types.h:602
uint32_t flags
Definition types.h:342
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_Vec3I16 normal
Definition types.h:951
struct Math_Vec3I16 normal
Definition types.h:963
int32_t plane_dist
Definition types.h:962
struct Math_Vec3I16 pos
Definition types.h:1115
int16_t normal_group_index
Definition types.h:1116
struct Math_Vec3I16 normal
Definition types.h:1151
int16_t normal_count
Definition types.h:1152
Definition types.h:322
uint16_t vert_start_index
Definition types.h:327
int16_t poly_start_index
Definition types.h:329
uint8_t flags
Definition types.h:324
uint16_t vert_count
Definition types.h:328
int16_t poly_count
Definition types.h:330
#define g_normalAccumulatorX
Definition values.h:330