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