initial import
[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
18 static struct screen scr = {
19         "rtxonoff",
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 = 6;
28
29 static const char *car_fname[2] = {"data/ldiablo.obj", 0};
30 static const char *cartex_fname[2] = {"data/ldiablo.png", 0};
31 static struct g3d_mesh mesh_car[2];
32 static struct pimage tex_car[2];
33
34 struct screen *rtxonoff_screen(void)
35 {
36         return &scr;
37 }
38
39 static int init(void)
40 {
41         int i;
42
43         for(i=0; i<sizeof car_fname / sizeof car_fname[0]; i++) {
44                 if(cartex_fname[i]) {
45                         if(!(tex_car[i].pixels = img_load_pixels(cartex_fname[i],
46                                                         &tex_car[i].width, &tex_car[i].height, IMG_FMT_RGB24))) {
47                                 fprintf(stderr, "failed to load car texture: %s\n", cartex_fname[i]);
48                         }
49                         convimg_rgb24_rgb16(tex_car[i].pixels, (unsigned char*)tex_car[i].pixels,
50                                         tex_car[i].width, tex_car[i].height);
51                 }
52                 if(car_fname[i]) {
53                         if(load_mesh(&mesh_car[i], car_fname[i]) == -1) {
54                                 return -1;
55                         }
56                 }
57         }
58         return 0;
59 }
60
61 static void destroy(void)
62 {
63 }
64
65 static void start(long trans_time)
66 {
67         g3d_matrix_mode(G3D_PROJECTION);
68         g3d_load_identity();
69         g3d_perspective(60.0, 1.3333333, 0.5, 100.0);
70
71         g3d_enable(G3D_CULL_FACE);
72         g3d_enable(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         int i;
84         float t = (float)time_msec / 16.0f;
85
86         update();
87
88         memset(fb_pixels, 0, fb_width * fb_height * 2);
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
96         g3d_polygon_mode(G3D_TEX_GOURAUD);
97
98         for(i=0; i<sizeof mesh_car / sizeof mesh_car[0]; i++) {
99                 if(mesh_car[i].varr) {
100                         g3d_set_texture(tex_car[i].width, tex_car[i].height, tex_car[i].pixels);
101
102                         zsort_mesh(&mesh_car[i]);
103                         draw_mesh(&mesh_car[i]);
104                 }
105         }
106
107         swap_buffers(fb_pixels);
108 }