From 8ea9cdf08c31bcb407a8ba4abe9e581d8db3279a Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Mon, 5 Jan 2026 23:02:30 +0900 Subject: [PATCH] shadow --- client/gl.c | 47 +++++++++++++++++++++++++++++++-------------- client/gl_font.c | 29 +++++----------------------- client/gl_main.c | 7 +++++++ client/gl_shader.c | 4 ++++ client/gl_util.c | 34 ++++++++++++++++++++++++++++++++ include/af_client.h | 5 +++++ resource/shadow.fs | 15 +++++++++++++-- resource/shadow.vs | 10 +++++++--- 8 files changed, 108 insertions(+), 43 deletions(-) create mode 100644 client/gl_util.c diff --git a/client/gl.c b/client/gl.c index 04f7ba9..6c15cd2 100644 --- a/client/gl.c +++ b/client/gl.c @@ -11,7 +11,9 @@ double gl_cam_z; int gl_scene = AF_SCENE_MAIN; -static GLuint shadow_texture, shadow; +GLuint gl_shadow; + +static GLuint shadow_texture; gl_scene_t gl_scenes[] = { {gl_main_changed, gl_main_init, gl_main_draw, gl_main_after}, /**/ @@ -25,7 +27,7 @@ void gl_resize(int width, int height) { } void gl_init(void) { - GLfloat lightamb[] = {0.1, 0.1, 0.1, 1.0}; + GLfloat lightamb[] = {0.25, 0.25, 0.25, 1.0}; GLfloat lightcol[] = {1.0, 1.0, 1.0, 1.0}; glEnable(GL_DEPTH_TEST); @@ -70,9 +72,10 @@ void gl_init(void) { glBindTexture(GL_TEXTURE_2D, 0); - if(gl_shader(&shadow, DATAROOTDIR "/assaultfish/shadow.vs", DATAROOTDIR "/assaultfish/shadow.fs")) { - glUseProgram(shadow); - glUniform1i(glGetUniformLocation(shadow, "texture"), 1); + if(gl_shader(&gl_shadow, DATAROOTDIR "/assaultfish/shadow.vs", DATAROOTDIR "/assaultfish/shadow.fs")) { + glUseProgram(gl_shadow); + glUniform1i(glGetUniformLocation(gl_shadow, "texture"), 1); + glUniform1i(glGetUniformLocation(gl_shadow, "texture_in"), 0); } } @@ -109,16 +112,34 @@ void gl_cube(double x, double y, double z, double xs, double ys, double zs, doub } static void gl_common(void) { - char buf[128]; + char buf1[128]; + char buf2[128]; + int l; + gl_util_begin_2d(); + + glColor4f(0, 0, 0, 0.5); + sprintf(buf1, "AssaultFish %s", AF_VERSION); + sprintf(buf2, "%s", AF_COPYRIGHT); + + l = gl_font_width(buf1); + if(l < gl_font_width(buf2)) l = gl_font_width(buf2); + + glBegin(GL_QUADS); + glVertex2f(MwGetInteger(opengl, MwNwidth) - l, MwGetInteger(opengl, MwNheight) - gl_font_height(buf1) - gl_font_height(buf2)); + glVertex2f(MwGetInteger(opengl, MwNwidth) - l, MwGetInteger(opengl, MwNheight)); + glVertex2f(MwGetInteger(opengl, MwNwidth), MwGetInteger(opengl, MwNheight)); + glVertex2f(MwGetInteger(opengl, MwNwidth), MwGetInteger(opengl, MwNheight) - gl_font_height(buf1) - gl_font_height(buf2)); + glEnd(); + glColor3f(1, 1, 1); - sprintf(buf, "AssaultFish %s", AF_VERSION); - gl_font_text(buf, MwGetInteger(opengl, MwNwidth) - gl_font_width(buf), MwGetInteger(opengl, MwNheight) - gl_font_height(buf) * 2, 1); + gl_font_text(buf1, MwGetInteger(opengl, MwNwidth) - gl_font_width(buf1), MwGetInteger(opengl, MwNheight) - gl_font_height(buf1) - gl_font_height(buf2), 1); - sprintf(buf, "%s", AF_COPYRIGHT); - gl_font_text(buf, MwGetInteger(opengl, MwNwidth) - gl_font_width(buf), MwGetInteger(opengl, MwNheight) - gl_font_height(buf), 1); + gl_font_text(buf2, MwGetInteger(opengl, MwNwidth) - gl_font_width(buf2), MwGetInteger(opengl, MwNheight) - gl_font_height(buf2), 1); gl_font_text(gl_status, 0, 0, 1); + + gl_util_end_2d(); } static void scene(void) { @@ -157,9 +178,11 @@ void gl_render(void) { scene(); + glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, shadow_texture); glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); glBindTexture(GL_TEXTURE_2D, 0); + glActiveTexture(GL_TEXTURE0); glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); @@ -201,8 +224,6 @@ void gl_render(void) { glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE0); - glUseProgram(0); - glMatrixMode(GL_TEXTURE); glLoadIdentity(); @@ -210,8 +231,6 @@ void gl_render(void) { gl_common(); - glUseProgram(shadow); - if(gl_scenes[gl_scene].after != NULL) gl_scenes[gl_scene].after(); } diff --git a/client/gl_font.c b/client/gl_font.c index 836bf98..72231fb 100644 --- a/client/gl_font.c +++ b/client/gl_font.c @@ -25,21 +25,11 @@ void gl_font_text(const char* text, int x, int y, double scale) { int gw = font_width / 16 * scale, gh = font_height / 16 * scale; int is = glIsEnabled(GL_TEXTURE_2D) ? 1 : 0; - glDisable(GL_DEPTH_TEST); - glDisable(GL_LIGHTING); - glDisable(GL_CULL_FACE); + gl_util_begin_2d(); + if(!is) glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, font_atlas); - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - glOrtho(0, MwGetInteger(opengl, MwNwidth), MwGetInteger(opengl, MwNheight), 0, -1, 1); - - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - glBegin(GL_QUADS); for(i = 0; text[i] != 0; i++) { double bx = 1.0 / 16 * (text[i] % 16), by = 1.0 / 16 * (text[i] / 16); @@ -55,19 +45,10 @@ void gl_font_text(const char* text, int x, int y, double scale) { } glEnd(); - glMatrixMode(GL_MODELVIEW); - glPopMatrix(); - - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - - glMatrixMode(GL_MODELVIEW); - glBindTexture(GL_TEXTURE_2D, 0); - if(!is) glEnable(GL_TEXTURE_2D); - glEnable(GL_CULL_FACE); - glEnable(GL_LIGHTING); - glEnable(GL_DEPTH_TEST); + if(!is) glDisable(GL_TEXTURE_2D); + + gl_util_end_2d(); } int gl_font_width(const char* text) { diff --git a/client/gl_main.c b/client/gl_main.c index 99f79ae..8e78651 100644 --- a/client/gl_main.c +++ b/client/gl_main.c @@ -1,5 +1,7 @@ #include +#include + static double rad; void gl_main_changed(void) { @@ -18,6 +20,8 @@ void gl_main_init(void) { void gl_main_draw(void) { int i; + glUniform1f(glGetUniformLocation(gl_shadow, "solid_color"), 1); + glPushMatrix(); glRotatef(rad, 1, 0, 0); glRotatef(rad, 0, 1, 0); @@ -31,6 +35,7 @@ void gl_main_draw(void) { glColor3f(c ? 0.75 : 0, 0, c ? 0 : 0.75); glBegin(GL_TRIANGLES); + glNormal3f(n[0], n[1], n[2]); glVertex3f(0, u * 1.4, 0); glVertex3f(-u, 0, u); @@ -53,6 +58,8 @@ void gl_main_draw(void) { glVertex3f(5, -1, 5); glVertex3f(5, -1, -5); glEnd(); + + glUniform1f(glGetUniformLocation(gl_shadow, "solid_color"), 0); } void gl_main_after(void) { diff --git a/client/gl_shader.c b/client/gl_shader.c index 742f76f..26d4e31 100644 --- a/client/gl_shader.c +++ b/client/gl_shader.c @@ -53,6 +53,8 @@ int gl_shader(GLuint* r, const char* vs, const char* fs) { glDeleteShader(fsi); free(vss); free(fss); + + printf("%s: compilation failure\n", vs); return 0; } @@ -63,6 +65,8 @@ int gl_shader(GLuint* r, const char* vs, const char* fs) { glDeleteShader(fsi); free(vss); free(fss); + + printf("%s: compilation failure\n", fs); return 0; } diff --git a/client/gl_util.c b/client/gl_util.c new file mode 100644 index 0000000..fc205e1 --- /dev/null +++ b/client/gl_util.c @@ -0,0 +1,34 @@ +#include + +void gl_util_begin_2d(void) { + glUseProgram(0); + + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + glDisable(GL_CULL_FACE); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glOrtho(0, MwGetInteger(opengl, MwNwidth), MwGetInteger(opengl, MwNheight), 0, -1, 1); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); +} + +void gl_util_end_2d(void) { + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + + glMatrixMode(GL_MODELVIEW); + + glEnable(GL_CULL_FACE); + glEnable(GL_LIGHTING); + glEnable(GL_DEPTH_TEST); + + glUseProgram(gl_shadow); +} diff --git a/include/af_client.h b/include/af_client.h index 22ef027..093c8ab 100644 --- a/include/af_client.h +++ b/include/af_client.h @@ -34,6 +34,7 @@ extern double gl_cam_ud; extern double gl_cam_x; extern double gl_cam_y; extern double gl_cam_z; +extern GLuint gl_shadow; void gl_init(void); void gl_render(void); @@ -55,6 +56,10 @@ void gl_main_init(void); void gl_main_draw(void); void gl_main_after(void); +/* gl_util.c */ +void gl_util_begin_2d(void); +void gl_util_end_2d(void); + /* gl_shader.c */ int gl_shader(GLuint* r, const char* vs, const char* fs); diff --git a/resource/shadow.fs b/resource/shadow.fs index 5a8ab62..abd43a0 100644 --- a/resource/shadow.fs +++ b/resource/shadow.fs @@ -2,7 +2,18 @@ uniform sampler2DShadow texture; varying vec4 shadow; - +varying vec4 color; +uniform float solid_color; +uniform sampler2D texture_in; + void main (void){ - gl_FragColor = shadow + (gl_Color - shadow) * shadow2DProj(texture, gl_TexCoord[0]); + vec4 c; + + if(solid_color > 0.5){ + c = color; + }else{ + c = texture2D(texture_in, gl_TexCoord[1].st); + } + + gl_FragColor = (shadow + (gl_Color - shadow) * shadow2DProj(texture, gl_TexCoord[0])) * c; } diff --git a/resource/shadow.vs b/resource/shadow.vs index 7777bfa..a4ce21d 100644 --- a/resource/shadow.vs +++ b/resource/shadow.vs @@ -1,24 +1,28 @@ #version 110 varying vec4 shadow; - +varying vec4 color; + void main(void){ vec3 position = vec3(gl_ModelViewMatrix * gl_Vertex); vec3 normal = normalize(gl_NormalMatrix * gl_Normal); vec3 light = normalize(gl_LightSource[0].position.xyz - position); float diffuse = dot(light, normal); + + color = gl_Color; - gl_FrontColor = gl_LightSource[0].ambient * gl_Color; + gl_FrontColor = gl_LightSource[0].ambient; shadow = gl_FrontColor; if (diffuse > 0.0){ vec3 view = normalize(position); vec3 halfway = normalize(light - view); float specular = pow(max(dot(normal, halfway), 0.0), gl_FrontMaterial.shininess); - vec4 temp = gl_LightSource[0].diffuse * gl_Color * diffuse; + vec4 temp = gl_LightSource[0].diffuse * diffuse; shadow += temp * 0.2; gl_FrontColor += temp + gl_FrontLightProduct[0].specular * specular; } gl_TexCoord[0] = gl_TextureMatrix[0] * gl_ModelViewMatrix * gl_Vertex; + gl_TexCoord[1] = gl_MultiTexCoord0; gl_Position = ftransform(); }