From 79b5e4a7b4532cde399a92442858c5ebc6cbd08d Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Mon, 2 Feb 2026 23:24:12 +0900 Subject: [PATCH 1/5] 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 +} From c99d6785bad8d8089e1f49b20d2826f2260778cc Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Mon, 2 Feb 2026 23:27:42 +0900 Subject: [PATCH 2/5] j --- engine/src/client.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/engine/src/client.c b/engine/src/client.c index 83144fb..959a3d9 100644 --- a/engine/src/client.c +++ b/engine/src/client.c @@ -54,11 +54,6 @@ 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) { From a1e4c9ab2c5142d5616795d17d936bc028848a95 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Tue, 3 Feb 2026 10:13:49 +0900 Subject: [PATCH 3/5] refactor --- client/main.c | 14 ++++++-------- engine/include/GearSrc/GL.h | 5 ++--- engine/include/GearSrc/Model.h | 2 +- engine/include/GearSrc/TypeDefs.h | 12 ++++++++++++ engine/src/core.c | 2 +- engine/src/gl/gl.c | 28 ++++++++++++++-------------- engine/src/gl/gl_camera.c | 2 +- engine/src/gl/gl_common.c | 20 ++++++++++---------- engine/src/gl/gl_shadow.c | 8 +++++--- engine/src/model.c | 18 +++++++++++++++--- 10 files changed, 67 insertions(+), 44 deletions(-) diff --git a/client/main.c b/client/main.c index 3ebaeed..2f717db 100644 --- a/client/main.c +++ b/client/main.c @@ -56,16 +56,14 @@ static void render(GSEngine self){ for(i = 0; i < sizeof(obj) / sizeof(obj[0]); i++){ GSVector3 rv; - GSMatrix3x3 rm; + GSRotation rot; + + rot.use_matrix = GSTrue; GSPhysicsGetPosition(obj[i], rv); - GSPhysicsGetRotation(obj[i], rm); + GSPhysicsGetRotation(obj[i], rot.u.matrix); - GSGLPushMatrix(gl); - GSGLSetPosition(gl, rv); - GSGLSetRotation3x3(gl, rm); - GSModelDraw(mdl); - GSGLPopMatrix(gl); + GSModelDraw(mdl, rv, &rot); } GSGLSetColor(gl, col2); @@ -106,7 +104,7 @@ int main(int argc, char** argv){ GSVector3 pos = {0, 1, 0}; GSVector3 sz = {1, 1, 1}; - mdl = GSModelOpen(engine, "game:/mdl/fish.gsm"); + mdl = GSModelOpen(engine, "game:/mdl/tetopear.gsm"); for(i = 0; i < sizeof(obj) / sizeof(obj[0]); i++){ obj[i] = GSPhysicsCreateSphere(GSServerGetPhysics(GSEngineGetServer(engine)), 1, 0.5); diff --git a/engine/include/GearSrc/GL.h b/engine/include/GearSrc/GL.h index 92a23b1..6c0b59c 100644 --- a/engine/include/GearSrc/GL.h +++ b/engine/include/GearSrc/GL.h @@ -45,7 +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); +GSDECL int GSGLShadowCascadeSteps(GSGL gl); /* gl.c */ GSDECL GSGL GSGLCreate(GSClient client); @@ -59,8 +59,7 @@ GSDECL void GSGLPolygon2D(GSGL gl, int pairs, GSVector2* vert, GSVector2* tex) GSDECL void GSGLBegin2D(GSGL gl); GSDECL void GSGLEnd2D(GSGL gl); GSDECL void GSGLSetPosition(GSGL gl, GSVector3 pos); -GSDECL void GSGLSetRotation(GSGL gl, GSVector3 rot); -GSDECL void GSGLSetRotation3x3(GSGL gl, GSMatrix3x3 rot); +GSDECL void GSGLSetRotation(GSGL gl, GSRotation* rot); GSDECL void GSGLPushMatrix(GSGL gl); GSDECL void GSGLPopMatrix(GSGL gl); GSDECL GLuint GSGLBeginList(GSGL gl); diff --git a/engine/include/GearSrc/Model.h b/engine/include/GearSrc/Model.h index a7f3f3e..b4ea1bc 100644 --- a/engine/include/GearSrc/Model.h +++ b/engine/include/GearSrc/Model.h @@ -9,7 +9,7 @@ extern "C" { #endif GSDECL GSModel GSModelOpen(GSEngine engine, const char* path); -GSDECL void GSModelDraw(GSModel model); +GSDECL void GSModelDraw(GSModel model, GSVector3 pos, GSRotation* rot); GSDECL void GSModelClose(GSModel model); #ifdef __cplusplus diff --git a/engine/include/GearSrc/TypeDefs.h b/engine/include/GearSrc/TypeDefs.h index ab640d4..f60d694 100644 --- a/engine/include/GearSrc/TypeDefs.h +++ b/engine/include/GearSrc/TypeDefs.h @@ -51,6 +51,8 @@ typedef struct _GSNetAddress GSNetAddress; typedef struct _GSBinary GSBinary; typedef struct _GSNetState GSNetState; typedef struct _GSNetKV GSNetKV; +typedef union _GSRotationUnion GSRotationUnion; +typedef struct _GSRotation GSRotation; #ifdef _GEARSRC typedef struct _GSClient* GSClient; typedef struct _GSServer* GSServer; @@ -176,6 +178,16 @@ struct _GSNetKV { GSNetState value; }; +union _GSRotationUnion { + GSVector3 vector; + GSMatrix3x3 matrix; +}; + +struct _GSRotation { + GSBool use_matrix; + GSRotationUnion u; +}; + #ifdef _GEARSRC #include #include diff --git a/engine/src/core.c b/engine/src/core.c index e9e57bd..91a8209 100644 --- a/engine/src/core.c +++ b/engine/src/core.c @@ -16,7 +16,7 @@ #include #ifdef _WIN32 -static void init_winsock(void){ +static void init_winsock(void) { WSADATA wsa; WSAStartup(MAKEWORD(1, 1), &wsa); diff --git a/engine/src/gl/gl.c b/engine/src/gl/gl.c index 7b671cc..589c1ed 100644 --- a/engine/src/gl/gl.c +++ b/engine/src/gl/gl.c @@ -121,21 +121,21 @@ void GSGLSetPosition(GSGL gl, GSVector3 pos) { glTranslatef(pos[0], pos[1], -pos[2]); } -void GSGLSetRotation(GSGL gl, GSVector3 rot) { - glRotatef(rot[0], 1, 0, 0); - glRotatef(rot[1], 0, 1, 0); - glRotatef(rot[2], 0, 0, 1); -} +void GSGLSetRotation(GSGL gl, GSRotation* rot) { + if(rot->use_matrix) { + GSMatrix4x4 out, out2; + GLdouble mat[16]; + int i; -void GSGLSetRotation3x3(GSGL gl, GSMatrix3x3 rot) { - GSMatrix4x4 out, out2; - GLdouble mat[16]; - int i; - - GSMath3x3To4x4(out, rot); - GSMathRowToColumn4x4(out2, out); - for(i = 0; i < 16; i++) mat[i] = out2[i]; - glMultMatrixd(mat); + GSMath3x3To4x4(out, rot->u.matrix); + GSMathRowToColumn4x4(out2, out); + for(i = 0; i < 16; i++) mat[i] = out2[i]; + glMultMatrixd(mat); + } else { + glRotatef(rot->u.vector[0], 1, 0, 0); + glRotatef(rot->u.vector[1], 0, 1, 0); + glRotatef(rot->u.vector[2], 0, 0, 1); + } } void GSGLPushMatrix(GSGL gl) { diff --git a/engine/src/gl/gl_camera.c b/engine/src/gl/gl_camera.c index b8962e0..74123de 100644 --- a/engine/src/gl/gl_camera.c +++ b/engine/src/gl/gl_camera.c @@ -64,7 +64,7 @@ void GSGLCameraLookAt(GSGL gl, GSVector3 camera, GSVector3 look_at) { glMultMatrixd(matrix); glTranslated(-camera[0], -camera[1], camera[2]); - if(gl->shadow_use_shader){ + if(gl->shadow_use_shader) { glUseProgram(gl->shadow_shader); glUniform3f(glGetUniformLocation(gl->shadow_shader, "camera"), camera[0], camera[1], camera[2]); glUseProgram(0); diff --git a/engine/src/gl/gl_common.c b/engine/src/gl/gl_common.c index 162d5b8..4a71628 100644 --- a/engine/src/gl/gl_common.c +++ b/engine/src/gl/gl_common.c @@ -179,11 +179,11 @@ void GSGLTetrakis(GSGL gl, GSNumber scale, GSVector4 col1, GSVector4 col2) { {0, 1, 0}, {-0.75, 0.75, -0.75}, {0.75, 0.75, -0.75}}; - GSVector3 n; - GSVector3 rot2 = {0, 90, 0}; - GSVector3 rot3 = {0, 0, 90}; - GSVector3 rot4 = {90, 0, 0}; - int i, j, k = 0; + GSVector3 n; + GSRotation rot2 = {0, {0, 90, 0}}; + GSRotation rot3 = {0, {0, 0, 90}}; + GSRotation rot4 = {0, {90, 0, 0}}; + int i, j, k = 0; for(i = 0; i < 3; i++) { for(j = 0; j < 3; j++) v[i][j] *= scale; @@ -197,17 +197,17 @@ void GSGLTetrakis(GSGL gl, GSNumber scale, GSVector4 col1, GSVector4 col2) { GSGLSetColor(gl, ((k + j) % 2) ? col2 : col1); GSGLPolygon(gl, 3, v, NULL, n); - GSGLSetRotation(gl, rot2); + GSGLSetRotation(gl, &rot2); } if(i == 3) { - GSGLSetRotation(gl, rot4); + GSGLSetRotation(gl, &rot4); k++; } else if(i == 4) { - GSGLSetRotation(gl, rot4); - GSGLSetRotation(gl, rot4); + GSGLSetRotation(gl, &rot4); + GSGLSetRotation(gl, &rot4); } else { - GSGLSetRotation(gl, rot3); + GSGLSetRotation(gl, &rot3); k++; } } diff --git a/engine/src/gl/gl_shadow.c b/engine/src/gl/gl_shadow.c index 6171532..e4549b9 100644 --- a/engine/src/gl/gl_shadow.c +++ b/engine/src/gl/gl_shadow.c @@ -74,7 +74,7 @@ GSBool GSGLShadowBegin(GSGL gl) { return GSTrue; } -int GSGLShadowCascadeSteps(GSGL gl){ +int GSGLShadowCascadeSteps(GSGL gl) { if(gl->engine->client->light0[3]) return 1; return GSGL_SHADOW_CASCADES; @@ -92,8 +92,10 @@ void GSGLShadowCascade(GSGL gl, int cascade) { // Simple cascade sizing if(cascade == 0) size = 10; - else if(cascade == 1) size = 40; - else size = 100; // Covers max distance roughly + else if(cascade == 1) + size = 40; + else + size = 100; // Covers max distance roughly // If light is directional (light0[3] == 0) // Current existing code logic: diff --git a/engine/src/model.c b/engine/src/model.c index 127429a..4a4b102 100644 --- a/engine/src/model.c +++ b/engine/src/model.c @@ -274,8 +274,16 @@ GSModel GSModelOpen(GSEngine engine, const char* path) { /* TODO: check for animation */ if(gl != NULL) { - GLuint list = GSGLBeginList(gl); - GSModelDraw(model); + GLuint list = GSGLBeginList(gl); + GSVector3 pos = {0, 0, 0}; + GSRotation rot; + + rot.use_matrix = GSFalse; + rot.u.vector[0] = 0; + rot.u.vector[1] = 0; + rot.u.vector[2] = 0; + + GSModelDraw(model, pos, &rot); GSGLEndList(gl); model->call_list = list; @@ -284,10 +292,13 @@ GSModel GSModelOpen(GSEngine engine, const char* path) { return model; } -void GSModelDraw(GSModel model) { +void GSModelDraw(GSModel model, GSVector3 pos, GSRotation* rot) { GSGL gl = model->engine->client->gl; int i; + GSGLPushMatrix(gl); + GSGLSetPosition(gl, pos); + GSGLSetRotation(gl, rot); if(model->call_list == 0) { char* k = "default"; @@ -307,6 +318,7 @@ void GSModelDraw(GSModel model) { } else { GSGLCallList(gl, model->call_list); } + GSGLPopMatrix(gl); } void GSModelClose(GSModel model) { From ee178645bf3ecdd4644c13dd4c17f4a850cbe5e6 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Tue, 3 Feb 2026 10:41:50 +0900 Subject: [PATCH 4/5] whatever --- engine/include/GearSrc/GL.h | 10 ++-- engine/include/GearSrc/TypeDefs.h | 7 +++ engine/src/gl/gl_camera.c | 89 +++++++++++++++++++++++++++++++ engine/src/model.c | 45 +++++++++++++--- 4 files changed, 139 insertions(+), 12 deletions(-) diff --git a/engine/include/GearSrc/GL.h b/engine/include/GearSrc/GL.h index 6c0b59c..360eab4 100644 --- a/engine/include/GearSrc/GL.h +++ b/engine/include/GearSrc/GL.h @@ -70,10 +70,12 @@ GSDECL void GSGLClearDepth(GSGL gl); GSDECL void GSGLInitialTranslation(GSGL gl); /* gl_camera.c */ -GSDECL void GSGLCameraPerspective(GSGL gl, GSNumber width, GSNumber height); -GSDECL void GSGLCameraSet(GSGL gl); -GSDECL void GSGLCameraLookAt(GSGL gl, GSVector3 camera, GSVector3 look_at); -GSDECL void GSGLCameraSetup(GSGL gl); +GSDECL void GSGLCameraPerspective(GSGL gl, GSNumber width, GSNumber height); +GSDECL void GSGLCameraSet(GSGL gl); +GSDECL void GSGLCameraLookAt(GSGL gl, GSVector3 camera, GSVector3 look_at); +GSDECL void GSGLCameraSetup(GSGL gl); +GSDECL void GSGLCameraFrustum(GSGL gl); +GSDECL GSBool GSGLCameraFrustumContain(GSGL gl, GSVector3 a, GSVector3 b); #ifdef __cplusplus } diff --git a/engine/include/GearSrc/TypeDefs.h b/engine/include/GearSrc/TypeDefs.h index f60d694..1a29497 100644 --- a/engine/include/GearSrc/TypeDefs.h +++ b/engine/include/GearSrc/TypeDefs.h @@ -206,6 +206,11 @@ struct _GSGL { GLdouble shadow_old_projection[16]; GLdouble shadow_old_modelview[16]; + GLdouble frustum_projection[16]; + GLdouble frustum_modelview[16]; + GSNumber frustum_planes[6][16]; + GSNumber frustum_clip[16]; + GSBool bold; }; @@ -253,6 +258,8 @@ struct _GSModel { float* vertex; float* texcoord; GSModelFace* face; + + GSVector3 bbox[2]; }; struct _GSSound { diff --git a/engine/src/gl/gl_camera.c b/engine/src/gl/gl_camera.c index 74123de..d96a48c 100644 --- a/engine/src/gl/gl_camera.c +++ b/engine/src/gl/gl_camera.c @@ -79,4 +79,93 @@ void GSGLCameraSetup(GSGL gl) { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); GSGLCameraSet(gl); + + GSGLCameraFrustum(gl); +} + +static void frustum_normalize(GSGL gl, int plane) { + GSVector3 v; + GSNumber l; + int i; + + v[0] = gl->frustum_planes[plane][0]; + v[1] = gl->frustum_planes[plane][1]; + v[2] = gl->frustum_planes[plane][2]; + + l = GSMathLength3(v); + + for(i = 0; i < 4; i++) { + gl->frustum_planes[plane][i] /= l; + } +} + +void GSGLCameraFrustum(GSGL gl) { + glGetDoublev(GL_PROJECTION_MATRIX, gl->frustum_projection); + glGetDoublev(GL_MODELVIEW_MATRIX, gl->frustum_modelview); + + gl->frustum_clip[0x0] = gl->frustum_modelview[0x0] * gl->frustum_projection[0x0] + gl->frustum_modelview[0x1] * gl->frustum_projection[0x4] + gl->frustum_modelview[0x2] * gl->frustum_projection[0x8] + gl->frustum_modelview[0x3] * gl->frustum_projection[0xc]; + gl->frustum_clip[0x1] = gl->frustum_modelview[0x0] * gl->frustum_projection[0x1] + gl->frustum_modelview[0x1] * gl->frustum_projection[0x5] + gl->frustum_modelview[0x2] * gl->frustum_projection[0x9] + gl->frustum_modelview[0x3] * gl->frustum_projection[0xd]; + gl->frustum_clip[0x2] = gl->frustum_modelview[0x0] * gl->frustum_projection[0x2] + gl->frustum_modelview[0x1] * gl->frustum_projection[0x6] + gl->frustum_modelview[0x2] * gl->frustum_projection[0xa] + gl->frustum_modelview[0x3] * gl->frustum_projection[0xe]; + gl->frustum_clip[0x3] = gl->frustum_modelview[0x0] * gl->frustum_projection[0x3] + gl->frustum_modelview[0x1] * gl->frustum_projection[0x7] + gl->frustum_modelview[0x2] * gl->frustum_projection[0xb] + gl->frustum_modelview[0x3] * gl->frustum_projection[0xf]; + gl->frustum_clip[0x4] = gl->frustum_modelview[0x4] * gl->frustum_projection[0x0] + gl->frustum_modelview[0x5] * gl->frustum_projection[0x4] + gl->frustum_modelview[0x6] * gl->frustum_projection[0x8] + gl->frustum_modelview[0x7] * gl->frustum_projection[0xc]; + gl->frustum_clip[0x5] = gl->frustum_modelview[0x4] * gl->frustum_projection[0x1] + gl->frustum_modelview[0x5] * gl->frustum_projection[0x5] + gl->frustum_modelview[0x6] * gl->frustum_projection[0x9] + gl->frustum_modelview[0x7] * gl->frustum_projection[0xd]; + gl->frustum_clip[0x6] = gl->frustum_modelview[0x4] * gl->frustum_projection[0x2] + gl->frustum_modelview[0x5] * gl->frustum_projection[0x6] + gl->frustum_modelview[0x6] * gl->frustum_projection[0xa] + gl->frustum_modelview[0x7] * gl->frustum_projection[0xe]; + gl->frustum_clip[0x7] = gl->frustum_modelview[0x4] * gl->frustum_projection[0x3] + gl->frustum_modelview[0x5] * gl->frustum_projection[0x7] + gl->frustum_modelview[0x6] * gl->frustum_projection[0xb] + gl->frustum_modelview[0x7] * gl->frustum_projection[0xf]; + gl->frustum_clip[0x8] = gl->frustum_modelview[0x8] * gl->frustum_projection[0x0] + gl->frustum_modelview[0x9] * gl->frustum_projection[0x4] + gl->frustum_modelview[0xa] * gl->frustum_projection[0x8] + gl->frustum_modelview[0xb] * gl->frustum_projection[0xc]; + gl->frustum_clip[0x9] = gl->frustum_modelview[0x8] * gl->frustum_projection[0x1] + gl->frustum_modelview[0x9] * gl->frustum_projection[0x5] + gl->frustum_modelview[0xa] * gl->frustum_projection[0x9] + gl->frustum_modelview[0xb] * gl->frustum_projection[0xd]; + gl->frustum_clip[0xa] = gl->frustum_modelview[0x8] * gl->frustum_projection[0x2] + gl->frustum_modelview[0x9] * gl->frustum_projection[0x6] + gl->frustum_modelview[0xa] * gl->frustum_projection[0xa] + gl->frustum_modelview[0xb] * gl->frustum_projection[0xe]; + gl->frustum_clip[0xb] = gl->frustum_modelview[0x8] * gl->frustum_projection[0x3] + gl->frustum_modelview[0x9] * gl->frustum_projection[0x7] + gl->frustum_modelview[0xa] * gl->frustum_projection[0xb] + gl->frustum_modelview[0xb] * gl->frustum_projection[0xf]; + gl->frustum_clip[0xc] = gl->frustum_modelview[0xc] * gl->frustum_projection[0x0] + gl->frustum_modelview[0xd] * gl->frustum_projection[0x4] + gl->frustum_modelview[0xe] * gl->frustum_projection[0x8] + gl->frustum_modelview[0xf] * gl->frustum_projection[0xc]; + gl->frustum_clip[0xd] = gl->frustum_modelview[0xc] * gl->frustum_projection[0x1] + gl->frustum_modelview[0xd] * gl->frustum_projection[0x5] + gl->frustum_modelview[0xe] * gl->frustum_projection[0x9] + gl->frustum_modelview[0xf] * gl->frustum_projection[0xd]; + gl->frustum_clip[0xe] = gl->frustum_modelview[0xc] * gl->frustum_projection[0x2] + gl->frustum_modelview[0xd] * gl->frustum_projection[0x6] + gl->frustum_modelview[0xe] * gl->frustum_projection[0xa] + gl->frustum_modelview[0xf] * gl->frustum_projection[0xe]; + gl->frustum_clip[0xf] = gl->frustum_modelview[0xc] * gl->frustum_projection[0x3] + gl->frustum_modelview[0xd] * gl->frustum_projection[0x7] + gl->frustum_modelview[0xe] * gl->frustum_projection[0xb] + gl->frustum_modelview[0xf] * gl->frustum_projection[0xf]; + gl->frustum_planes[0][0] = gl->frustum_clip[0x3] - gl->frustum_clip[0x0]; + gl->frustum_planes[0][1] = gl->frustum_clip[0x7] - gl->frustum_clip[0x4]; + gl->frustum_planes[0][2] = gl->frustum_clip[0xb] - gl->frustum_clip[0x8]; + gl->frustum_planes[0][3] = gl->frustum_clip[0xf] - gl->frustum_clip[0xc]; + frustum_normalize(gl, 0); + gl->frustum_planes[1][0] = gl->frustum_clip[0x3] + gl->frustum_clip[0x0]; + gl->frustum_planes[1][1] = gl->frustum_clip[0x7] + gl->frustum_clip[0x4]; + gl->frustum_planes[1][2] = gl->frustum_clip[0xb] + gl->frustum_clip[0x8]; + gl->frustum_planes[1][3] = gl->frustum_clip[0xf] + gl->frustum_clip[0xc]; + frustum_normalize(gl, 1); + gl->frustum_planes[2][0] = gl->frustum_clip[0x3] - gl->frustum_clip[0x1]; + gl->frustum_planes[2][1] = gl->frustum_clip[0x7] - gl->frustum_clip[0x5]; + gl->frustum_planes[2][2] = gl->frustum_clip[0xb] - gl->frustum_clip[0x9]; + gl->frustum_planes[2][3] = gl->frustum_clip[0xf] - gl->frustum_clip[0xd]; + frustum_normalize(gl, 2); + gl->frustum_planes[3][0] = gl->frustum_clip[0x3] + gl->frustum_clip[0x1]; + gl->frustum_planes[3][1] = gl->frustum_clip[0x7] + gl->frustum_clip[0x5]; + gl->frustum_planes[3][2] = gl->frustum_clip[0xb] + gl->frustum_clip[0x9]; + gl->frustum_planes[3][3] = gl->frustum_clip[0xf] + gl->frustum_clip[0xd]; + frustum_normalize(gl, 3); + gl->frustum_planes[4][0] = gl->frustum_clip[0x3] - gl->frustum_clip[0x2]; + gl->frustum_planes[4][1] = gl->frustum_clip[0x7] - gl->frustum_clip[0x6]; + gl->frustum_planes[4][2] = gl->frustum_clip[0xb] - gl->frustum_clip[0xa]; + gl->frustum_planes[4][3] = gl->frustum_clip[0xf] - gl->frustum_clip[0xe]; + frustum_normalize(gl, 4); + gl->frustum_planes[5][0] = gl->frustum_clip[0x3] + gl->frustum_clip[0x2]; + gl->frustum_planes[5][1] = gl->frustum_clip[0x7] + gl->frustum_clip[0x6]; + gl->frustum_planes[5][2] = gl->frustum_clip[0xb] + gl->frustum_clip[0xa]; + gl->frustum_planes[5][3] = gl->frustum_clip[0xf] + gl->frustum_clip[0xe]; + frustum_normalize(gl, 5); +} + +GSBool GSGLCameraFrustumContain(GSGL gl, GSVector3 a, GSVector3 b) { + int i; + for(i = 0; i < 6; i++) { + GSBool bv = GSTrue; + + bv = bv && gl->frustum_planes[i][0] * a[0] + gl->frustum_planes[i][1] * a[1] + gl->frustum_planes[i][2] * a[2] + gl->frustum_planes[i][3] <= 0.0; + bv = bv && gl->frustum_planes[i][0] * b[0] + gl->frustum_planes[i][1] * a[1] + gl->frustum_planes[i][2] * a[2] + gl->frustum_planes[i][3] <= 0.0; + bv = bv && gl->frustum_planes[i][0] * a[0] + gl->frustum_planes[i][1] * b[1] + gl->frustum_planes[i][2] * a[2] + gl->frustum_planes[i][3] <= 0.0; + bv = bv && gl->frustum_planes[i][0] * b[0] + gl->frustum_planes[i][1] * b[1] + gl->frustum_planes[i][2] * a[2] + gl->frustum_planes[i][3] <= 0.0; + bv = bv && gl->frustum_planes[i][0] * a[0] + gl->frustum_planes[i][1] * a[1] + gl->frustum_planes[i][2] * b[2] + gl->frustum_planes[i][3] <= 0.0; + bv = bv && gl->frustum_planes[i][0] * b[0] + gl->frustum_planes[i][1] * a[1] + gl->frustum_planes[i][2] * b[2] + gl->frustum_planes[i][3] <= 0.0; + bv = bv && gl->frustum_planes[i][0] * a[0] + gl->frustum_planes[i][1] * b[1] + gl->frustum_planes[i][2] * b[2] + gl->frustum_planes[i][3] <= 0.0; + bv = bv && gl->frustum_planes[i][0] * b[0] + gl->frustum_planes[i][1] * b[1] + gl->frustum_planes[i][2] * b[2] + gl->frustum_planes[i][3] <= 0.0; + if(bv) return GSFalse; + } + + return GSTrue; } diff --git a/engine/src/model.c b/engine/src/model.c index 4a4b102..4f2e787 100644 --- a/engine/src/model.c +++ b/engine/src/model.c @@ -10,13 +10,17 @@ #include static void parse_obj(GSModel model, char* txt) { - char* f = txt; - float lm = 0; /* leftmost */ - float rm = 0; /* rightmost */ - float um = 0; /* upmost */ - float dm = 0; /* downmost */ - float fm = 0; /* frontmost */ - float bm = 0; /* backmost */ + char* f = txt; + float lm = 0; /* leftmost */ + float rm = 0; /* rightmost */ + float um = 0; /* upmost */ + float dm = 0; /* downmost */ + float fm = 0; /* frontmost */ + float bm = 0; /* backmost */ + float l = 0; + float blr = fabs(lm - rm); + float bud = fabs(um - dm); + float bbf = fabs(fm - bm); while(1) { char* s = strchr(f, '\n'); @@ -158,6 +162,22 @@ static void parse_obj(GSModel model, char* txt) { if(s == NULL) break; f = s + 1; } + + blr = fabs(lm - rm); + bud = fabs(um - dm); + bbf = fabs(fm - bm); + + if(l < blr) l = blr; + if(l < bud) l = bud; + if(l < bbf) l = bbf; + + model->bbox[0][0] = l; + model->bbox[0][1] = l; + model->bbox[0][2] = l; + + model->bbox[1][0] = -model->bbox[0][0]; + model->bbox[1][1] = -model->bbox[0][1]; + model->bbox[1][2] = -model->bbox[0][2]; } static void parse_mtl(GSModel model, char* txt) { @@ -316,7 +336,16 @@ void GSModelDraw(GSModel model, GSVector3 pos, GSRotation* rot) { GSGLTextureSet(gl, 0); } else { - GSGLCallList(gl, model->call_list); + GSVector3 a, b; + + for(i = 0; i < 3; i++) { + a[i] = b[i] = pos[i]; + + a[i] += model->bbox[0][i]; + b[i] += model->bbox[1][i]; + } + + if(GSGLCameraFrustumContain(gl, a, b)) GSGLCallList(gl, model->call_list); } GSGLPopMatrix(gl); } From 7ae7b7507a98081114748feef5374e1fcae750ee Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Wed, 8 Apr 2026 17:09:25 +0900 Subject: [PATCH 5/5] several fixes after ages --- client/main.c | 2 +- engine/data/shader/shadow.fs | 7 ++++++- engine/include/GearSrc/GL.h | 1 + engine/include/GearSrc/TypeDefs.h | 1 + engine/src/client.c | 6 ++++++ engine/src/gl/gl_shadow.c | 15 +++++++++++++++ engine/src/model.c | 6 +++--- 7 files changed, 33 insertions(+), 5 deletions(-) diff --git a/client/main.c b/client/main.c index 2f717db..2bc1d4e 100644 --- a/client/main.c +++ b/client/main.c @@ -104,7 +104,7 @@ int main(int argc, char** argv){ GSVector3 pos = {0, 1, 0}; GSVector3 sz = {1, 1, 1}; - mdl = GSModelOpen(engine, "game:/mdl/tetopear.gsm"); + mdl = GSModelOpen(engine, "game:/mdl/crate.gsm"); for(i = 0; i < sizeof(obj) / sizeof(obj[0]); i++){ obj[i] = GSPhysicsCreateSphere(GSServerGetPhysics(GSEngineGetServer(engine)), 1, 0.5); diff --git a/engine/data/shader/shadow.fs b/engine/data/shader/shadow.fs index 2acc948..5d1894c 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 float enable_shadow; uniform vec3 camera; vec4 PhongShading(void) @@ -83,7 +84,11 @@ float ShadowCoef(void){ void main(void){ vec4 c = PhongShading(); vec4 ambient = gl_LightSource[0].ambient; - float shadow_coef = ShadowCoef(); + float shadow_coef = 1.0; + + if(enable_shadow >= 0.5){ + shadow_coef = ShadowCoef(); + } if(enable_texture >= 0.5){ c = c * texture2D(current_texture, gl_TexCoord[0].st); diff --git a/engine/include/GearSrc/GL.h b/engine/include/GearSrc/GL.h index 360eab4..7c9cb4c 100644 --- a/engine/include/GearSrc/GL.h +++ b/engine/include/GearSrc/GL.h @@ -46,6 +46,7 @@ GSDECL void GSGLShadowDeinit(GSGL gl); GSDECL void GSGLShadowDisable(GSGL gl); GSDECL void GSGLShadowEnable(GSGL gl); GSDECL int GSGLShadowCascadeSteps(GSGL gl); +GSDECL void GSGLShadowRender(GSGL gl, int toggle); /* gl.c */ GSDECL GSGL GSGLCreate(GSClient client); diff --git a/engine/include/GearSrc/TypeDefs.h b/engine/include/GearSrc/TypeDefs.h index 1a29497..090f842 100644 --- a/engine/include/GearSrc/TypeDefs.h +++ b/engine/include/GearSrc/TypeDefs.h @@ -212,6 +212,7 @@ struct _GSGL { GSNumber frustum_clip[16]; GSBool bold; + GSBool shadow; }; struct _GSSkyBox { diff --git a/engine/src/client.c b/engine/src/client.c index 959a3d9..8d8088f 100644 --- a/engine/src/client.c +++ b/engine/src/client.c @@ -132,6 +132,7 @@ void GSClientStep(GSClient client) { GSGLTextBold(client->gl, 0); +#ifdef SHADOW if(GSGLShadowBegin(client->gl)) { int c; for(c = 0; c < GSGLShadowCascadeSteps(client->gl); c++) { @@ -141,12 +142,17 @@ void GSClientStep(GSClient client) { } GSGLShadowApply(client->gl); } +#else + GSGLShadowRender(client->gl, 0); +#endif if(client->skybox_enabled) { if(client->skybox != NULL) GSSkyBoxDraw(client->skybox); } scene(client); +#ifdef SHADOW GSGLShadowEnd(client->gl); +#endif GSVersionGet(&ver); diff --git a/engine/src/gl/gl_shadow.c b/engine/src/gl/gl_shadow.c index e4549b9..e11076d 100644 --- a/engine/src/gl/gl_shadow.c +++ b/engine/src/gl/gl_shadow.c @@ -40,6 +40,7 @@ void GSGLShadowInit(GSGL gl) { glUniform1i(glGetUniformLocation(gl->shadow_shader, "current_texture"), 0); glUniform1f(glGetUniformLocation(gl->shadow_shader, "enable_lighting"), 1); glUniform1f(glGetUniformLocation(gl->shadow_shader, "enable_texture"), 0); + glUniform1f(glGetUniformLocation(gl->shadow_shader, "enable_shadow"), 1); glUseProgram(0); gl->shadow_use_shader = 1; @@ -48,6 +49,8 @@ void GSGLShadowInit(GSGL gl) { gl->shadow_use_shader = 0; } + + gl->shadow = GSFalse; } GSBool GSGLShadowBegin(GSGL gl) { @@ -207,6 +210,8 @@ void GSGLShadowDisable(GSGL gl) { } glActiveTexture(GL_TEXTURE0); } + + gl->shadow = GSFalse; } void GSGLShadowEnable(GSGL gl) { @@ -222,6 +227,16 @@ void GSGLShadowEnable(GSGL gl) { } glActiveTexture(GL_TEXTURE0); } + + gl->shadow = GSTrue; +} + +void GSGLShadowRender(GSGL gl, int toggle){ + if(gl->shadow_use_shader){ + if(!gl->shadow) glUseProgram(gl->shadow_shader); + glUniform1f(glGetUniformLocation(gl->shadow_shader, "enable_shadow"), toggle); + if(!gl->shadow) glUseProgram(0); + } } void GSGLShadowEnd(GSGL gl) { diff --git a/engine/src/model.c b/engine/src/model.c index 4f2e787..ed7f2d6 100644 --- a/engine/src/model.c +++ b/engine/src/model.c @@ -171,9 +171,9 @@ static void parse_obj(GSModel model, char* txt) { if(l < bud) l = bud; if(l < bbf) l = bbf; - model->bbox[0][0] = l; - model->bbox[0][1] = l; - model->bbox[0][2] = l; + model->bbox[0][0] = -l * 2; + model->bbox[0][1] = -l * 2; + model->bbox[0][2] = -l * 2; model->bbox[1][0] = -model->bbox[0][0]; model->bbox[1][1] = -model->bbox[0][1];