vrchess

view src/game.cc @ 2:879194e4b1f0

fixed line-endings
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 25 Apr 2014 05:44:09 +0300
parents cd7755e4663a
children a797e426e309
line source
1 #include "game.h"
2 #include "opengl.h"
3 #include "camera.h"
4 #include "texture.h"
5 //#include "OVR_CAPI_GL.h"
7 static void draw_scene();
9 static const float move_speed = 10.0f;
11 static int fb_width, fb_height;
12 static FlyCamera cam;
13 static Texture floor_tex;
14 static bool keystate[256];
16 bool game_init()
17 {
18 glEnable(GL_DEPTH_TEST);
19 glEnable(GL_CULL_FACE);
20 glEnable(GL_LIGHTING);
21 glEnable(GL_LIGHT0);
23 glClearColor(0.1, 0.1, 0.1, 1);
25 if(!floor_tex.load("data/tiles.png")) {
26 return false;
27 }
29 cam.input_move(0, 0, 5);
30 return true;
31 }
33 void game_cleanup()
34 {
35 floor_tex.destroy();
36 }
39 void game_update(unsigned int msec)
40 {
41 static unsigned int prev_msec;
42 float dt = (msec - prev_msec) / 1000.0f;
43 float offs = dt * move_speed;
44 prev_msec = msec;
46 Vector3 move;
47 float roll = 0.0f;
49 if(keystate['d'] || keystate['D']) {
50 move.x += offs;
51 }
52 if(keystate['a'] || keystate['A']) {
53 move.x -= offs;
54 }
55 if(keystate['s'] || keystate['S']) {
56 move.z += offs;
57 }
58 if(keystate['w'] || keystate['W']) {
59 move.z -= offs;
60 }
61 if(keystate['e'] || keystate['E']) {
62 roll += dt;
63 }
64 if(keystate['q'] || keystate['Q']) {
65 roll -= dt;
66 }
68 cam.input_move(move.x, move.y, move.z);
69 cam.input_rotate(0, 0, roll);
70 }
72 void game_render(int eye)
73 {
74 Matrix4x4 view_matrix = cam.get_matrix().inverse();
76 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
78 glMatrixMode(GL_PROJECTION);
79 glLoadIdentity();
80 gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
82 glMatrixMode(GL_MODELVIEW);
83 glLoadIdentity();
84 glLoadTransposeMatrixf(view_matrix[0]);
86 draw_scene();
87 }
89 void game_reshape(int x, int y)
90 {
91 glViewport(0, 0, x, y);
92 fb_width = x;
93 fb_height = y;
94 }
96 void game_keyboard(int key, bool pressed, int x, int y)
97 {
98 if(pressed) {
99 switch(key) {
100 case 27:
101 exit(0);
102 }
103 }
105 if(key < 256) {
106 keystate[key] = pressed;
107 }
108 }
110 static int prev_x, prev_y;
111 static bool bnstate[32];
113 void game_mouse(int bn, bool pressed, int x, int y)
114 {
115 bnstate[bn] = pressed;
116 prev_x = x;
117 prev_y = y;
118 }
120 void game_motion(int x, int y)
121 {
122 int dx = x - prev_x;
123 int dy = y - prev_y;
124 prev_x = x;
125 prev_y = y;
127 if(!dx && !dy) return;
129 if(bnstate[0]) {
130 float xrot = dy * 0.5;
131 float yrot = dx * 0.5;
132 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
133 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
134 }
135 }
137 void game_6dof_move(float x, float y, float z)
138 {
139 cam.input_move(x, y, z);
140 }
142 void game_6dof_rotate(float x, float y, float z)
143 {
144 cam.input_rotate(x, y, z);
145 }
147 static void draw_scene()
148 {
149 glMatrixMode(GL_MODELVIEW);
150 glTranslatef(0, -1.5, 0);
152 float lpos[] = {-20, 30, 10, 1};
153 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
155 glEnable(GL_TEXTURE_2D);
156 floor_tex.bind();
158 glMatrixMode(GL_TEXTURE);
159 glScalef(8, 8, 8);
161 glBegin(GL_QUADS);
162 glNormal3f(0, 1, 0);
163 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
164 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
165 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
166 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
167 glEnd();
168 glDisable(GL_TEXTURE_2D);
169 glLoadIdentity();
171 glMatrixMode(GL_MODELVIEW);
172 glPushMatrix();
173 glTranslatef(0, 0.75, 0);
175 glFrontFace(GL_CW);
176 glutSolidTeapot(1.0);
177 glFrontFace(GL_CCW);
179 glPopMatrix();
180 }