# HG changeset patch # User John Tsiombikas # Date 1322604256 -7200 # Node ID 1e9f0b3616fa9dacd5c2cab04c970bc0236a176a # Parent cb676ff89e69cb454ffff967c008bfcab8b39721 fixed the matrix multiplication order diff -r cb676ff89e69 -r 1e9f0b3616fa firstp/firstp.c --- a/firstp/firstp.c Tue Nov 29 07:23:57 2011 +0200 +++ b/firstp/firstp.c Wed Nov 30 00:04:16 2011 +0200 @@ -23,7 +23,7 @@ static void sighandler(int s); -static float cam_x, cam_y, cam_z = 10; +static float cam_x, cam_y, cam_z; static float cam_theta, cam_phi; static float walk_speed = 0.1; @@ -111,9 +111,10 @@ mgl_matrix_mode(MGL_MODELVIEW); mgl_load_identity(); + mgl_rotate(cam_phi, 1, 0, 0); mgl_rotate(cam_theta, 0, 1, 0); - mgl_rotate(cam_phi, 1, 0, 0); mgl_translate(-cam_x, -cam_y, -cam_z); + mgl_translate(0, -2.0, 0); mgl_light_intensity(0, 1.0); mgl_light_position(0, 0, 5, 0, 1); @@ -124,6 +125,14 @@ copy_frame(fbuf); } +#define DEG_TO_RAD(x) (M_PI * (x) / 180.0) +void cam_move(float dx, float dy) +{ + float angle = DEG_TO_RAD(cam_theta); + cam_x += cos(angle) * dx + sin(angle) * dy; + cam_z -= -sin(angle) * dx + cos(angle) * dy; +} + static int proc_events(void) { static int prev_mx, prev_my, prev_bnmask; @@ -150,35 +159,24 @@ static int keyb(int key) { - float dir_x, dir_y, right_x, right_y; - - dir_x = sin(DEG2RAD(cam_theta)) * walk_speed; - dir_y = cos(DEG2RAD(cam_theta)) * walk_speed; - right_x = dir_y; - right_y = -dir_x; - switch(key) { case 27: return 0; case 'w': - cam_x += dir_x; - cam_z -= dir_y; + cam_move(0, walk_speed); break; case 's': - cam_x -= dir_x; - cam_z += dir_y; + cam_move(0, -walk_speed); break; case 'a': - cam_x -= right_x; - cam_z += right_y; + cam_move(-walk_speed, 0); break; case 'd': - cam_x += right_x; - cam_z -= right_y; + cam_move(walk_speed, 0); break; case '`': diff -r cb676ff89e69 -r 1e9f0b3616fa src/mingl.c --- a/src/mingl.c Tue Nov 29 07:23:57 2011 +0200 +++ b/src/mingl.c Wed Nov 30 00:04:16 2011 +0200 @@ -19,6 +19,7 @@ #include #include #include +#include #include #include "mingl.h" #include "mglimpl.h" @@ -412,7 +413,7 @@ memcpy(dest, mat, 16 * sizeof *dest); } -#define M(i,j) (((j) << 2) + (i)) +#define M(i,j) (((i) << 2) + (j)) void mgl_mult_matrix(float *m2) { int i, j; @@ -557,7 +558,7 @@ for(i=0; i> i) == 1) { *shiftp = i; - *maskp = ~(0xffff << i); + *maskp = ~(UINT_MAX << i); return 0; } } diff -r cb676ff89e69 -r 1e9f0b3616fa src/scantmpl.h --- a/src/scantmpl.h Tue Nov 29 07:23:57 2011 +0200 +++ b/src/scantmpl.h Wed Nov 30 00:04:16 2011 +0200 @@ -141,7 +141,7 @@ float e, de, dfde; #endif #ifdef INTERP_TEX - int tx, ty; + unsigned int tx, ty; float u, v, du, dv, dfdu, dfdv; #endif struct vertex *left, *right; @@ -253,8 +253,8 @@ z += dfdz; #endif #ifdef INTERP_TEX - tx = (int)(u * st->tex.width) & st->tex.xmask; - ty = (int)(v * st->tex.height) & st->tex.ymask; + tx = (unsigned int)(u * st->tex.width) & st->tex.xmask; + ty = (unsigned int)(v * st->tex.height) & st->tex.ymask; c = st->tex.pixels[(ty << st->tex.xshift) + tx]; u += dfdu; diff -r cb676ff89e69 -r 1e9f0b3616fa src/scene.c --- a/src/scene.c Tue Nov 29 07:23:57 2011 +0200 +++ b/src/scene.c Wed Nov 30 00:04:16 2011 +0200 @@ -5,7 +5,13 @@ #include #include "scene.h" #include "cvec.h" +#include "palman.h" + +#ifdef USE_GL +#include +#else #include "mingl.h" +#endif struct face { @@ -231,6 +237,9 @@ if(strcmp(tok, "newmtl") == 0) { if(m) { + if(!m->tex) { + palm_add_color(m->kd[0], m->kd[1], m->kd[2]); + } scn_add_material(scn, m); } if(!(m = malloc(sizeof *m)) || mtl_init(m) == -1) { @@ -239,15 +248,25 @@ mtl_set_name(m, rest); } else if(strcmp(tok, "Kd") == 0) { - if(sscanf(rest, "%f %f %f", &m->kd.x, &m->kd.y, &m->kd.z) != 3) { + float r, g, b; + if(sscanf(rest, "%f %f %f", &r, &g, &b) != 3) { continue; } + m->kd[0] = (int)(r * 255.0); + m->kd[1] = (int)(g * 255.0); + m->kd[2] = (int)(b * 255.0); + } else if(strcmp(tok, "map_Kd") == 0) { - printf("ignoring texture: `%s'\n", rest); + if(!(m->tex = load_texture(rest))) { + fprintf(stderr, "failed to load texture: `%s'\n", rest); + } } } if(m) { + if(!m->tex) { + palm_add_color(m->kd[0], m->kd[1], m->kd[2]); + } scn_add_material(scn, m); } @@ -282,8 +301,39 @@ void scn_render(struct scene *scn) { - struct mesh *m = scn->meshlist; + struct mesh *m; + if(!scn->ready) { + struct material *mtl = scn->matlist; + while(mtl) { + if(mtl->tex) { + get_texture_pixels(mtl->tex); +#ifdef USE_GL + { + int i, npix = mtl->tex->width * mtl->tex->height; + int range = palm_color_range(); + unsigned char *rgb = malloc(npix * 3); + struct palm_color *pal = palm_palette(); + + for(i=0; itex->pixels[i] + range - 1; + rgb[i * 3] = pal[idx].r; + rgb[i * 3 + 1] = pal[idx].g; + rgb[i * 3 + 2] = pal[idx].b; + } + free(mtl->tex->pixels); + mtl->tex->pixels = rgb; + } +#endif /* USE_GL */ + } else { + mtl->kd_base = palm_color_base(mtl->kd[0], mtl->kd[1], mtl->kd[2]); + } + mtl = mtl->next; + } + scn->ready = 1; + } + + m = scn->meshlist; while(m) { mesh_draw(m); m = m->next; @@ -365,15 +415,51 @@ void mesh_draw(struct mesh *m) { int i, numv; + struct material *mtl = m->mtl; + + numv = cvec_size(m->vert); + +#ifdef USE_GL + if(mtl->tex) { + glEnable(GL_TEXTURE_2D); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + + glTexImage2D(GL_TEXTURE_2D, 0, 3, mtl->tex->width, mtl->tex->height, 0, GL_RGB, GL_UNSIGNED_BYTE, mtl->tex->pixels); + } + + glBegin(GL_TRIANGLES); + for(i=0; inorm[i].x, m->norm[i].y, m->norm[i].z); + glTexCoord2f(m->texcoord[i].x, m->texcoord[i].y); + glVertex3f(m->vert[i].x, m->vert[i].y, m->vert[i].z); + } + glEnd(); + + if(mtl->tex) { + glDisable(GL_TEXTURE_2D); + } +#else + if(mtl->tex) { + /*mgl_enable(MGL_TEXTURE_2D);*/ + mgl_teximage(mtl->tex->width, mtl->tex->height, mtl->tex->pixels); + } else { + mgl_index(mtl->kd_base); + } mgl_begin(MGL_TRIANGLES); - numv = cvec_size(m->vert); for(i=0; inorm[i].x, m->norm[i].y, m->norm[i].z); mgl_texcoord2f(m->texcoord[i].x, m->texcoord[i].y); mgl_vertex3f(m->vert[i].x, m->vert[i].y, m->vert[i].z); } + mgl_end(); - mgl_end(); + if(mtl->tex) { + mgl_disable(MGL_TEXTURE_2D); + } +#endif } diff -r cb676ff89e69 -r 1e9f0b3616fa src/scene.h --- a/src/scene.h Tue Nov 29 07:23:57 2011 +0200 +++ b/src/scene.h Wed Nov 30 00:04:16 2011 +0200 @@ -7,7 +7,7 @@ struct material { char *name; - vec3_t kd; + int kd[3]; int kd_base; struct texture *tex; @@ -25,6 +25,7 @@ }; struct scene { + int ready; struct material *matlist; struct mesh *meshlist; };