reflection
[dosrtxon] / src / parts / rtxonoff.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.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 keypress(int key);
18
19 static struct screen scr = {
20         "rtxonoff",
21         init,
22         destroy,
23         start, 0,
24         draw,
25         keypress
26 };
27
28 static float cam_theta = -29, cam_phi = 35;
29 static float cam_dist = 10;
30
31 static const char *car_fname[2] = {"data/ldiablo.obj", 0};
32 static const char *cartex_fname[2] = {"data/ldiablo.png", 0};
33 static struct g3d_mesh mesh_car[2];
34 static struct pimage tex_car[2];
35
36 static int shading = G3D_TEX_GOURAUD;
37 static int do_clip = 1;
38
39
40 struct screen *rtxonoff_screen(void)
41 {
42         return &scr;
43 }
44
45 static int init(void)
46 {
47         int i;
48
49         for(i=0; i<sizeof car_fname / sizeof car_fname[0]; i++) {
50                 if(cartex_fname[i]) {
51                         if(!(tex_car[i].pixels = img_load_pixels(cartex_fname[i],
52                                                         &tex_car[i].width, &tex_car[i].height, IMG_FMT_RGB24))) {
53                                 fprintf(stderr, "failed to load car texture: %s\n", cartex_fname[i]);
54                                 return -1;
55                         }
56                         convimg_rgb24_rgb16(tex_car[i].pixels, (unsigned char*)tex_car[i].pixels,
57                                         tex_car[i].width, tex_car[i].height);
58                 }
59                 if(car_fname[i]) {
60                         if(load_mesh(&mesh_car[i], car_fname[i]) == -1) {
61                                 return -1;
62                         }
63                 }
64         }
65         return 0;
66 }
67
68 static void destroy(void)
69 {
70         int i;
71
72         for(i=0; i<2; i++) {
73                 free(mesh_car[i].varr);
74                 free(mesh_car[i].iarr);
75         }
76 }
77
78 static void start(long trans_time)
79 {
80         g3d_matrix_mode(G3D_PROJECTION);
81         g3d_load_identity();
82         g3d_perspective(60.0, 1.3333333, 0.5, 100.0);
83
84         g3d_enable(G3D_CULL_FACE);
85         g3d_enable(G3D_LIGHTING);
86         g3d_enable(G3D_LIGHT0);
87
88         g3d_polygon_mode(shading);
89 }
90
91 static void update(void)
92 {
93         mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
94 }
95
96 static void draw(void)
97 {
98         int i;
99         static float vdir[3];
100         float t = (float)time_msec / 16.0f;
101
102         update();
103
104         memset(fb_pixels, 0, fb_width * fb_height * 2);
105
106         g3d_matrix_mode(G3D_MODELVIEW);
107         g3d_load_identity();
108         g3d_translate(0, 0, -cam_dist);
109         g3d_rotate(cam_phi, 1, 0, 0);
110         g3d_rotate(cam_theta, 0, 1, 0);
111
112         g3d_set_texture(tex_car[0].width, tex_car[0].height, tex_car[0].pixels);
113         zsort_mesh(&mesh_car[0]);
114
115         g3d_light_color(0, 0.3, 0.3, 0.3);
116         g3d_push_matrix();
117         g3d_scale(1, -1, 1);
118         g3d_front_face(G3D_CW);
119         draw_mesh(&mesh_car[0]);
120         g3d_front_face(G3D_CCW);
121         g3d_pop_matrix();
122
123         g3d_light_color(0, 1, 1, 1);
124         draw_mesh(&mesh_car[0]);
125
126         swap_buffers(fb_pixels);
127 }
128
129 static void keypress(int key)
130 {
131         static int lighting = 1;
132         static int clipping = 1;
133
134         switch(key) {
135         case ' ':
136                 shading = (shading + 1) % 5;
137                 g3d_polygon_mode(shading);
138                 break;
139
140         case 'l':
141                 lighting = !lighting;
142                 if(lighting) {
143                         g3d_enable(G3D_LIGHTING);
144                 } else {
145                         g3d_disable(G3D_LIGHTING);
146                 }
147                 break;
148
149         case 'c':
150                 clipping = !clipping;
151                 if(clipping) {
152                         g3d_enable(G3D_CLIP_FRUSTUM);
153                 } else {
154                         g3d_disable(G3D_CLIP_FRUSTUM);
155                 }
156                 break;
157         }
158 }