-
Notifications
You must be signed in to change notification settings - Fork 2
/
env.cpp
477 lines (395 loc) · 12.4 KB
/
env.cpp
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/* --------------------------------------------------------------------
EXTREME TUXRACER
Copyright (C) 1999-2001 Jasmin F. Patry (Tuxracer)
Copyright (C) 2010 Extreme Tuxracer Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
---------------------------------------------------------------------*/
#include "env.h"
#include "ogl.h"
#include "textures.h"
#include "particles.h"
#include "spx.h"
#include "view.h"
#include "course.h"
// --------------------------------------------------------------------
// defaults
// --------------------------------------------------------------------
static float def_amb[] = {0.45, 0.53, 0.75, 1.0};
static float def_diff[] = {1.0, 0.9, 1.0, 1.0};
static float def_spec[] = {0.6, 0.6, 0.6, 1.0};
static float def_pos[] = {1, 2, 2, 0.0};
static float def_fogcol[] = {0.9, 0.9, 1.0, 0.0};
static float def_partcol[] = {0.8, 0.8, 0.9, 0.0};
CEnvironment Env;
CEnvironment::CEnvironment () {
int i;
EnvID = -1;
for (i=0; i<MAX_LOCATIONS; i++) locs[i].name = "";
numLocs = 0;
EnvDir = "";
LightIndex = "[sunny]0[cloudy]1[evening]2[night]3";
lightcond[0].name = "sunny";
lightcond[1].name = "cloudy";
lightcond[2].name = "evening";
lightcond[3].name = "night";
for (i=0; i<6; i++) Skybox[i] = 0;
default_light.is_on = true;
for (i=0; i<4; i++) {
default_light.ambient[i] = def_amb[i];
default_light.diffuse[i] = def_diff[i];
default_light.specular[i] = def_spec[i];
default_light.position[i] = def_pos[i];
}
default_fog.is_on = true;
default_fog.mode = GL_LINEAR;
default_fog.start = 20.0;
default_fog.end = 70.0;
default_fog.height = 0;
for (i=0; i<4; i++) {
default_fog.color[i] = def_fogcol[i];
default_fog.part_color[i] = def_partcol[i];
}
}
CEnvironment::~CEnvironment () {}
void CEnvironment::ResetSkybox () {
for (int i=0; i<6; i++) {
if (Skybox[i] != 0) glDeleteTextures (1, &Skybox[i]);
Skybox[i] = 0;
}
}
void CEnvironment::SetupLight () {
glLightfv (GL_LIGHT0, GL_POSITION, lights[0].position);
glLightfv (GL_LIGHT0, GL_AMBIENT, lights[0].ambient);
glLightfv (GL_LIGHT0, GL_DIFFUSE, lights[0].diffuse);
glLightfv (GL_LIGHT0, GL_SPECULAR, lights[0].specular);
glEnable (GL_LIGHT0);
if (lights[1].is_on) {
glLightfv (GL_LIGHT1, GL_POSITION, lights[1].position);
glLightfv (GL_LIGHT1, GL_AMBIENT, lights[1].ambient);
glLightfv (GL_LIGHT1, GL_DIFFUSE, lights[1].diffuse);
glLightfv (GL_LIGHT1, GL_SPECULAR, lights[1].specular);
glEnable (GL_LIGHT1);
}
if (lights[2].is_on) {
glLightfv (GL_LIGHT2, GL_POSITION, lights[2].position);
glLightfv (GL_LIGHT2, GL_AMBIENT, lights[2].ambient);
glLightfv (GL_LIGHT2, GL_DIFFUSE, lights[2].diffuse);
glLightfv (GL_LIGHT2, GL_SPECULAR, lights[2].specular);
glEnable (GL_LIGHT2);
}
if (lights[3].is_on) {
glLightfv (GL_LIGHT3, GL_POSITION, lights[3].position);
glLightfv (GL_LIGHT3, GL_AMBIENT, lights[3].ambient);
glLightfv (GL_LIGHT3, GL_DIFFUSE, lights[3].diffuse);
glLightfv (GL_LIGHT3, GL_SPECULAR, lights[3].specular);
glEnable (GL_LIGHT3);
}
glEnable (GL_LIGHTING);
}
void CEnvironment::SetupFog () {
glEnable (GL_FOG);
glFogf (GL_FOG_MODE, fog.mode);
glFogf (GL_FOG_START, fog.start);
glFogf (GL_FOG_END, fog.end);
glFogfv (GL_FOG_COLOR, fog.color);
if (param.perf_level > 1) {
glHint (GL_FOG_HINT, GL_NICEST );
} else {
glHint (GL_FOG_HINT, GL_FASTEST );
}
}
void CEnvironment::ResetLight () {
lights[0] = default_light;
for (int i=1; i<4; i++) lights[i].is_on = false;
glDisable (GL_LIGHT1);
glDisable (GL_LIGHT2);
glDisable (GL_LIGHT3);
}
void CEnvironment::ResetFog () {
fog = default_fog;
}
void CEnvironment::Reset () {
ResetSkybox ();
ResetLight ();
ResetFog ();
}
bool CEnvironment::LoadEnvironmentList () {
int i;
string line;
CSPList list (32, true);
if (!list.Load (param.env_dir2, "environment.lst")) {
Message ("could not load environment.lst");
return false;
}
numLocs = 0;
for (i=0; i<list.Count(); i++) {
line = list.Line (i);
locs[i].name = SPStrN (line, "location", "");
numLocs++;
}
list.MakeIndex (EnvIndex, "location");
return true;
}
string CEnvironment::GetDir (int location, int light) {
if (location < 0 || location >= numLocs) return "";
if (light < 0 || light >= 4) return "";
string res =
param.env_dir2 + SEP +
locs[location].name + SEP + lightcond[light].name;
return res;
}
void CEnvironment::LoadSkybox () {
Skybox[0] = Tex.LoadTexture (EnvDir.c_str(), "front.png");
Skybox[1] = Tex.LoadTexture (EnvDir.c_str(), "left.png");
Skybox[2] = Tex.LoadTexture (EnvDir.c_str(), "right.png");
if (param.full_skybox) {
Skybox[3] = Tex.LoadTexture (EnvDir.c_str(), "top.png");
Skybox[4] = Tex.LoadTexture (EnvDir.c_str(), "bottom.png");
Skybox[5] = Tex.LoadTexture (EnvDir.c_str(), "back.png");
}
}
void CEnvironment::LoadLight () {
string idxstr = "[fog]-1[0]0[1]1[2]2[3]3[4]4[5]5[6]6";
string line;
string item;
int i, idx;
CSPList list(24);
if (!list.Load (EnvDir.c_str(), "light.lst")) {
Message ("could not load light file", "");
return;
}
for (i=0; i<list.Count(); i++) {
line = list.Line(i);
item = SPStrN (line, "light", "none");
idx = SPIntN (idxstr, item, -1);
if (idx < 0) {
fog.is_on = SPBoolN (line, "fog", true);
fog.start = SPFloatN (line, "fogstart", 20);
fog.end = SPFloatN (line, "fogend", param.forward_clip_distance);
fog.height = SPFloatN (line, "fogheight", 0);
SPArrN (line, "fogcol", fog.color, 4, 1);
SPArrN (line, "partcol", fog.part_color, 4, 1);
} else if (idx < 4){
lights[idx].is_on = true;
SPArrN (line, "amb", lights[idx].ambient, 4, 1);
SPArrN (line, "diff", lights[idx].diffuse, 4, 1);
SPArrN (line, "spec", lights[idx].specular, 4, 1);
SPArrN (line, "pos", lights[idx].position, 4, 1);
}
}
}
void CEnvironment::DrawSkybox (TVector3 pos) {
set_gl_options (SKY);
double aa, bb;
#if defined (OS_LINUX)
aa = 0.0;
bb = 1.0;
#else
aa = 0.005;
bb = 0.995;
#endif
glColor4f (1.0, 1.0, 1.0, 1.0);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glPushMatrix();
glTranslatef (pos.x, pos.y, pos.z);
const GLfloat tex[] = {
aa, aa,
bb, aa,
bb, bb,
aa, bb
};
// front
const GLfloat front[] = {
-1, -1, -1,
1, -1, -1,
1, 1, -1,
-1, 1, -1
};
glBindTexture (GL_TEXTURE_2D, Skybox[0]);
glVertexPointer(3, GL_FLOAT, 0, front);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
// left
const GLfloat left[] = {
-1, -1, 1,
-1, -1, -1,
-1, 1, -1,
-1, 1, 1
};
glBindTexture (GL_TEXTURE_2D, Skybox[1]);
glVertexPointer(3, GL_FLOAT, 0, left);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
// right
const GLfloat right[] = {
1, -1, -1,
1, -1, 1,
1, 1, 1,
1, 1, -1
};
glBindTexture (GL_TEXTURE_2D, Skybox[2]);
glVertexPointer(3, GL_FLOAT, 0, right);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
// normally, the following textures are unvisible
// see game_config.cpp (param.full_skybox)
if (param.full_skybox) {
// top
const GLfloat top[] = {
-1, 1, -1,
1, 1, -1,
1, 1, 1,
-1, 1, 1
};
glBindTexture (GL_TEXTURE_2D, Skybox[3]);
glVertexPointer(3, GL_FLOAT, 0, top);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
// bottom
const GLfloat bottom[] = {
-1, -1, 1,
1, -1, 1,
1, -1, -1,
-1, -1, -1
};
glBindTexture (GL_TEXTURE_2D, Skybox[4]);
glVertexPointer(3, GL_FLOAT, 0, bottom);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
// back
const GLfloat back[] = {
1, -1, 1,
-1, -1, 1,
-1, 1, 1,
1, 1, 1
};
glBindTexture (GL_TEXTURE_2D, Skybox[5]);
glVertexPointer(3, GL_FLOAT, 0, back);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
}
glPopMatrix();
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
void CEnvironment::DrawFog () {
TPlane bottom_plane, top_plane;
TVector3 left, right;
TVector3 topleft, topright;
TVector3 bottomleft = NullVec;
TVector3 bottomright = NullVec;
float height;
if (!fog.is_on) return;
// the clipping planes are calculated by view frustum (view.cpp)
leftclip = get_left_clip_plane ();
rightclip = get_right_clip_plane ();
farclip = get_far_clip_plane ();
bottomclip = get_bottom_clip_plane ();
// --------------- calculate the planes ---------------------------
float slope = tan (ANGLES_TO_RADIANS (Course.GetCourseAngle()));
// TPlane left_edge_plane = MakePlane (1.0, 0.0, 0.0, 0.0);
// TPlane right_edge_plane = MakePlane (-1.0, 0.0, 0.0, Course.width);
bottom_plane.nml = MakeVector (0.0, 1, -slope);
height = Course.GetBaseHeight (0);
bottom_plane.d = -height * bottom_plane.nml.y;
top_plane.nml = bottom_plane.nml;
height = Course.GetMaxHeight (0) + fog.height;
top_plane.d = -height * top_plane.nml.y;
if (!IntersectPlanes (bottom_plane, farclip, leftclip, &left)) return;
if (!IntersectPlanes (bottom_plane, farclip, rightclip, &right)) return;
if (!IntersectPlanes (top_plane, farclip, leftclip, &topleft)) return;
if (!IntersectPlanes (top_plane, farclip, rightclip, &topright)) return;
if (!IntersectPlanes (bottomclip, farclip, leftclip, &bottomleft)) return;
if (!IntersectPlanes (bottomclip, farclip, rightclip, &bottomright)) return;
TVector3 leftvec = SubtractVectors (topleft, left);
TVector3 rightvec = SubtractVectors (topright, right);
TVector3 vpoint1 = AddVectors (topleft, leftvec);
TVector3 vpoint2 = AddVectors (topright, rightvec);
TVector3 vpoint3 = AddVectors (topleft, ScaleVector (3.0, leftvec));
TVector3 vpoint4 = AddVectors (topright, ScaleVector (3.0, rightvec));
// --------------- draw the fog plane -----------------------------
set_gl_options (FOG_PLANE);
glEnable (GL_FOG);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
// only the alpha channel is used
const GLfloat col[] = {
// bottom density
0, 0, 0, 1.0,
0, 0, 0, 1.0,
0, 0, 0, 1.0,
0, 0, 0, 1.0,
// top density
0, 0, 0, 0.9,
0, 0, 0, 0.9,
// left/right density
0, 0, 0, 0.3,
0, 0, 0, 0.3,
// top/bottom density
0, 0, 0, 0.0,
0, 0, 0, 0.0
};
const GLfloat vtx[] = {
bottomleft.x, bottomleft.y, bottomleft.z,
bottomright.x, bottomright.y, bottomright.z,
left.x, left.y, left.z,
right.x, right.y, right.z,
topleft.x, topleft.y, topleft.z,
topright.x, topright.y, topright.z,
vpoint1.x, vpoint1.y, vpoint1.z,
vpoint2.x, vpoint2.y, vpoint2.z,
vpoint3.x, vpoint3.y, vpoint3.z,
vpoint4.x, vpoint4.y, vpoint4.z
};
glColorPointer(4, GL_FLOAT, 0, col);
glVertexPointer(3, GL_FLOAT, 0, vtx);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
bool CEnvironment::LoadEnvironment (int loc, int light) {
if (loc < 0 || loc >= numLocs) loc = 0;
if (light < 0 || light >= 4) light = 0;
// remember: with (example) 3 locations and 4 lights there
// are 12 different environments
int env_id = loc * 100 + light;
if (env_id == EnvID) {
Message ("same environment");
return true;
}
// Set directory. The dir is used several times.
EnvDir = GetDir (loc, light);
// Load skybox. If the sky can't be loaded for any reason, the
// texture id's are set to 0 and the sky will not be drawn.
// There is no error handlung, you see the result on the screen.
ResetSkybox ();
LoadSkybox ();
// Load light conditions.
ResetFog ();
ResetLight ();
LoadLight ();
return true;
}
TColor CEnvironment::ParticleColor () {
TColor res;
res.r = fog.part_color[0];
res.g = fog.part_color[1];
res.b = fog.part_color[2];
res.a = fog.part_color[3];
return res;
}
int CEnvironment::GetEnvIdx (const string tag) {
return SPIntN (EnvIndex, tag, 0);
}
int CEnvironment::GetLightIdx (const string tag) {
return SPIntN (LightIndex, tag, 0);
}