This commit is contained in:
Nishi 2026-01-11 18:27:41 +09:00
commit 08ea9a0e7b
Signed by: nishi
GPG key ID: 27EF69B208EB9343
7 changed files with 40 additions and 14 deletions

View file

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

View file

@ -76,6 +76,7 @@ struct _GBResourceKV {
struct _GBGL {
GBEngine engine;
int shadow_use_shader;
GLuint shadow_texture;
GLuint shadow_shader;
GLdouble shadow_modelview[16];

View file

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

View file

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

View file

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

View file

@ -35,6 +35,7 @@ GBGL GBGLCreate(GBClient client) {
}
void GBGLDestroy(GBGL gl) {
GBGLShadowDeinit(gl);
free(gl);
}

View file

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