From ed5740b6f667628f344d9dd17686424ce9e2de82 Mon Sep 17 00:00:00 2001 From: taw27 Date: Sat, 26 Jul 2008 23:19:32 +0000 Subject: Initial normal mapping stuff git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@176 84d2e878-0bd5-11dd-ad15-13eda11d74c5 --- data/Makefile.am | 7 +++--- data/shaders/lighting.frag | 42 +++++++++++++++++++++++-------- data/shaders/lighting.vert | 2 ++ src/render.c | 41 +++++++++++++----------------- src/texture.c | 63 ++++++++++++++++++++++++++++++---------------- src/types.h | 3 ++- 6 files changed, 99 insertions(+), 59 deletions(-) diff --git a/data/Makefile.am b/data/Makefile.am index 28241e0..9ea8369 100644 --- a/data/Makefile.am +++ b/data/Makefile.am @@ -1,12 +1,13 @@ modelsdir = $(datadir)/thrust3d/models -models_DATA = models/floor models/lander models/randombox models/walle models/walln models/wallw models/walls models/platform \ - models/ceiling models/tiledfloor +models_DATA = models/floor models/lander models/randombox models/walle models/walln models/wallw models/walls \ + models/platform models/ceiling models/tiledfloor shadersdir = $(datadir)/thrust3d/shaders shaders_DATA = shaders/lighting.vert shaders/lighting.frag shaders/swirlytron.vert shaders/swirlytron.frag texturesdir = $(datadir)/thrust3d/textures -textures_DATA = textures/floor1.png textures/tiledwall.png textures/radioactive.png textures/fuel.png textures/font.png textures/concrete.png +textures_DATA = textures/floor1.png textures/tiledwall.png textures/radioactive.png textures/fuel.png \ + textures/font.png textures/concrete.png textures/tiledwall-normals.png roomsdir = $(datadir)/thrust3d/rooms rooms_DATA = rooms/00-00-00 rooms/00-00-01 rooms/00-00-02 rooms/00-00-03 rooms/00-00-04 rooms/00-01-04 rooms/00-02-04 diff --git a/data/shaders/lighting.frag b/data/shaders/lighting.frag index ef7ce18..0b11b36 100644 --- a/data/shaders/lighting.frag +++ b/data/shaders/lighting.frag @@ -12,11 +12,15 @@ varying vec3 normal; varying vec3 light0vc; +varying vec3 light0hvc; varying vec3 light1vc; varying vec3 light2vc; varying vec3 light2hvc; uniform sampler2D texture; +uniform sampler2D normalmap; + +uniform bool has_normals; uniform bool fill_light_enabled; uniform bool texture_enabled; uniform bool texture_emits; @@ -31,9 +35,13 @@ void main() { vec3 emit; vec3 diff = vec3(0.0, 0.0, 0.0); vec3 spec = vec3(0.0, 0.0, 0.0); + vec3 norm = normal; + + if ( has_normals ) { + norm += (texture2D(normalmap, gl_TexCoord[0].st).rgb - vec3(0.5, 0.5, 0.5)) / 2.0; + } - vec3 norm = normalize(normal); - float ndothv = max(dot(norm, normalize(light2hvc)), 0.0); + norm = normalize(norm); /* Ambient */ ambi = col_ambi_diff * gl_LightModel.ambient.rgb; @@ -43,21 +51,33 @@ void main() { /* Fill-in light (light 2) */ if ( fill_light_enabled ) { - + + float ndothv; + + ndothv = max(dot(norm, normalize(light2hvc)), 0.0); diff += col_ambi_diff * gl_LightSource[2].diffuse.rgb * max(dot(vec3(light1vc), norm), 0.0); spec += gl_LightSource[2].specular.rgb * pow(ndothv, 80.0); } else { /* Spotlight (light 0) - positional, spotlight */ - float falloff = 1/ ( gl_LightSource[0].constantAttenuation + float falloff; + float spot; + float ndothv; + + falloff = 1/ ( gl_LightSource[0].constantAttenuation + gl_LightSource[0].linearAttenuation * length(light0vc) + gl_LightSource[0].quadraticAttenuation * pow(length(light0vc), 2.0) ); - float spot = max(dot(normalize(-light0vc), gl_LightSource[0].spotDirection), 0.0); + spot = max(dot(normalize(-light0vc), gl_LightSource[0].spotDirection), 0.0); spot = pow(spot, gl_LightSource[0].spotExponent); - diff += col_ambi_diff * gl_LightSource[0].diffuse.rgb * spot * falloff * max(dot(normalize(light0vc).xyz, norm), 0.0); - spec += vec3(1.0, 1.0, 1.0) * gl_LightSource[0].specular.rgb * pow(ndothv, 80.0); - + + ndothv = max(dot(norm, normalize(light0hvc)), 0.0); + + diff += col_ambi_diff * gl_LightSource[0].diffuse.rgb * spot * falloff * + max(dot(normalize(light0vc).xyz, norm), 0.0); + + spec += vec3(1.0, 1.0, 1.0) * gl_LightSource[0].specular.rgb * spot * falloff * pow(ndothv, 80.0); + /* Background glow (light 1) - diffuse only, directional */ diff += col_ambi_diff * gl_LightSource[1].diffuse.rgb * max(dot(vec3(light1vc), norm), 0.0); @@ -73,9 +93,9 @@ void main() { min(emit.b + ambi.b + diff.b + spec.b + tex.b, 1.0), alpha); } else { - gl_FragColor = vec4(tex.r * min(emit.r + ambi.r + diff.r + spec.r, 1.0), - tex.g * min(emit.g + ambi.g + diff.g + spec.g, 1.0), - tex.b * min(emit.b + ambi.b + diff.b + spec.b, 1.0), + gl_FragColor = vec4(min(tex.r * (ambi.r + diff.r) + spec.r, 1.0), + min(tex.g * (ambi.g + diff.g) + spec.g, 1.0), + min(tex.b * (ambi.b + diff.b) + spec.b, 1.0), alpha); } diff --git a/data/shaders/lighting.vert b/data/shaders/lighting.vert index 294d5ba..ddbe8ab 100644 --- a/data/shaders/lighting.vert +++ b/data/shaders/lighting.vert @@ -12,6 +12,7 @@ varying vec3 normal; varying vec3 light0vc; +varying vec3 light0hvc; varying vec3 light1vc; varying vec3 light2vc; varying vec3 light2hvc; @@ -24,6 +25,7 @@ void main() { /* Spotlight - positional light */ vec4 vert = gl_ModelViewMatrix * gl_Vertex; light0vc = gl_LightSource[0].position.xyz - vert.xyz; + light0hvc = normalize(gl_LightSource[0].halfVector.xyz); /* Diffuse "background glow" - this can be normalised only once, here, since 'position' * is really 'direction' and is the same for all vertices. */ diff --git a/src/render.c b/src/render.c index b3a32cb..6c7cd5b 100644 --- a/src/render.c +++ b/src/render.c @@ -384,6 +384,12 @@ static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderC glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); if ( r->shaders ) render_setuni(r->lighting_program, "texture_enabled", 1); + if ( r->shaders && texture->has_normals ) { + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, texture->normalmap); + render_setuni(r->lighting_program, "has_normals", 1); + glActiveTexture(GL_TEXTURE0); + } } else { if ( r->shaders ) render_setuni(r->lighting_program, "texture_enabled", 0); } @@ -406,6 +412,7 @@ static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderC glDrawArrays(render_gltype(p->type), 0, p->num_vertices); *nvert += p->num_vertices; glDisable(GL_TEXTURE_2D); + render_setuni(r->lighting_program, "has_normals", 0); glPopMatrix(); if ( p->attribs & ATTRIB_SWIRLY ) { @@ -656,9 +663,8 @@ void render_draw(Game *game, Uint32 t) { RenderContext *r; GLfloat amb[] = { 0.0, 0.0, 0.0, 1.0 }; - GLfloat fx, fy, fz, ux, uy, uz; + GLfloat fx, fy, fz; GLfloat fxt, fyt, fzt; - GLfloat uxt, uyt, uzt; r = game->render; @@ -719,43 +725,32 @@ void render_draw(Game *game, Uint32 t) { fx = 0.0; fy = -game->view_dist; fz = 0.0; - ux = 0.0; - uy = 0.0; - uz = 1.0; fxt = fx; fyt = fy; fzt = fz; - uxt = ux; uyt = uy; uzt = uz; fx = fxt; fy = fyt*cos(game->view_angle) + fzt*sin(game->view_angle); fz = fyt*sin(game->view_angle) + fzt*cos(game->view_angle); - ux = uxt; - uy = uyt*cos(game->view_angle) - uzt*sin(game->view_angle); - uz = -uyt*sin(game->view_angle) + uzt*cos(game->view_angle); fxt = fx; fyt = fy; fzt = fz; - uxt = ux; uyt = uy; uzt = uz; fx = fxt*cos(game->view_yaw+game->lander->yaw) + fyt*sin(game->view_yaw+game->lander->yaw); fy = -fxt*sin(game->view_yaw+game->lander->yaw) + fyt*cos(game->view_yaw+game->lander->yaw); fz = fzt; - ux = uxt*cos(game->view_yaw+game->lander->yaw) - uyt*sin(game->view_yaw+game->lander->yaw); - uy = -uxt*sin(game->view_yaw+game->lander->yaw) + uyt*cos(game->view_yaw+game->lander->yaw); - uz = uzt; - ux = 0.0; - uy = 0.0; - uz = 1.0; fx += game->lander->x; fy += game->lander->y; fz += game->lander->z; - gluLookAt(fx, fy, fz, game->lander->x, game->lander->y, game->lander->z, ux, uy, uz); - //gluLookAt(0.0, 0.0, -0.0, 0.0, 0.0, -5.0, 0.0, 1.0, 0.0); + gluLookAt(fx, fy, fz, game->lander->x, game->lander->y, game->lander->z, 0.0, 0.0, 1.0); - if ( r->shaders ) glUseProgram(r->lighting_program); - if ( r->shaders ) render_setuni(game->render->lighting_program, "texture", 0); /* GL_TEXTURE0 */ - if ( r->shaders ) render_setuni(game->render->lighting_program, "texture_only", 0); - if ( r->shaders ) render_setuni(game->render->lighting_program, "texture_enabled", 1); - if ( r->shaders ) render_setuni(game->render->lighting_program, "fill_light_enabled", 0); + if ( r->shaders ) { + glUseProgram(r->lighting_program); + render_setuni(game->render->lighting_program, "texture", 0); /* GL_TEXTURE0 */ + render_setuni(game->render->lighting_program, "normalmap", 1); /* GL_TEXTURE1 */ + render_setuni(game->render->lighting_program, "texture_only", 0); + render_setuni(game->render->lighting_program, "texture_enabled", 1); + render_setuni(game->render->lighting_program, "has_normals", 0); + render_setuni(game->render->lighting_program, "fill_light_enabled", 0); + } render_setup_lighting(game); amb[0] = 0.02; amb[1] = 0.02; amb[2] = 0.02; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb); diff --git a/src/texture.c b/src/texture.c index fcd8659..9079a3e 100644 --- a/src/texture.c +++ b/src/texture.c @@ -20,7 +20,7 @@ #include "render.h" -void texture_load(RenderContext *ctx, char *name) { +static int texture_load_png(char *filename, GLuint *name) { FILE *fh; png_bytep header; @@ -34,25 +34,23 @@ void texture_load(RenderContext *ctx, char *name) { png_bytep *row_pointers; unsigned int x; unsigned int y; - char tmp[128]; uint8_t *texels; /* Open file */ - snprintf(tmp, 127, "%s/textures/%s.png", DATADIR, name); - fh = fopen(tmp, "rb"); + fh = fopen(filename, "rb"); if ( !fh ) { - fprintf(stderr, "Couldn't open texture file '%s'\n", tmp); - return ; + fprintf(stderr, "Couldn't open texture file '%s'\n", filename); + return 1; } /* Check it's actually a PNG file */ header = malloc(8); fread(header, 1, 8, fh); if ( png_sig_cmp(header, 0, 8)) { - fprintf(stderr, "Texture file '%s' is not a PNG file.\n", tmp); + fprintf(stderr, "Texture file '%s' is not a PNG file.\n", filename); free(header); fclose(fh); - return; + return 1; } free(header); @@ -60,7 +58,7 @@ void texture_load(RenderContext *ctx, char *name) { if ( !png_ptr ) { fprintf(stderr, "Couldn't create PNG read structure.\n"); fclose(fh); - return ; + return 1; } info_ptr = png_create_info_struct(png_ptr); @@ -68,7 +66,7 @@ void texture_load(RenderContext *ctx, char *name) { png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); fprintf(stderr, "Couldn't create PNG info structure.\n"); fclose(fh); - return; + return 1; } end_info = png_create_info_struct(png_ptr); @@ -76,14 +74,14 @@ void texture_load(RenderContext *ctx, char *name) { png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); printf("Couldn't create PNG end info structure.\n"); fclose(fh); - return; + return 1; } if ( setjmp(png_jmpbuf(png_ptr)) ) { png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); fclose(fh); fprintf(stderr, "PNG read failed.\n"); - return; + return 1; } png_init_io(png_ptr, fh); @@ -97,16 +95,16 @@ void texture_load(RenderContext *ctx, char *name) { bit_depth = png_get_bit_depth(png_ptr, info_ptr); channels = png_get_channels(png_ptr, info_ptr); if ( bit_depth != 8 ) { - fprintf(stderr, "Texture image '%s' doesn't have 8 bits per channel per pixel.\n", tmp); + fprintf(stderr, "Texture image '%s' doesn't have 8 bits per channel per pixel.\n", filename); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); fclose(fh); - return; + return 1; } if ( channels != 4 ) { - fprintf(stderr, "Texture image '%s' doesn't have 4 channels.\n", tmp); + fprintf(stderr, "Texture image '%s' doesn't have 4 channels.\n", filename); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); fclose(fh); - return; + return 1; } /* Get image data */ @@ -131,20 +129,43 @@ void texture_load(RenderContext *ctx, char *name) { } } - glGenTextures(1, &(ctx->textures[ctx->num_textures].texname)); - glBindTexture(GL_TEXTURE_2D, ctx->textures[ctx->num_textures].texname); + glGenTextures(1, name); + glBindTexture(GL_TEXTURE_2D, *name); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texels); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, width, height, GL_RGBA, GL_UNSIGNED_BYTE, texels); free(texels); - ctx->textures[ctx->num_textures].name = name; - ctx->num_textures++; png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); fclose(fh); + + return 0; + +} + +void texture_load(RenderContext *ctx, char *name) { + + GLuint colourmap, normalmap; + char colourmap_filename[256]; + char normalmap_filename[256]; + + snprintf(colourmap_filename, 255, "%s/textures/%s.png", DATADIR, name); + snprintf(normalmap_filename, 255, "%s/textures/%s-normals.png", DATADIR, name); + + if ( texture_load_png(colourmap_filename, &colourmap) != 0 ) return; + ctx->textures[ctx->num_textures].texname = colourmap; + + if ( texture_load_png(normalmap_filename, &normalmap) == 0 ) { + ctx->textures[ctx->num_textures].normalmap = normalmap; + ctx->textures[ctx->num_textures].has_normals = 1; + } else { + ctx->textures[ctx->num_textures].has_normals = 0; + } + + ctx->textures[ctx->num_textures].name = strdup(name); + ctx->num_textures++; } diff --git a/src/types.h b/src/types.h index ae335ba..923d791 100644 --- a/src/types.h +++ b/src/types.h @@ -122,8 +122,9 @@ typedef struct { typedef struct { char *name; - GLuint *data; GLuint texname; + GLuint normalmap; + int has_normals; } Texture; typedef struct { -- cgit v1.2.3