rayzor
diff src/modeller.cc @ 9:70e332156d02
moving along
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 10 Apr 2014 02:31:31 +0300 |
parents | |
children | d94a69933a71 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/modeller.cc Thu Apr 10 02:31:31 2014 +0300 1.3 @@ -0,0 +1,150 @@ 1.4 +#include <string.h> 1.5 +#include <assert.h> 1.6 +#include "modeller.h" 1.7 +#include "min3d.h" 1.8 +#include "rayzor.h" 1.9 +#include "scene.h" 1.10 + 1.11 +struct ModellerImpl { 1.12 + int mx, my; 1.13 + float cam_theta, cam_phi, cam_dist; 1.14 + 1.15 + struct m3d_image rbuf; 1.16 + 1.17 + bool bnstate[8]; 1.18 + int prev_x, prev_y; 1.19 +}; 1.20 + 1.21 +static void draw_grid(float size, float spacing); 1.22 + 1.23 +Modeller::Modeller() 1.24 +{ 1.25 + set_name("modeller"); 1.26 +} 1.27 + 1.28 +Modeller::~Modeller() 1.29 +{ 1.30 + shutdown(); 1.31 +} 1.32 + 1.33 +bool Modeller::init() 1.34 +{ 1.35 + mod = new ModellerImpl; 1.36 + memset(mod, 0, sizeof *mod); 1.37 + 1.38 + mod->cam_phi = 25; 1.39 + mod->cam_dist = 5; 1.40 + 1.41 + m3d_init(); 1.42 + mod->rbuf.pixels = fb_pixels; 1.43 + mod->rbuf.xsz = fb_width; 1.44 + mod->rbuf.ysz = fb_height; 1.45 + m3d_set_buffers(&mod->rbuf, 0); 1.46 + 1.47 + m3d_matrix_mode(M3D_PROJECTION); 1.48 + m3d_load_identity(); 1.49 + m3d_perspective(50.0, (float)fb_width / (float)fb_height, 0.5, 500.0); 1.50 + 1.51 + m3d_enable(M3D_CULL_FACE); 1.52 + return true; 1.53 +} 1.54 + 1.55 +void Modeller::shutdown() 1.56 +{ 1.57 + if(mod) { 1.58 + m3d_shutdown(); 1.59 + 1.60 + delete mod; 1.61 + mod = 0; 1.62 + } 1.63 +} 1.64 + 1.65 +void Modeller::draw() const 1.66 +{ 1.67 + m3d_clear(M3D_COLOR_BUFFER_BIT); 1.68 + 1.69 + m3d_matrix_mode(M3D_MODELVIEW); 1.70 + m3d_load_identity(); 1.71 + m3d_translate(0, 0, -mod->cam_dist); 1.72 + m3d_rotate(mod->cam_phi, 1, 0, 0); 1.73 + m3d_rotate(mod->cam_theta, 0, 1, 0); 1.74 + 1.75 + draw_grid(10.0, 1.0); 1.76 + 1.77 + scene->draw(); 1.78 +} 1.79 + 1.80 + 1.81 +static void draw_grid(float size, float spacing) 1.82 +{ 1.83 + int num_lines = size / spacing; 1.84 + float dist = size / 2.0; 1.85 + 1.86 + m3d_disable(M3D_LIGHTING); 1.87 + 1.88 + m3d_begin(M3D_LINES); 1.89 + m3d_color(0.4, 0.4, 0.4); 1.90 + 1.91 + float x = -dist; 1.92 + for(int i=0; i<=num_lines; i++) { 1.93 + if(i != num_lines / 2) { 1.94 + m3d_vertex(-dist, 0, x); 1.95 + m3d_vertex(dist, 0, x); 1.96 + m3d_vertex(x, 0, -dist); 1.97 + m3d_vertex(x, 0, dist); 1.98 + } 1.99 + x += spacing; 1.100 + } 1.101 + m3d_end(); 1.102 + 1.103 + m3d_begin(M3D_LINES); 1.104 + m3d_color(1.0, 0, 0); 1.105 + m3d_vertex(-dist, 0, 0); 1.106 + m3d_vertex(dist, 0, 0); 1.107 + m3d_color(0, 1.0, 0); 1.108 + m3d_vertex(0, 0, -dist); 1.109 + m3d_vertex(0, 0, dist); 1.110 + m3d_end(); 1.111 +} 1.112 + 1.113 + 1.114 +void Modeller::handle_keyboard(int key, bool press) 1.115 +{ 1.116 + if(press) { 1.117 + switch(key) { 1.118 + case 27: 1.119 + quit_app(); 1.120 + break; 1.121 + 1.122 + default: 1.123 + break; 1.124 + } 1.125 + } 1.126 +} 1.127 + 1.128 +void Modeller::handle_mbutton(int bn, bool press, int x, int y) 1.129 +{ 1.130 + mod->bnstate[bn] = press; 1.131 + mod->prev_x = x; 1.132 + mod->prev_y = y; 1.133 +} 1.134 + 1.135 +void Modeller::handle_mmotion(int x, int y) 1.136 +{ 1.137 + int dx = x - mod->prev_x; 1.138 + int dy = y - mod->prev_y; 1.139 + mod->prev_x = x; 1.140 + mod->prev_y = y; 1.141 + 1.142 + if(mod->bnstate[0]) { 1.143 + mod->cam_theta += dx * 0.5; 1.144 + mod->cam_phi += dy * 0.5; 1.145 + 1.146 + if(mod->cam_phi < -90) mod->cam_phi = -90; 1.147 + if(mod->cam_phi > 90) mod->cam_phi = 90; 1.148 + } 1.149 + if(mod->bnstate[1]) { 1.150 + mod->cam_dist += dy * 0.1; 1.151 + if(mod->cam_dist < 0) mod->cam_dist = 0; 1.152 + } 1.153 +}