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@0
|
23 bool game_init()
|
nuclear@0
|
24 {
|
nuclear@1
|
25 printf("initializing OpenGL state\n");
|
nuclear@1
|
26 glEnable(GL_CULL_FACE);
|
nuclear@1
|
27 glEnable(GL_DEPTH_TEST);
|
nuclear@1
|
28 glEnable(GL_LIGHTING);
|
nuclear@1
|
29 glEnable(GL_LIGHT0);
|
nuclear@1
|
30
|
nuclear@2
|
31 float lpos[] = {-1, 1, 1, 0};
|
nuclear@2
|
32 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
|
nuclear@2
|
33
|
nuclear@1
|
34 printf("generating level\n");
|
nuclear@1
|
35 level = new Level;
|
nuclear@1
|
36 level->generate();
|
nuclear@1
|
37
|
nuclear@3
|
38 printf("initializing renderer\n");
|
nuclear@3
|
39 rend = new Renderer;
|
nuclear@3
|
40 if(!rend->init(level)) {
|
nuclear@3
|
41 return false;
|
nuclear@3
|
42 }
|
nuclear@3
|
43
|
nuclear@1
|
44 cam.input_move(0, 2, 2);
|
nuclear@1
|
45 cam.input_rotate(0, M_PI / 5, 0);
|
nuclear@1
|
46
|
nuclear@0
|
47 return true;
|
nuclear@0
|
48 }
|
nuclear@0
|
49
|
nuclear@0
|
50 void game_shutdown()
|
nuclear@0
|
51 {
|
nuclear@1
|
52 delete rend;
|
nuclear@1
|
53 delete level;
|
nuclear@0
|
54 }
|
nuclear@0
|
55
|
nuclear@0
|
56 void game_iter(double dt)
|
nuclear@0
|
57 {
|
nuclear@2
|
58 float offs = 4.0 * dt;
|
nuclear@1
|
59 float dx = 0, dy = 0;
|
nuclear@1
|
60
|
nuclear@1
|
61 // handle key input
|
nuclear@1
|
62 if(keystate['w'] || keystate['W']) {
|
nuclear@1
|
63 dy -= offs;
|
nuclear@1
|
64 }
|
nuclear@1
|
65 if(keystate['s'] || keystate['S']) {
|
nuclear@1
|
66 dy += offs;
|
nuclear@1
|
67 }
|
nuclear@1
|
68 if(keystate['d'] || keystate['D']) {
|
nuclear@1
|
69 dx += offs;
|
nuclear@1
|
70 }
|
nuclear@1
|
71 if(keystate['a'] || keystate['A']) {
|
nuclear@1
|
72 dx -= offs;
|
nuclear@1
|
73 }
|
nuclear@1
|
74
|
nuclear@1
|
75 cam.input_move(dx, 0, dy);
|
nuclear@0
|
76 }
|
nuclear@0
|
77
|
nuclear@0
|
78 void game_render()
|
nuclear@0
|
79 {
|
nuclear@3
|
80 rend->set_aspect((float)win_xsz / (float)win_ysz);
|
nuclear@3
|
81
|
nuclear@2
|
82 if(opt.stereo) {
|
nuclear@2
|
83 glDrawBuffer(GL_BACK_LEFT);
|
nuclear@1
|
84
|
nuclear@2
|
85 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
86 glLoadIdentity();
|
nuclear@2
|
87 proj_matrix(-1);
|
nuclear@2
|
88 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
89 glLoadIdentity();
|
nuclear@2
|
90 view_matrix(-1);
|
nuclear@1
|
91
|
nuclear@3
|
92 rend->render();
|
nuclear@1
|
93
|
nuclear@2
|
94 glDrawBuffer(GL_BACK_RIGHT);
|
nuclear@2
|
95
|
nuclear@2
|
96 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
97 glLoadIdentity();
|
nuclear@2
|
98 proj_matrix(1);
|
nuclear@2
|
99 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
100 glLoadIdentity();
|
nuclear@2
|
101 view_matrix(1);
|
nuclear@2
|
102
|
nuclear@3
|
103 rend->render();
|
nuclear@2
|
104 } else {
|
nuclear@2
|
105 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
106 glLoadIdentity();
|
nuclear@2
|
107 proj_matrix(0);
|
nuclear@2
|
108 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
109 glLoadIdentity();
|
nuclear@2
|
110 view_matrix(0);
|
nuclear@2
|
111
|
nuclear@3
|
112 rend->render();
|
nuclear@2
|
113 }
|
nuclear@0
|
114 }
|
nuclear@1
|
115
|
nuclear@1
|
116 void game_input_shoot(int bn)
|
nuclear@1
|
117 {
|
nuclear@1
|
118 }
|
nuclear@1
|
119
|
nuclear@1
|
120 void game_input_move(float x, float y, float z)
|
nuclear@1
|
121 {
|
nuclear@1
|
122 cam.input_move(x, y, z);
|
nuclear@1
|
123 }
|
nuclear@1
|
124
|
nuclear@1
|
125 void game_input_rot(float x, float y)
|
nuclear@1
|
126 {
|
nuclear@1
|
127 cam.input_rotate(x * 6.0, y * 6.0, 0);
|
nuclear@1
|
128 }
|
nuclear@2
|
129
|
nuclear@2
|
130
|
nuclear@2
|
131 static void view_matrix(int eye)
|
nuclear@2
|
132 {
|
nuclear@2
|
133 float offs = stereo_eye_sep * eye * 0.5;
|
nuclear@2
|
134 glTranslatef(-offs, 0, 0);
|
nuclear@3
|
135 cam.use();
|
nuclear@2
|
136 }
|
nuclear@2
|
137
|
nuclear@2
|
138 static void proj_matrix(int eye)
|
nuclear@2
|
139 {
|
nuclear@2
|
140 static const float fov = M_PI / 4.0;
|
nuclear@2
|
141 static const float near_clip = 0.1;
|
nuclear@2
|
142 static const float far_clip = 500.0;
|
nuclear@2
|
143
|
nuclear@2
|
144 float top = near_clip * tan(fov * 0.5);
|
nuclear@2
|
145 float right = top * (float)win_xsz / (float)win_ysz;
|
nuclear@2
|
146
|
nuclear@2
|
147 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
|
nuclear@2
|
148 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
|
nuclear@2
|
149 }
|