added serial spaceball support in the dos version. can be used for
[dosdemo] / src / polytest.c
index b9ad891..1815bb9 100644 (file)
@@ -6,6 +6,8 @@
 #include "demo.h"
 #include "3dgfx.h"
 #include "gfxutil.h"
+#include "polyfill.h"  /* just for struct pimage */
+#include "cfgopt.h"
 
 struct mesh {
        int prim;
@@ -23,6 +25,7 @@ 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",
@@ -32,10 +35,13 @@ static struct screen scr = {
        draw
 };
 
-static float theta, phi = 25;
+static float cam_theta, cam_phi = 25;
+static float cam_dist = 3;
 static struct mesh cube, torus;
 
-#define LOWRES_SCALE   16
+static struct pimage tex;
+
+#define LOWRES_SCALE   10
 static uint16_t *lowres_pixels;
 static int lowres_width, lowres_height;
 
@@ -46,12 +52,24 @@ struct screen *polytest_screen(void)
 
 static int init(void)
 {
+       int i;
+
        gen_cube(&cube, 1.0);
        gen_torus(&torus, 1.0, 0.25, 24, 12);
+       /* scale texcoords */
+       for(i=0; i<torus.vcount; i++) {
+               torus.varr[i].u *= 4.0;
+               torus.varr[i].v *= 2.0;
+       }
 
+       gen_texture(&tex, 128, 128);
+
+#ifdef DEBUG_POLYFILL
        lowres_width = fb_width / LOWRES_SCALE;
        lowres_height = fb_height / LOWRES_SCALE;
        lowres_pixels = malloc(lowres_width * lowres_height * 2);
+       scr.draw = draw_debug;
+#endif
 
        return 0;
 }
@@ -71,6 +89,10 @@ static void start(long trans_time)
        g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
 
        g3d_enable(G3D_CULL_FACE);
+       g3d_enable(G3D_LIGHTING);
+       g3d_enable(G3D_LIGHT0);
+
+       g3d_polygon_mode(G3D_TEX_GOURAUD);
 }
 
 static void update(void)
@@ -79,16 +101,23 @@ static void update(void)
        static unsigned int prev_bmask;
 
        if(mouse_bmask) {
-               if(mouse_bmask ^ prev_bmask == 0) {
+               if((mouse_bmask ^ prev_bmask) == 0) {
                        int dx = mouse_x - prev_mx;
                        int dy = mouse_y - prev_my;
 
                        if(dx || dy) {
-                               theta += dx * 2.0;
-                               phi += dy * 2.0;
-
-                               if(phi < -90) phi = -90;
-                               if(phi > 90) phi = 90;
+                               if(mouse_bmask & 1) {
+                                       cam_theta += dx * 1.0;
+                                       cam_phi += dy * 1.0;
+
+                                       if(cam_phi < -90) cam_phi = -90;
+                                       if(cam_phi > 90) cam_phi = 90;
+                               }
+                               if(mouse_bmask & 4) {
+                                       cam_dist += dy * 0.5;
+
+                                       if(cam_dist < 0) cam_dist = 0;
+                               }
                        }
                }
        }
@@ -97,17 +126,47 @@ static void update(void)
        prev_bmask = mouse_bmask;
 }
 
+
 static void draw(void)
 {
        update();
 
+       memset(fb_pixels, 0, fb_width * fb_height * 2);
+
+       g3d_matrix_mode(G3D_MODELVIEW);
+       g3d_load_identity();
+       g3d_translate(0, 0, -cam_dist);
+       if(opt.sball) {
+               g3d_mult_matrix(sball_matrix);
+       } else {
+               g3d_rotate(cam_phi, 1, 0, 0);
+               g3d_rotate(cam_theta, 0, 1, 0);
+       }
+
+       g3d_light_pos(0, -10, 10, 20);
+
+       zsort(&torus);
+
+       g3d_mtl_diffuse(0.4, 0.7, 1.0);
+       g3d_set_texture(tex.width, tex.height, tex.pixels);
+
+       draw_mesh(&torus);
+
+       /*draw_mesh(&cube);*/
+       swap_buffers(fb_pixels);
+}
+
+static void draw_debug(void)
+{
+       update();
+
        memset(lowres_pixels, 0, lowres_width * lowres_height * 2);
 
        g3d_matrix_mode(G3D_MODELVIEW);
        g3d_load_identity();
-       g3d_translate(0, 0, -3);
-       g3d_rotate(phi, 1, 0, 0);
-       g3d_rotate(theta, 0, 1, 0);
+       g3d_translate(0, 0, -cam_dist);
+       g3d_rotate(cam_phi, 1, 0, 0);
+       g3d_rotate(cam_theta, 0, 1, 0);
 
        g3d_framebuffer(lowres_width, lowres_height, lowres_pixels);
        /*zsort(&torus);*/
@@ -237,6 +296,8 @@ static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int
        nfaces = usub * vsub;
        mesh->icount = nfaces * 4;
 
+       printf("generating torus with %d faces (%d vertices)\n", nfaces, mesh->vcount);
+
        if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
                return -1;
        }
@@ -347,3 +408,26 @@ static void draw_lowres_raster(void)
                dptr += fb_width * LOWRES_SCALE - fb_width;
        }
 }
+
+static int gen_texture(struct pimage *img, int xsz, int ysz)
+{
+       int i, j;
+       uint16_t *pix;
+
+       if(!(img->pixels = malloc(xsz * ysz * sizeof *pix))) {
+               return -1;
+       }
+       pix = img->pixels;
+
+       for(i=0; i<ysz; i++) {
+               for(j=0; j<xsz; j++) {
+                       int val = i ^ j;
+
+                       *pix++ = PACK_RGB16(val, val, val);
+               }
+       }
+
+       img->width = xsz;
+       img->height = ysz;
+       return 0;
+}