functional cascade shadow

This commit is contained in:
Nishi 2026-02-02 23:24:12 +09:00
commit 79b5e4a7b4
Signed by: nishi
GPG key ID: 27EF69B208EB9343
7 changed files with 39 additions and 10 deletions

View file

@ -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;
}
}

View file

@ -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);

View file

@ -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);

View file

@ -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) {

View file

@ -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);
}
}
}