removed redundant immediate mode draw_cube from infcubes
[dosdemo] / src / infcubes.c
index 5fe7df9..4da85c6 100644 (file)
@@ -1,15 +1,20 @@
 #include <stdio.h>
+#include <string.h>
 #include <math.h>
 #include "demo.h"
 #include "3dgfx.h"
 #include "screen.h"
 #include "cfgopt.h"
+#include "polyfill.h"
+#include "imago2.h"
+#include "gfxutil.h"
+#include "mesh.h"
 
 static int init(void);
 static void destroy(void);
 static void start(long trans_time);
 static void draw(void);
-static void draw_cube(void);
+static void draw_cube(float sz);
 
 static struct screen scr = {
        "infcubes",
@@ -19,8 +24,10 @@ static struct screen scr = {
        draw
 };
 
-static float cam_theta, cam_phi;
+static float cam_theta = -29, cam_phi = 35;
 static float cam_dist = 5;
+static struct pimage tex_inner, tex_outer;
+static struct g3d_mesh mesh_cube;
 
 struct screen *infcubes_screen(void)
 {
@@ -30,11 +37,29 @@ struct screen *infcubes_screen(void)
 
 static int init(void)
 {
+       if(!(tex_inner.pixels = img_load_pixels("data/crate.jpg", &tex_inner.width,
+                                       &tex_inner.height, IMG_FMT_RGB24))) {
+               fprintf(stderr, "infcubes: failed to load crate texture\n");
+               return -1;
+       }
+       convimg_rgb24_rgb16(tex_inner.pixels, (unsigned char*)tex_inner.pixels, tex_inner.width, tex_inner.height);
+
+       if(!(tex_outer.pixels = img_load_pixels("data/steelfrm.jpg", &tex_outer.width,
+                                       &tex_outer.height, IMG_FMT_RGB24))) {
+               fprintf(stderr, "infcubes: failed to load ornamental texture\n");
+               return -1;
+       }
+       convimg_rgb24_rgb16(tex_outer.pixels, (unsigned char*)tex_outer.pixels, tex_outer.width, tex_outer.height);
+
+       if(gen_cube_mesh(&mesh_cube, 1.0f, 3) == -1) {
+               return -1;
+       }
        return 0;
 }
 
 static void destroy(void)
 {
+       img_free_pixels(tex_inner.pixels);
 }
 
 static void start(long trans_time)
@@ -43,7 +68,8 @@ static void start(long trans_time)
        g3d_load_identity();
        g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
 
-       g3d_enable(G3D_LIGHTING);
+       g3d_enable(G3D_CULL_FACE);
+       g3d_disable(G3D_LIGHTING);
        g3d_enable(G3D_LIGHT0);
 }
 
@@ -67,43 +93,16 @@ static void draw(void)
 
        memset(fb_pixels, 0, fb_width * fb_height * 2);
 
-       draw_cube();
+       g3d_polygon_mode(G3D_TEX);
 
-       swap_buffers(fb_pixels);
-}
+       g3d_push_matrix();
+       g3d_scale(-6, -6, -6);
+       g3d_set_texture(tex_outer.width, tex_outer.height, tex_outer.pixels);
+       draw_mesh(&mesh_cube);
+       g3d_pop_matrix();
 
-static void draw_cube(void)
-{
-       g3d_begin(G3D_QUADS);
-       g3d_normal(0, 0, 1);
-       g3d_vertex(-1, -1, 1);
-       g3d_vertex(1, -1, 1);
-       g3d_vertex(1, 1, 1);
-       g3d_vertex(-1, 1, 1);
-       g3d_normal(1, 0, 0);
-       g3d_vertex(1, -1, 1);
-       g3d_vertex(1, -1, -1);
-       g3d_vertex(1, 1, -1);
-       g3d_vertex(1, 1, 1);
-       g3d_normal(0, 0, -1);
-       g3d_vertex(1, -1, -1);
-       g3d_vertex(-1, -1, -1);
-       g3d_vertex(-1, 1, -1);
-       g3d_vertex(1, 1, -1);
-       g3d_normal(-1, 0, 0);
-       g3d_vertex(-1, -1, -1);
-       g3d_vertex(-1, -1, 1);
-       g3d_vertex(-1, 1, 1);
-       g3d_vertex(-1, 1, -1);
-       g3d_normal(0, 1, 0);
-       g3d_vertex(-1, 1, 1);
-       g3d_vertex(1, 1, 1);
-       g3d_vertex(1, 1, -1);
-       g3d_vertex(-1, 1, -1);
-       g3d_normal(0, -1, 0);
-       g3d_vertex(1, -1, 1);
-       g3d_vertex(-1, -1, 1);
-       g3d_vertex(-1, -1, -1);
-       g3d_vertex(1, -1, -1);
-       g3d_end();
+       g3d_set_texture(tex_inner.width, tex_inner.height, tex_inner.pixels);
+       draw_mesh(&mesh_cube);
+
+       swap_buffers(fb_pixels);
 }