X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=dosdemo;a=blobdiff_plain;f=src%2Fpolytest.c;h=dfca5ef2a5978aeb3e52d1e3287ccdd5bc290525;hp=fc38c16747a2dcd8629bcd872bb96cf858203f93;hb=45f6f46fe758d15aafccdb69ae837fc7d84ee466;hpb=5d9ec41cc62ebddf5d406511714e561e88884987 diff --git a/src/polytest.c b/src/polytest.c index fc38c16..dfca5ef 100644 --- a/src/polytest.c +++ b/src/polytest.c @@ -5,22 +5,18 @@ #include "screen.h" #include "demo.h" #include "3dgfx.h" - -struct mesh { - int prim; - struct g3d_vertex *varr; - int16_t *iarr; - int vcount, icount; -}; +#include "gfxutil.h" +#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_mesh(struct mesh *mesh); -static int gen_cube(struct mesh *mesh, float sz); -static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub); -static void zsort(struct mesh *m); +static void draw_lowres_raster(void); +static int gen_texture(struct pimage *img, int xsz, int ysz); static struct screen scr = { "polytest", @@ -30,7 +26,16 @@ static struct screen scr = { draw }; -static struct mesh cube, torus; +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; + +#define LOWRES_SCALE 10 +static uint16_t *lowres_pixels; +static int lowres_width, lowres_height; struct screen *polytest_screen(void) { @@ -39,16 +44,41 @@ struct screen *polytest_screen(void) static int init(void) { - gen_cube(&cube, 1.0); - gen_torus(&torus, 1.0, 0.25, 24, 12); + int i; + + gen_cube_mesh(&cube, 1.0, 0); + gen_torus_mesh(&torus, 1.0, 0.25, 24, 12); + /* scale texcoords */ + for(i=0; i 90) phi = 90; - } +static void draw(void) +{ + float vdir[3]; + float mat[16]; - /*float angle = (float)time_msec / 50.0;*/ + update(); memset(fb_pixels, 0, fb_width * fb_height * 2); g3d_matrix_mode(G3D_MODELVIEW); g3d_load_identity(); - g3d_translate(0, 0, -5); - g3d_rotate(phi, 1, 0, 0); - g3d_rotate(theta, 0, 1, 0); - - zsort(&torus); - draw_mesh(&torus); - - swap_buffers(fb_pixels); -} - -static void draw_mesh(struct mesh *mesh) -{ - if(mesh->iarr) { - g3d_draw_indexed(mesh->prim, mesh->varr, mesh->vcount, mesh->iarr, mesh->icount); + g3d_translate(0, 0, -cam_dist); + if(opt.sball) { + g3d_mult_matrix(sball_matrix); } else { - g3d_draw(mesh->prim, mesh->varr, mesh->vcount); + g3d_rotate(cam_phi, 1, 0, 0); + g3d_rotate(cam_theta, 0, 1, 0); } -} -#define NORMAL(vp, x, y, z) do { vp->nx = x; vp->ny = y; vp->nz = z; } while(0) -#define COLOR(vp, cr, cg, cb) do { vp->r = cr; vp->g = cg; vp->b = cb; } while(0) -#define TEXCOORD(vp, tu, tv) do { vp->u = tu; vp->v = tv; } while(0) -#define VERTEX(vp, vx, vy, vz) \ - do { \ - vp->x = vx; vp->y = vy; vp->z = vz; vp->w = 1.0f; \ - ++vp; \ - } while(0) + /* calc world-space view direction */ + g3d_get_matrix(G3D_MODELVIEW, mat); + /* transform (0, 0, -1) with transpose(mat3x3) */ + vdir[0] = -mat[2]; + vdir[1] = -mat[6]; + vdir[2] = -mat[10]; -static int gen_cube(struct mesh *mesh, float sz) -{ - struct g3d_vertex *vptr; - float hsz = sz / 2.0; - - mesh->prim = G3D_QUADS; - mesh->iarr = 0; - mesh->icount = 0; - mesh->vcount = 24; - if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) { - return -1; - } - vptr = mesh->varr; - - /* -Z */ - NORMAL(vptr, 0, 0, -1); - COLOR(vptr, 255, 0, 255); - VERTEX(vptr, hsz, -hsz, -hsz); - VERTEX(vptr, -hsz, -hsz, -hsz); - VERTEX(vptr, -hsz, hsz, -hsz); - VERTEX(vptr, hsz, hsz, -hsz); - /* -Y */ - NORMAL(vptr, 0, -1, 0); - COLOR(vptr, 0, 255, 255); - VERTEX(vptr, -hsz, -hsz, -hsz); - VERTEX(vptr, hsz, -hsz, -hsz); - VERTEX(vptr, hsz, -hsz, hsz); - VERTEX(vptr, -hsz, -hsz, hsz); - /* -X */ - NORMAL(vptr, -1, 0, 0); - COLOR(vptr, 255, 255, 0); - VERTEX(vptr, -hsz, -hsz, -hsz); - VERTEX(vptr, -hsz, -hsz, hsz); - VERTEX(vptr, -hsz, hsz, hsz); - VERTEX(vptr, -hsz, hsz, -hsz); - /* +X */ - NORMAL(vptr, 1, 0, 0); - COLOR(vptr, 255, 0, 0); - VERTEX(vptr, hsz, -hsz, hsz); - VERTEX(vptr, hsz, -hsz, -hsz); - VERTEX(vptr, hsz, hsz, -hsz); - VERTEX(vptr, hsz, hsz, hsz); - /* +Y */ - NORMAL(vptr, 0, 1, 0); - COLOR(vptr, 0, 255, 0); - VERTEX(vptr, -hsz, hsz, hsz); - VERTEX(vptr, hsz, hsz, hsz); - VERTEX(vptr, hsz, hsz, -hsz); - VERTEX(vptr, -hsz, hsz, -hsz); - /* +Z */ - NORMAL(vptr, 0, 0, 1); - COLOR(vptr, 0, 0, 255); - VERTEX(vptr, -hsz, -hsz, hsz); - VERTEX(vptr, hsz, -hsz, hsz); - VERTEX(vptr, hsz, hsz, hsz); - VERTEX(vptr, -hsz, hsz, hsz); + g3d_light_pos(0, -10, 10, 20); - return 0; -} + zsort_mesh(&torus); -static void torusvec(float *res, float theta, float phi, float mr, float rr) -{ - float rx, ry, rz; - theta = -theta; + g3d_mtl_diffuse(0.4, 0.7, 1.0); + g3d_set_texture(tex.width, tex.height, tex.pixels); - rx = -cos(phi) * rr + mr; - ry = sin(phi) * rr; - rz = 0.0f; + /*draw_mesh(&torus);*/ + draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]); - res[0] = rx * sin(theta) + rz * cos(theta); - res[1] = ry; - res[2] = -rx * cos(theta) + rz * sin(theta); + /*draw_mesh(&cube);*/ + swap_buffers(fb_pixels); } -static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub) +static void draw_debug(void) { - int i, j; - int nfaces, uverts, vverts; - struct g3d_vertex *vptr; - int16_t *iptr; - - mesh->prim = G3D_QUADS; + update(); - if(usub < 4) usub = 4; - if(vsub < 2) vsub = 2; + memset(lowres_pixels, 0, lowres_width * lowres_height * 2); - uverts = usub + 1; - vverts = vsub + 1; + g3d_matrix_mode(G3D_MODELVIEW); + g3d_load_identity(); + g3d_translate(0, 0, -cam_dist); + g3d_rotate(cam_phi, 1, 0, 0); + g3d_rotate(cam_theta, 0, 1, 0); - mesh->vcount = uverts * vverts; - nfaces = usub * vsub; - mesh->icount = nfaces * 4; + g3d_framebuffer(lowres_width, lowres_height, lowres_pixels); + /*zsort(&torus);*/ + draw_mesh(&cube); - if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) { - return -1; - } - if(!(mesh->iarr = malloc(mesh->icount * sizeof *mesh->iarr))) { - return -1; - } - vptr = mesh->varr; - iptr = mesh->iarr; - - for(i=0; ix, theta, phi, rad, ringrad); - - vptr->nx = (vptr->x - rcent[0]) / ringrad; - vptr->ny = (vptr->y - rcent[1]) / ringrad; - vptr->nz = (vptr->z - rcent[2]) / ringrad; - vptr->u = u; - vptr->v = v; - vptr->r = chess ? 255 : 64; - vptr->g = 128; - vptr->b = chess ? 64 : 255; - ++vptr; - - if(i < usub && j < vsub) { - int idx = i * vverts + j; - *iptr++ = idx; - *iptr++ = idx + 1; - *iptr++ = idx + vverts + 1; - *iptr++ = idx + vverts; - } - } - } - return 0; -} + draw_lowres_raster(); -static struct { - struct g3d_vertex *varr; - const float *xform; -} zsort_cls; + g3d_framebuffer(fb_width, fb_height, fb_pixels); -static int zsort_cmp(const void *aptr, const void *bptr) -{ - const int16_t *a = (const int16_t*)aptr; - const int16_t *b = (const int16_t*)bptr; + g3d_polygon_mode(G3D_WIRE); + draw_mesh(&cube); + g3d_polygon_mode(G3D_FLAT); - const float *m = zsort_cls.xform; + swap_buffers(fb_pixels); +} - const struct g3d_vertex *va = zsort_cls.varr + a[0]; - const struct g3d_vertex *vb = zsort_cls.varr + b[0]; - float za = m[2] * va->x + m[6] * va->y + m[10] * va->z + m[14]; - float zb = m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14]; +static void draw_huge_pixel(uint16_t *dest, int dest_width, uint16_t color) +{ + int i, j; + uint16_t grid_color = PACK_RGB16(127, 127, 127); - va = zsort_cls.varr + a[2]; - vb = zsort_cls.varr + b[2]; + for(i=0; ix + m[6] * va->y + m[10] * va->z + m[14]; - zb += m[2] * vb->x + m[6] * vb->y + m[10] * vb->z + m[14]; +static void draw_lowres_raster(void) +{ + int i, j; + uint16_t *sptr = lowres_pixels; + uint16_t *dptr = fb_pixels; - return za - zb; + for(i=0; iicount / m->prim; + int i, j; + uint16_t *pix; + + if(!(img->pixels = malloc(xsz * ysz * sizeof *pix))) { + return -1; + } + pix = img->pixels; - zsort_cls.varr = m->varr; - zsort_cls.xform = g3d_get_matrix(G3D_MODELVIEW, 0); + for(i=0; iiarr, nfaces, m->prim * sizeof *m->iarr, zsort_cmp); + *pix++ = PACK_RGB16(val, val, val); + } + } + + img->width = xsz; + img->height = ysz; + return 0; }