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