vrfileman

view src/app.cc @ 3:2cbf85690e03

more stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Feb 2015 20:56:16 +0200
parents 282da6123fd4
children 85e26116ba5a
line source
1 #include <assert.h>
2 #include "opengl.h"
3 #include "app.h"
4 #include "user.h"
5 #include "timer.h"
7 static void draw_grid(int num, float sep);
9 static User user;
10 static float eye_level = 1.6;
12 static int win_width, win_height;
13 static bool keystate[256];
14 static bool bnstate[16];
15 static int prev_x, prev_y, click_x, click_y;
17 bool app_init()
18 {
19 if(!init_opengl()) {
20 return false;
21 }
22 return true;
23 }
25 void app_shutdown()
26 {
27 }
29 static void update(unsigned int msec)
30 {
31 static unsigned int prev_upd;
32 float dt = (float)(msec - prev_upd) / 1000.0f;
33 prev_upd = msec;
35 static const float walk_speed = 1.0;
36 float fwd = 0.0f, right = 0.0f;
38 if(keystate['w'] || keystate['W']) {
39 fwd += walk_speed * dt;
40 redisplay();
41 }
42 if(keystate['s'] || keystate['S']) {
43 fwd -= walk_speed * dt;
44 redisplay();
45 }
46 if(keystate['d'] || keystate['D']) {
47 right += walk_speed * 0.5 * dt;
48 redisplay();
49 }
50 if(keystate['a'] || keystate['A']) {
51 right -= walk_speed * 0.5 * dt;
52 redisplay();
53 }
55 user.posrot.move(fwd, right);
56 }
58 void app_display()
59 {
60 unsigned int msec = get_time_msec();
61 update(msec);
63 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
65 glMatrixMode(GL_MODELVIEW);
66 glLoadIdentity();
68 Matrix4x4 viewmat;
69 user.posrot.calc_inv_matrix(&viewmat);
70 glLoadTransposeMatrixf(viewmat.m[0]);
71 glTranslatef(0, -eye_level, 0);
73 draw_grid(20, 1.0);
75 swap_buffers();
76 assert(glGetError() == GL_NO_ERROR);
77 }
79 void app_reshape(int x, int y)
80 {
81 win_width = x;
82 win_height = y;
84 glViewport(0, 0, x, y);
86 glMatrixMode(GL_PROJECTION);
87 glLoadIdentity();
88 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
89 }
91 void app_keyboard(int key, bool pressed, int x, int y)
92 {
93 if(pressed) {
94 switch(key) {
95 case 27:
96 quit();
97 }
98 }
100 if(key < 256) {
101 keystate[key] = pressed;
102 }
104 redisplay();
105 }
107 void app_mouse_button(int bn, bool pressed, int x, int y)
108 {
109 bnstate[bn] = pressed;
110 prev_x = x;
111 prev_y = y;
113 if(bn == 0 && pressed) {
114 click_x = x;
115 click_y = y;
116 }
117 }
119 void app_mouse_motion(int x, int y)
120 {
121 static const float rot_speed = 10.0;
123 float dx = (x - prev_x) / win_width;
124 float dy = (y - prev_y) / win_height;
125 prev_x = x;
126 prev_y = y;
128 if(bnstate[0]) {
129 user.posrot.rotate(dx * rot_speed, dy * rot_speed);
130 redisplay();
131 }
132 }
134 void app_sball_motion(float x, float y, float z)
135 {
136 }
138 void app_sball_rotate(float x, float y, float z)
139 {
140 }
142 void app_sball_button(int bn, bool pressed)
143 {
144 }
146 static void draw_grid(int num, float sep)
147 {
148 int hnum = num / 2;
149 float max_dist = hnum * sep;
151 glBegin(GL_LINES);
152 glColor3f(0.4, 0.4, 0.4);
153 glVertex3f(0, 0, -max_dist);
154 glVertex3f(0, 0, max_dist);
155 glVertex3f(-max_dist, 0, 0);
156 glVertex3f(max_dist, 0, 0);
158 for(int i=1; i<hnum; i++) {
159 float x = i * sep;
161 glVertex3f(-x, 0, -max_dist);
162 glVertex3f(-x, 0, max_dist);
163 glVertex3f(x, 0, -max_dist);
164 glVertex3f(x, 0, max_dist);
165 glVertex3f(-max_dist, 0, -x);
166 glVertex3f(max_dist, 0, -x);
167 glVertex3f(-max_dist, 0, x);
168 glVertex3f(max_dist, 0, x);
169 }
170 glEnd();
171 }