From 79b5e4a7b4532cde399a92442858c5ebc6cbd08d Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Mon, 2 Feb 2026 23:24:12 +0900 Subject: [PATCH] functional cascade shadow --- client/main.c | 15 ++++++++++----- data/mdl/fish.gsm | Bin 931880 -> 931880 bytes engine/data/shader/shadow.fs | 9 +++++++-- engine/include/GearSrc/GL.h | 1 + engine/src/client.c | 7 ++++++- engine/src/gl/gl_camera.c | 6 ++++++ engine/src/gl/gl_shadow.c | 11 +++++++++-- 7 files changed, 39 insertions(+), 10 deletions(-) diff --git a/client/main.c b/client/main.c index c8b9977..3ebaeed 100644 --- a/client/main.c +++ b/client/main.c @@ -39,6 +39,7 @@ static void tick(void){ } GSPhysicsObject obj[128]; +GSModel mdl; static void render(GSEngine self){ GSGL gl = GSClientGetGL(GSEngineGetClient(self)); @@ -46,10 +47,10 @@ static void render(GSEngine self){ GSVector4 col1 = {1, 0, 0, 1}; GSVector4 col2 = {1, 1, 1, 1}; GSVector3 v[] = { - {-20, 0, 20}, - {-20, 0, -20}, - {20, 0, -20}, - {20, 0, 20} + {-40, 0, 40}, + {-40, 0, -40}, + {40, 0, -40}, + {40, 0, 40} }; GSVector3 n = {0, 1, 0}; @@ -63,7 +64,7 @@ static void render(GSEngine self){ GSGLPushMatrix(gl); GSGLSetPosition(gl, rv); GSGLSetRotation3x3(gl, rm); - GSGLTetrakis(gl, 0.5, col1, col2); + GSModelDraw(mdl); GSGLPopMatrix(gl); } @@ -104,6 +105,9 @@ int main(int argc, char** argv){ int i; GSVector3 pos = {0, 1, 0}; GSVector3 sz = {1, 1, 1}; + + mdl = GSModelOpen(engine, "game:/mdl/fish.gsm"); + for(i = 0; i < sizeof(obj) / sizeof(obj[0]); i++){ obj[i] = GSPhysicsCreateSphere(GSServerGetPhysics(GSEngineGetServer(engine)), 1, 0.5); @@ -116,6 +120,7 @@ int main(int argc, char** argv){ GSClientSetSkybox(GSEngineGetClient(engine), GSTrue); GSEngineLoop(engine); + GSModelClose(mdl); GSEngineDestroy(engine); } } diff --git a/data/mdl/fish.gsm b/data/mdl/fish.gsm index b080492784a1cc8afa81eba52abe84dfd4762dce..028436eb18ce9cb253fdd961400d98c51958c06d 100644 GIT binary patch delta 106 zcmZ4S!D7V+3xWCoM{h<3Fkl9fffKc*7#SuCNHPg?O?30vY|407Xky~>W+9PwArVF( nW&&bnAZ7t#Rv=~rVs;?r0Afxc<^p1FAm#yL-t9snd}1m9G$a<2 delta 106 zcmZ4S!D7V+3xWCoM{h<3Fkl9fffKbQnHU%*3P>?7pXlZ>d4_P+#zZdRW+9PwArVF( nW&&bnAZ7t#Rv=~rVs;?r0Afxc<^p1FAm#yL-t9snd}1m9I;j?u diff --git a/engine/data/shader/shadow.fs b/engine/data/shader/shadow.fs index 33368c1..2acc948 100644 --- a/engine/data/shader/shadow.fs +++ b/engine/data/shader/shadow.fs @@ -13,6 +13,7 @@ uniform sampler2D shadow_map2; uniform sampler2D current_texture; uniform float enable_lighting; uniform float enable_texture; +uniform vec3 camera; vec4 PhongShading(void) { @@ -68,7 +69,11 @@ float SampleShadow(sampler2D shadowMap, vec4 shadowCoord) { } float ShadowCoef(void){ - float dist = -vPos.z; + float dist = length(normalize(camera + gl_LightSource[0].position.xyz) - vPos.xyz); + + if(gl_LightSource[0].position.w > 0.5){ + if(dist >= 10.0) return 1.0; + } if(dist < 10.0) return SampleShadow(shadow_map0, vShadowCoord0); if(dist < 40.0) return SampleShadow(shadow_map1, vShadowCoord1); @@ -86,4 +91,4 @@ void main(void){ gl_FragColor = ambient * shadow_coef * c + (1.0 - ambient) * c; gl_FragColor.a = vColor.a; -} \ No newline at end of file +} diff --git a/engine/include/GearSrc/GL.h b/engine/include/GearSrc/GL.h index 162589a..92a23b1 100644 --- a/engine/include/GearSrc/GL.h +++ b/engine/include/GearSrc/GL.h @@ -45,6 +45,7 @@ GSDECL void GSGLShadowEnd(GSGL gl); GSDECL void GSGLShadowDeinit(GSGL gl); GSDECL void GSGLShadowDisable(GSGL gl); GSDECL void GSGLShadowEnable(GSGL gl); +GSDECL int GSGLShadowCascadeSteps(GSGL gl); /* gl.c */ GSDECL GSGL GSGLCreate(GSClient client); diff --git a/engine/src/client.c b/engine/src/client.c index a12a6c3..83144fb 100644 --- a/engine/src/client.c +++ b/engine/src/client.c @@ -54,6 +54,11 @@ GSClient GSClientCreate(GSEngine engine) { client->light0[2] = 0; client->light0[3] = 0; + client->light0[0] = 5; + client->light0[1] = 5; + client->light0[2] = 0; + client->light0[3] = 1; + client->skybox_enabled = GSFalse; if((client->sengine = GSSoundEngineCreate(client)) == NULL) { @@ -134,7 +139,7 @@ void GSClientStep(GSClient client) { if(GSGLShadowBegin(client->gl)) { int c; - for(c = 0; c < GSGL_SHADOW_CASCADES; c++) { + for(c = 0; c < GSGLShadowCascadeSteps(client->gl); c++) { GSGLShadowCascade(client->gl, c); scene(client); GSGLShadowCopy(client->gl, c); diff --git a/engine/src/gl/gl_camera.c b/engine/src/gl/gl_camera.c index 26a9124..b8962e0 100644 --- a/engine/src/gl/gl_camera.c +++ b/engine/src/gl/gl_camera.c @@ -63,6 +63,12 @@ void GSGLCameraLookAt(GSGL gl, GSVector3 camera, GSVector3 look_at) { glMultMatrixd(matrix); glTranslated(-camera[0], -camera[1], camera[2]); + + if(gl->shadow_use_shader){ + glUseProgram(gl->shadow_shader); + glUniform3f(glGetUniformLocation(gl->shadow_shader, "camera"), camera[0], camera[1], camera[2]); + glUseProgram(0); + } } void GSGLCameraSetup(GSGL gl) { diff --git a/engine/src/gl/gl_shadow.c b/engine/src/gl/gl_shadow.c index 9e34ccd..6171532 100644 --- a/engine/src/gl/gl_shadow.c +++ b/engine/src/gl/gl_shadow.c @@ -74,6 +74,12 @@ GSBool GSGLShadowBegin(GSGL gl) { return GSTrue; } +int GSGLShadowCascadeSteps(GSGL gl){ + if(gl->engine->client->light0[3]) return 1; + + return GSGL_SHADOW_CASCADES; +} + void GSGLShadowCascade(GSGL gl, int cascade) { GSVector3 zero = {0, 0, 0}; GSNumber size; @@ -217,12 +223,13 @@ void GSGLShadowEnable(GSGL gl) { } void GSGLShadowEnd(GSGL gl) { + int i; if(!gl->shadow_use_shader) return; GSGLShadowDisable(gl); // Reset texture matrices - for(int i = 0; i < GSGL_SHADOW_CASCADES; i++) { + for(i = 0; i < GSGL_SHADOW_CASCADES; i++) { glActiveTexture(GL_TEXTURE5 + i); glMatrixMode(GL_TEXTURE); glLoadIdentity(); @@ -236,4 +243,4 @@ void GSGLShadowDeinit(GSGL gl) { glDeleteTextures(GSGL_SHADOW_CASCADES, gl->shadow_texture); glDeleteProgram(gl->shadow_shader); } -} \ No newline at end of file +}