From 08ea9a0e7b704f73d0cc131c252cda45c7305075 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Sun, 11 Jan 2026 18:27:41 +0900 Subject: [PATCH] aa --- engine/include/GearBox/GL.h | 3 ++- engine/include/GearBox/TypeDefs.h | 1 + engine/resource/shader/shadow.fs | 8 +++++--- engine/resource/shader/shadow.vs | 2 +- engine/src/client.c | 17 +++++++++-------- engine/src/gl.c | 1 + engine/src/gl_shadow.c | 22 +++++++++++++++++++++- 7 files changed, 40 insertions(+), 14 deletions(-) diff --git a/engine/include/GearBox/GL.h b/engine/include/GearBox/GL.h index c5a5ce9..db3fcd1 100644 --- a/engine/include/GearBox/GL.h +++ b/engine/include/GearBox/GL.h @@ -18,9 +18,10 @@ GBDECL int GBGLTexturePrepare(GBGL gl, GLuint* texture, unsigned char* rgba, int /* gl_shadow.c */ GBDECL void GBGLShadowInit(GBGL gl); -GBDECL void GBGLShadowBeforeMapping(GBGL gl); +GBDECL int GBGLShadowBeforeMapping(GBGL gl); GBDECL void GBGLShadowAfterMapping(GBGL gl); GBDECL void GBGLShadowEnd(GBGL gl); +GBDECL void GBGLShadowDeinit(GBGL gl); /* gl.c */ GBDECL GBGL GBGLCreate(GBClient client); diff --git a/engine/include/GearBox/TypeDefs.h b/engine/include/GearBox/TypeDefs.h index 7f8d210..1351ba3 100644 --- a/engine/include/GearBox/TypeDefs.h +++ b/engine/include/GearBox/TypeDefs.h @@ -76,6 +76,7 @@ struct _GBResourceKV { struct _GBGL { GBEngine engine; + int shadow_use_shader; GLuint shadow_texture; GLuint shadow_shader; GLdouble shadow_modelview[16]; diff --git a/engine/resource/shader/shadow.fs b/engine/resource/shader/shadow.fs index 6d3a3d8..02e94b0 100644 --- a/engine/resource/shader/shadow.fs +++ b/engine/resource/shader/shadow.fs @@ -15,7 +15,7 @@ vec4 PhongShading(void) vec4 ambient = gl_FrontLightProduct[0].ambient; float dcoef = max(dot(L, N), 0.0); - vec4 diffuse = gl_FrontLightProduct[0].diffuse*dcoef; + vec4 diffuse = gl_FrontLightProduct[0].diffuse * dcoef; vec4 specular = vec4(0.0); if(dcoef > 0.0){ @@ -24,7 +24,7 @@ vec4 PhongShading(void) vec3 R = reflect(-L, N); float specularLight = pow(max(dot(R, V), 0.0), gl_FrontMaterial.shininess); - specular = gl_FrontLightProduct[0].specular*specularLight; + specular = gl_FrontLightProduct[0].specular * specularLight; } return ambient+diffuse+specular; } @@ -43,10 +43,12 @@ float ShadowCoef(void){ } void main(void){ - vec4 c = gl_Color; + vec4 c = PhongShading(); vec4 ambient = gl_LightSource[0].ambient; float shadow_coef = ShadowCoef(); + //c = c * texture2D(current_texture, gl_TexCoord[0].st); + gl_FragColor = ambient * shadow_coef * c + (1.0 - ambient) * c; gl_FragColor.a = 1.0; } diff --git a/engine/resource/shader/shadow.vs b/engine/resource/shader/shadow.vs index ffeafdb..659d2c7 100644 --- a/engine/resource/shader/shadow.vs +++ b/engine/resource/shader/shadow.vs @@ -11,5 +11,5 @@ void main(void){ gl_Position = gl_ProjectionMatrix * vPos; gl_FrontColor = gl_Color; - gl_TexCoord[0] * gl_MultiTexCoord0; + gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; } diff --git a/engine/src/client.c b/engine/src/client.c index e4e530d..3750fe4 100644 --- a/engine/src/client.c +++ b/engine/src/client.c @@ -10,9 +10,9 @@ GBClient GBClientCreate(GBEngine engine) { client->engine = engine; - client->camera[0] = 0; + client->camera[0] = 2; client->camera[1] = 1.5; - client->camera[2] = 0; + client->camera[2] = 2; client->look_at[0] = 0; client->look_at[1] = 0; @@ -21,7 +21,7 @@ GBClient GBClientCreate(GBEngine engine) { client->light0[0] = 5; client->light0[1] = 5; client->light0[2] = 5; - client->light0[3] = 0; + client->light0[3] = 1; client->gl = GBGLCreate(client); @@ -114,9 +114,10 @@ void GBClientStep(GBClient client) { glLightfv(GL_LIGHT0, GL_POSITION, f); - GBGLShadowBeforeMapping(client->gl); - scene(); - GBGLShadowAfterMapping(client->gl); + if(GBGLShadowBeforeMapping(client->gl)) { + scene(); + GBGLShadowAfterMapping(client->gl); + } scene(); GBGLShadowEnd(client->gl); @@ -127,8 +128,8 @@ void GBClientStep(GBClient client) { client->camera[0] = cos(r) * 2; client->camera[2] = sin(r) * 2; - // client->light0[0] = -cos(r) * 2; - // client->light0[2] = -sin(r) * 2; + client->light0[0] = -cos(r) * 5; + client->light0[2] = -sin(r) * 5; r += 0.1; } diff --git a/engine/src/gl.c b/engine/src/gl.c index d6e1e89..d007f3e 100644 --- a/engine/src/gl.c +++ b/engine/src/gl.c @@ -35,6 +35,7 @@ GBGL GBGLCreate(GBClient client) { } void GBGLDestroy(GBGL gl) { + GBGLShadowDeinit(gl); free(gl); } diff --git a/engine/src/gl_shadow.c b/engine/src/gl_shadow.c index f8b839e..316395d 100644 --- a/engine/src/gl_shadow.c +++ b/engine/src/gl_shadow.c @@ -4,6 +4,7 @@ #define SHADOW_WIDTH 1024 #define SHADOW_HEIGHT 1024 +#define SHADOW_CULL void GBGLShadowInit(GBGL gl) { glGenTextures(1, &gl->shadow_texture); @@ -29,12 +30,20 @@ void GBGLShadowInit(GBGL gl) { glUniform1i(glGetUniformLocation(gl->shadow_shader, "depth_texture"), 7); glUniform1i(glGetUniformLocation(gl->shadow_shader, "current_texture"), 0); glUseProgram(0); + + gl->shadow_use_shader = 1; + } else { + glDeleteTextures(1, &gl->shadow_texture); + + gl->shadow_use_shader = 0; } } -void GBGLShadowBeforeMapping(GBGL gl) { +int GBGLShadowBeforeMapping(GBGL gl) { GBVector3 zero = {0, 0, 0}; + if(!gl->shadow_use_shader) return 0; + glClear(GL_DEPTH_BUFFER_BIT); glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); @@ -65,6 +74,8 @@ void GBGLShadowBeforeMapping(GBGL gl) { glPolygonOffset(1.1f, 4.0f); glEnable(GL_POLYGON_OFFSET_FILL); + + return 1; } void GBGLShadowAfterMapping(GBGL gl) { @@ -125,6 +136,8 @@ void GBGLShadowAfterMapping(GBGL gl) { } void GBGLShadowEnd(GBGL gl) { + if(!gl->shadow_use_shader) return; + glUseProgram(0); glActiveTexture(GL_TEXTURE7); @@ -139,3 +152,10 @@ void GBGLShadowEnd(GBGL gl) { glMatrixMode(GL_MODELVIEW); } + +void GBGLShadowDeinit(GBGL gl) { + if(gl->shadow_use_shader) { + glDeleteTextures(1, &gl->shadow_texture); + glDeleteProgram(gl->shadow_shader); + } +}