rev |
line source |
nuclear@0
|
1 #include "game.h"
|
nuclear@2
|
2 #include "opt.h"
|
nuclear@0
|
3 #include "opengl.h"
|
nuclear@1
|
4 #include "level.h"
|
nuclear@1
|
5 #include "renderer.h"
|
nuclear@1
|
6 #include "camera.h"
|
nuclear@1
|
7
|
nuclear@2
|
8 static void view_matrix(int eye);
|
nuclear@2
|
9 static void proj_matrix(int eye);
|
nuclear@2
|
10
|
nuclear@2
|
11 int win_xsz, win_ysz;
|
nuclear@2
|
12
|
nuclear@2
|
13 bool keystate[GAME_MAX_KEYS];
|
nuclear@2
|
14 bool bnstate[GAME_MAX_BUTTONS];
|
nuclear@2
|
15
|
nuclear@2
|
16 float stereo_focus_dist = 0.25;
|
nuclear@2
|
17 float stereo_eye_sep = stereo_focus_dist / 30.0;
|
nuclear@1
|
18
|
nuclear@1
|
19 static Level *level;
|
nuclear@1
|
20 static Renderer *rend;
|
nuclear@1
|
21 static FpsCamera cam;
|
nuclear@0
|
22
|
nuclear@4
|
23 static const float fog_color[] = {0.76, 0.64, 0.91, 1.0};
|
nuclear@4
|
24 static Vector3 gravity;
|
nuclear@4
|
25
|
nuclear@0
|
26 bool game_init()
|
nuclear@0
|
27 {
|
nuclear@1
|
28 printf("initializing OpenGL state\n");
|
nuclear@1
|
29 glEnable(GL_CULL_FACE);
|
nuclear@1
|
30 glEnable(GL_DEPTH_TEST);
|
nuclear@1
|
31 glEnable(GL_LIGHTING);
|
nuclear@1
|
32 glEnable(GL_LIGHT0);
|
nuclear@1
|
33
|
nuclear@2
|
34 float lpos[] = {-1, 1, 1, 0};
|
nuclear@2
|
35 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
|
nuclear@2
|
36
|
nuclear@1
|
37 printf("generating level\n");
|
nuclear@1
|
38 level = new Level;
|
nuclear@4
|
39 level->world_size = Vector3(8, 8, 4);
|
nuclear@1
|
40 level->generate();
|
nuclear@1
|
41
|
nuclear@3
|
42 printf("initializing renderer\n");
|
nuclear@3
|
43 rend = new Renderer;
|
nuclear@3
|
44 if(!rend->init(level)) {
|
nuclear@3
|
45 return false;
|
nuclear@3
|
46 }
|
nuclear@3
|
47
|
nuclear@1
|
48 cam.input_move(0, 2, 2);
|
nuclear@1
|
49 cam.input_rotate(0, M_PI / 5, 0);
|
nuclear@1
|
50
|
nuclear@4
|
51 glClearColor(fog_color[0], fog_color[1], fog_color[2], 1.0);
|
nuclear@4
|
52 glFogfv(GL_FOG_COLOR, fog_color);
|
nuclear@4
|
53
|
nuclear@4
|
54 gravity = Vector3(0, -0.01, 0);
|
nuclear@4
|
55
|
nuclear@0
|
56 return true;
|
nuclear@0
|
57 }
|
nuclear@0
|
58
|
nuclear@0
|
59 void game_shutdown()
|
nuclear@0
|
60 {
|
nuclear@1
|
61 delete rend;
|
nuclear@1
|
62 delete level;
|
nuclear@0
|
63 }
|
nuclear@0
|
64
|
nuclear@0
|
65 void game_iter(double dt)
|
nuclear@0
|
66 {
|
nuclear@2
|
67 float offs = 4.0 * dt;
|
nuclear@1
|
68 float dx = 0, dy = 0;
|
nuclear@1
|
69
|
nuclear@1
|
70 // handle key input
|
nuclear@1
|
71 if(keystate['w'] || keystate['W']) {
|
nuclear@1
|
72 dy -= offs;
|
nuclear@1
|
73 }
|
nuclear@1
|
74 if(keystate['s'] || keystate['S']) {
|
nuclear@1
|
75 dy += offs;
|
nuclear@1
|
76 }
|
nuclear@1
|
77 if(keystate['d'] || keystate['D']) {
|
nuclear@1
|
78 dx += offs;
|
nuclear@1
|
79 }
|
nuclear@1
|
80 if(keystate['a'] || keystate['A']) {
|
nuclear@1
|
81 dx -= offs;
|
nuclear@1
|
82 }
|
nuclear@1
|
83
|
nuclear@1
|
84 cam.input_move(dx, 0, dy);
|
nuclear@4
|
85
|
nuclear@4
|
86 for(size_t i=0; i<level->blobs.size(); i++) {
|
nuclear@4
|
87 Blob *b = &level->blobs[i];
|
nuclear@4
|
88
|
nuclear@4
|
89 b->velocity += gravity * dt;
|
nuclear@4
|
90 Vector3 npos = b->pos + b->velocity * dt;
|
nuclear@4
|
91
|
nuclear@4
|
92 Vector3 normal;
|
nuclear@4
|
93 if(level->collision(b->pos, npos, &npos, &normal)) {
|
nuclear@4
|
94 b->velocity = b->velocity.reflection(normal);
|
nuclear@4
|
95 }
|
nuclear@4
|
96 }
|
nuclear@0
|
97 }
|
nuclear@0
|
98
|
nuclear@0
|
99 void game_render()
|
nuclear@0
|
100 {
|
nuclear@3
|
101 rend->set_aspect((float)win_xsz / (float)win_ysz);
|
nuclear@3
|
102
|
nuclear@2
|
103 if(opt.stereo) {
|
nuclear@2
|
104 glDrawBuffer(GL_BACK_LEFT);
|
nuclear@4
|
105 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@1
|
106
|
nuclear@2
|
107 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
108 glLoadIdentity();
|
nuclear@2
|
109 proj_matrix(-1);
|
nuclear@2
|
110 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
111 glLoadIdentity();
|
nuclear@2
|
112 view_matrix(-1);
|
nuclear@1
|
113
|
nuclear@3
|
114 rend->render();
|
nuclear@1
|
115
|
nuclear@2
|
116 glDrawBuffer(GL_BACK_RIGHT);
|
nuclear@4
|
117 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@2
|
118
|
nuclear@2
|
119 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
120 glLoadIdentity();
|
nuclear@2
|
121 proj_matrix(1);
|
nuclear@2
|
122 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
123 glLoadIdentity();
|
nuclear@2
|
124 view_matrix(1);
|
nuclear@2
|
125
|
nuclear@3
|
126 rend->render();
|
nuclear@2
|
127 } else {
|
nuclear@4
|
128 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@4
|
129
|
nuclear@2
|
130 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
131 glLoadIdentity();
|
nuclear@2
|
132 proj_matrix(0);
|
nuclear@2
|
133 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
134 glLoadIdentity();
|
nuclear@2
|
135 view_matrix(0);
|
nuclear@2
|
136
|
nuclear@3
|
137 rend->render();
|
nuclear@2
|
138 }
|
nuclear@0
|
139 }
|
nuclear@1
|
140
|
nuclear@1
|
141 void game_input_shoot(int bn)
|
nuclear@1
|
142 {
|
nuclear@1
|
143 }
|
nuclear@1
|
144
|
nuclear@1
|
145 void game_input_move(float x, float y, float z)
|
nuclear@1
|
146 {
|
nuclear@1
|
147 cam.input_move(x, y, z);
|
nuclear@1
|
148 }
|
nuclear@1
|
149
|
nuclear@1
|
150 void game_input_rot(float x, float y)
|
nuclear@1
|
151 {
|
nuclear@1
|
152 cam.input_rotate(x * 6.0, y * 6.0, 0);
|
nuclear@1
|
153 }
|
nuclear@2
|
154
|
nuclear@2
|
155
|
nuclear@2
|
156 static void view_matrix(int eye)
|
nuclear@2
|
157 {
|
nuclear@2
|
158 float offs = stereo_eye_sep * eye * 0.5;
|
nuclear@2
|
159 glTranslatef(-offs, 0, 0);
|
nuclear@3
|
160 cam.use();
|
nuclear@2
|
161 }
|
nuclear@2
|
162
|
nuclear@2
|
163 static void proj_matrix(int eye)
|
nuclear@2
|
164 {
|
nuclear@2
|
165 static const float fov = M_PI / 4.0;
|
nuclear@2
|
166 static const float near_clip = 0.1;
|
nuclear@2
|
167 static const float far_clip = 500.0;
|
nuclear@2
|
168
|
nuclear@2
|
169 float top = near_clip * tan(fov * 0.5);
|
nuclear@2
|
170 float right = top * (float)win_xsz / (float)win_ysz;
|
nuclear@2
|
171
|
nuclear@2
|
172 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
|
nuclear@2
|
173 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
|
nuclear@2
|
174 }
|