4da85c6f08183e100885ec4d44ebb591f34fe6df
[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_inner, tex_outer;
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         if(!(tex_inner.pixels = img_load_pixels("data/crate.jpg", &tex_inner.width,
41                                         &tex_inner.height, IMG_FMT_RGB24))) {
42                 fprintf(stderr, "infcubes: failed to load crate texture\n");
43                 return -1;
44         }
45         convimg_rgb24_rgb16(tex_inner.pixels, (unsigned char*)tex_inner.pixels, tex_inner.width, tex_inner.height);
46
47         if(!(tex_outer.pixels = img_load_pixels("data/steelfrm.jpg", &tex_outer.width,
48                                         &tex_outer.height, IMG_FMT_RGB24))) {
49                 fprintf(stderr, "infcubes: failed to load ornamental texture\n");
50                 return -1;
51         }
52         convimg_rgb24_rgb16(tex_outer.pixels, (unsigned char*)tex_outer.pixels, tex_outer.width, tex_outer.height);
53
54         if(gen_cube_mesh(&mesh_cube, 1.0f, 3) == -1) {
55                 return -1;
56         }
57         return 0;
58 }
59
60 static void destroy(void)
61 {
62         img_free_pixels(tex_inner.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
76 static void update(void)
77 {
78         mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
79 }
80
81 static void draw(void)
82 {
83         update();
84
85         g3d_matrix_mode(G3D_MODELVIEW);
86         g3d_load_identity();
87         g3d_translate(0, 0, -cam_dist);
88         g3d_rotate(cam_phi, 1, 0, 0);
89         g3d_rotate(cam_theta, 0, 1, 0);
90         if(opt.sball) {
91                 g3d_mult_matrix(sball_matrix);
92         }
93
94         memset(fb_pixels, 0, fb_width * fb_height * 2);
95
96         g3d_polygon_mode(G3D_TEX);
97
98         g3d_push_matrix();
99         g3d_scale(-6, -6, -6);
100         g3d_set_texture(tex_outer.width, tex_outer.height, tex_outer.pixels);
101         draw_mesh(&mesh_cube);
102         g3d_pop_matrix();
103
104         g3d_set_texture(tex_inner.width, tex_inner.height, tex_inner.pixels);
105         draw_mesh(&mesh_cube);
106
107         swap_buffers(fb_pixels);
108 }