]> git.jsancho.org Git - lugaru.git/blob - Source/Skybox.cpp
f42dd4d87534af4ccf50f769e4e6aa8553829d78
[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
34 SkyBox::load (const char *ffront, const char *fleft, const char *fback,
35               const char *fright, const char *fup,   const char *fdown)
36 {
37         Game::LoadTexture(ffront, &front, true, false);
38         Game::LoadTexture(fleft,  &left,  true, false);
39         Game::LoadTexture(fback,  &back,  true, false);
40         Game::LoadTexture(fright, &right, true, false);
41         Game::LoadTexture(fup,    &up,    true, false);
42         Game::LoadTexture(fdown,  &down,  true, false);
43 }
44
45 void    SkyBox::draw()
46 {
47         static float size=viewdistance/4;
48         glPushMatrix();
49         static GLfloat M[16];
50         glGetFloatv(GL_MODELVIEW_MATRIX,M);
51         M[12]=0;
52         M[13]=0;
53         M[14]=0;
54         glLoadMatrixf(M);
55         if(environment==2)glScalef(1+blurness/1000,1,1+blurness/1000);
56         if(environment!=2)glColor3f(.85*skyboxr,.85*skyboxg,.95*skyboxb);
57         else 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)glEnable(GL_TEXTURE_2D);
68         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
69         glBindTexture(GL_TEXTURE_2D, front); 
70         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
71         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
72         glBegin(GL_QUADS);
73         glNormal3f( 0.0f, 0.0f, -1);
74         glTexCoord2f(0, 0); glVertex3f(-size, -size,  size);
75         glTexCoord2f(1, 0); glVertex3f( size, -size,  size);
76         glTexCoord2f(1, 1); glVertex3f( size,  size,  size);
77         glTexCoord2f(0, 1); glVertex3f(-size,  size,  size);
78         glEnd();
79         glBindTexture(GL_TEXTURE_2D, back);
80         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
81         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
82         glBegin(GL_QUADS);
83         glNormal3f( 0.0f, 0.0f, 1);
84         glTexCoord2f(1, 0); glVertex3f(-size, -size, -size);
85         glTexCoord2f(1, 1); glVertex3f(-size,  size, -size);
86         glTexCoord2f(0, 1); glVertex3f( size,  size, -size);
87         glTexCoord2f(0, 0); glVertex3f( size, -size, -size);
88         glEnd();
89         glBindTexture(GL_TEXTURE_2D, up);
90         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
91         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
92         glBegin(GL_QUADS);
93         glNormal3f( 0.0f, -1.0f, 0);
94         glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
95         glTexCoord2f(0, 0); glVertex3f(-size,  size,  size);
96         glTexCoord2f(1, 0); glVertex3f( size,  size,  size);
97         glTexCoord2f(1, 1); glVertex3f( size,  size, -size);
98         glEnd();
99         glBindTexture(GL_TEXTURE_2D, down);
100         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
101         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
102         glBegin(GL_QUADS);
103         glNormal3f( 0.0f, 1.0f, 0);
104
105         glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
106         glTexCoord2f(1, 0); glVertex3f( size, -size, -size);
107         glTexCoord2f(1, 1); glVertex3f( size, -size,  size);
108         glTexCoord2f(0, 1); glVertex3f(-size, -size,  size);
109         glEnd();
110         glBindTexture(GL_TEXTURE_2D, right);
111         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
112         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
113         glBegin(GL_QUADS);
114         glNormal3f( -1.0f, 0.0f, 0);
115         glTexCoord2f(1, 0); glVertex3f( size, -size, -size);
116         glTexCoord2f(1, 1); glVertex3f( size,  size, -size);
117         glTexCoord2f(0, 1); glVertex3f( size,  size,  size);
118         glTexCoord2f(0, 0); glVertex3f( size, -size,  size);
119         glEnd();
120         glBindTexture(GL_TEXTURE_2D, left);
121         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
122         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
123         glBegin(GL_QUADS);
124         glNormal3f( 1.0f, 0.0f, 0);
125         glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
126         glTexCoord2f(1, 0); glVertex3f(-size, -size,  size);
127         glTexCoord2f(1, 1); glVertex3f(-size,  size,  size);
128         glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
129         glEnd();
130         glEnable(GL_CULL_FACE);
131         glDepthMask(1);
132         glPopMatrix();
133 }
134
135 SkyBox::~SkyBox()
136 {
137         glDeleteTextures( 1, &front );
138         glDeleteTextures( 1, &left );
139         glDeleteTextures( 1, &back );
140         glDeleteTextures( 1, &right );
141         glDeleteTextures( 1, &up );
142         glDeleteTextures( 1, &down );
143 };
144