102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference |
GitLab repository
Toggle main menu visibility
Loading...
Searching...
No Matches
normals.c
Go to the documentation of this file.
1
#include "
pcdogs/animation/functions.h
"
2
#include "
pcdogs/animation/constants.h
"
3
#include "
pcdogs/material/types.h
"
4
#include "
pcdogs/scene/constants.h
"
5
#include "
pcdogs/animation/types.h
"
6
#include "
pcdogs/graphics/constants.h
"
7
#include "
pcdogs/graphics/types.h
"
8
#include "
pcdogs/image/values.h
"
9
#include "
pcdogs/math/constants.h
"
10
#include "
pcdogs/math/functions.h
"
11
12
static
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
20
static
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
26
static
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
39
static
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
58
static
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
66
static
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
77
void
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
104
normal =
face_normal
(
edge_between
(c, a),
edge_between
(
triangle_anchor
(), b));
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
125
void
Bone_ComputeNormalsPostTransform
(
struct
Bone_SkinningOwnerView
*actor,
struct
Submesh_Entry
*mesh_piece)
126
{
127
if
(!(mesh_piece->
flags
&
ANIMATION_SKIN_ACCUMULATE_NORMALS
) && actor->
object_type
==
SCENE_NODE_SCALED_GEOMETRY
)
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;
148
if
(actor->
behavior_flags_low
&
ANIMATION_SKIN_FULL_SCALE_PLANE
)
149
distance = (int32_t)(0u - (uint32_t)dot);
150
else
151
{
152
distance = -(dot >> 2);
153
if
(actor->
object_type
==
SCENE_NODE_SCALED_GEOMETRY
)
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
162
if
(mesh_piece->
flags
&
ANIMATION_SKIN_ACCUMULATE_NORMALS
)
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
178
if
(mesh_piece->
flags
&
ANIMATION_SKIN_ACCUMULATE_NORMALS
)
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
{
185
struct
Math_Vec3I16
*acc = &actor->
normal_accumulators
[v->
normal_group_index
].
normal
;
186
*acc =
normalize_packed
(*acc);
187
}
188
}
189
}
190
}
constants.h
ANIMATION_SKIN_ACCUMULATE_NORMALS
#define ANIMATION_SKIN_ACCUMULATE_NORMALS
Definition
constants.h:45
ANIMATION_SKIN_FULL_SCALE_PLANE
#define ANIMATION_SKIN_FULL_SCALE_PLANE
Definition
constants.h:49
ANIMATION_SKIN_FACE_REBUILD_MASK
#define ANIMATION_SKIN_FACE_REBUILD_MASK
Definition
constants.h:48
functions.h
types.h
constants.h
POLYGON_TRIANGLE
#define POLYGON_TRIANGLE
Definition
constants.h:7
POLYGON_TWO_SIDED
#define POLYGON_TWO_SIDED
Definition
constants.h:8
types.h
MATERIAL_TWO_SIDED
#define MATERIAL_TWO_SIDED
Definition
constants.h:24
types.h
constants.h
MATH_Q12_SHIFT
#define MATH_Q12_SHIFT
Definition
constants.h:8
MATH_Q12_ONE
#define MATH_Q12_ONE
Definition
constants.h:9
functions.h
Math_SqrtFP12
int32_t Math_SqrtFP12(uint32_t value)
Definition
fixed_point.c:112
face_normal
static struct Math_Vec3I16 face_normal(struct Math_Vec3I16 a, struct Math_Vec3I16 b)
Definition
normals.c:39
Bone_ComputeNormalsPostTransform
void Bone_ComputeNormalsPostTransform(struct Bone_SkinningOwnerView *actor, struct Submesh_Entry *mesh_piece)
Definition
normals.c:125
edge_between
static struct Math_Vec3I16 edge_between(struct Math_Vec3I16 end, struct Math_Vec3I16 start)
Definition
normals.c:12
triangle_anchor
static struct Math_Vec3I16 triangle_anchor(void)
Definition
normals.c:66
Mesh_CalculateVertexNormals
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
cross_length_squared_q12
static uint32_t cross_length_squared_q12(struct Math_Vec3I32 cross)
Definition
normals.c:20
normalize_packed
static struct Math_Vec3I16 normalize_packed(struct Math_Vec3I16 vector)
Definition
normals.c:58
unit_vector
static struct Math_Vec3I16 unit_vector(struct Math_Vec3I32 vector, uint32_t length_squared_q12)
Definition
normals.c:26
constants.h
SCENE_NODE_SCALED_GEOMETRY
@ SCENE_NODE_SCALED_GEOMETRY
Definition
constants.h:9
Bone_SkinningOwnerView
Definition
types.h:475
Bone_SkinningOwnerView::object_type
uint8_t object_type
Definition
types.h:477
Bone_SkinningOwnerView::normal_accumulators
struct Mesh_AccumulatedNormal * normal_accumulators
Definition
types.h:481
Bone_SkinningOwnerView::runtime_vertices
struct Mesh_RuntimeVertex * runtime_vertices
Definition
types.h:480
Bone_SkinningOwnerView::behavior_flags_low
uint8_t behavior_flags_low
Definition
types.h:483
Bone_SkinningOwnerView::polygon_refs
struct Graphics_Render_PolygonRenderRef * polygon_refs
Definition
types.h:479
Graphics_Render_PolygonRenderRef
Definition
types.h:203
Graphics_Render_PolygonRenderRef::flags
uint16_t flags
Definition
types.h:207
Graphics_Render_PolygonRenderRef::material_ref
struct Material_TableEntry * material_ref
Definition
types.h:204
Graphics_Render_PolygonRenderRef::face_normal
struct Mesh_FaceNormal * face_normal
Definition
types.h:206
Graphics_Render_PolygonRenderRef::vertex_indices
uint16_t vertex_indices[0x4]
Definition
types.h:205
Graphics_Render_SpriteVertexData
Definition
types.h:601
Graphics_Render_SpriteVertexData::pos
struct Math_Vec3I16 pos
Definition
types.h:602
Material_TableEntry::flags
uint32_t flags
Definition
types.h:342
Math_Vec3I16
Definition
types.h:457
Math_Vec3I16::z
int16_t z
Definition
types.h:460
Math_Vec3I16::x
int16_t x
Definition
types.h:458
Math_Vec3I16::y
int16_t y
Definition
types.h:459
Math_Vec3I32
Definition
types.h:469
Math_Vec3I32::y
int32_t y
Definition
types.h:471
Math_Vec3I32::x
int32_t x
Definition
types.h:470
Math_Vec3I32::z
int32_t z
Definition
types.h:472
Mesh_AccumulatedNormal::normal
struct Math_Vec3I16 normal
Definition
types.h:951
Mesh_FaceNormal::normal
struct Math_Vec3I16 normal
Definition
types.h:963
Mesh_FaceNormal::plane_dist
int32_t plane_dist
Definition
types.h:962
Mesh_RuntimeVertex
Definition
types.h:1110
Mesh_RuntimeVertex::pos
struct Math_Vec3I16 pos
Definition
types.h:1115
Mesh_RuntimeVertex::normal_group_index
int16_t normal_group_index
Definition
types.h:1116
Mesh_VertexNormal
Definition
types.h:1150
Mesh_VertexNormal::normal
struct Math_Vec3I16 normal
Definition
types.h:1151
Mesh_VertexNormal::normal_count
int16_t normal_count
Definition
types.h:1152
Submesh_Entry
Definition
types.h:322
Submesh_Entry::vert_start_index
uint16_t vert_start_index
Definition
types.h:327
Submesh_Entry::poly_start_index
int16_t poly_start_index
Definition
types.h:329
Submesh_Entry::flags
uint8_t flags
Definition
types.h:324
Submesh_Entry::vert_count
uint16_t vert_count
Definition
types.h:328
Submesh_Entry::poly_count
int16_t poly_count
Definition
types.h:330
values.h
g_normalAccumulatorX
#define g_normalAccumulatorX
Definition
values.h:330
src
animation
normals.c
Generated by
1.17.0