deepstone

changeset 4:c3e0bccd673e

added texture mapping
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Nov 2011 11:51:23 +0200
parents 0e781cc43178
children c8ffdbc6139e
files .hgignore src/palman.c src/palman.h src/test.c src/texture.c src/texture.h
diffstat 6 files changed, 153 insertions(+), 28 deletions(-) [+]
line diff
     1.1 --- a/.hgignore	Mon Nov 21 10:16:09 2011 +0200
     1.2 +++ b/.hgignore	Mon Nov 21 11:51:23 2011 +0200
     1.3 @@ -1,4 +1,5 @@
     1.4  \.o$
     1.5  \.obj$
     1.6  \.d$
     1.7 +\.swp$
     1.8  ^test$
     2.1 --- a/src/palman.c	Mon Nov 21 10:16:09 2011 +0200
     2.2 +++ b/src/palman.c	Mon Nov 21 11:51:23 2011 +0200
     2.3 @@ -44,6 +44,18 @@
     2.4  	return 0;
     2.5  }
     2.6  
     2.7 +int palm_color_index(unsigned char r, unsigned char g, unsigned char b)
     2.8 +{
     2.9 +	int i;
    2.10 +
    2.11 +	for(i=0; i<ncol; i++) {
    2.12 +		if(colors[i].r == r && colors[i].g == g && colors[i].b == b) {
    2.13 +			return i;
    2.14 +		}
    2.15 +	}
    2.16 +	return -1;
    2.17 +}
    2.18 +
    2.19  /* TODO: build an octree */
    2.20  int palm_build(void)
    2.21  {
    2.22 @@ -94,14 +106,12 @@
    2.23  
    2.24  int palm_color_base(unsigned char r, unsigned char g, unsigned char b)
    2.25  {
    2.26 -	int i;
    2.27 +	int c;
    2.28  
    2.29 -	for(i=0; i<pcol; i++) {
    2.30 -		if(colors[i].r == r && colors[i].g == g && colors[i].b == b) {
    2.31 -			return base_index[i];
    2.32 -		}
    2.33 +	if((c = palm_color_index(r, g, b)) == -1) {
    2.34 +		return -1;
    2.35  	}
    2.36 -	return -1;
    2.37 +	return base_index[c];
    2.38  }
    2.39  
    2.40  int palm_color_range(void)
     3.1 --- a/src/palman.h	Mon Nov 21 10:16:09 2011 +0200
     3.2 +++ b/src/palman.h	Mon Nov 21 11:51:23 2011 +0200
     3.3 @@ -25,6 +25,7 @@
     3.4  
     3.5  void palm_clear(void);
     3.6  int palm_add_color(unsigned char r, unsigned char g, unsigned char b);
     3.7 +int palm_color_index(unsigned char r, unsigned char g, unsigned char b);
     3.8  
     3.9  int palm_build(void);
    3.10  
     4.1 --- a/src/test.c	Mon Nov 21 10:16:09 2011 +0200
     4.2 +++ b/src/test.c	Mon Nov 21 11:51:23 2011 +0200
     4.3 @@ -41,6 +41,7 @@
     4.4  static unsigned char *fbuf;
     4.5  
     4.6  static struct texture *tex;
     4.7 +static char *texfile;
     4.8  
     4.9  static int white_base, red_base, green_base, blue_base;
    4.10  static int grad_range;
    4.11 @@ -113,16 +114,38 @@
    4.12  	signal(SIGILL, sighandler);
    4.13  	signal(SIGABRT, sighandler);
    4.14  
    4.15 -	palm_add_color(255, 255, 255);
    4.16 -	palm_add_color(255, 0, 0);
    4.17 -	palm_add_color(0, 255, 0);
    4.18 -	palm_add_color(0, 0, 255);
    4.19 -	palm_build();
    4.20  
    4.21 -	white_base = palm_color_base(255, 255, 255);
    4.22 -	red_base = palm_color_base(255, 0, 0);
    4.23 -	green_base = palm_color_base(0, 255, 0);
    4.24 -	blue_base = palm_color_base(0, 0, 255);
    4.25 +	if(mgl_init(320, 200) == -1) {
    4.26 +		fprintf(stderr, "mgl init failed\n");
    4.27 +		return -1;
    4.28 +	}
    4.29 +	fbuf = mgl_framebuffer();
    4.30 +
    4.31 +
    4.32 +	if(!texfile) {
    4.33 +		palm_add_color(255, 255, 255);
    4.34 +		palm_add_color(255, 0, 0);
    4.35 +		palm_add_color(0, 255, 0);
    4.36 +		palm_add_color(0, 0, 255);
    4.37 +		palm_build();
    4.38 +
    4.39 +		white_base = palm_color_base(255, 255, 255);
    4.40 +		red_base = palm_color_base(255, 0, 0);
    4.41 +		green_base = palm_color_base(0, 255, 0);
    4.42 +		blue_base = palm_color_base(0, 0, 255);
    4.43 +
    4.44 +		tex = tex_gen_checker(64, 64, 3, 3, red_base, blue_base);
    4.45 +	} else {
    4.46 +		if(!(tex = load_texture(texfile))) {
    4.47 +			return -1;
    4.48 +		}
    4.49 +
    4.50 +		palm_build();
    4.51 +		get_texture_pixels(tex);
    4.52 +
    4.53 +		mgl_enable(MGL_TEXTURE_2D);
    4.54 +	}
    4.55 +
    4.56  	grad_range = palm_color_range();
    4.57  
    4.58  	pal = palm_palette();
    4.59 @@ -130,12 +153,6 @@
    4.60  		set_palette(i, pal[i].r, pal[i].g, pal[i].b);
    4.61  	}
    4.62  
    4.63 -	if(mgl_init(320, 200) == -1) {
    4.64 -		fprintf(stderr, "mgl init failed\n");
    4.65 -		return -1;
    4.66 -	}
    4.67 -	fbuf = mgl_framebuffer();
    4.68 -
    4.69  	mgl_enable(MGL_CULL_FACE);
    4.70  	mgl_enable(MGL_SMOOTH);
    4.71  	mgl_color_range(grad_range - 1);	/* gradient range */
    4.72 @@ -148,10 +165,6 @@
    4.73  	mgl_load_identity();
    4.74  	mgl_perspective(45.0, 320.0 / 200.0, 0.5, 100.0);
    4.75  
    4.76 -	if(!(tex = tex_gen_checker(64, 64, 3, 3, red_base, blue_base))) {
    4.77 -		fprintf(stderr, "failed to generate texture\n");
    4.78 -		return -1;
    4.79 -	}
    4.80  	mgl_teximage(tex->width, tex->height, tex->pixels);
    4.81  
    4.82      return 0;
    4.83 @@ -358,6 +371,10 @@
    4.84  				auto_rotate = !auto_rotate;
    4.85  				break;
    4.86  
    4.87 +			case 't':
    4.88 +				texfile = argv[++i];
    4.89 +				break;
    4.90 +
    4.91  			case 'v':
    4.92  				use_vsync = !use_vsync;
    4.93  				break;
     5.1 --- a/src/texture.c	Mon Nov 21 10:16:09 2011 +0200
     5.2 +++ b/src/texture.c	Mon Nov 21 11:51:23 2011 +0200
     5.3 @@ -16,13 +16,96 @@
     5.4  along with this program.  If not, see <http://www.gnu.org/licenses/>.
     5.5  */
     5.6  
     5.7 +#include <stdio.h>
     5.8 +#include <string.h>
     5.9  #include <stdlib.h>
    5.10  #include "texture.h"
    5.11  #include "palman.h"
    5.12  
    5.13 -struct texture *tex_load(const char *fname)
    5.14 +struct texture *load_texture(const char *fname)
    5.15  {
    5.16 -	return 0;	/* TODO */
    5.17 +	int i, num_pixels;
    5.18 +	struct texture *tex;
    5.19 +	long fpos;
    5.20 +
    5.21 +	if(!(tex = malloc(sizeof *tex))) {
    5.22 +		return 0;
    5.23 +	}
    5.24 +	memset(tex, 0, sizeof *tex);
    5.25 +
    5.26 +	if(!(tex->file = fopen(fname, "rb"))) {
    5.27 +		fprintf(stderr, "failed to open texture: %s\n", fname);
    5.28 +		free_texture(tex);
    5.29 +		return 0;
    5.30 +	}
    5.31 +	if(fscanf(tex->file, "P6 %d %d 255 ", &tex->width, &tex->height) != 2) {
    5.32 +		fprintf(stderr, "invalid pixmap: %s\n", fname);
    5.33 +		free_texture(tex);
    5.34 +		return 0;
    5.35 +	}
    5.36 +	fpos = ftell(tex->file);
    5.37 +
    5.38 +	num_pixels = tex->width * tex->height;
    5.39 +	for(i=0; i<num_pixels; i++) {
    5.40 +		int r, g, b;
    5.41 +
    5.42 +		r = fgetc(tex->file);
    5.43 +		g = fgetc(tex->file);
    5.44 +		b = fgetc(tex->file);
    5.45 +
    5.46 +		if(feof(tex->file)) {
    5.47 +			fprintf(stderr, "unexpected EOF while reading: %s\n", fname);
    5.48 +			free_texture(tex);
    5.49 +			return 0;
    5.50 +		}
    5.51 +
    5.52 +		if(palm_color_index(r, g, b) == -1) {
    5.53 +			palm_add_color(r, g, b);
    5.54 +			printf("adding color %d %d %d\n", r, g, b);
    5.55 +		}
    5.56 +	}
    5.57 +	fseek(tex->file, fpos, SEEK_SET);
    5.58 +	return tex;
    5.59 +}
    5.60 +
    5.61 +void free_texture(struct texture *tex)
    5.62 +{
    5.63 +	if(tex) {
    5.64 +		if(tex->file) {
    5.65 +			fclose(tex->file);
    5.66 +		}
    5.67 +		free(tex->pixels);
    5.68 +		free(tex);
    5.69 +	}
    5.70 +}
    5.71 +
    5.72 +unsigned char *get_texture_pixels(struct texture *tex)
    5.73 +{
    5.74 +	if(!tex->pixels && tex->file) {
    5.75 +		int i, num_pixels = tex->width * tex->height;
    5.76 +
    5.77 +		if(!(tex->pixels = malloc(num_pixels))) {
    5.78 +			return 0;
    5.79 +		}
    5.80 +
    5.81 +		for(i=0; i<num_pixels; i++) {
    5.82 +			int r, g, b, base;
    5.83 +
    5.84 +			r = fgetc(tex->file);
    5.85 +			g = fgetc(tex->file);
    5.86 +			b = fgetc(tex->file);
    5.87 +
    5.88 +			if((base = palm_color_base(r, g, b)) == -1) {
    5.89 +				base = 0;
    5.90 +			}
    5.91 +			tex->pixels[i] = base;
    5.92 +		}
    5.93 +
    5.94 +		fclose(tex->file);
    5.95 +		tex->file = 0;
    5.96 +	}
    5.97 +
    5.98 +	return tex->pixels;
    5.99  }
   5.100  
   5.101  struct texture *tex_gen_checker(int xsz, int ysz, int ush, int vsh, int c1, int c2)
   5.102 @@ -34,6 +117,8 @@
   5.103  	if(!(tex = malloc(sizeof *tex))) {
   5.104  		return 0;
   5.105  	}
   5.106 +	memset(tex, 0, sizeof *tex);
   5.107 +
   5.108  	if(!(tex->pixels = malloc(xsz * ysz))) {
   5.109  		free(tex);
   5.110  		return 0;
     6.1 --- a/src/texture.h	Mon Nov 21 10:16:09 2011 +0200
     6.2 +++ b/src/texture.h	Mon Nov 21 11:51:23 2011 +0200
     6.3 @@ -21,10 +21,21 @@
     6.4  struct texture {
     6.5  	int width, height;
     6.6  	unsigned char *pixels;
     6.7 +
     6.8 +	struct {
     6.9 +		unsigned char r, g, b;
    6.10 +	} *palette;
    6.11 +	int num_colors;
    6.12 +
    6.13 +	FILE *file;
    6.14  };
    6.15  
    6.16 -struct texture *tex_load(const char *fname);
    6.17 +struct texture *load_texture(const char *fname);
    6.18 +void free_texture(struct texture *tex);
    6.19  
    6.20  struct texture *tex_gen_checker(int xsz, int ysz, int usub, int vsub, int c1, int c2);
    6.21  
    6.22 +unsigned char *get_texture_pixels(struct texture *tex);
    6.23 +int find_texture_color(struct texture *tex, int r, int g, int b);
    6.24 +
    6.25  #endif	/* TEXTURE_H_ */