cybersun
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 14 Jul 2020 02:17:57 +0000 (05:17 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 14 Jul 2020 02:17:57 +0000 (05:17 +0300)
src/3dgfx/3dgfx.c
src/scr/cybersun.c

index ef569e6..dcd28c6 100644 (file)
@@ -503,6 +503,8 @@ void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
        g3d_draw_indexed(prim, varr, varr_size, 0, 0);
 }
 
+#define NEED_NORMALS   (st->opt & (G3D_LIGHTING | G3D_TEXTURE_GEN))
+
 void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
                const uint16_t *iarr, int iarr_size)
 {
@@ -516,8 +518,10 @@ void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
        tmpv = alloca(prim * 6 * sizeof *tmpv);
 
        /* calc the normal matrix */
-       memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
-       st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
+       if(NEED_NORMALS) {
+               memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
+               st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
+       }
 
        nfaces = (iarr ? iarr_size : varr_size) / prim;
 
@@ -528,14 +532,16 @@ void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
                        v[i] = iarr ? varr[*iarr++] : *varr++;
 
                        xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
-                       xform3_vec3(st->norm_mat, &v[i].nx);
 
-                       if(st->opt & G3D_LIGHTING) {
-                               shade(v + i);
-                       }
-                       if(st->opt & G3D_TEXTURE_GEN) {
-                               v[i].u = v[i].nx * 0.5 + 0.5;
-                               v[i].v = 0.5 - v[i].ny * 0.5;
+                       if(NEED_NORMALS) {
+                               xform3_vec3(st->norm_mat, &v[i].nx);
+                               if(st->opt & G3D_LIGHTING) {
+                                       shade(v + i);
+                               }
+                               if(st->opt & G3D_TEXTURE_GEN) {
+                                       v[i].u = v[i].nx * 0.5 + 0.5;
+                                       v[i].v = 0.5 - v[i].ny * 0.5;
+                               }
                        }
                        if(st->opt & G3D_TEXTURE_MAT) {
                                float *mat = st->mat[G3D_TEXTURE][st->mtop[G3D_TEXTURE]];
index bb6055a..7a247d2 100644 (file)
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <math.h>
 #include "demo.h"
 #include "3dgfx.h"
 #include "screen.h"
@@ -21,11 +22,11 @@ static struct screen scr = {
 };
 
 static float cam_theta = 0, cam_phi = 10;
-static float cam_dist = 6;
+static float cam_dist = 0;
 
 static struct g3d_mesh gmesh;
-#define GMESH_GRIDSZ   20
-#define GMESH_SIZE             50
+#define GMESH_GRIDSZ   25
+#define GMESH_SIZE             128
 static struct image gtex;
 
 struct screen *cybersun_screen(void)
@@ -68,15 +69,42 @@ static void start(long trans_time)
        g3d_clear_color(0, 0, 0);
 }
 
+static void update(void)
+{
+       int i, j;
+       float t = time_msec / 1000.0f;
+       struct g3d_vertex *vptr;
+
+       mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
+
+       /* update mesh */
+       vptr = gmesh.varr;
+       for(i=0; i<GMESH_GRIDSZ + 1; i++) {
+               for(j=0; j<GMESH_GRIDSZ + 1; j++) {
+                       float u = (float)j / GMESH_GRIDSZ - 0.5f;
+                       float v = (float)i / GMESH_GRIDSZ - 0.5f;
+                       float x = u * 32.0f;
+                       float y = v * 32.0f;
+                       float r = sqrt(x * x + y * y);
+
+                       vptr->z = sin(x * 0.5 + t) + cos(x * 0.8f) * 0.5f;
+                       vptr->z += cos(y * 0.5 + t);
+                       vptr->z += sin(r + t) * 0.5f;
+                       vptr->z *= r * 0.1f > 1.0f ? 1.0f : r * 0.1f;
+                       vptr++;
+               }
+       }
+}
+
 static void draw(void)
 {
        int i;
 
-       mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
+       update();
 
        g3d_matrix_mode(G3D_MODELVIEW);
        g3d_load_identity();
-       g3d_translate(0, 0, -cam_dist);
+       g3d_translate(0, -2, -cam_dist);
        g3d_rotate(cam_phi, 1, 0, 0);
        g3d_rotate(cam_theta, 0, 1, 0);
        if(opt.sball) {
@@ -87,12 +115,14 @@ static void draw(void)
 
        g3d_set_texture(gtex.width, gtex.height, gtex.pixels);
        g3d_enable(G3D_TEXTURE_2D);
+       g3d_enable(G3D_DEPTH_TEST);
 
        g3d_push_matrix();
        g3d_rotate(-90, 1, 0, 0);
        draw_mesh(&gmesh);
        g3d_pop_matrix();
 
+       g3d_disable(G3D_DEPTH_TEST);
        g3d_disable(G3D_TEXTURE_2D);
 
        swap_buffers(fb_pixels);