Paper Mario DX
Paper Mario (N64) modding
 
Loading...
Searching...
No Matches
sprite_shading.c File Reference

Go to the source code of this file.

Functions

void appendGfx_shading_palette (Matrix4f mtx, s32 uls, s32 ult, s32 lrs, s32 lrt, s32 alpha, f32 shadowX, f32 shadowY, f32 shadowZ, s32 shadowR, s32 shadowG, s32 shadowB, s32 highlightR, s32 highlightG, s32 highlightB, s32 ambientPower, s32 otherModeLBits)
 
void clear_sprite_shading_data (void)
 
void init_sprite_shading_data (void)
 
void sprite_shading_set_light_source (u32 idx, s8 flags, f32 x, f32 y, f32 z, u8 r, u8 g, u8 b, f32 falloff, s8 arg9)
 
void create_shading_palette (Matrix4f mtx, s32 uls, s32 ult, s32 lrs, s32 lrt, s32 alpha, s32 otherModeLBits)
 
void func_801491E4 (Matrix4f mtx, s32 arg1, s32 arg2, s32 arg3, s32 arg4, s32 alpha)
 

Variables

SpriteShadingProfilegSpriteShadingProfile
 
SpriteShadingProfilegAuxSpriteShadingProfile
 
BSS SpriteShadingProfile wSpriteShadingProfile
 
BSS SpriteShadingProfile bSpriteShadingProfile
 
BSS SpriteShadingProfile wSpriteShadingProfileAux
 
BSS SpriteShadingProfile bSpriteShadingProfileAux
 
BSS PAL_BIN SpriteShadingPalette [16]
 

Function Documentation

◆ appendGfx_shading_palette()

void appendGfx_shading_palette ( Matrix4f mtx,
s32 uls,
s32 ult,
s32 lrs,
s32 lrt,
s32 alpha,
f32 shadowX,
f32 shadowY,
f32 shadowZ,
s32 shadowR,
s32 shadowG,
s32 shadowB,
s32 highlightR,
s32 highlightG,
s32 highlightB,
s32 ambientPower,
s32 otherModeLBits )

Definition at line 276 of file sprite_shading.c.

284{
286 f32 mtx01;
287 f32 mtx11;
288 f32 mtx21;
289 f32 offsetX;
290 f32 offsetY;
295 f32 ex, ey, ez;
296 f32 pm02, pm12, pm22;
297
299
300 if (shadowMag < 1.0) {
301 ambientPower *= shadowMag;
302 }
303 if (shadowMag != 0.0f) {
304 shadowMag = 1.0f / sqrtf(shadowMag);
305 }
309
310 if (((-mtx[0][2] * camera->mtxPerspective[0][2]) + (mtx[2][2] * camera->mtxPerspective[2][2])) < 0.0f) {
311 facingDir = 1.0f;
312 } else {
313 facingDir = -1.0f;
314 }
315
316 if (facingDir < 0.0f) {
317 ex = mtx[0][2];
318 ey = mtx[1][2];
319 ez = -mtx[2][2];
320 } else {
321 ex = -mtx[0][2];
322 ey = mtx[1][2];
323 ez = mtx[2][2];
324 }
325
326 pm02 = camera->mtxPerspective[0][2];
327 pm12 = camera->mtxPerspective[1][2];
328 pm22 = camera->mtxPerspective[2][2];
329
330 offsetX = ambientPower * ((shadowX * -pm22) + (shadowZ * pm02));
331
333 if (shadowXZ != 0.0f) {
335 }
336 mtx01 = mtx[0][1];
337 mtx11 = mtx[1][1];
338 mtx21 = mtx[2][1];
339 var_f12_2 = SQ(mtx01) + SQ(mtx21);
340 if (var_f12_2 != 0.0f) {
342 }
343 offsetY = -((shadowXZ * var_f12_2) + (shadowY * mtx11)) * ambientPower;
344
345 if (shadowR > 255) {
346 shadowR = 255;
347 }
348 if (shadowG > 255) {
349 shadowG = 255;
350 }
351 if (shadowB > 255) {
352 shadowB = 255;
353 }
354 if (highlightR > 255) {
355 highlightR = 255;
356 }
357 if (highlightG > 255) {
358 highlightG = 255;
359 }
360 if (highlightB > 255) {
361 highlightB = 255;
362 }
363
368
372
374
378 gSPTextureRectangle(gMainGfxPos++, 0, 0, 16 << 2, 1 << 2, 2, 0, 0, 4 << 10, 1 << 10);
381
383 gMainGfxPos++, 0,
384 camera->viewportStartX,
385 camera->viewportStartY,
386 camera->viewportStartX + camera->viewportW,
387 camera->viewportStartY + camera->viewportH
388 );
389
391
395
396 gDPSetRenderMode(gMainGfxPos++, G_RM_PASS, renderMode);
397 gDPSetEnvColor(gMainGfxPos++, 100, 100, 100, 255);
398
399 if (alpha == 255) {
401 } else {
403 }
404
406 gMainGfxPos++,
407 0,
408 ((uls + 0x100) << 2) + (s32)(offsetX * facingDir),
409 ((ult + 0x100) << 2) + (s32)offsetY,
410 ((lrs + 0x100 - 1) << 2) + (s32)(offsetX * facingDir),
411 ((lrt + 0x100 - 1) << 2) + (s32)offsetY
412 );
413}
BSS s32 PopupMenu_SelectedIndex
u16 * nuGfxCfb_ptr
Definition cam_main.c:14
#define sqrtf
#define PM_CC_53
Definition macros.h:495
#define SCREEN_WIDTH
Definition macros.h:109
#define PM_CC_52
Definition macros.h:493
#define PM_CC_50
Definition macros.h:491
#define PM_CC_51
Definition macros.h:492
#define PM_CC_55
Definition macros.h:497
#define SQ(x)
Definition macros.h:170
#define PM_CC_54
Definition macros.h:496
BSS PAL_BIN SpriteShadingPalette[16]
Camera gCameras[4]
Definition cam_main.c:17
Gfx * gMainGfxPos
Definition cam_main.c:15
s32 gCurrentCameraID
Definition cam_math.c:4

Referenced by create_shading_palette().

◆ clear_sprite_shading_data()

void clear_sprite_shading_data ( void )

Definition at line 20 of file sprite_shading.c.

20 {
21 s32 i;
22
24
25 for (i = 0; i < ARRAY_COUNT(gSpriteShadingProfile->sources); i++) {
28 }
29
33
37}
SpriteShadingLightSource sources[7]
@ LIGHT_SOURCE_DISABLED
Definition enums.h:2419
#define ARRAY_COUNT(arr)
Definition macros.h:40
SpriteShadingProfile * gSpriteShadingProfile
SpriteShadingProfile * gAuxSpriteShadingProfile
void init_sprite_shading_data(void)

Referenced by load_demo_battle(), load_engine_data(), load_map_by_IDs(), state_step_battle(), state_step_demo(), state_step_intro(), state_step_pause(), and state_step_startup().

◆ init_sprite_shading_data()

void init_sprite_shading_data ( void )

Definition at line 39 of file sprite_shading.c.

39 {
43 } else {
46 }
47}
@ CONTEXT_WORLD
Definition enums.h:3528
BSS SpriteShadingProfile wSpriteShadingProfileAux
BSS SpriteShadingProfile wSpriteShadingProfile
BSS SpriteShadingProfile bSpriteShadingProfile
BSS SpriteShadingProfile bSpriteShadingProfileAux
GameStatus * gGameStatusPtr
Definition main_loop.c:32

Referenced by clear_sprite_shading_data(), state_step_end_battle(), and state_step_unpause().

◆ sprite_shading_set_light_source()

void sprite_shading_set_light_source ( u32 idx,
s8 flags,
f32 x,
f32 y,
f32 z,
u8 r,
u8 g,
u8 b,
f32 falloff,
s8 arg9 )

Definition at line 49 of file sprite_shading.c.

49 {
51
55 lightSource->pos.x = x;
56 lightSource->pos.y = y;
57 lightSource->pos.z = z;
58 lightSource->rgb.r = r;
59 lightSource->rgb.g = g;
60 lightSource->rgb.b = b;
61 lightSource->falloff = falloff;
62 lightSource->unk_14 = arg9;
63 }
64}
f32 falloff
Definition demo_api.c:18
s8 flags
Definition demo_api.c:15

◆ create_shading_palette()

void create_shading_palette ( Matrix4f mtx,
s32 uls,
s32 ult,
s32 lrs,
s32 lrt,
s32 alpha,
s32 otherModeLBits )

Definition at line 66 of file sprite_shading.c.

66 {
70 f32 posX, posY, posZ;
72 f32 Mxz, Myz, Mzz;
82 f32 distSq;
83 f32 dx, dy, dz;
84 f32 dist;
86 f32 qx, qy, qz;
87 f32 wx, wy, wz;
88 f32 ex, ey, ez;
89 f32 Pxz, Pzz;
90 s32 i;
91
92 shadowDirX = 0.0f;
93 shadowDirY = 0.0f;
94 shadowDirZ = 0.0f;
95
96 posX = mtx[3][0];
97 posY = mtx[3][1];
98 posZ = mtx[3][2];
99
100 Mxz = mtx[0][2];
101 Myz = mtx[1][2];
102 Mzz = mtx[2][2];
103
104 commonColorR = 0.0f;
105 commonColorG = 0.0f;
106 commonColorB = 0.0f;
107
108 backColorR = 0.0f;
109 backColorG = 0.0f;
110 backColorB = 0.0f;
111
112 frontColorR = 0.0f;
113 frontColorG = 0.0f;
114 frontColorB = 0.0f;
115
119
120 Pxz = camera->mtxPerspective[0][2];
121 Pzz = camera->mtxPerspective[2][2];
122
123 if ((-Mxz * Pxz + Mzz * Pzz) < 0.0f) {
124 facingDir = 1.0f;
125 } else {
126 facingDir = -1.0f;
127 }
128
129 for (i = 0; i < ARRAY_COUNT(gSpriteShadingProfile->sources); i++) {
131 if (lightSource->flags & LIGHT_SOURCE_ENABLED) {
132 dx = posX - lightSource->pos.x;
133 dy = posY - lightSource->pos.y;
134 dz = posZ - lightSource->pos.z;
135
136 distSq = SQ(dx) + SQ(dy) + SQ(dz);
137
138 if (distSq != 0.0f) {
139 dist = sqrtf(distSq);
140 invDist = 1.0f / dist;
141 } else {
142 dist = 0.0f;
143 invDist = 0.0f;
144 }
145
146 // normalize dx/dy/dz
147 // they now form a normal vector pointing from light source to sprite
148 dx *= invDist;
149 dy *= invDist;
150 dz *= invDist;
151
152 // apply falloff from light source
154 if ((dist == 0.0f) && (lightSource->falloff == 0.0f)) {
155 intensityScale = 1.0f;
156 } else {
157 intensityScale = 1.0f / (dist * lightSource->falloff);
161 }
162 } else if (lightSource->flags & LIGHT_SOURCE_QUADRATIC_FALLOFF) {
163 if ((distSq == 0.0f) && (lightSource->falloff == 0.0f)) {
164 intensityScale = 1.0f;
165 } else {
166 intensityScale = 1.0f / (distSq * lightSource->falloff);
170 }
171 } else {
172 intensityScale = 1.0f;
173 }
174
175 if (intensityScale > 1.0f) {
176 intensityScale = 1.0f;
177 }
178
179 shadowDirX += dx;
180 shadowDirY += dy;
181 shadowDirZ += dz;
182
183 if (facingDir < 0.0f) {
184 ex = Mxz;
185 ey = Myz;
186 ez = -Mzz;
187 } else {
188 ex = -Mxz;
189 ey = Myz;
190 ez = Mzz;
191 }
192 temp_f10 = ex * dx + ey * dy + ez * dz;
193
194 if (facingDir < 0.0f) {
195 wx = Mzz;
196 wy = Myz;
197 wz = Mxz;
198 } else {
199 wx = -Mzz;
200 wy = Myz;
201 wz = -Mxz;
202 }
203 temp_f16 = wx * dx + wy * dy + wz * dz;
204
207 if (temp_f10 > 0.0f) {
211 if (temp_f16 > 0.0f) {
215 } else {
219 }
220 } else {
224 if (temp_f16 > 0.0f) {
228 } else {
232 }
233 }
234 }
235 }
236
237 if (facingDir < 0.0f) {
238 qx = Mzz;
239 qy = Myz;
240 qz = Mxz;
241 } else {
242 qx = -Mzz;
243 qy = Myz;
244 qz = -Mxz;
245 }
246
247 if (qx * shadowDirX + qy * shadowDirY + qz * shadowDirZ > 0.0f) {
249 mtx,
250 uls, ult, lrs, lrt,
251 alpha,
259 );
260 } else {
262 mtx,
263 uls, ult, lrs, lrt,
264 alpha,
272 );
273 }
274}
@ LIGHT_SOURCE_ENABLED
Definition enums.h:2420
@ LIGHT_SOURCE_QUADRATIC_FALLOFF
Definition enums.h:2422
@ LIGHT_SOURCE_LINEAR_FALLOFF
Definition enums.h:2421
f32 fabsf(f32 f)
void appendGfx_shading_palette(Matrix4f mtx, s32 uls, s32 ult, s32 lrs, s32 lrt, s32 alpha, f32 shadowX, f32 shadowY, f32 shadowZ, s32 shadowR, s32 shadowG, s32 shadowB, s32 highlightR, s32 highlightG, s32 highlightB, s32 ambientPower, s32 otherModeLBits)

Referenced by imgfx_appendGfx_mesh_anim(), imgfx_appendGfx_mesh_basic(), imgfx_appendGfx_mesh_grid(), and spr_appendGfx_component_flat().

◆ func_801491E4()

void func_801491E4 ( Matrix4f mtx,
s32 arg1,
s32 arg2,
s32 arg3,
s32 arg4,
s32 alpha )

Definition at line 415 of file sprite_shading.c.

415 {
416 gDPSetPrimColor(gMainGfxPos++, 0, 0, 0, 0, 0, alpha);
417
418 if (alpha == 255) {
420 } else {
422 }
423}
#define PM_CC_WINDOW_5
Definition macros.h:445
#define PM_CC_02
Definition macros.h:281

Referenced by appendGfx_item_entity(), and render_item_entities().

Variable Documentation

◆ gSpriteShadingProfile

◆ gAuxSpriteShadingProfile

SpriteShadingProfile* gAuxSpriteShadingProfile

Definition at line 6 of file sprite_shading.c.

Referenced by clear_sprite_shading_data(), and init_sprite_shading_data().

◆ wSpriteShadingProfile

BSS SpriteShadingProfile wSpriteShadingProfile

Definition at line 8 of file sprite_shading.c.

Referenced by init_sprite_shading_data().

◆ bSpriteShadingProfile

BSS SpriteShadingProfile bSpriteShadingProfile

Definition at line 9 of file sprite_shading.c.

Referenced by init_sprite_shading_data().

◆ wSpriteShadingProfileAux

BSS SpriteShadingProfile wSpriteShadingProfileAux

Definition at line 10 of file sprite_shading.c.

Referenced by init_sprite_shading_data().

◆ bSpriteShadingProfileAux

BSS SpriteShadingProfile bSpriteShadingProfileAux

Definition at line 11 of file sprite_shading.c.

Referenced by init_sprite_shading_data().

◆ SpriteShadingPalette

BSS PAL_BIN SpriteShadingPalette[16]

Definition at line 12 of file sprite_shading.c.

Referenced by appendGfx_shading_palette().