rayzor

view src/modeller.cc @ 12:d94a69933a71

lots of stuff, can't remember
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 12 Apr 2014 23:28:24 +0300
parents 70e332156d02
children a9a948809c6f
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"
10 struct ModellerImpl {
11 int mx, my;
12 float cam_theta, cam_phi, cam_dist;
14 struct m3d_image rbuf;
16 bool bnstate[8];
17 int prev_x, prev_y;
18 };
20 static void draw_grid(float size, float spacing);
22 Modeller::Modeller()
23 {
24 set_name("modeller");
25 }
27 Modeller::~Modeller()
28 {
29 shutdown();
30 }
32 bool Modeller::init()
33 {
34 mod = new ModellerImpl;
35 memset(mod, 0, sizeof *mod);
37 mod->cam_phi = 25;
38 mod->cam_dist = 5;
40 m3d_init();
41 mod->rbuf.pixels = fb_pixels;
42 mod->rbuf.xsz = fb_width;
43 mod->rbuf.ysz = fb_height;
44 m3d_set_buffers(&mod->rbuf, 0);
46 m3d_matrix_mode(M3D_PROJECTION);
47 m3d_load_identity();
48 m3d_perspective(50.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
50 m3d_enable(M3D_CULL_FACE);
51 return true;
52 }
54 void Modeller::shutdown()
55 {
56 if(mod) {
57 m3d_shutdown();
59 delete mod;
60 mod = 0;
61 }
62 }
64 void Modeller::draw() const
65 {
66 m3d_clear(M3D_COLOR_BUFFER_BIT);
68 m3d_matrix_mode(M3D_MODELVIEW);
69 m3d_load_identity();
70 m3d_translate(0, 0, -mod->cam_dist);
71 m3d_rotate(mod->cam_phi, 1, 0, 0);
72 m3d_rotate(mod->cam_theta, 0, 1, 0);
74 draw_grid(10.0, 1.0);
76 scene->draw();
77 }
80 static void draw_grid(float size, float spacing)
81 {
82 int num_lines = size / spacing;
83 float dist = size / 2.0;
85 m3d_disable(M3D_LIGHTING);
87 m3d_begin(M3D_LINES);
88 m3d_color(0.4, 0.4, 0.4);
90 float x = -dist;
91 for(int i=0; i<=num_lines; i++) {
92 if(i != num_lines / 2) {
93 m3d_vertex(-dist, 0, x);
94 m3d_vertex(dist, 0, x);
95 m3d_vertex(x, 0, -dist);
96 m3d_vertex(x, 0, dist);
97 }
98 x += spacing;
99 }
100 m3d_end();
102 m3d_begin(M3D_LINES);
103 m3d_color(1.0, 0, 0);
104 m3d_vertex(-dist, 0, 0);
105 m3d_vertex(dist, 0, 0);
106 m3d_color(0, 1.0, 0);
107 m3d_vertex(0, 0, -dist);
108 m3d_vertex(0, 0, dist);
109 m3d_end();
110 }
113 void Modeller::handle_keyboard(int key, bool press)
114 {
115 if(press) {
116 switch(key) {
117 case 'q':
118 quit_app();
119 break;
121 case 27:
122 scene->clear_selection();
123 break;
125 case '\t':
126 {
127 int s = scene->get_selection();
128 if(s >= 0) {
129 s = (s + 1) % scene->get_node_count();
130 scene->clear_selection();
131 } else {
132 s = 0;
133 }
134 scene->select(s);
135 }
136 break;
138 default:
139 break;
140 }
141 }
142 }
144 void Modeller::handle_mbutton(int bn, bool press, int x, int y)
145 {
146 mod->bnstate[bn] = press;
147 mod->prev_x = x;
148 mod->prev_y = y;
149 }
151 void Modeller::handle_mmotion(int x, int y)
152 {
153 int dx = x - mod->prev_x;
154 int dy = y - mod->prev_y;
155 mod->prev_x = x;
156 mod->prev_y = y;
158 if(kb_isdown(KB_ALT) || kb_isdown(KB_CTRL)) {
159 if(mod->bnstate[0]) {
160 mod->cam_theta += dx * 0.5;
161 mod->cam_phi += dy * 0.5;
163 if(mod->cam_phi < -90) mod->cam_phi = -90;
164 if(mod->cam_phi > 90) mod->cam_phi = 90;
165 }
166 if(mod->bnstate[1]) {
167 mod->cam_dist += dy * 0.1;
168 if(mod->cam_dist < 0) mod->cam_dist = 0;
169 }
170 }
171 }