vrfileman

view src/app.cc @ 5:d487181ee1d9

fixed movement
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 03 Feb 2015 03:35:14 +0200
parents 85e26116ba5a
children
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 const float walk_speed = 5.0;
14 static int win_width, win_height;
15 static bool keystate[256];
16 static bool bnstate[16];
17 static int prev_x, prev_y, click_x, click_y;
18 static unsigned int prev_upd;
20 bool app_init()
21 {
22 if(!init_opengl()) {
23 return false;
24 }
25 return true;
26 }
28 void app_shutdown()
29 {
30 }
32 static void update(unsigned int msec)
33 {
34 float dt = (float)(msec - prev_upd) / 1000.0f;
35 prev_upd = msec;
37 float fwd = 0.0f, right = 0.0f;
39 if(keystate['w'] || keystate['W']) {
40 fwd += walk_speed * dt;
41 redisplay();
42 }
43 if(keystate['s'] || keystate['S']) {
44 fwd -= walk_speed * dt;
45 redisplay();
46 }
47 if(keystate['d'] || keystate['D']) {
48 right += walk_speed * 0.7 * dt;
49 redisplay();
50 }
51 if(keystate['a'] || keystate['A']) {
52 right -= walk_speed * 0.7 * dt;
53 redisplay();
54 }
56 user.move(fwd, right);
57 }
59 void app_display()
60 {
61 unsigned int msec = get_time_msec();
62 update(msec);
64 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
66 glMatrixMode(GL_MODELVIEW);
67 glLoadIdentity();
69 Matrix4x4 viewmat;
70 user.calc_inv_matrix(&viewmat);
71 glLoadTransposeMatrixf(viewmat.m[0]);
72 glTranslatef(0, -eye_level, 0);
74 draw_grid(20, 1.0);
76 swap_buffers();
77 assert(glGetError() == GL_NO_ERROR);
78 }
80 void app_reshape(int x, int y)
81 {
82 win_width = x;
83 win_height = y;
85 glViewport(0, 0, x, y);
87 glMatrixMode(GL_PROJECTION);
88 glLoadIdentity();
89 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
90 }
92 void app_keyboard(int key, bool pressed, int x, int y)
93 {
94 if(pressed) {
95 switch(key) {
96 case 27:
97 quit();
99 case 'w':
100 case 'W':
101 case 'd':
102 case 'D':
103 case 's':
104 case 'S':
105 case 'a':
106 case 'A':
107 prev_upd = get_time_msec();
108 redisplay();
109 }
110 }
112 if(key < 256) {
113 keystate[key] = pressed;
114 }
115 }
117 void app_mouse_button(int bn, bool pressed, int x, int y)
118 {
119 bnstate[bn] = pressed;
120 prev_x = x;
121 prev_y = y;
123 if(bn == 0 && pressed) {
124 click_x = x;
125 click_y = y;
126 }
127 }
129 void app_mouse_motion(int x, int y)
130 {
131 static const float rot_speed = 250.0;
133 float dx = (float)(x - prev_x) / (float)win_width;
134 float dy = (float)(y - prev_y) / (float)win_height;
135 prev_x = x;
136 prev_y = y;
138 if(bnstate[0]) {
139 user.rotate(dx * rot_speed, dy * rot_speed);
140 redisplay();
141 }
142 }
144 void app_sball_motion(float x, float y, float z)
145 {
146 }
148 void app_sball_rotate(float x, float y, float z)
149 {
150 }
152 void app_sball_button(int bn, bool pressed)
153 {
154 }
156 static void draw_grid(int num, float sep)
157 {
158 int hnum = num / 2;
159 float max_dist = (hnum - 1) * sep;
161 glBegin(GL_LINES);
162 glColor3f(0.4, 0.4, 0.4);
163 glVertex3f(0, 0, -max_dist);
164 glVertex3f(0, 0, max_dist);
165 glVertex3f(-max_dist, 0, 0);
166 glVertex3f(max_dist, 0, 0);
168 for(int i=1; i<hnum; i++) {
169 float x = i * sep;
171 glVertex3f(-x, 0, -max_dist);
172 glVertex3f(-x, 0, max_dist);
173 glVertex3f(x, 0, -max_dist);
174 glVertex3f(x, 0, max_dist);
175 glVertex3f(-max_dist, 0, -x);
176 glVertex3f(max_dist, 0, -x);
177 glVertex3f(-max_dist, 0, x);
178 glVertex3f(max_dist, 0, x);
179 }
180 glEnd();
181 }