X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=dosdemo;a=blobdiff_plain;f=src%2Fpolytest.c;h=0b0531372d640dfa653c9376226428e721279b80;hp=85b31282382b0d7bdfa0e51682fe021b469b741b;hb=5f36e95f19ad8d7a5a1dd546ffeb54ce95d51749;hpb=a1ede8e271fee1b4d029752603096db2be780e67 diff --git a/src/polytest.c b/src/polytest.c index 85b3128..0b05313 100644 --- a/src/polytest.c +++ b/src/polytest.c @@ -9,12 +9,14 @@ #include "polyfill.h" /* just for struct pimage */ #include "cfgopt.h" #include "mesh.h" +#include "bsptree.h" static int init(void); static void destroy(void); static void start(long trans_time); static void draw(void); static void draw_lowres_raster(void); +static void keypress(int key); static int gen_texture(struct pimage *img, int xsz, int ysz); static struct screen scr = { @@ -22,15 +24,19 @@ static struct screen scr = { init, destroy, start, 0, - draw + draw, + keypress }; static float cam_theta, cam_phi = 25; static float cam_dist = 3; static struct g3d_mesh cube, torus; +static struct bsptree torus_bsp; static struct pimage tex; +static int use_bsp = 0; + #define LOWRES_SCALE 10 static uint16_t *lowres_pixels; static int lowres_width, lowres_height; @@ -52,6 +58,14 @@ static int init(void) torus.varr[i].v *= 2.0; } + /* + init_bsp(&torus_bsp); + if(bsp_add_mesh(&torus_bsp, &torus) == -1) { + fprintf(stderr, "failed to construct torus BSP tree\n"); + return -1; + } + */ + gen_texture(&tex, 128, 128); #ifdef DEBUG_POLYFILL @@ -70,6 +84,9 @@ static void destroy(void) free(cube.varr); free(torus.varr); free(torus.iarr); + /* + destroy_bsp(&torus_bsp); + */ } static void start(long trans_time) @@ -82,7 +99,8 @@ static void start(long trans_time) g3d_enable(G3D_LIGHTING); g3d_enable(G3D_LIGHT0); - g3d_polygon_mode(G3D_TEX_GOURAUD); + g3d_polygon_mode(G3D_GOURAUD); + g3d_enable(G3D_TEXTURE_2D); } static void update(void) @@ -93,6 +111,9 @@ static void update(void) static void draw(void) { + float vdir[3]; + const float *mat; + update(); memset(fb_pixels, 0, fb_width * fb_height * 2); @@ -109,12 +130,22 @@ static void draw(void) g3d_light_pos(0, -10, 10, 20); - zsort_mesh(&torus); - g3d_mtl_diffuse(0.4, 0.7, 1.0); g3d_set_texture(tex.width, tex.height, tex.pixels); - draw_mesh(&torus); + if(use_bsp) { + /* calc world-space view direction */ + mat = g3d_get_matrix(G3D_MODELVIEW, 0); + /* transform (0, 0, -1) with transpose(mat3x3) */ + vdir[0] = -mat[2]; + vdir[1] = -mat[6]; + vdir[2] = -mat[10]; + + draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]); + } else { + zsort_mesh(&torus); + draw_mesh(&torus); + } /*draw_mesh(&cube);*/ swap_buffers(fb_pixels); @@ -177,6 +208,16 @@ static void draw_lowres_raster(void) } } +static void keypress(int key) +{ + switch(key) { + case 'b': + use_bsp = !use_bsp; + printf("drawing with %s\n", use_bsp ? "BSP tree" : "z-sorting"); + break; + } +} + static int gen_texture(struct pimage *img, int xsz, int ysz) { int i, j;