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