deepstone

changeset 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 777be77b6432
files firstp/firstp.c src/mingl.c src/scantmpl.h src/scene.c src/scene.h
diffstat 5 files changed, 114 insertions(+), 28 deletions(-) [+]
line diff
     1.1 --- a/firstp/firstp.c	Tue Nov 29 07:23:57 2011 +0200
     1.2 +++ b/firstp/firstp.c	Wed Nov 30 00:04:16 2011 +0200
     1.3 @@ -23,7 +23,7 @@
     1.4  static void sighandler(int s);
     1.5  
     1.6  
     1.7 -static float cam_x, cam_y, cam_z = 10;
     1.8 +static float cam_x, cam_y, cam_z;
     1.9  static float cam_theta, cam_phi;
    1.10  
    1.11  static float walk_speed = 0.1;
    1.12 @@ -111,9 +111,10 @@
    1.13  
    1.14  	mgl_matrix_mode(MGL_MODELVIEW);
    1.15  	mgl_load_identity();
    1.16 +	mgl_rotate(cam_phi, 1, 0, 0);
    1.17  	mgl_rotate(cam_theta, 0, 1, 0);
    1.18 -	mgl_rotate(cam_phi, 1, 0, 0);
    1.19  	mgl_translate(-cam_x, -cam_y, -cam_z);
    1.20 +	mgl_translate(0, -2.0, 0);
    1.21  
    1.22  	mgl_light_intensity(0, 1.0);
    1.23  	mgl_light_position(0, 0, 5, 0, 1);
    1.24 @@ -124,6 +125,14 @@
    1.25  	copy_frame(fbuf);
    1.26  }
    1.27  
    1.28 +#define DEG_TO_RAD(x)	(M_PI * (x) / 180.0)
    1.29 +void cam_move(float dx, float dy)
    1.30 +{
    1.31 +	float angle = DEG_TO_RAD(cam_theta);
    1.32 +	cam_x += cos(angle) * dx + sin(angle) * dy;
    1.33 +	cam_z -= -sin(angle) * dx + cos(angle) * dy;
    1.34 +}
    1.35 +
    1.36  static int proc_events(void)
    1.37  {
    1.38  	static int prev_mx, prev_my, prev_bnmask;
    1.39 @@ -150,35 +159,24 @@
    1.40  
    1.41  static int keyb(int key)
    1.42  {
    1.43 -	float dir_x, dir_y, right_x, right_y;
    1.44 -
    1.45 -	dir_x = sin(DEG2RAD(cam_theta)) * walk_speed;
    1.46 -	dir_y = cos(DEG2RAD(cam_theta)) * walk_speed;
    1.47 -	right_x = dir_y;
    1.48 -	right_y = -dir_x;
    1.49 -
    1.50  	switch(key) {
    1.51  	case 27:
    1.52  		return 0;
    1.53  
    1.54  	case 'w':
    1.55 -		cam_x += dir_x;
    1.56 -		cam_z -= dir_y;
    1.57 +		cam_move(0, walk_speed);
    1.58  		break;
    1.59  
    1.60  	case 's':
    1.61 -		cam_x -= dir_x;
    1.62 -		cam_z += dir_y;
    1.63 +		cam_move(0, -walk_speed);
    1.64  		break;
    1.65  
    1.66  	case 'a':
    1.67 -		cam_x -= right_x;
    1.68 -		cam_z += right_y;
    1.69 +		cam_move(-walk_speed, 0);
    1.70  		break;
    1.71  
    1.72  	case 'd':
    1.73 -		cam_x += right_x;
    1.74 -		cam_z -= right_y;
    1.75 +		cam_move(walk_speed, 0);
    1.76  		break;
    1.77  
    1.78  	case '`':
     2.1 --- a/src/mingl.c	Tue Nov 29 07:23:57 2011 +0200
     2.2 +++ b/src/mingl.c	Wed Nov 30 00:04:16 2011 +0200
     2.3 @@ -19,6 +19,7 @@
     2.4  #include <stdlib.h>
     2.5  #include <string.h>
     2.6  #include <math.h>
     2.7 +#include <limits.h>
     2.8  #include <assert.h>
     2.9  #include "mingl.h"
    2.10  #include "mglimpl.h"
    2.11 @@ -412,7 +413,7 @@
    2.12  	memcpy(dest, mat, 16 * sizeof *dest);
    2.13  }
    2.14  
    2.15 -#define M(i,j)	(((j) << 2) + (i))
    2.16 +#define M(i,j)	(((i) << 2) + (j))
    2.17  void mgl_mult_matrix(float *m2)
    2.18  {
    2.19  	int i, j;
    2.20 @@ -557,7 +558,7 @@
    2.21  	for(i=0; i<MAX_SHIFT; i++) {
    2.22  		if((val >> i) == 1) {
    2.23  			*shiftp = i;
    2.24 -			*maskp = ~(0xffff << i);
    2.25 +			*maskp = ~(UINT_MAX << i);
    2.26  			return 0;
    2.27  		}
    2.28  	}
     3.1 --- a/src/scantmpl.h	Tue Nov 29 07:23:57 2011 +0200
     3.2 +++ b/src/scantmpl.h	Wed Nov 30 00:04:16 2011 +0200
     3.3 @@ -141,7 +141,7 @@
     3.4  	float e, de, dfde;
     3.5  #endif
     3.6  #ifdef INTERP_TEX
     3.7 -	int tx, ty;
     3.8 +	unsigned int tx, ty;
     3.9  	float u, v, du, dv, dfdu, dfdv;
    3.10  #endif
    3.11  	struct vertex *left, *right;
    3.12 @@ -253,8 +253,8 @@
    3.13  		z += dfdz;
    3.14  #endif
    3.15  #ifdef INTERP_TEX
    3.16 -		tx = (int)(u * st->tex.width) & st->tex.xmask;
    3.17 -		ty = (int)(v * st->tex.height) & st->tex.ymask;
    3.18 +		tx = (unsigned int)(u * st->tex.width) & st->tex.xmask;
    3.19 +		ty = (unsigned int)(v * st->tex.height) & st->tex.ymask;
    3.20  		c = st->tex.pixels[(ty << st->tex.xshift) + tx];
    3.21  
    3.22  		u += dfdu;
     4.1 --- a/src/scene.c	Tue Nov 29 07:23:57 2011 +0200
     4.2 +++ b/src/scene.c	Wed Nov 30 00:04:16 2011 +0200
     4.3 @@ -5,7 +5,13 @@
     4.4  #include <errno.h>
     4.5  #include "scene.h"
     4.6  #include "cvec.h"
     4.7 +#include "palman.h"
     4.8 +
     4.9 +#ifdef USE_GL
    4.10 +#include <GL/gl.h>
    4.11 +#else
    4.12  #include "mingl.h"
    4.13 +#endif
    4.14  
    4.15  
    4.16  struct face {
    4.17 @@ -231,6 +237,9 @@
    4.18  
    4.19  		if(strcmp(tok, "newmtl") == 0) {
    4.20  			if(m) {
    4.21 +				if(!m->tex) {
    4.22 +					palm_add_color(m->kd[0], m->kd[1], m->kd[2]);
    4.23 +				}
    4.24  				scn_add_material(scn, m);
    4.25  			}
    4.26  			if(!(m = malloc(sizeof *m)) || mtl_init(m) == -1) {
    4.27 @@ -239,15 +248,25 @@
    4.28  			mtl_set_name(m, rest);
    4.29  
    4.30  		} else if(strcmp(tok, "Kd") == 0) {
    4.31 -			if(sscanf(rest, "%f %f %f", &m->kd.x, &m->kd.y, &m->kd.z) != 3) {
    4.32 +			float r, g, b;
    4.33 +			if(sscanf(rest, "%f %f %f", &r, &g, &b) != 3) {
    4.34  				continue;
    4.35  			}
    4.36 +			m->kd[0] = (int)(r * 255.0);
    4.37 +			m->kd[1] = (int)(g * 255.0);
    4.38 +			m->kd[2] = (int)(b * 255.0);
    4.39 +
    4.40  		} else if(strcmp(tok, "map_Kd") == 0) {
    4.41 -			printf("ignoring texture: `%s'\n", rest);
    4.42 +			if(!(m->tex = load_texture(rest))) {
    4.43 +				fprintf(stderr, "failed to load texture: `%s'\n", rest);
    4.44 +			}
    4.45  		}
    4.46  	}
    4.47  
    4.48  	if(m) {
    4.49 +		if(!m->tex) {
    4.50 +			palm_add_color(m->kd[0], m->kd[1], m->kd[2]);
    4.51 +		}
    4.52  		scn_add_material(scn, m);
    4.53  	}
    4.54  
    4.55 @@ -282,8 +301,39 @@
    4.56  
    4.57  void scn_render(struct scene *scn)
    4.58  {
    4.59 -	struct mesh *m = scn->meshlist;
    4.60 +	struct mesh *m;
    4.61  
    4.62 +	if(!scn->ready) {
    4.63 +		struct material *mtl = scn->matlist;
    4.64 +		while(mtl) {
    4.65 +			if(mtl->tex) {
    4.66 +				get_texture_pixels(mtl->tex);
    4.67 +#ifdef USE_GL
    4.68 +				{
    4.69 +					int i, npix = mtl->tex->width * mtl->tex->height;
    4.70 +					int range = palm_color_range();
    4.71 +					unsigned char *rgb = malloc(npix * 3);
    4.72 +					struct palm_color *pal = palm_palette();
    4.73 +
    4.74 +					for(i=0; i<npix; i++) {
    4.75 +						int idx = mtl->tex->pixels[i] + range - 1;
    4.76 +						rgb[i * 3] = pal[idx].r;
    4.77 +						rgb[i * 3 + 1] = pal[idx].g;
    4.78 +						rgb[i * 3 + 2] = pal[idx].b;
    4.79 +					}
    4.80 +					free(mtl->tex->pixels);
    4.81 +					mtl->tex->pixels = rgb;
    4.82 +				}
    4.83 +#endif	/* USE_GL */
    4.84 +			} else {
    4.85 +				mtl->kd_base = palm_color_base(mtl->kd[0], mtl->kd[1], mtl->kd[2]);
    4.86 +			}
    4.87 +			mtl = mtl->next;
    4.88 +		}
    4.89 +		scn->ready = 1;
    4.90 +	}
    4.91 +
    4.92 +	m = scn->meshlist;
    4.93  	while(m) {
    4.94  		mesh_draw(m);
    4.95  		m = m->next;
    4.96 @@ -365,15 +415,51 @@
    4.97  void mesh_draw(struct mesh *m)
    4.98  {
    4.99  	int i, numv;
   4.100 +	struct material *mtl = m->mtl;
   4.101 +
   4.102 +	numv = cvec_size(m->vert);
   4.103 +
   4.104 +#ifdef USE_GL
   4.105 +	if(mtl->tex) {
   4.106 +		glEnable(GL_TEXTURE_2D);
   4.107 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   4.108 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   4.109 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   4.110 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   4.111 +
   4.112 +		glTexImage2D(GL_TEXTURE_2D, 0, 3, mtl->tex->width, mtl->tex->height, 0, GL_RGB, GL_UNSIGNED_BYTE, mtl->tex->pixels);
   4.113 +	}
   4.114 +
   4.115 +	glBegin(GL_TRIANGLES);
   4.116 +	for(i=0; i<numv; i++) {
   4.117 +		glNormal3f(m->norm[i].x, m->norm[i].y, m->norm[i].z);
   4.118 +		glTexCoord2f(m->texcoord[i].x, m->texcoord[i].y);
   4.119 +		glVertex3f(m->vert[i].x, m->vert[i].y, m->vert[i].z);
   4.120 +	}
   4.121 +	glEnd();
   4.122 +
   4.123 +	if(mtl->tex) {
   4.124 +		glDisable(GL_TEXTURE_2D);
   4.125 +	}
   4.126 +#else
   4.127 +	if(mtl->tex) {
   4.128 +		/*mgl_enable(MGL_TEXTURE_2D);*/
   4.129 +		mgl_teximage(mtl->tex->width, mtl->tex->height, mtl->tex->pixels);
   4.130 +	} else {
   4.131 +		mgl_index(mtl->kd_base);
   4.132 +	}
   4.133  
   4.134  	mgl_begin(MGL_TRIANGLES);
   4.135  
   4.136 -	numv = cvec_size(m->vert);
   4.137  	for(i=0; i<numv; i++) {
   4.138  		mgl_normal(m->norm[i].x, m->norm[i].y, m->norm[i].z);
   4.139  		mgl_texcoord2f(m->texcoord[i].x, m->texcoord[i].y);
   4.140  		mgl_vertex3f(m->vert[i].x, m->vert[i].y, m->vert[i].z);
   4.141  	}
   4.142 +	mgl_end();
   4.143  
   4.144 -	mgl_end();
   4.145 +	if(mtl->tex) {
   4.146 +		mgl_disable(MGL_TEXTURE_2D);
   4.147 +	}
   4.148 +#endif
   4.149  }
     5.1 --- a/src/scene.h	Tue Nov 29 07:23:57 2011 +0200
     5.2 +++ b/src/scene.h	Wed Nov 30 00:04:16 2011 +0200
     5.3 @@ -7,7 +7,7 @@
     5.4  
     5.5  struct material {
     5.6  	char *name;
     5.7 -	vec3_t kd;
     5.8 +	int kd[3];
     5.9  	int kd_base;
    5.10  	struct texture *tex;
    5.11  
    5.12 @@ -25,6 +25,7 @@
    5.13  };
    5.14  
    5.15  struct scene {
    5.16 +	int ready;
    5.17  	struct material *matlist;
    5.18  	struct mesh *meshlist;
    5.19  };