-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.cc
More file actions
337 lines (264 loc) · 8.3 KB
/
Copy pathworld.cc
File metadata and controls
337 lines (264 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "first.h"
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <plib/sg.h>
#include "globals.h"
#include "world.h"
#include "billboard.h"
#include "printer.h"
Billboard light_tex;
int
create_lightbulb_texture(int size)
{
int radius = size / 2;
float *data = new float[size * size * 4];
for (int i = 0; i < size * size * 4; i++)
{
data[i]= 1.0;
}
// memset(data, 255, size * size * 4 * sizeof(float));
float radius2 = radius * radius;
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
float pix;
float xf = x - radius;
float yf = y - radius;
float dist2 = (xf * xf + yf * yf) * 1.1;
if (dist2 < radius2)
{
pix = 1.0 * sqrt(dist2 / radius2);
if (pix > 1.0)
pix = 1.0;
else if (pix < 0)
pix = 0.0;
pix = 1.0 - pix;
}
else
{
pix = 0.0f;
}
data[(x * size * 4) + (y * 4) + 3] = pix;
}
}
globals.lightbulb_tex.create_from_data(size, size, 4,
// GL_LUMINANCE,
GL_RGBA,
GL_FLOAT, data);
return 1;
}
void
World::init()
{
globals.world = this;
glClearColor(0.6f, 0.0f, 0.9f, 0.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
globals.frustum = &frustum;
// load asteroids
char name[] = "data/objects/asteroidX.obj";
for (int i = 0; i < 8; i++)
{
name[21] = '0' + i;
asteroid[i] = Mesh::create_from_OBJ_file(name);
assert(asteroid[i]);
if (i <= 3) asteroid[i]->scale(40.0);
else asteroid[i]->scale(20.0);
asteroid[i]->update_bsphere();
}
// load bonus objects...
apple = Mesh::create_from_OBJ_file("data/objects/apple.obj");
banana = Mesh::create_from_OBJ_file("data/objects/banana.obj");
assert(apple && banana);
apple->scale(5.0);
apple->update_bsphere();
banana->scale(5.0);
banana->update_bsphere();
// load level data
bool success = load_d3l_level("data/levels/roketz10.d3l");
// load_d3l_level("data/levels/cres.d3l");
// load_d3l_level("data/levels/reactor.d3l");
// load_d3l_level("data/levels/collision.d3l");
assert(success);
// initialize rooms
for (unsigned r = 0; r < rooms.size(); r++)
{
rooms[r]->init();
}
// exit(1);
// if (!test_tex.create_from_file("data/gfx/textures/tegel1.png")) {
// assert(0);
// }
create_lightbulb_texture(256);
// light_tex.add_texture(*rooms[0]->faces[0].texture);
// light_tex.add_images("data/gfx/smoke_exp/black_smoke", 3, ".tga");
// light_tex.add_images("data/gfx/explosion/ExplosionCC", 3, ".tga");
// light_tex.add_image("data/gfx/flare.tga");
// light_tex.set_color(1.0, 1.0, 1.0, 0.5);
// light_tex.add_texture(lm_textures[0]);
light_tex.add_texture(globals.cube_tex);
// light_tex.set_center(2100, 100, 2100.0);
// light_tex.set_blend(GL_ONE_MINUS_SRC_COLOR, GL_SRC_COLOR);
light_tex.set_blend(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
light_tex.set_fps(32.0);
// create axis aligned bsp tree with all polygons in the world
aabsp.create_face_tree(rooms, nr_faces);
init_materials();
}
void
World::update(const sgVec3 pvp, const sgVec3 cvp)
{
glPushMatrix();
sgCopyVec3(cur_vp, cvp);
sgCopyVec3(prev_vp, pvp);
// don't need to check portals if we haven't moved...
if (cur_vp[0] == prev_vp[0] &&
cur_vp[1] == prev_vp[1] &&
cur_vp[2] == prev_vp[2]) return;
Room *new_room = cur_room->check_portals(prev_vp, cur_vp);
if (new_room != NULL) {
// switch rooms...
cur_room->is_current = false;
cur_room = new_room;
cur_room->is_current = true;
}
glPopMatrix();
}
void
World::draw()
{
sgMat4 c, v, p;
unsigned i;
vector<Room *> collected_rooms;
vector<Object *> collected_objs;
// setup view frustum
glGetFloatv(GL_MODELVIEW_MATRIX, (float*)v);
glGetFloatv(GL_PROJECTION_MATRIX, (float *)p);
sgMultMat4(c, p, v);
frustum.setup_from_matrix(c);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glDisable(GL_LIGHTING);
// draw rooms
cur_room->collect_and_recurse(cur_vp, collected_rooms, collected_objs, 0, 8);
// update rooms and draw lightmaps
for (i = 0; i < collected_rooms.size(); i++)
{
// printf("frame %ld: updating room %d\n",
// globals.frame, collected_rooms[i]->id);
collected_rooms[i]->update();
collected_rooms[i]->draw_lightmaps();
}
// draw dynamic lights
for (i = 0; i < dynamic_lights.size(); i++)
{
dynamic_lights[i]->light_environment();
}
// draw objects and dynamic lights
// printf("%d objects collected\n", collected_objs.size());
for (i = 0; i < collected_objs.size(); i++)
{
collected_objs[i]->draw();
// printf("frame %ld: drawing obj %d\n",
// globals.frame, collected_objs[i]->get_id());
}
// finally, render textures on top of all...
for (i = 0; i < collected_rooms.size(); i++)
{
collected_rooms[i]->draw_textures();
}
if (globals.frame < 2) {
// light_tex.set_center(cur_vp[0] - 200, cur_vp[1] - 100, cur_vp[2]);
light_tex.set_center(start_vp[0] - 100, start_vp[1] + 50, start_vp[2]);
}
light_tex.update(globals.ticks_per_frame);// / 1000.0);
// light_tex.draw();
}
// just draw rooms... used to render cubemap faces.
void
World::draw_rooms(Room *start_room, sgVec3 vp)
{
sgMat4 c, v, p;
unsigned i;
vector<Room *> collected_rooms;
vector<Object *> collected_objs;
// setup view frustum
glGetFloatv(GL_MODELVIEW_MATRIX, (float*)v);
glGetFloatv(GL_PROJECTION_MATRIX, (float *)p);
sgMultMat4(c, p, v);
frustum.setup_from_matrix(c);
glClear(GL_DEPTH_BUFFER_BIT);
glDisable(GL_LIGHTING);
// draw rooms,
start_room->collect_and_recurse(vp, collected_rooms,
collected_objs, 0, 1);
// update rooms and draw lightmaps
for (i = 0; i < collected_rooms.size(); i++)
{
collected_rooms[i]->draw_lightmaps();
}
// no dynamic lights for cubemaps now
#if 0
for (i = 0; i < dynamic_lights.size(); i++)
{
dynamic_lights[i]->light_environment();
}
#endif
// finally, render textures on top of all...
for (i = 0; i < collected_rooms.size(); i++)
{
collected_rooms[i]->draw_textures();
}
}
Room *
World::room_by_id(unsigned id)
{
for (unsigned i = 0; i < rooms.size(); i++) {
if (rooms[i]->id == id) return rooms[i];
}
return NULL;
}
// hardcoded materials. this is all a BIG kludge... FIX!
void
World::init_materials()
{
Material m;
// 0: rocket material
sgSetVec4(m.ambient, 0, 0, 0, 0);
sgSetVec4(m.diffuse, 0.5f, 0.5f, 0.5f, 1.0f);
sgSetVec4(m.specular, 1.0f, 1.0f, 1.0f, 1.0f);
m.shininess = 30.0f;
materials.push_back(m);
// 1: asteroid material
sgSetVec4(m.diffuse, 0.5f, 0.5f, 0.5f, 1.0f);
sgSetVec4(m.specular, 0.8f, 0.8f, 0.8f, 1.0f);
m.shininess = 10.0f;
materials.push_back(m);
// 2: apple green
sgSetVec4(m.diffuse, 0.1f, 1.0f, 0.0f, 1.0f);
sgSetVec4(m.specular, 1.0f, 1.0f, 1.0f, 1.0f);
m.shininess = 10.0f;
materials.push_back(m);
// 3: apple brown
sgSetVec4(m.diffuse, 0.5f, 0.3f, 0.0f, 1.0f);
sgSetVec4(m.specular, 1.0f, 0.6f, 0.0f, 1.0f);
m.shininess = 5.0f;
materials.push_back(m);
// 4: apple red
sgSetVec4(m.diffuse, 1.0f, 0.1f, 0.0f, 1.0f);
sgSetVec4(m.specular, 1.0f, 1.0f, 1.0f, 1.0f);
m.shininess = 30.0f;
materials.push_back(m);
// 5: banana black
sgSetVec4(m.diffuse, 0.1f, 0.3f, 0.1f, 1.0f);
sgSetVec4(m.specular, 0.2f, 0.6f, 0.2f, 1.0f);
m.shininess = 5.0f;
materials.push_back(m);
// 6: banana yellow
sgSetVec4(m.diffuse, 0.86f, 0.79f, 0.1f, 1.0f);
sgSetVec4(m.specular, 1.0f, 0.9f, 0.2f, 1.0f);
m.shininess = 8.0f;
materials.push_back(m);
}