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