rayzor
view src/modeller.cc @ 11:fe94d9e986ae
optimized 16bpp swap_buffers
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 10 Apr 2014 08:42:33 +0300 |
parents | |
children | d94a69933a71 |
line source
1 #include <string.h>
2 #include <assert.h>
3 #include "modeller.h"
4 #include "min3d.h"
5 #include "rayzor.h"
6 #include "scene.h"
8 struct ModellerImpl {
9 int mx, my;
10 float cam_theta, cam_phi, cam_dist;
12 struct m3d_image rbuf;
14 bool bnstate[8];
15 int prev_x, prev_y;
16 };
18 static void draw_grid(float size, float spacing);
20 Modeller::Modeller()
21 {
22 set_name("modeller");
23 }
25 Modeller::~Modeller()
26 {
27 shutdown();
28 }
30 bool Modeller::init()
31 {
32 mod = new ModellerImpl;
33 memset(mod, 0, sizeof *mod);
35 mod->cam_phi = 25;
36 mod->cam_dist = 5;
38 m3d_init();
39 mod->rbuf.pixels = fb_pixels;
40 mod->rbuf.xsz = fb_width;
41 mod->rbuf.ysz = fb_height;
42 m3d_set_buffers(&mod->rbuf, 0);
44 m3d_matrix_mode(M3D_PROJECTION);
45 m3d_load_identity();
46 m3d_perspective(50.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
48 m3d_enable(M3D_CULL_FACE);
49 return true;
50 }
52 void Modeller::shutdown()
53 {
54 if(mod) {
55 m3d_shutdown();
57 delete mod;
58 mod = 0;
59 }
60 }
62 void Modeller::draw() const
63 {
64 m3d_clear(M3D_COLOR_BUFFER_BIT);
66 m3d_matrix_mode(M3D_MODELVIEW);
67 m3d_load_identity();
68 m3d_translate(0, 0, -mod->cam_dist);
69 m3d_rotate(mod->cam_phi, 1, 0, 0);
70 m3d_rotate(mod->cam_theta, 0, 1, 0);
72 draw_grid(10.0, 1.0);
74 scene->draw();
75 }
78 static void draw_grid(float size, float spacing)
79 {
80 int num_lines = size / spacing;
81 float dist = size / 2.0;
83 m3d_disable(M3D_LIGHTING);
85 m3d_begin(M3D_LINES);
86 m3d_color(0.4, 0.4, 0.4);
88 float x = -dist;
89 for(int i=0; i<=num_lines; i++) {
90 if(i != num_lines / 2) {
91 m3d_vertex(-dist, 0, x);
92 m3d_vertex(dist, 0, x);
93 m3d_vertex(x, 0, -dist);
94 m3d_vertex(x, 0, dist);
95 }
96 x += spacing;
97 }
98 m3d_end();
100 m3d_begin(M3D_LINES);
101 m3d_color(1.0, 0, 0);
102 m3d_vertex(-dist, 0, 0);
103 m3d_vertex(dist, 0, 0);
104 m3d_color(0, 1.0, 0);
105 m3d_vertex(0, 0, -dist);
106 m3d_vertex(0, 0, dist);
107 m3d_end();
108 }
111 void Modeller::handle_keyboard(int key, bool press)
112 {
113 if(press) {
114 switch(key) {
115 case 27:
116 quit_app();
117 break;
119 default:
120 break;
121 }
122 }
123 }
125 void Modeller::handle_mbutton(int bn, bool press, int x, int y)
126 {
127 mod->bnstate[bn] = press;
128 mod->prev_x = x;
129 mod->prev_y = y;
130 }
132 void Modeller::handle_mmotion(int x, int y)
133 {
134 int dx = x - mod->prev_x;
135 int dy = y - mod->prev_y;
136 mod->prev_x = x;
137 mod->prev_y = y;
139 if(mod->bnstate[0]) {
140 mod->cam_theta += dx * 0.5;
141 mod->cam_phi += dy * 0.5;
143 if(mod->cam_phi < -90) mod->cam_phi = -90;
144 if(mod->cam_phi > 90) mod->cam_phi = 90;
145 }
146 if(mod->bnstate[1]) {
147 mod->cam_dist += dy * 0.1;
148 if(mod->cam_dist < 0) mod->cam_dist = 0;
149 }
150 }