- added mesh operations: apply_mesh_xform, append_mesh, indexify_mesh (untested)...
[dosdemo] / src / infcubes.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include "demo.h"
5 #include "3dgfx.h"
6 #include "screen.h"
7 #include "cfgopt.h"
8 #include "polyfill.h"
9 #include "imago2.h"
10 #include "gfxutil.h"
11 #include "mesh.h"
12
13 static int init(void);
14 static void destroy(void);
15 static void start(long trans_time);
16 static void draw(void);
17 static void draw_cube(float sz);
18
19 static struct screen scr = {
20         "infcubes",
21         init,
22         destroy,
23         start, 0,
24         draw
25 };
26
27 static float cam_theta = -29, cam_phi = 35;
28 static float cam_dist = 5;
29 static struct pimage tex_crate;
30 static struct g3d_mesh mesh_cube;
31
32 struct screen *infcubes_screen(void)
33 {
34         return &scr;
35 }
36
37
38 static int init(void)
39 {
40         int i, npixels;
41         unsigned char *src;
42         uint16_t *dst;
43
44         if(!(tex_crate.pixels = img_load_pixels("data/crate.jpg", &tex_crate.width,
45                                         &tex_crate.height, IMG_FMT_RGB24))) {
46                 fprintf(stderr, "infcubes: failed to load crate texture\n");
47                 return -1;
48         }
49
50         npixels = tex_crate.width * tex_crate.height;
51         src = (unsigned char*)tex_crate.pixels;
52         dst = tex_crate.pixels;
53         for(i=0; i<npixels; i++) {
54                 int r = *src++;
55                 int g = *src++;
56                 int b = *src++;
57                 *dst++ = PACK_RGB16(r, g, b);
58         }
59
60         if(gen_cube_mesh(&mesh_cube, 1.0f, 3) == -1) {
61                 return -1;
62         }
63         return 0;
64 }
65
66 static void destroy(void)
67 {
68         img_free_pixels(tex_crate.pixels);
69 }
70
71 static void start(long trans_time)
72 {
73         g3d_matrix_mode(G3D_PROJECTION);
74         g3d_load_identity();
75         g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
76
77         g3d_enable(G3D_CULL_FACE);
78         g3d_disable(G3D_LIGHTING);
79         g3d_enable(G3D_LIGHT0);
80
81         g3d_set_texture(tex_crate.width, tex_crate.height, tex_crate.pixels);
82 }
83
84 static void update(void)
85 {
86         mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
87 }
88
89 static void draw(void)
90 {
91         update();
92
93         g3d_matrix_mode(G3D_MODELVIEW);
94         g3d_load_identity();
95         g3d_translate(0, 0, -cam_dist);
96         g3d_rotate(cam_phi, 1, 0, 0);
97         g3d_rotate(cam_theta, 0, 1, 0);
98         if(opt.sball) {
99                 g3d_mult_matrix(sball_matrix);
100         }
101
102         memset(fb_pixels, 0, fb_width * fb_height * 2);
103
104         g3d_polygon_mode(G3D_FLAT);
105         draw_cube(-6);
106
107         g3d_polygon_mode(G3D_TEX);
108         /*draw_cube(1);*/
109         draw_mesh(&mesh_cube);
110
111         swap_buffers(fb_pixels);
112 }
113
114 static void draw_cube(float sz)
115 {
116         float hsz = sz * 0.5f;
117         g3d_begin(G3D_QUADS);
118         g3d_color3b(255, 0, 0);
119         g3d_normal(0, 0, 1);
120         g3d_texcoord(0, 0); g3d_vertex(-hsz, -hsz, hsz);
121         g3d_texcoord(1, 0); g3d_vertex(hsz, -hsz, hsz);
122         g3d_texcoord(1, 1); g3d_vertex(hsz, hsz, hsz);
123         g3d_texcoord(0, 1); g3d_vertex(-hsz, hsz, hsz);
124         g3d_color3b(0, 255, 0);
125         g3d_normal(1, 0, 0);
126         g3d_texcoord(0, 0); g3d_vertex(hsz, -hsz, hsz);
127         g3d_texcoord(1, 0); g3d_vertex(hsz, -hsz, -hsz);
128         g3d_texcoord(1, 1); g3d_vertex(hsz, hsz, -hsz);
129         g3d_texcoord(0, 1); g3d_vertex(hsz, hsz, hsz);
130         g3d_color3b(0, 0, 255);
131         g3d_normal(0, 0, -1);
132         g3d_texcoord(0, 0); g3d_vertex(hsz, -hsz, -hsz);
133         g3d_texcoord(1, 0); g3d_vertex(-hsz, -hsz, -hsz);
134         g3d_texcoord(1, 1); g3d_vertex(-hsz, hsz, -hsz);
135         g3d_texcoord(0, 1); g3d_vertex(hsz, hsz, -hsz);
136         g3d_color3b(255, 0, 255);
137         g3d_normal(-1, 0, 0);
138         g3d_texcoord(0, 0); g3d_vertex(-hsz, -hsz, -hsz);
139         g3d_texcoord(1, 0); g3d_vertex(-hsz, -hsz, hsz);
140         g3d_texcoord(1, 1); g3d_vertex(-hsz, hsz, hsz);
141         g3d_texcoord(0, 1); g3d_vertex(-hsz, hsz, -hsz);
142         g3d_color3b(255, 255, 0);
143         g3d_normal(0, 1, 0);
144         g3d_texcoord(0, 0); g3d_vertex(-hsz, hsz, hsz);
145         g3d_texcoord(1, 0); g3d_vertex(hsz, hsz, hsz);
146         g3d_texcoord(1, 1); g3d_vertex(hsz, hsz, -hsz);
147         g3d_texcoord(0, 1); g3d_vertex(-hsz, hsz, -hsz);
148         g3d_color3b(0, 255, 255);
149         g3d_normal(0, -1, 0);
150         g3d_texcoord(0, 0); g3d_vertex(hsz, -hsz, hsz);
151         g3d_texcoord(1, 0); g3d_vertex(-hsz, -hsz, hsz);
152         g3d_texcoord(1, 1); g3d_vertex(-hsz, -hsz, -hsz);
153         g3d_texcoord(0, 1); g3d_vertex(hsz, -hsz, -hsz);
154         g3d_end();
155 }