14edf1c2b35288ce9285f2c00da474c9d02bf14b
[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 struct screen *rtxonoff_screen(void)
37 {
38         return &scr;
39 }
40
41 static int init(void)
42 {
43         int i;
44
45         for(i=0; i<sizeof car_fname / sizeof car_fname[0]; i++) {
46                 if(cartex_fname[i]) {
47                         if(!(tex_car[i].pixels = img_load_pixels(cartex_fname[i],
48                                                         &tex_car[i].width, &tex_car[i].height, IMG_FMT_RGB24))) {
49                                 fprintf(stderr, "failed to load car texture: %s\n", cartex_fname[i]);
50                                 return -1;
51                         }
52                         convimg_rgb24_rgb16(tex_car[i].pixels, (unsigned char*)tex_car[i].pixels,
53                                         tex_car[i].width, tex_car[i].height);
54                 }
55                 if(car_fname[i]) {
56                         if(load_mesh(&mesh_car[i], car_fname[i]) == -1) {
57                                 return -1;
58                         }
59                 }
60         }
61         return 0;
62 }
63
64 static void destroy(void)
65 {
66         int i;
67
68         for(i=0; i<2; i++) {
69                 free(mesh_car[i].varr);
70                 free(mesh_car[i].iarr);
71         }
72 }
73
74 static void start(long trans_time)
75 {
76         g3d_matrix_mode(G3D_PROJECTION);
77         g3d_load_identity();
78         g3d_perspective(60.0, 1.3333333, 0.5, 100.0);
79
80         g3d_enable(G3D_CULL_FACE);
81         g3d_enable(G3D_LIGHTING);
82         g3d_enable(G3D_LIGHT0);
83 }
84
85 static void update(void)
86 {
87         mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
88 }
89
90 static void draw(void)
91 {
92         int i;
93         static float vdir[3];
94         float t = (float)time_msec / 16.0f;
95
96         update();
97
98         memset(fb_pixels, 0, fb_width * fb_height * 2);
99
100         g3d_matrix_mode(G3D_MODELVIEW);
101         g3d_load_identity();
102         g3d_translate(0, 0, -cam_dist);
103         g3d_rotate(cam_phi, 1, 0, 0);
104         g3d_rotate(cam_theta, 0, 1, 0);
105
106         g3d_polygon_mode(G3D_TEX_GOURAUD);
107
108         for(i=0; i<sizeof mesh_car / sizeof mesh_car[0]; i++) {
109                 if(mesh_car[i].varr) {
110                         g3d_set_texture(tex_car[i].width, tex_car[i].height, tex_car[i].pixels);
111
112                         zsort_mesh(&mesh_car[i]);
113                         draw_mesh(&mesh_car[i]);
114                 }
115         }
116
117         swap_buffers(fb_pixels);
118 }
119
120 static void keypress(int key)
121 {
122 }