rayzor

view src/modeller.cc @ 15:be616b58df99

continued the renderer slightly
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 13 Apr 2014 09:54:36 +0300
parents a9a948809c6f
children 79609d482762
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "modeller.h"
5 #include "min3d.h"
6 #include "rayzor.h"
7 #include "scene.h"
8 #include "keyb.h"
9 #include "scrman.h"
10 #include "logger.h"
12 struct ModellerImpl {
13 int mx, my;
14 float cam_theta, cam_phi, cam_dist;
15 Camera *viewport_cam;
17 struct m3d_image rbuf;
19 bool bnstate[8];
20 int prev_x, prev_y;
21 };
23 static void draw_grid(float size, float spacing);
25 Modeller::Modeller()
26 {
27 set_name("modeller");
28 }
30 Modeller::~Modeller()
31 {
32 shutdown();
33 }
35 bool Modeller::init()
36 {
37 mod = new ModellerImpl;
38 memset(mod, 0, sizeof *mod);
40 mod->viewport_cam = new Camera;
42 mod->cam_phi = 25;
43 mod->cam_dist = 5;
45 m3d_init();
46 mod->rbuf.pixels = fb_pixels;
47 mod->rbuf.xsz = fb_width;
48 mod->rbuf.ysz = fb_height;
49 m3d_set_buffers(&mod->rbuf, 0);
51 m3d_matrix_mode(M3D_PROJECTION);
52 m3d_load_identity();
53 m3d_perspective(50.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
55 m3d_enable(M3D_CULL_FACE);
56 return true;
57 }
59 void Modeller::shutdown()
60 {
61 if(mod) {
62 m3d_shutdown();
64 delete mod;
65 mod = 0;
66 }
67 }
69 void Modeller::draw() const
70 {
71 m3d_clear(M3D_COLOR_BUFFER_BIT);
73 m3d_matrix_mode(M3D_MODELVIEW);
74 m3d_load_identity();
75 m3d_translate(0, 0, -mod->cam_dist);
76 m3d_rotate(mod->cam_phi, 1, 0, 0);
77 m3d_rotate(mod->cam_theta, 0, 1, 0);
79 draw_grid(10.0, 1.0);
81 scene->draw();
82 }
85 static void draw_grid(float size, float spacing)
86 {
87 int num_lines = size / spacing;
88 float dist = size / 2.0;
90 m3d_disable(M3D_LIGHTING);
92 m3d_begin(M3D_LINES);
93 m3d_color(0.4, 0.4, 0.4);
95 float x = -dist;
96 for(int i=0; i<=num_lines; i++) {
97 if(i != num_lines / 2) {
98 m3d_vertex(-dist, 0, x);
99 m3d_vertex(dist, 0, x);
100 m3d_vertex(x, 0, -dist);
101 m3d_vertex(x, 0, dist);
102 }
103 x += spacing;
104 }
105 m3d_end();
107 m3d_begin(M3D_LINES);
108 m3d_color(0.8, 0, 0);
109 m3d_vertex(-dist, 0, 0);
110 m3d_vertex(dist, 0, 0);
111 m3d_color(0.1, 0.3, 0.8);
112 m3d_vertex(0, 0, -dist);
113 m3d_vertex(0, 0, dist);
114 m3d_end();
115 }
118 void Modeller::handle_keyboard(int key, bool press)
119 {
120 if(press) {
121 switch(key) {
122 case 'q':
123 quit_app();
124 break;
126 case 27:
127 scene->clear_selection();
128 break;
130 case '\t':
131 {
132 int s = scene->get_selection();
133 if(s >= 0) {
134 s = (s + 1) % scene->get_node_count();
135 scene->clear_selection();
136 } else {
137 s = 0;
138 }
139 scene->select(s);
140 }
141 break;
143 case 'r':
144 case 'p':
145 if(kb_isdown(KB_ALT) || kb_isdown(KB_CTRL)) {
146 case KB_F5:
147 case KB_F6:
148 Screen *rs = get_screen("renderer");
149 if(rs) {
150 activate_screen(rs);
152 if(key == 'r' || key == KB_F5) {
153 // start a rendering, and make sure there is a camera
154 if(!scene->get_active_camera()) {
155 scene->set_active_camera(mod->viewport_cam);
156 }
157 Vector3 dir;
158 dir.x = sin(DEG2RAD(mod->cam_theta)) * cos(DEG2RAD(mod->cam_phi)) * mod->cam_dist;
159 dir.y = sin(DEG2RAD(mod->cam_phi));
160 dir.z = cos(DEG2RAD(mod->cam_theta)) * cos(DEG2RAD(mod->cam_phi)) * mod->cam_dist;
161 mod->viewport_cam->set_position(dir);
162 mod->viewport_cam->set_target(Vector3(0, 0, 0));
164 rs->message(message_atom("start"));
165 }
166 } else {
167 printlog("failed to find renderer screen!\n");
168 }
169 }
170 break;
172 default:
173 break;
174 }
175 }
176 }
178 void Modeller::handle_mbutton(int bn, bool press, int x, int y)
179 {
180 mod->bnstate[bn] = press;
181 mod->prev_x = x;
182 mod->prev_y = y;
183 }
185 void Modeller::handle_mmotion(int x, int y)
186 {
187 int dx = x - mod->prev_x;
188 int dy = y - mod->prev_y;
189 mod->prev_x = x;
190 mod->prev_y = y;
192 if(kb_isdown(KB_ALT) || kb_isdown(KB_CTRL)) {
193 if(mod->bnstate[0]) {
194 mod->cam_theta += dx * 0.5;
195 mod->cam_phi += dy * 0.5;
197 if(mod->cam_phi < -90) mod->cam_phi = -90;
198 if(mod->cam_phi > 90) mod->cam_phi = 90;
199 }
200 if(mod->bnstate[1]) {
201 mod->cam_dist += dy * 0.1;
202 if(mod->cam_dist < 0) mod->cam_dist = 0;
203 }
204 }
205 }