]> git.jsancho.org Git - lugaru.git/blob - Source/Skybox.cpp
Added 'mesa_wgl.h' to make OpenGL headers happy on Windows
[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 int detail;
27 extern bool trilinear;
28 extern float blurness;
29 extern int environment;
30 extern TGAImageRec texture;
31 extern bool skyboxtexture;
32 extern float skyboxr;
33 extern float skyboxg;
34 extern float skyboxb;
35 extern int tutoriallevel;
36
37 bool    SkyBox::load( const char *ffront,const char *fleft,const char *fback,const char *fright,const char *fup,const char *fdown,const char *fcloud,const char *freflect)
38 {
39 /*      static GLuint           type;
40         unsigned char fileNamep[256];
41 */
42         LOGFUNC;
43
44         //front
45         Game::LoadTexture(ffront, &front, true, false);
46         //left
47         Game::LoadTexture(fleft, &left, true, false);
48         //back
49         Game::LoadTexture(fback, &back, true, false);
50         //right
51         Game::LoadTexture(fright, &right, true, false);
52         //up
53         Game::LoadTexture(fup, &up, true, false);
54         //down
55         Game::LoadTexture(fdown, &down, true, false);
56         //cloud
57         Game::LoadTexture(fcloud, &cloud, true, false);
58         //reflect
59         Game::LoadTexture(freflect, &reflect, true, false);
60 /*
61         //front
62         CopyCStringToPascal(ffront,fileNamep);
63         upload_image( fileNamep ,0); 
64         if(1==1){
65                 if ( texture.bpp == 24 )
66                         type = GL_RGB;
67                 else
68                         type = GL_RGBA;
69
70                 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
71
72                 if(!front)glGenTextures( 1, &front );
73                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
74
75                 glBindTexture( GL_TEXTURE_2D, front);
76                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
77                 //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
78                 if(trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
79                 if(!trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
80
81                 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
82
83         }
84
85         //left
86         CopyCStringToPascal(fleft,fileNamep);
87         upload_image( fileNamep ,0); 
88         if(1==1){
89                 if ( texture.bpp == 24 )
90                         type = GL_RGB;
91                 else
92                         type = GL_RGBA;
93
94                 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
95
96                 if(!left)glGenTextures( 1, &left );
97                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
98
99                 glBindTexture( GL_TEXTURE_2D, left);
100                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
101                 //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
102                 if(trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
103                 if(!trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
104
105                 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
106
107         }
108
109         //back
110         CopyCStringToPascal(fback,fileNamep);
111         upload_image( fileNamep ,0); 
112         if(1==1){
113                 if ( texture.bpp == 24 )
114                         type = GL_RGB;
115                 else
116                         type = GL_RGBA;
117
118                 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
119
120                 if(!back)glGenTextures( 1, &back );
121                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
122
123                 glBindTexture( GL_TEXTURE_2D, back);
124                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
125                 //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
126                 if(trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
127                 if(!trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
128
129                 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
130
131         }
132
133         //right
134         CopyCStringToPascal(fright,fileNamep);
135         upload_image( fileNamep ,0); 
136         if(1==1){
137                 if ( texture.bpp == 24 )
138                         type = GL_RGB;
139                 else
140                         type = GL_RGBA;
141
142                 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
143
144                 if(!right)glGenTextures( 1, &right );
145                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
146
147                 glBindTexture( GL_TEXTURE_2D, right);
148                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
149                 //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
150                 if(trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
151                 if(!trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
152
153                 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
154
155         }
156
157         //up
158         CopyCStringToPascal(fup,fileNamep);
159         upload_image( fileNamep ,0); 
160         if(1==1){
161                 if ( texture.bpp == 24 )
162                         type = GL_RGB;
163                 else
164                         type = GL_RGBA;
165
166                 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
167
168                 if(!up)glGenTextures( 1, &up );
169                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
170
171                 glBindTexture( GL_TEXTURE_2D, up);
172                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
173                 //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
174                 if(trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
175                 if(!trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
176
177                 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
178
179         }
180
181         //down
182         CopyCStringToPascal(fdown,fileNamep);
183         upload_image( fileNamep ,0); 
184         if(1==1){
185                 if ( texture.bpp == 24 )
186                         type = GL_RGB;
187                 else
188                         type = GL_RGBA;
189
190                 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
191
192                 if(!down)glGenTextures( 1, &down );
193                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
194
195                 glBindTexture( GL_TEXTURE_2D, down);
196                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
197                 //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
198                 if(trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
199                 if(!trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
200
201                 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
202
203         }
204
205         //cloud
206         CopyCStringToPascal(fcloud,fileNamep);
207         upload_image( fileNamep ,0); 
208         if(1==1){
209                 if ( texture.bpp == 24 )
210                         type = GL_RGB;
211                 else
212                         type = GL_RGBA;
213
214                 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
215
216                 if(!cloud)glGenTextures( 1, &cloud );
217                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
218
219                 glBindTexture( GL_TEXTURE_2D, cloud);
220                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
221                 //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
222                 if(trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
223                 if(!trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
224
225                 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
226
227         }
228
229         //up
230         CopyCStringToPascal(freflect,fileNamep);
231         upload_image( fileNamep ,0); 
232         if(1==1){
233                 if ( texture.bpp == 24 )
234                         type = GL_RGB;
235                 else
236                         type = GL_RGBA;
237
238                 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
239
240                 if(!reflect)glGenTextures( 1, &reflect );
241                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
242
243                 glBindTexture( GL_TEXTURE_2D, reflect);
244                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
245                 //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
246                 if(trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
247                 if(!trilinear)glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
248
249                 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
250
251         }
252 */
253         return true;
254 }
255
256 void    SkyBox::draw()
257 {
258         static float size=viewdistance/4;
259         glPushMatrix();
260         static GLfloat M[16];
261         glGetFloatv(GL_MODELVIEW_MATRIX,M);
262         M[12]=0;
263         M[13]=0;
264         M[14]=0;
265         glLoadMatrixf(M);
266         //if(environment==2)glTranslatef(0,blurness*viewdistance/1000,0);
267         if(environment==2)glScalef(1+blurness/1000,1,1+blurness/1000);
268         if(environment!=2)glColor3f(.85*skyboxr,.85*skyboxg,.95*skyboxb);
269         else glColor3f(1*skyboxr,.95*skyboxg,.95*skyboxb);
270
271         if(!skyboxtexture){
272                 glDisable(GL_TEXTURE_2D); 
273                 glColor3f(skyboxr*.8,skyboxg*.8,skyboxb*.8);
274         }
275         glDepthMask(0);
276         glDisable(GL_CULL_FACE);
277         glEnable(GL_BLEND);
278         glDisable(GL_LIGHTING);
279         //              glActiveTextureARB(GL_TEXTURE0_ARB);
280         if(skyboxtexture)glEnable(GL_TEXTURE_2D);
281         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
282         glBindTexture(GL_TEXTURE_2D, front); 
283         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
284         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
285         glBegin(GL_QUADS);
286         glNormal3f( 0.0f, 0.0f, -1);
287         glTexCoord2f(0, 0); glVertex3f(-size, -size,  size);
288         glTexCoord2f(1, 0); glVertex3f( size, -size,  size);
289         glTexCoord2f(1, 1); glVertex3f( size,  size,  size);
290         glTexCoord2f(0, 1); glVertex3f(-size,  size,  size);
291         glEnd();
292         glBindTexture(GL_TEXTURE_2D, back);
293         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
294         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
295         glBegin(GL_QUADS);
296         glNormal3f( 0.0f, 0.0f, 1);
297         glTexCoord2f(1, 0); glVertex3f(-size, -size, -size);
298         glTexCoord2f(1, 1); glVertex3f(-size,  size, -size);
299         glTexCoord2f(0, 1); glVertex3f( size,  size, -size);
300         glTexCoord2f(0, 0); glVertex3f( size, -size, -size);
301         glEnd();
302         glBindTexture(GL_TEXTURE_2D, up);
303         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
304         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
305         glBegin(GL_QUADS);
306         glNormal3f( 0.0f, -1.0f, 0);
307         glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
308         glTexCoord2f(0, 0); glVertex3f(-size,  size,  size);
309         glTexCoord2f(1, 0); glVertex3f( size,  size,  size);
310         glTexCoord2f(1, 1); glVertex3f( size,  size, -size);
311         glEnd();
312         //if(detail!=0){
313         glBindTexture(GL_TEXTURE_2D, down);
314         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
315         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
316         glBegin(GL_QUADS);
317         glNormal3f( 0.0f, 1.0f, 0);
318
319         glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
320         glTexCoord2f(1, 0); glVertex3f( size, -size, -size);
321         glTexCoord2f(1, 1); glVertex3f( size, -size,  size);
322         glTexCoord2f(0, 1); glVertex3f(-size, -size,  size);
323         glEnd();
324         //}
325         glBindTexture(GL_TEXTURE_2D, right);
326         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
327         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
328         glBegin(GL_QUADS);
329         glNormal3f( -1.0f, 0.0f, 0);
330         glTexCoord2f(1, 0); glVertex3f( size, -size, -size);
331         glTexCoord2f(1, 1); glVertex3f( size,  size, -size);
332         glTexCoord2f(0, 1); glVertex3f( size,  size,  size);
333         glTexCoord2f(0, 0); glVertex3f( size, -size,  size);
334         glEnd();
335         glBindTexture(GL_TEXTURE_2D, left);
336         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
337         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
338         glBegin(GL_QUADS);
339         glNormal3f( 1.0f, 0.0f, 0);
340         glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
341         glTexCoord2f(1, 0); glVertex3f(-size, -size,  size);
342         glTexCoord2f(1, 1); glVertex3f(-size,  size,  size);
343         glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
344         glEnd();
345         /*
346         glEnable(GL_BLEND);
347         glColor4f(1,1,1,1);
348         glBindTexture(GL_TEXTURE_2D, cloud);
349         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
350         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
351         glBegin(GL_QUADS);
352         glNormal3f( 0.0f, -1.0f, 0);
353         glTexCoord2f(0,                                 (1+cloudmove)*30);      glVertex3f(-size,       size/200, -size);
354         glTexCoord2f(0,                                 0);                             glVertex3f(-size,       size/200,  size);
355         glTexCoord2f((1+cloudmove)*30,  0);                             glVertex3f( size,       size/200,  size);
356         glTexCoord2f((1+cloudmove)*30,  (1+cloudmove)*30);      glVertex3f( size,       size/200, -size);
357         glEnd();
358         glDisable(GL_BLEND);*/
359         glEnable(GL_CULL_FACE);
360         glDepthMask(1);
361         glPopMatrix();
362 }
363
364 SkyBox::SkyBox()
365 {
366         front = 0,left = 0,back = 0,right = 0,up = 0,down = 0,cloud = 0,reflect = 0;
367         cloudmove = 0;
368 }
369 SkyBox::~SkyBox()
370 {
371         if (front) glDeleteTextures( 1, &front );
372         if (left) glDeleteTextures( 1, &left );
373         if (back) glDeleteTextures( 1, &back );
374         if (right) glDeleteTextures( 1, &right );
375         if (up) glDeleteTextures( 1, &up );
376         if (down) glDeleteTextures( 1, &down );
377         if (cloud) glDeleteTextures( 1, &cloud );
378         if (reflect) glDeleteTextures( 1, &reflect );
379 };
380