-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
rend_sky.c
506 lines (430 loc) · 14.2 KB
/
rend_sky.c
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/**\file rend_sky.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2011 Daniel Swanson <danij@dengine.net>
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include <math.h>
#include "de_base.h"
#include "de_console.h"
#include "de_graphics.h"
#include "de_render.h"
#include "texture.h"
#include "texturevariant.h"
#include "materialvariant.h"
#include "r_sky.h"
/**
* @defgroup skySphereRenderFlags Sky Render Flags
* @{
*/
#define SKYHEMI_UPPER 0x1
#define SKYHEMI_LOWER 0x2
#define SKYHEMI_JUST_CAP 0x4 // Just draw the top or bottom cap.
/**@}*/
typedef struct {
float pos[3];
} skyvertex_t;
typedef struct {
boolean fadeout, texXFlip;
Size2Raw texSize;
float texOffset;
ColorRawf capColor;
} renderhemispherestate_t;
int skyDetail = 6, skyColumns = 4*6, skyRows = 3;
float skyDistance = 1600;
static void constructSphere(void);
static void destroySphere(void);
// CVar callback function which marks the sphere as needing to be rebuilt.
static void updateSphere(void);
static void rebuildHemisphere(void);
// @c true iff this module has been initialized.
static boolean initedOk = false;
// Hemisphere geometry used with the sky sphere.
static skyvertex_t* skyVerts; // Crest is up.
static int numSkyVerts;
static boolean needRebuildHemisphere = true;
// Sphere render state paramaters. Global for performance reasons.
static renderhemispherestate_t rs;
void Rend_SkyRegister(void)
{
C_VAR_INT2("rend-sky-detail", &skyDetail, 0, 3, 7, updateSphere);
C_VAR_INT2("rend-sky-rows", &skyRows, 0, 1, 8, updateSphere);
C_VAR_FLOAT("rend-sky-distance", &skyDistance, CVF_NO_MAX, 1, 0);
}
void Rend_SkyInit(void)
{
if(novideo || isDedicated || initedOk) return;
initedOk = true;
}
void Rend_SkyShutdown(void)
{
if(novideo || isDedicated || !initedOk) return;
destroySphere();
initedOk = false;
}
static void renderSkyModels(void)
{
rendmodelparams_t params;
skymodel_t* sky;
float pos[3];
float inter;
int i, c;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Setup basic translation.
glTranslatef(vx, vy, vz);
for(i = 0, sky = skyModels; i < NUM_SKY_MODELS; ++i, sky++)
{
if(!sky->def)
continue;
if(!R_SkyLayerActive(sky->def->layer))
{
// The model has been assigned to a layer, but the layer is
// not visible.
continue;
}
// Calculate the coordinates for the model.
pos[0] = vx * -sky->def->coordFactor[0];
pos[1] = vy * -sky->def->coordFactor[1];
pos[2] = vz * -sky->def->coordFactor[2];
inter = (sky->maxTimer > 0 ? sky->timer / (float) sky->maxTimer : 0);
memset(¶ms, 0, sizeof(params));
params.distance = 1;
params.center[VX] = pos[0];
params.center[VY] = pos[2];
params.center[VZ] = params.gzt = pos[1];
params.extraYawAngle = params.yawAngleOffset = sky->def->rotate[0];
params.extraPitchAngle = params.pitchAngleOffset = sky->def->rotate[1];
params.inter = inter;
params.mf = sky->model;
params.alwaysInterpolate = true;
R_SetModelFrame(sky->model, sky->frame);
params.yaw = sky->yaw;
for(c = 0; c < 4; ++c)
{
params.ambientColor[c] = sky->def->color[c];
}
params.vLightListIdx = 0;
params.shineTranslateWithViewerPos = true;
Rend_RenderModel(¶ms);
}
// We don't want that anything interferes with what was drawn.
//glClear(GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
// Look up the precalculated vertex.
static __inline skyvertex_t* skyVertex(int r, int c)
{
return skyVerts + (r*skyColumns + c%skyColumns);
}
static void renderHemisphereCap(void)
{
int c;
// Use the appropriate color.
glColor3fv(rs.capColor.rgb);
// Draw the cap.
glBegin(GL_TRIANGLE_FAN);
for(c = 0; c < skyColumns; ++c)
{
glVertex3fv((const GLfloat*)skyVertex(0, c)->pos);
}
glEnd();
// Are we doing a colored fadeout?
if(!rs.fadeout) return;
// We must fill the background for the top row since it'll be
// partially translucent.
glBegin(GL_TRIANGLE_STRIP);
glVertex3fv((const GLfloat*)skyVertex(0, 0)->pos);
for(c = 0; c < skyColumns; ++c)
{
// One step down.
glVertex3fv((const GLfloat*)skyVertex(1, c)->pos);
// And one step right.
glVertex3fv((const GLfloat*)skyVertex(0, c + 1)->pos);
}
glVertex3fv((const GLfloat*)skyVertex(1, c)->pos);
glEnd();
}
static void renderHemisphere(void)
{
#define WRITESKYVERTEX(r_, c_) { \
svtx = skyVertex(r_, c_); \
if(rs.texSize.width != 0) \
glTexCoord2f((c_) / (float) skyColumns, (r_) / (float) skyRows); \
if(rs.fadeout) \
{ \
if((r_) == 0) glColor4f(1, 1, 1, 0); \
else glColor3f(1, 1, 1); \
} \
else \
{ \
if((r_) == 0) glColor3f(0, 0, 0); \
else glColor3f(1, 1, 1); \
} \
glVertex3fv((const GLfloat*)svtx->pos); \
}
const skyvertex_t* svtx;
int r, c;
for(r = 0; r < skyRows; ++r)
{
glBegin(GL_TRIANGLE_STRIP);
WRITESKYVERTEX(r, 0);
WRITESKYVERTEX(r + 1, 0);
for(c = 1; c <= skyColumns; ++c)
{
WRITESKYVERTEX(r, c);
WRITESKYVERTEX(r + 1, c);
}
glEnd();
}
}
typedef enum {
HC_NONE = 0,
HC_TOP,
HC_BOTTOM
} hemispherecap_t;
static void configureRenderHemisphereStateForLayer(int layer, hemispherecap_t setupCap)
{
int magMode = GL_LINEAR;
DGLuint tex = 0;
// Default state is no texture and no fadeout.
rs.texSize.width = rs.texSize.height = 0;
if(setupCap != HC_NONE)
rs.fadeout = false;
rs.texXFlip = true;
if(renderTextures != 0)
{
const materialvariantspecification_t* spec;
const materialsnapshot_t* ms;
material_t* mat;
if(renderTextures == 2)
{
mat = Materials_ToMaterial(Materials_ResolveUriCString(MN_SYSTEM_NAME":gray"));
}
else
{
mat = R_SkyLayerMaterial(layer);
if(!mat)
{
mat = Materials_ToMaterial(Materials_ResolveUriCString(MN_SYSTEM_NAME":missing"));
rs.texXFlip = false;
}
}
assert(mat);
spec = Materials_VariantSpecificationForContext(MC_SKYSPHERE,
TSF_NO_COMPRESSION | (R_SkyLayerMasked(layer)? TSF_ZEROMASK : 0),
0, 0, 0, GL_REPEAT, GL_CLAMP_TO_EDGE, 1, -2, -1, false, true, false, false);
ms = Materials_Prepare(mat, spec, true);
tex = MSU_gltexture(ms, MTU_PRIMARY);
magMode = MSU(ms, MTU_PRIMARY).magMode;
rs.texSize.width = Texture_Width(MSU_texture(ms, MTU_PRIMARY));
rs.texSize.height = Texture_Height(MSU_texture(ms, MTU_PRIMARY));
if(rs.texSize.width == 0 || rs.texSize.height == 0)
{
// Disable texturing.
rs.texSize.width = rs.texSize.height = 0;
tex = 0;
}
if(setupCap != HC_NONE)
{
const averagecolor_analysis_t* avgLineColor = (const averagecolor_analysis_t*)
Texture_Analysis(MSU_texture(ms, MTU_PRIMARY),
(setupCap == HC_TOP? TA_LINE_TOP_COLOR : TA_LINE_BOTTOM_COLOR));
const float fadeoutLimit = R_SkyLayerFadeoutLimit(layer);
if(!avgLineColor)
Con_Error("configureRenderHemisphereStateForLayer: Texture id:%u has no %s analysis.", Textures_Id(MSU_texture(ms, MTU_PRIMARY)), (setupCap == HC_TOP? "TA_LINE_TOP_COLOR" : "TA_LINE_BOTTOM_COLOR"));
V3_Copy(rs.capColor.rgb, avgLineColor->color.rgb);
// Is the colored fadeout in use?
rs.fadeout = (rs.capColor.red >= fadeoutLimit ||
rs.capColor.green >= fadeoutLimit ||
rs.capColor.blue >= fadeoutLimit);
}
}
if(setupCap != HC_NONE && !rs.fadeout)
{
// Default color is black.
V3_Set(rs.capColor.rgb, 0, 0, 0);
}
GL_BindTexture(tex, magMode);
}
/// @param flags @see skySphereRenderFlags
static void renderSkyHemisphere(int flags)
{
int firstSkyLayer = R_SkyFirstActiveLayer();
const boolean yflip = !!(flags & SKYHEMI_LOWER);
hemispherecap_t cap = !!(flags & SKYHEMI_LOWER)? HC_BOTTOM : HC_TOP;
// Rebuild the hemisphere model if necessary.
rebuildHemisphere();
if(yflip)
{
// The lower hemisphere must be flipped.
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glScalef(1.0f, -1.0f, 1.0f);
}
// First render the cap and the background for fadeouts, if needed.
configureRenderHemisphereStateForLayer(firstSkyLayer, cap);
renderHemisphereCap();
if(!(flags & SKYHEMI_JUST_CAP))
{
int i;
for(i = firstSkyLayer; i <= MAX_SKY_LAYERS; ++i)
{
if(!R_SkyLayerActive(i)) continue;
if(i != firstSkyLayer)
{
configureRenderHemisphereStateForLayer(i, HC_NONE);
}
if(rs.texSize.width != 0)
{
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glScalef(1024.f / rs.texSize.width * (rs.texXFlip? 1.0f : -1.0f), yflip? -1.0f : 1.0f, 1.0f);
glTranslatef(rs.texOffset / rs.texSize.width, yflip? -1.0f : 0.0f, 0.0f);
}
renderHemisphere();
if(rs.texSize.width != 0)
{
glMatrixMode(GL_TEXTURE);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
}
}
if(yflip)
{
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
}
void Rend_RenderSky(void)
{
if(novideo || isDedicated || !initedOk) return;
// Is there a sky to be rendered?
if(!R_SkyFirstActiveLayer()) return;
// If sky models have been inited, they will be used.
if(!skyModelsInited || alwaysDrawSphere)
{
// We don't want anything written in the depth buffer.
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
// Disable culling, all triangles face the viewer.
glDisable(GL_CULL_FACE);
GL_DisableArrays(true, true, DDMAXINT);
// Setup a proper matrix.
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(vx, vy, vz);
glScalef(skyDistance, skyDistance, skyDistance);
// Always draw both hemispheres.
renderSkyHemisphere(SKYHEMI_LOWER);
renderSkyHemisphere(SKYHEMI_UPPER);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
// Restore assumed default GL state.
glEnable(GL_CULL_FACE);
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
}
// How about some 3D models?
if(skyModelsInited)
{
renderSkyModels();
}
}
static void destroySphere(void)
{
if(skyVerts)
{
free(skyVerts);
skyVerts = NULL;
}
numSkyVerts = 0;
}
/**
* The top row (row 0) is the one that's faded out.
* There must be at least 4 columns. The preferable number is 4n, where
* n is 1, 2, 3... There should be at least two rows because the first
* one is always faded.
*
* The total number of triangles per hemisphere can be calculated thus:
*
* Sum: rows * columns * 2 + (hemisphere)
* rows * 2 + (fadeout)
* rows - 2 (cap)
*/
static void constructSphere(void)
{
const float maxSideAngle = (float) PI / 2 * R_SkyHeight();
const float horizonOffset = (float) PI / 2 * R_SkyHorizonOffset();
const float scale = 1;
float realRadius, topAngle, sideAngle;
int c, r;
if(skyDetail < 1) skyDetail = 1;
if(skyRows < 1) skyRows = 1;
skyColumns = 4 * skyDetail;
numSkyVerts = skyColumns * (skyRows + 1);
skyVerts = (skyvertex_t*)realloc(skyVerts, sizeof *skyVerts * numSkyVerts);
if(!skyVerts)
Con_Error("constructSphere: Failed (re)allocation of %lu bytes for SkyVertex list.", (unsigned long) sizeof *skyVerts * numSkyVerts);
// Calculate the vertices.
for(r = 0; r < skyRows + 1; ++r)
for(c = 0; c < skyColumns; ++c)
{
skyvertex_t* svtx = skyVertex(r, c);
topAngle = ((c / (float) skyColumns) *2) * PI;
sideAngle = horizonOffset + maxSideAngle * (skyRows - r) / (float) skyRows;
realRadius = scale * cos(sideAngle);
svtx->pos[VX] = realRadius * cos(topAngle);
svtx->pos[VY] = scale * sin(sideAngle); // The height.
svtx->pos[VZ] = realRadius * sin(topAngle);
}
}
static void rebuildHemisphere(void)
{
static boolean firstBuild = true;
static float oldHorizonOffset;
static float oldHeight;
// Rebuild our hemisphere model if any paramaters have changed.
if(firstBuild || R_SkyHorizonOffset() != oldHorizonOffset)
{
oldHorizonOffset = R_SkyHorizonOffset();
needRebuildHemisphere = true;
}
if(firstBuild || R_SkyHeight() != oldHeight)
{
oldHeight = R_SkyHeight();
needRebuildHemisphere = true;
}
firstBuild = false;
if(!needRebuildHemisphere) return;
// We have work to do...
constructSphere();
needRebuildHemisphere = false;
}
/// \note A CVar callback.
static void updateSphere(void)
{
// Defer this task until render time, when we can be sure we are in correct thread.
needRebuildHemisphere = true;
}