Skip to content

Commit 24d388c

Browse files
authored
xShadow progress (#778)
* These get used in xShadowRenderWorld but I scrapped what I had. * xShadow progress
1 parent 263b63f commit 24d388c

2 files changed

Lines changed: 220 additions & 5 deletions

File tree

src/SB/Core/x/xShadow.cpp

Lines changed: 219 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include "xShadow.h"
22

3+
#include "rpcollis.h"
34
#include "rpworld.h"
45

56
#include "xMath.h"
7+
#include "iCollide.h"
68
#include "iModel.h"
79
#include "zGlobals.h"
10+
#include "zGrid.h"
811

912
#include <types.h>
1013

@@ -15,12 +18,16 @@ static F32 SHADOW_BOTH;
1518
RwCamera* ShadowCamera;
1619
RwRaster* ShadowCameraRaster;
1720
static RwRaster* ShadowRenderRaster;
21+
U32 gShadowFlags;
1822
F32 gShadowObjectRadius;
1923
static S32 shadow_ent_count;
24+
static F32 rscale;
25+
static RwV3d gCamPos;
2026
static RwRaster* gc_saveraster;
2127
static xShadowMgr* sMgrList;
2228
static S32 sMgrCount;
2329
static S32 sMgrTotal;
30+
static F32 ShadowStrength = 0.3f;
2431

2532
void xShadowInit();
2633
static void ShadowCameraDestroy(RwCamera* shadowCamera);
@@ -32,13 +39,16 @@ void xShadowRenderWorld(xVec3* a, F32 b, F32 c);
3239
void xShadow_ListAdd(xEnt* ent);
3340
void xShadowManager_Add(xEnt* ent);
3441
static void GCSaveFrameBuffer();
35-
RwCamera* ShadowCameraSetSpherePersp(RwCamera* camera, RwV3d* center, float radius);
36-
int Im2DRenderQuad(float x1, float y1, float x2, float y2, float z, float recipCamZ, float uvOffset);
42+
static void GCRestoreFrameBuffer();
43+
RwCamera* ShadowCameraSetSpherePersp(RwCamera* camera, RwV3d* center, F32 radius);
44+
S32 Im2DRenderQuad(F32 x1, F32 y1, F32 x2, F32 y2, F32 z, F32 recipCamZ, F32 uvOffset);
45+
void InvertRaster(RwCamera* cam);
46+
static S32 ShadowRender( RwCamera* shadowCamera, RwRaster* arg1, RpIntersection* shadowZone, F32 shadowFactor, F32 fadeDist);
3747

3848
/* Three more params than in DWARF.
3949
RwCamera* ShadowCameraUpdate(RwCamera* shadowCamera, void* model, void(*renderCB)(void*))
4050
*/
41-
RwCamera* ShadowCameraUpdate(RwCamera* shadowCamera, void* model, void(*renderCB)(void*), RwV3d* center, float radius, int);
51+
RwCamera* ShadowCameraUpdate(RwCamera* shadowCamera, void* model, void(*renderCB)(void*), RwV3d* center, F32 radius, S32 param_6 );
4252

4353
void xShadowInit()
4454
{
@@ -86,13 +96,30 @@ void xShadowSetWorld(RpWorld* world)
8696
SHADOW_BOTH = 2.0f;
8797
}
8898

99+
void xShadowSetLight(xVec3* target_pos, xVec3* in_vec, F32 param3) {
100+
xVec3 zvec;
101+
xMat4x3 matrix;
102+
RwFrame* camFrame;
103+
RwMatrixTag* camMatrix;
104+
105+
xVec3Normalize(&zvec, in_vec);
106+
xMat3x3LookVec(&matrix, &zvec);
107+
zvec += *target_pos;
108+
camFrame = (RwFrame*)ShadowCamera->object.object.parent;
109+
camMatrix = &camFrame->modelling;
110+
xMat4x3Copy( (xMat4x3*) camMatrix,&matrix);
111+
RwFrameOrthoNormalize( camFrame);
112+
RwMatrixUpdate( camMatrix);
113+
RwFrameUpdateObjects( camFrame);
114+
}
115+
89116
U32 xShadowCameraCreate()
90117
{
91118
U32 setup = SetupShadow();
92119
return ((-setup | setup) >> 0x1f);
93120
}
94121

95-
void xShadowCameraUpdate(void* model, void(*renderCB)(void*), xVec3* center, float radius, int shadowMode)
122+
void xShadowCameraUpdate(void* model, void(*renderCB)(void*), xVec3* center, F32 radius, int shadowMode)
96123
{
97124
ShadowCameraSetSpherePersp(ShadowCamera, (RwV3d*)center, radius);
98125
ShadowCameraUpdate(ShadowCamera, model, renderCB, (RwV3d*)center, radius, shadowMode);
@@ -119,11 +146,127 @@ S32 xShadowReceiveShadowSetup(xEnt* ent)
119146
return 0;
120147
}
121148

149+
void xShadowRender(xEnt* ent, F32 max_dist) {
150+
xVec3 center;
151+
F32 radius;
152+
153+
zEntGetShadowParams(ent, &center, &radius, (xEntShadow::radius_enum) 1);
154+
xShadowCameraUpdate(ent->model, modelRenderCB, &center, radius, 0);
155+
xShadowRender(&center, radius, max_dist);
156+
}
157+
122158
void xShadow_ListAdd(xEnt* ent)
123159
{
124160
xShadowManager_Add(ent);
125161
}
126162

163+
S32 Im2DRenderQuad(F32 x1, F32 y1, F32 x2, F32 y2, F32 z, F32 recipCamZ, F32 uvOffset)
164+
{
165+
166+
RwIm2DVertex vx[4];
167+
168+
vx[0].x = x1;
169+
vx[0].y = y1;
170+
vx[0].z = z;
171+
vx[0].emissiveColor.red = 0xFF;
172+
vx[0].emissiveColor.green = 0xFF;
173+
vx[0].emissiveColor.blue = 0xFF;
174+
vx[0].emissiveColor.alpha = 0xFF;
175+
vx[0].u = uvOffset;
176+
vx[0].v = uvOffset;
177+
178+
vx[1].x = x1;
179+
vx[1].y = y2;
180+
vx[1].z = z;
181+
vx[1].emissiveColor.red = 0xFF;
182+
vx[1].emissiveColor.green = 0xFF;
183+
vx[1].emissiveColor.blue = 0xFF;
184+
vx[1].emissiveColor.alpha = 0xFF;
185+
vx[1].u = uvOffset;
186+
vx[1].v = 1.0f + uvOffset;
187+
188+
vx[2].x = x2;
189+
vx[2].y = y1;
190+
vx[2].z = z;
191+
vx[2].emissiveColor.red = 0xFF;
192+
vx[2].emissiveColor.green = 0xFF;
193+
vx[2].emissiveColor.blue = 0xFF;
194+
vx[2].emissiveColor.alpha = 0xFF;
195+
vx[2].u = 1.0f + uvOffset;
196+
vx[2].v = uvOffset;
197+
198+
vx[3].x = x2;
199+
vx[3].y = y2;
200+
vx[3].z = z;
201+
vx[3].emissiveColor.red = 0xFF;
202+
vx[3].emissiveColor.green = 0xFF;
203+
vx[3].emissiveColor.blue = 0xFF;
204+
vx[3].emissiveColor.alpha = 0xFF;
205+
vx[3].u = 1.0f + uvOffset;
206+
vx[3].v = 1.0f + uvOffset;
207+
208+
RwIm2DRenderPrimitive(rwPRIMTYPETRISTRIP, vx,4);
209+
return 1;
210+
}
211+
212+
void InvertRaster(RwCamera* cam)
213+
{
214+
rwGameCube2DVertex vx[4];
215+
216+
F32 cam_width = (F32)cam->frameBuffer->width;
217+
F32 cam_height = (F32)cam->frameBuffer->height;
218+
219+
vx[0].x = 0.0f;
220+
vx[0].y = 0.0f;
221+
vx[0].z = RwIm2DGetNearScreenZ();
222+
vx[0].emissiveColor.red = 0xFF;
223+
vx[0].emissiveColor.green = 0xFF;
224+
vx[0].emissiveColor.blue = 0xFF;
225+
vx[0].emissiveColor.alpha = 0xFF;
226+
vx[0].u = 0.0f;
227+
vx[0].v = 0.0f;
228+
229+
vx[1].x = 0.0f;
230+
vx[1].y = cam_height;
231+
vx[1].z = RwIm2DGetNearScreenZ();
232+
vx[1].emissiveColor.red = 0xFF;
233+
vx[1].emissiveColor.green = 0xFF;
234+
vx[1].emissiveColor.blue = 0xFF;
235+
vx[1].emissiveColor.alpha = 0xFF;
236+
vx[1].u = 0.0f;
237+
vx[1].v = 1.0f;
238+
239+
vx[2].x = 0.0f;
240+
vx[2].y = cam_width;
241+
vx[2].z = RwIm2DGetNearScreenZ();
242+
vx[2].emissiveColor.red = 0xFF;
243+
vx[2].emissiveColor.green = 0xFF;
244+
vx[2].emissiveColor.blue = 0xFF;
245+
vx[2].emissiveColor.alpha = 0xFF;
246+
vx[2].u = 1.0f;
247+
vx[2].v = 0.0f;
248+
249+
vx[3].x = cam_width;
250+
vx[3].y = cam_height;
251+
vx[3].z = RwIm2DGetNearScreenZ();
252+
vx[3].emissiveColor.red = 0xFF;
253+
vx[3].emissiveColor.green = 0xFF;
254+
vx[3].emissiveColor.blue = 0xFF;
255+
vx[3].emissiveColor.alpha = 0xFF;
256+
vx[3].u = 1.0f;
257+
vx[3].v = 0.0f;
258+
259+
RwRenderStateSet(rwRENDERSTATEZTESTENABLE, (void*) 0);
260+
RwRenderStateSet(rwRENDERSTATETEXTURERASTER, (void*) 0);
261+
RwRenderStateSet(rwRENDERSTATEVERTEXALPHAENABLE, (void*) 1);
262+
RwRenderStateSet(rwRENDERSTATEDESTBLEND, (void*) 1);
263+
RwRenderStateSet(rwRENDERSTATESRCBLEND, (void*) 10);
264+
RwIm2DRenderPrimitive(rwPRIMTYPETRISTRIP, &vx[0], 4);
265+
RwRenderStateSet(rwRENDERSTATEZTESTENABLE, (void*) 1);
266+
RwRenderStateSet(rwRENDERSTATEDESTBLEND, (void*) 6);
267+
RwRenderStateSet(rwRENDERSTATESRCBLEND, (void*) 5);
268+
}
269+
127270
void ShadowCameraDestroy(RwCamera* shadowCamera)
128271
{
129272
if (shadowCamera == NULL)
@@ -156,6 +299,49 @@ void ShadowCameraDestroy(RwCamera* shadowCamera)
156299
RwCameraDestroy(shadowCamera);
157300
}
158301

302+
RwCamera* ShadowCameraUpdate(RwCamera* shadowCamera, void* model, void(*renderCB)(void*), xVec3* center, F32 radius, S32 invertRasterFlag)
303+
{
304+
RwRGBA bgColor [4];
305+
RwCamera* camera;
306+
S32 fogstate;
307+
308+
bgColor[0].red = 0.0f;
309+
camera = *(RwCamera **)RwEngineInstance;
310+
RwRenderStateGet(rwRENDERSTATEFOGENABLE, &fogstate);
311+
RwRenderStateSet(rwRENDERSTATEFOGENABLE, 0);
312+
if (camera != (RwCamera *)0x0) {
313+
RwCameraEndUpdate(camera);
314+
}
315+
GCSaveFrameBuffer();
316+
shadowCamera->frameBuffer->width = shadowCamera->frameBuffer->width - 1;
317+
shadowCamera->frameBuffer->height = shadowCamera->frameBuffer->height - 1;
318+
RwCameraClear(shadowCamera, bgColor, 1);
319+
shadowCamera->frameBuffer->width = shadowCamera->frameBuffer->width + 1;
320+
shadowCamera->frameBuffer->height = shadowCamera->frameBuffer->height + 1;
321+
RwFrameOrthoNormalize( (RwFrame*) shadowCamera->object.object.parent);
322+
if (RwCameraBeginUpdate(shadowCamera) != 0) {
323+
iCameraFrustumPlanes( shadowCamera, globals.camera.frustplane );
324+
RwRenderStateSet(rwRENDERSTATETEXTURERASTER, 0);
325+
RwRenderStateSet(rwRENDERSTATEZWRITEENABLE, 0);
326+
RwRenderStateSet(rwRENDERSTATEZTESTENABLE, 0);
327+
RwRenderStateSet(rwRENDERSTATEVERTEXALPHAENABLE, (RwRaster*) 1U);
328+
if (invertRasterFlag == 0) {
329+
InvertRaster(shadowCamera);
330+
}
331+
RwRenderStateSet(rwRENDERSTATEZWRITEENABLE, (RwRaster*) 1U);
332+
RwRenderStateSet(rwRENDERSTATEZTESTENABLE, (RwRaster*) 1U);
333+
RwCameraEndUpdate(shadowCamera);
334+
RwGameCubeCameraTextureFlush(shadowCamera->frameBuffer, 0);
335+
}
336+
if (camera != (RwCamera *)0x0) {
337+
RwCameraBeginUpdate(camera);
338+
iCameraFrustumPlanes(camera, globals.camera.frustplane);
339+
}
340+
GCRestoreFrameBuffer();
341+
RwRenderStateSet(rwRENDERSTATEFOGENABLE, (void*) fogstate);
342+
return shadowCamera;
343+
}
344+
159345
static RwRaster* ShadowRasterCreate(S32 res)
160346
{
161347
return RwRasterCreate(res, res, 0, 5);
@@ -213,6 +399,35 @@ static RwCamera* ShadowCameraCreatePersp(S32 param)
213399
return NULL;
214400
}
215401

402+
RwCamera* ShadowCameraSetSpherePersp(RwCamera* camera, RwV3d* center, F32 radius) {
403+
404+
RwFrame* camFrame;
405+
RwMatrixTag* camMatrix;
406+
RwV3d* camPos;
407+
F32 objDepth;
408+
F32 nearZ;
409+
F32 farZ;
410+
411+
camFrame = (RwFrame*) (camera->object.object.parent);
412+
objDepth = 572.95807f * radius;
413+
camMatrix = &camFrame->modelling;
414+
camPos = &camMatrix->pos;
415+
nearZ = objDepth - rscale * radius;
416+
farZ = objDepth + rscale * radius;
417+
camera->nearPlane = nearZ;
418+
camera->farPlane = farZ;
419+
RwCameraSetNearClipPlane(camera, nearZ);
420+
RwCameraSetFarClipPlane(camera, farZ);
421+
camPos = center;
422+
RwV3dIncrementScaledMacro( camPos, &camMatrix->at, -objDepth );
423+
camPos = &camMatrix->at;
424+
gCamPos = *camPos;
425+
RwMatrixUpdate(camMatrix);
426+
RwFrameUpdateObjects(camFrame);
427+
gShadowObjectRadius = radius;
428+
return camera;
429+
}
430+
216431
void xShadowManager_Init(S32 numEnts)
217432
{
218433
sMgrList = (xShadowMgr*)xMemAlloc(gActiveHeap, numEnts << 4, 0);

src/SB/Core/x/xShadow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct xShadowMgr
3636
void xShadowInit();
3737
void xShadow_ListAdd(xEnt* ent);
3838
void xShadowSetWorld(RpWorld* world);
39-
void xShadowSetLight(xVec3* param1, xVec3* param2, F32 param3);
39+
void xShadowSetLight(xVec3* target_pos, xVec3* in_vec, F32 param3);
4040
void xShadowManager_Init(S32 numEnts);
4141
void xShadowManager_Reset();
4242
void xShadowManager_Render();

0 commit comments

Comments
 (0)