rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <assert.h>
|
nuclear@0
|
3 #include "opengl.h"
|
nuclear@0
|
4 #include "game.h"
|
nuclear@0
|
5 #include "sdr.h"
|
nuclear@0
|
6 #include "shader.h"
|
nuclear@0
|
7 #include "shadow.h"
|
nuclear@0
|
8 #include "opt.h"
|
nuclear@0
|
9
|
nuclear@0
|
10 static void draw_scene();
|
nuclear@0
|
11
|
nuclear@0
|
12 int win_width, win_height;
|
nuclear@0
|
13 unsigned long cur_time;
|
nuclear@0
|
14 bool dbg_wireframe;
|
nuclear@0
|
15 int dbg_int;
|
nuclear@0
|
16
|
nuclear@0
|
17 unsigned int sdr_shadow, sdr_shadow_notex;
|
nuclear@0
|
18
|
nuclear@0
|
19 static float cam_theta, cam_phi = 25, cam_dist = 8;
|
nuclear@0
|
20 static bool bnstate[8];
|
nuclear@0
|
21 static int prev_x, prev_y;
|
nuclear@0
|
22
|
nuclear@0
|
23 static unsigned int modkeys;
|
nuclear@0
|
24
|
nuclear@0
|
25
|
nuclear@0
|
26 bool game_init()
|
nuclear@0
|
27 {
|
nuclear@0
|
28 if(init_opengl() == -1) {
|
nuclear@0
|
29 return false;
|
nuclear@0
|
30 }
|
nuclear@0
|
31
|
nuclear@0
|
32 glEnable(GL_DEPTH_TEST);
|
nuclear@0
|
33 glEnable(GL_CULL_FACE);
|
nuclear@0
|
34 glEnable(GL_NORMALIZE);
|
nuclear@0
|
35 glEnable(GL_LIGHTING);
|
nuclear@0
|
36 glEnable(GL_LIGHT0);
|
nuclear@0
|
37
|
nuclear@0
|
38 float amb[] = {0.3, 0.3, 0.3, 1.0};
|
nuclear@0
|
39 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
|
nuclear@0
|
40
|
nuclear@0
|
41 if(glcaps.sep_spec) {
|
nuclear@0
|
42 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
|
nuclear@0
|
43 }
|
nuclear@0
|
44 glEnable(GL_MULTISAMPLE);
|
nuclear@0
|
45
|
nuclear@0
|
46 if(!init_shadow(4096)) {
|
nuclear@0
|
47 fprintf(stderr, "failed to initialize shadowmaps\n");
|
nuclear@0
|
48 return false;
|
nuclear@0
|
49 }
|
nuclear@0
|
50 if(!(sdr_shadow = create_program_load("sdr/shadow.v.glsl", "sdr/shadow.p.glsl"))) {
|
nuclear@0
|
51 return false;
|
nuclear@0
|
52 }
|
nuclear@0
|
53 set_uniform_int(sdr_shadow, "tex", 0);
|
nuclear@0
|
54 set_uniform_int(sdr_shadow, "shadowmap", 1);
|
nuclear@0
|
55
|
nuclear@0
|
56 if(!(sdr_shadow_notex = create_program_load("sdr/shadow.v.glsl", "sdr/shadow-notex.p.glsl"))) {
|
nuclear@0
|
57 return false;
|
nuclear@0
|
58 }
|
nuclear@0
|
59 set_uniform_int(sdr_shadow_notex, "shadowmap", 1);
|
nuclear@0
|
60
|
nuclear@0
|
61
|
nuclear@0
|
62 assert(glGetError() == GL_NO_ERROR);
|
nuclear@0
|
63 return true;
|
nuclear@0
|
64 }
|
nuclear@0
|
65
|
nuclear@0
|
66 void game_cleanup()
|
nuclear@0
|
67 {
|
nuclear@0
|
68 }
|
nuclear@0
|
69
|
nuclear@0
|
70 void game_update(unsigned long time_msec)
|
nuclear@0
|
71 {
|
nuclear@0
|
72 cur_time = time_msec;
|
nuclear@0
|
73 }
|
nuclear@0
|
74
|
nuclear@0
|
75 void game_display()
|
nuclear@0
|
76 {
|
nuclear@0
|
77 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@0
|
78
|
nuclear@0
|
79 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
80 glLoadIdentity();
|
nuclear@0
|
81 glTranslatef(0, 0.1, -cam_dist);
|
nuclear@0
|
82 glRotatef(cam_phi, 1, 0, 0);
|
nuclear@0
|
83 glRotatef(cam_theta, 0, 1, 0);
|
nuclear@0
|
84
|
nuclear@0
|
85 float lpos[] = {-10, 20, 10, 1};
|
nuclear@0
|
86 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
|
nuclear@0
|
87
|
nuclear@0
|
88 if(opt.shadows && sdr_shadow) {
|
nuclear@0
|
89 begin_shadow_pass(Vector3(lpos[0], lpos[1], lpos[2]), Vector3(0, 0, 0), 5);
|
nuclear@0
|
90 draw_scene();
|
nuclear@0
|
91 end_shadow_pass();
|
nuclear@0
|
92
|
nuclear@0
|
93 glActiveTexture(GL_TEXTURE1);
|
nuclear@0
|
94 glBindTexture(GL_TEXTURE_2D, get_shadow_tex());
|
nuclear@0
|
95
|
nuclear@0
|
96 glMatrixMode(GL_TEXTURE);
|
nuclear@0
|
97 Matrix4x4 shadow_matrix = get_shadow_matrix();
|
nuclear@0
|
98 glLoadTransposeMatrixf(shadow_matrix[0]);
|
nuclear@0
|
99
|
nuclear@0
|
100 glActiveTexture(GL_TEXTURE0);
|
nuclear@0
|
101 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
102
|
nuclear@0
|
103 set_shader(sdr_shadow_notex);
|
nuclear@0
|
104
|
nuclear@0
|
105 draw_scene();
|
nuclear@0
|
106
|
nuclear@0
|
107 glActiveTexture(GL_TEXTURE1);
|
nuclear@0
|
108 glBindTexture(GL_TEXTURE_2D, 0);
|
nuclear@0
|
109 glActiveTexture(GL_TEXTURE0);
|
nuclear@0
|
110 glBindTexture(GL_TEXTURE_2D, 0);
|
nuclear@0
|
111 } else {
|
nuclear@0
|
112 draw_scene();
|
nuclear@0
|
113 }
|
nuclear@0
|
114 }
|
nuclear@0
|
115
|
nuclear@0
|
116 static void glmaterial(float r, float g, float b, float spec, float shin)
|
nuclear@0
|
117 {
|
nuclear@0
|
118 float color[] = {r, g, b, 1};
|
nuclear@0
|
119 float scolor[] = {spec, spec, spec, 1};
|
nuclear@0
|
120 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
|
nuclear@0
|
121 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scolor);
|
nuclear@0
|
122 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
|
nuclear@0
|
123 }
|
nuclear@0
|
124
|
nuclear@0
|
125 static void draw_scene()
|
nuclear@0
|
126 {
|
nuclear@0
|
127 glPushMatrix();
|
nuclear@0
|
128 glTranslatef(0, -1, 0);
|
nuclear@0
|
129
|
nuclear@0
|
130 glmaterial(0.5, 0.5, 0.5, 0.8, 40.0);
|
nuclear@0
|
131
|
nuclear@0
|
132 glBegin(GL_QUADS);
|
nuclear@0
|
133 glNormal3f(0, 1, 0);
|
nuclear@0
|
134 glVertex3f(-10, 0, 10);
|
nuclear@0
|
135 glVertex3f(10, 0, 10);
|
nuclear@0
|
136 glVertex3f(10, 0, -10);
|
nuclear@0
|
137 glVertex3f(-10, 0, -10);
|
nuclear@0
|
138 glEnd();
|
nuclear@0
|
139
|
nuclear@0
|
140 glPopMatrix();
|
nuclear@0
|
141
|
nuclear@0
|
142 glmaterial(0.2, 0.3, 1.0, 0.8, 60.0);
|
nuclear@0
|
143 draw_teapot();
|
nuclear@0
|
144 }
|
nuclear@0
|
145
|
nuclear@0
|
146
|
nuclear@0
|
147 void game_reshape(int x, int y)
|
nuclear@0
|
148 {
|
nuclear@0
|
149 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
150 glLoadIdentity();
|
nuclear@0
|
151 gluPerspective(45, (float)x / (float)y, 0.2, 200.0);
|
nuclear@0
|
152
|
nuclear@0
|
153 glViewport(0, 0, x, y);
|
nuclear@0
|
154 }
|
nuclear@0
|
155
|
nuclear@0
|
156 void game_keyboard(int bn, bool press)
|
nuclear@0
|
157 {
|
nuclear@0
|
158 if(press) {
|
nuclear@0
|
159 switch(bn) {
|
nuclear@0
|
160 case 27:
|
nuclear@0
|
161 quit();
|
nuclear@0
|
162
|
nuclear@0
|
163 case 'w':
|
nuclear@0
|
164 dbg_wireframe = !dbg_wireframe;
|
nuclear@0
|
165 redisplay();
|
nuclear@0
|
166 break;
|
nuclear@0
|
167
|
nuclear@0
|
168 case 's':
|
nuclear@0
|
169 opt.shadows = !opt.shadows;
|
nuclear@0
|
170 redisplay();
|
nuclear@0
|
171 break;
|
nuclear@0
|
172 }
|
nuclear@0
|
173 }
|
nuclear@0
|
174 }
|
nuclear@0
|
175
|
nuclear@0
|
176 void game_modifier_key(int key, bool press)
|
nuclear@0
|
177 {
|
nuclear@0
|
178 if(press) {
|
nuclear@0
|
179 modkeys |= (1 << key);
|
nuclear@0
|
180 } else {
|
nuclear@0
|
181 modkeys &= ~(1 << key);
|
nuclear@0
|
182 }
|
nuclear@0
|
183 }
|
nuclear@0
|
184
|
nuclear@0
|
185 void game_mbutton(int bn, bool press, int x, int y)
|
nuclear@0
|
186 {
|
nuclear@0
|
187 bnstate[bn] = press;
|
nuclear@0
|
188 prev_x = x;
|
nuclear@0
|
189 prev_y = y;
|
nuclear@0
|
190
|
nuclear@0
|
191 if(modkeys) {
|
nuclear@0
|
192 return;
|
nuclear@0
|
193 }
|
nuclear@0
|
194
|
nuclear@0
|
195 if(bn == 0) {
|
nuclear@0
|
196 }
|
nuclear@0
|
197 }
|
nuclear@0
|
198
|
nuclear@0
|
199 void game_mmotion(int x, int y)
|
nuclear@0
|
200 {
|
nuclear@0
|
201 int dx = x - prev_x;
|
nuclear@0
|
202 int dy = y - prev_y;
|
nuclear@0
|
203 prev_x = x;
|
nuclear@0
|
204 prev_y = y;
|
nuclear@0
|
205
|
nuclear@0
|
206 if(modkeys) {
|
nuclear@0
|
207 if(bnstate[0]) {
|
nuclear@0
|
208 cam_theta += dx * 0.5;
|
nuclear@0
|
209 cam_phi += dy * 0.5;
|
nuclear@0
|
210
|
nuclear@0
|
211 if(cam_phi < -90) cam_phi = -90;
|
nuclear@0
|
212 if(cam_phi > 90) cam_phi = 90;
|
nuclear@0
|
213 }
|
nuclear@0
|
214 if(bnstate[2]) {
|
nuclear@0
|
215 cam_dist += dy * 0.1;
|
nuclear@0
|
216 if(cam_dist < 0.0) cam_dist = 0.0;
|
nuclear@0
|
217 }
|
nuclear@0
|
218 }
|
nuclear@0
|
219 }
|