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