deepstone

diff src/scene.c @ 17:1e9f0b3616fa

fixed the matrix multiplication order
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 30 Nov 2011 00:04:16 +0200
parents cb676ff89e69
children c10f62b2bd56
line diff
     1.1 --- a/src/scene.c	Tue Nov 29 07:23:57 2011 +0200
     1.2 +++ b/src/scene.c	Wed Nov 30 00:04:16 2011 +0200
     1.3 @@ -5,7 +5,13 @@
     1.4  #include <errno.h>
     1.5  #include "scene.h"
     1.6  #include "cvec.h"
     1.7 +#include "palman.h"
     1.8 +
     1.9 +#ifdef USE_GL
    1.10 +#include <GL/gl.h>
    1.11 +#else
    1.12  #include "mingl.h"
    1.13 +#endif
    1.14  
    1.15  
    1.16  struct face {
    1.17 @@ -231,6 +237,9 @@
    1.18  
    1.19  		if(strcmp(tok, "newmtl") == 0) {
    1.20  			if(m) {
    1.21 +				if(!m->tex) {
    1.22 +					palm_add_color(m->kd[0], m->kd[1], m->kd[2]);
    1.23 +				}
    1.24  				scn_add_material(scn, m);
    1.25  			}
    1.26  			if(!(m = malloc(sizeof *m)) || mtl_init(m) == -1) {
    1.27 @@ -239,15 +248,25 @@
    1.28  			mtl_set_name(m, rest);
    1.29  
    1.30  		} else if(strcmp(tok, "Kd") == 0) {
    1.31 -			if(sscanf(rest, "%f %f %f", &m->kd.x, &m->kd.y, &m->kd.z) != 3) {
    1.32 +			float r, g, b;
    1.33 +			if(sscanf(rest, "%f %f %f", &r, &g, &b) != 3) {
    1.34  				continue;
    1.35  			}
    1.36 +			m->kd[0] = (int)(r * 255.0);
    1.37 +			m->kd[1] = (int)(g * 255.0);
    1.38 +			m->kd[2] = (int)(b * 255.0);
    1.39 +
    1.40  		} else if(strcmp(tok, "map_Kd") == 0) {
    1.41 -			printf("ignoring texture: `%s'\n", rest);
    1.42 +			if(!(m->tex = load_texture(rest))) {
    1.43 +				fprintf(stderr, "failed to load texture: `%s'\n", rest);
    1.44 +			}
    1.45  		}
    1.46  	}
    1.47  
    1.48  	if(m) {
    1.49 +		if(!m->tex) {
    1.50 +			palm_add_color(m->kd[0], m->kd[1], m->kd[2]);
    1.51 +		}
    1.52  		scn_add_material(scn, m);
    1.53  	}
    1.54  
    1.55 @@ -282,8 +301,39 @@
    1.56  
    1.57  void scn_render(struct scene *scn)
    1.58  {
    1.59 -	struct mesh *m = scn->meshlist;
    1.60 +	struct mesh *m;
    1.61  
    1.62 +	if(!scn->ready) {
    1.63 +		struct material *mtl = scn->matlist;
    1.64 +		while(mtl) {
    1.65 +			if(mtl->tex) {
    1.66 +				get_texture_pixels(mtl->tex);
    1.67 +#ifdef USE_GL
    1.68 +				{
    1.69 +					int i, npix = mtl->tex->width * mtl->tex->height;
    1.70 +					int range = palm_color_range();
    1.71 +					unsigned char *rgb = malloc(npix * 3);
    1.72 +					struct palm_color *pal = palm_palette();
    1.73 +
    1.74 +					for(i=0; i<npix; i++) {
    1.75 +						int idx = mtl->tex->pixels[i] + range - 1;
    1.76 +						rgb[i * 3] = pal[idx].r;
    1.77 +						rgb[i * 3 + 1] = pal[idx].g;
    1.78 +						rgb[i * 3 + 2] = pal[idx].b;
    1.79 +					}
    1.80 +					free(mtl->tex->pixels);
    1.81 +					mtl->tex->pixels = rgb;
    1.82 +				}
    1.83 +#endif	/* USE_GL */
    1.84 +			} else {
    1.85 +				mtl->kd_base = palm_color_base(mtl->kd[0], mtl->kd[1], mtl->kd[2]);
    1.86 +			}
    1.87 +			mtl = mtl->next;
    1.88 +		}
    1.89 +		scn->ready = 1;
    1.90 +	}
    1.91 +
    1.92 +	m = scn->meshlist;
    1.93  	while(m) {
    1.94  		mesh_draw(m);
    1.95  		m = m->next;
    1.96 @@ -365,15 +415,51 @@
    1.97  void mesh_draw(struct mesh *m)
    1.98  {
    1.99  	int i, numv;
   1.100 +	struct material *mtl = m->mtl;
   1.101 +
   1.102 +	numv = cvec_size(m->vert);
   1.103 +
   1.104 +#ifdef USE_GL
   1.105 +	if(mtl->tex) {
   1.106 +		glEnable(GL_TEXTURE_2D);
   1.107 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   1.108 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   1.109 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   1.110 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   1.111 +
   1.112 +		glTexImage2D(GL_TEXTURE_2D, 0, 3, mtl->tex->width, mtl->tex->height, 0, GL_RGB, GL_UNSIGNED_BYTE, mtl->tex->pixels);
   1.113 +	}
   1.114 +
   1.115 +	glBegin(GL_TRIANGLES);
   1.116 +	for(i=0; i<numv; i++) {
   1.117 +		glNormal3f(m->norm[i].x, m->norm[i].y, m->norm[i].z);
   1.118 +		glTexCoord2f(m->texcoord[i].x, m->texcoord[i].y);
   1.119 +		glVertex3f(m->vert[i].x, m->vert[i].y, m->vert[i].z);
   1.120 +	}
   1.121 +	glEnd();
   1.122 +
   1.123 +	if(mtl->tex) {
   1.124 +		glDisable(GL_TEXTURE_2D);
   1.125 +	}
   1.126 +#else
   1.127 +	if(mtl->tex) {
   1.128 +		/*mgl_enable(MGL_TEXTURE_2D);*/
   1.129 +		mgl_teximage(mtl->tex->width, mtl->tex->height, mtl->tex->pixels);
   1.130 +	} else {
   1.131 +		mgl_index(mtl->kd_base);
   1.132 +	}
   1.133  
   1.134  	mgl_begin(MGL_TRIANGLES);
   1.135  
   1.136 -	numv = cvec_size(m->vert);
   1.137  	for(i=0; i<numv; i++) {
   1.138  		mgl_normal(m->norm[i].x, m->norm[i].y, m->norm[i].z);
   1.139  		mgl_texcoord2f(m->texcoord[i].x, m->texcoord[i].y);
   1.140  		mgl_vertex3f(m->vert[i].x, m->vert[i].y, m->vert[i].z);
   1.141  	}
   1.142 +	mgl_end();
   1.143  
   1.144 -	mgl_end();
   1.145 +	if(mtl->tex) {
   1.146 +		mgl_disable(MGL_TEXTURE_2D);
   1.147 +	}
   1.148 +#endif
   1.149  }