diff options
author | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-07-26 23:19:32 +0000 |
---|---|---|
committer | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-07-26 23:19:32 +0000 |
commit | ed5740b6f667628f344d9dd17686424ce9e2de82 (patch) | |
tree | 124d4fe3fd9d04e8f89550c82e5487491b35e889 /src/texture.c | |
parent | 5f8d27519c76fd297eb61144ff38bd5d1afa51bb (diff) |
Initial normal mapping stuff
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@176 84d2e878-0bd5-11dd-ad15-13eda11d74c5
Diffstat (limited to 'src/texture.c')
-rw-r--r-- | src/texture.c | 63 |
1 files changed, 42 insertions, 21 deletions
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++; } |