14 static int init(void);
15 static void destroy(void);
16 static void start(long trans_time);
17 static void draw(void);
18 static void keypress(int key);
20 static struct screen scr = {
29 static float cam_theta = -29, cam_phi = 35;
30 static float cam_dist = 10;
32 static int use_bsp = 1;
34 static const char *car_fname[2] = {"data/ldiablo.obj", 0};
35 static const char *cartex_fname[2] = {"data/ldiablo.png", 0};
36 static struct g3d_mesh mesh_car[2];
37 static struct pimage tex_car[2];
38 static struct bsptree bsp_car[2];
40 struct screen *rtxonoff_screen(void)
49 for(i=0; i<sizeof car_fname / sizeof car_fname[0]; 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]);
56 convimg_rgb24_rgb16(tex_car[i].pixels, (unsigned char*)tex_car[i].pixels,
57 tex_car[i].width, tex_car[i].height);
60 if(load_mesh(&mesh_car[i], car_fname[i]) == -1) {
64 init_bsp(&bsp_car[i]);
65 if(bsp_add_mesh(&bsp_car[i], &mesh_car[i]) == -1) {
66 fprintf(stderr, "failed to construct BSP tree %d\n", i);
69 bsp_build(&bsp_car[i]);
75 static void destroy(void)
80 free(mesh_car[i].varr);
81 free(mesh_car[i].iarr);
82 destroy_bsp(&bsp_car[i]);
86 static void start(long trans_time)
88 g3d_matrix_mode(G3D_PROJECTION);
90 g3d_perspective(60.0, 1.3333333, 0.5, 100.0);
92 g3d_enable(G3D_CULL_FACE);
93 g3d_enable(G3D_LIGHTING);
94 g3d_enable(G3D_LIGHT0);
97 static void update(void)
99 mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
102 static void draw(void)
105 static float vdir[3];
106 float t = (float)time_msec / 16.0f;
110 memset(fb_pixels, 0, fb_width * fb_height * 2);
112 g3d_matrix_mode(G3D_MODELVIEW);
114 g3d_translate(0, 0, -cam_dist);
115 g3d_rotate(cam_phi, 1, 0, 0);
116 g3d_rotate(cam_theta, 0, 1, 0);
119 const float *mat = g3d_get_matrix(G3D_MODELVIEW, 0);
120 /* transform (0, 0, -1) with transpose(mat3x3) */
126 g3d_polygon_mode(G3D_TEX_GOURAUD);
128 for(i=0; i<sizeof mesh_car / sizeof mesh_car[0]; i++) {
129 if(mesh_car[i].varr) {
130 g3d_set_texture(tex_car[i].width, tex_car[i].height, tex_car[i].pixels);
133 draw_bsp(&bsp_car[i], vdir[0], vdir[1], vdir[2]);
135 zsort_mesh(&mesh_car[i]);
136 draw_mesh(&mesh_car[i]);
141 swap_buffers(fb_pixels);
144 static void keypress(int key)
149 printf("drawing with %s\n", use_bsp ? "BSP tree" : "z-sorting");