writing obj loader
[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         /*
55         if(gen_cube_mesh(&mesh_cube, 1.0f, 3) == -1) {
56                 return -1;
57         }
58         */
59         if(load_mesh(&mesh_cube, "data/bevelbox.obj") == -1) {
60                 return -1;
61         }
62         return 0;
63 }
64
65 static void destroy(void)
66 {
67         img_free_pixels(tex_inner.pixels);
68 }
69
70 static void start(long trans_time)
71 {
72         g3d_matrix_mode(G3D_PROJECTION);
73         g3d_load_identity();
74         g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
75
76         g3d_enable(G3D_CULL_FACE);
77         g3d_disable(G3D_LIGHTING);
78         g3d_enable(G3D_LIGHT0);
79 }
80
81 static void update(void)
82 {
83         mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
84 }
85
86 static void draw(void)
87 {
88         update();
89
90         g3d_matrix_mode(G3D_MODELVIEW);
91         g3d_load_identity();
92         g3d_translate(0, 0, -cam_dist);
93         g3d_rotate(cam_phi, 1, 0, 0);
94         g3d_rotate(cam_theta, 0, 1, 0);
95         if(opt.sball) {
96                 g3d_mult_matrix(sball_matrix);
97         }
98
99         memset(fb_pixels, 0, fb_width * fb_height * 2);
100
101         g3d_polygon_mode(G3D_TEX);
102
103         g3d_push_matrix();
104         g3d_scale(-6, -6, -6);
105         g3d_set_texture(tex_outer.width, tex_outer.height, tex_outer.pixels);
106         draw_mesh(&mesh_cube);
107         g3d_pop_matrix();
108
109         g3d_set_texture(tex_inner.width, tex_inner.height, tex_inner.pixels);
110         draw_mesh(&mesh_cube);
111
112         swap_buffers(fb_pixels);
113 }