]> git.jsancho.org Git - lugaru.git/blob - Source/Skybox.cpp
License: Update GPLv2+ header to match current FSF recommendation
[lugaru.git] / Source / Skybox.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "Skybox.h"
21 #include "Game.h"
22
23 extern float viewdistance;
24 extern float blurness;
25 extern int environment;
26 extern bool skyboxtexture;
27 extern float skyboxr;
28 extern float skyboxg;
29 extern float skyboxb;
30
31 void SkyBox::load (const char *ffront, const char *fleft, const char *fback,
32                    const char *fright, const char *fup,   const char *fdown)
33 {
34     front.load(ffront, true, false);
35     left.load(fleft, true, false);
36     back.load(fback, true, false);
37     right.load(fright, true, false);
38     up.load(fup, true, false);
39     down.load(fdown, true, false);
40 }
41
42 void SkyBox::draw()
43 {
44     static float size = viewdistance / 4;
45     glPushMatrix();
46     static GLfloat M[16];
47     glGetFloatv(GL_MODELVIEW_MATRIX, M);
48     M[12] = 0;
49     M[13] = 0;
50     M[14] = 0;
51     glLoadMatrixf(M);
52     if (environment == 2)
53         glScalef(1 + blurness / 1000, 1, 1 + blurness / 1000);
54     if (environment != 2)
55         glColor3f(.85 * skyboxr, .85 * skyboxg, .95 * skyboxb);
56     else
57         glColor3f(1 * skyboxr, .95 * skyboxg, .95 * skyboxb);
58
59     if (!skyboxtexture) {
60         glDisable(GL_TEXTURE_2D);
61         glColor3f(skyboxr * .8, skyboxg * .8, skyboxb * .8);
62     }
63     glDepthMask(0);
64     glDisable(GL_CULL_FACE);
65     glEnable(GL_BLEND);
66     glDisable(GL_LIGHTING);
67     if (skyboxtexture)
68         glEnable(GL_TEXTURE_2D);
69     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
70     front.bind();
71     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
72     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
73     glBegin(GL_QUADS);
74     glNormal3f( 0.0f, 0.0f, -1);
75     glTexCoord2f(0, 0);
76     glVertex3f(-size, -size,  size);
77     glTexCoord2f(1, 0);
78     glVertex3f( size, -size,  size);
79     glTexCoord2f(1, 1);
80     glVertex3f( size,  size,  size);
81     glTexCoord2f(0, 1);
82     glVertex3f(-size,  size,  size);
83     glEnd();
84     back.bind();
85     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
86     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
87     glBegin(GL_QUADS);
88     glNormal3f( 0.0f, 0.0f, 1);
89     glTexCoord2f(1, 0);
90     glVertex3f(-size, -size, -size);
91     glTexCoord2f(1, 1);
92     glVertex3f(-size,  size, -size);
93     glTexCoord2f(0, 1);
94     glVertex3f( size,  size, -size);
95     glTexCoord2f(0, 0);
96     glVertex3f( size, -size, -size);
97     glEnd();
98     up.bind();
99     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
100     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
101     glBegin(GL_QUADS);
102     glNormal3f( 0.0f, -1.0f, 0);
103     glTexCoord2f(0, 1);
104     glVertex3f(-size,  size, -size);
105     glTexCoord2f(0, 0);
106     glVertex3f(-size,  size,  size);
107     glTexCoord2f(1, 0);
108     glVertex3f( size,  size,  size);
109     glTexCoord2f(1, 1);
110     glVertex3f( size,  size, -size);
111     glEnd();
112     down.bind();
113     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
114     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
115     glBegin(GL_QUADS);
116     glNormal3f( 0.0f, 1.0f, 0);
117
118     glTexCoord2f(0, 0);
119     glVertex3f(-size, -size, -size);
120     glTexCoord2f(1, 0);
121     glVertex3f( size, -size, -size);
122     glTexCoord2f(1, 1);
123     glVertex3f( size, -size,  size);
124     glTexCoord2f(0, 1);
125     glVertex3f(-size, -size,  size);
126     glEnd();
127     right.bind();
128     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
129     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
130     glBegin(GL_QUADS);
131     glNormal3f( -1.0f, 0.0f, 0);
132     glTexCoord2f(1, 0);
133     glVertex3f( size, -size, -size);
134     glTexCoord2f(1, 1);
135     glVertex3f( size,  size, -size);
136     glTexCoord2f(0, 1);
137     glVertex3f( size,  size,  size);
138     glTexCoord2f(0, 0);
139     glVertex3f( size, -size,  size);
140     glEnd();
141     left.bind();
142     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
143     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
144     glBegin(GL_QUADS);
145     glNormal3f( 1.0f, 0.0f, 0);
146     glTexCoord2f(0, 0);
147     glVertex3f(-size, -size, -size);
148     glTexCoord2f(1, 0);
149     glVertex3f(-size, -size,  size);
150     glTexCoord2f(1, 1);
151     glVertex3f(-size,  size,  size);
152     glTexCoord2f(0, 1);
153     glVertex3f(-size,  size, -size);
154     glEnd();
155     glEnable(GL_CULL_FACE);
156     glDepthMask(1);
157     glPopMatrix();
158 }
159
160 SkyBox::~SkyBox()
161 {
162     front.destroy();
163     left.destroy();
164     back.destroy();
165     right.destroy();
166     up.destroy();
167     down.destroy();
168 };
169