-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskybox.cpp
More file actions
356 lines (280 loc) · 12.2 KB
/
Copy pathskybox.cpp
File metadata and controls
356 lines (280 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* skybox.cpp - By Lucas McLane */
/* for Travis McLane's DScape 2002, 2003 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <stdio.h> // Header File For Standard Input/Output
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include "gl/glaux.h" // Header File For The Glaux Library
#include "skybox.h"
AUX_RGBImageRec *LoadBMP(char *Filename);
GLuint texture[6]; // Storage For 3 Textures
AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File=fopen(Filename,"r"); // Check To See If The File Exists
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}
void SkyBox::Render( TextureMap *SkyBox )
{
TextureMap *SkyBoxPtr = SkyBox;
/* glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glDisable(GL_FOG);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
*/
glDisable(GL_LIGHTING);
glColor3f(1.0f, 1.0f, 1.0f);
float SkyPoints[8][3] = //The skybox vertices...
{ //(created this way to avoid gaps between sides)
{-1.0f, -1.0f, -1.0f}, // 0
{ 1.0f, -1.0f, -1.0f}, // 1
{ 1.0f, 1.0f, -1.0f}, // 2
{-1.0f, 1.0f, -1.0f}, // 3
{-1.0f, -1.0f, 1.0f}, // 4
{ 1.0f, -1.0f, 1.0f}, // 5
{ 1.0f, 1.0f, 1.0f}, // 6
{-1.0f, 1.0f, 1.0f}, // 7
};
glVertexPointer(3, GL_FLOAT, 0, SkyPoints);
// glScalef(0.5f, 0.5f, 0.5f);
glBindTexture(GL_TEXTURE_2D, SkyBoxPtr->ID);
glBegin(GL_QUADS);
glNormal3f( -1.0, 0.0, 0.0);
glTexCoord2f(1.0f, 0.0f); glArrayElement(1); //Right face
glTexCoord2f(1.0f, 1.0f); glArrayElement(2);
glTexCoord2f(0.0f, 1.0f); glArrayElement(6);
glTexCoord2f(0.0f, 0.0f); glArrayElement(5);
glEnd();
SkyBoxPtr++;
glBindTexture(GL_TEXTURE_2D, SkyBoxPtr->ID);
glBegin(GL_QUADS);
glNormal3f( 0.0, 0.0, 1.0);
glTexCoord2f(0.0f, 0.0f); glArrayElement(0); //Left face
glTexCoord2f(1.0f, 0.0f); glArrayElement(4);
glTexCoord2f(1.0f, 1.0f); glArrayElement(7);
glTexCoord2f(0.0f, 1.0f); glArrayElement(3);
glEnd();
SkyBoxPtr++;
glBindTexture(GL_TEXTURE_2D, SkyBoxPtr->ID );
glBegin(GL_QUADS);
glNormal3f( 0.0, 1.0, 0.0);
glTexCoord2f(0.0f, 0.0f); glArrayElement(4); //Bottom face
glTexCoord2f(1.0f, 0.0f); glArrayElement(5);
glTexCoord2f(1.0f, 1.0f); glArrayElement(6);
glTexCoord2f(0.0f, 1.0f); glArrayElement(7);
glEnd();
SkyBoxPtr++;
glBindTexture(GL_TEXTURE_2D, SkyBoxPtr->ID);
glBegin(GL_QUADS);
glNormal3f( 0.0, -1.0, 0.0);
glTexCoord2f(1.0f, 0.0f); glArrayElement(0); //Top face
glTexCoord2f(1.0f, 1.0f); glArrayElement(3);
glTexCoord2f(0.0f, 1.0f); glArrayElement(2);
glTexCoord2f(0.0f, 0.0f); glArrayElement(1);
glEnd();
SkyBoxPtr++;
glBindTexture(GL_TEXTURE_2D, SkyBoxPtr->ID);
glBegin(GL_QUADS);
glNormal3f( 0.0, 0.0, -1.0);
glTexCoord2f(1.0f, 0.0f); glArrayElement(3); //Back face
glTexCoord2f(1.0f, 1.0f); glArrayElement(7);
glTexCoord2f(0.0f, 1.0f); glArrayElement(6);
glTexCoord2f(0.0f, 0.0f); glArrayElement(2);
glEnd();
SkyBoxPtr++;
glBindTexture(GL_TEXTURE_2D, SkyBoxPtr->ID);
glBegin(GL_QUADS);
glNormal3f( 1.0, 0.0, 0.0);
glTexCoord2f(1.0f, 1.0f); glArrayElement(0); //Front face
glTexCoord2f(0.0f, 1.0f); glArrayElement(1);
glTexCoord2f(0.0f, 0.0f); glArrayElement(5);
glTexCoord2f(1.0f, 0.0f); glArrayElement(4);
glEnd();
glDisableClientState(GL_VERTEX_ARRAY);
// glDisable(GL_BLEND);
// glEnable(GL_DEPTH_TEST);
// glEnable(GL_CULL_FACE);
// glEnable(GL_FOG);
}
bool TextureMap::LoadTGA(char* filename, GLfloat minFilter, GLfloat maxFilter)
{
GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0}; //Uncompressed TGA header
GLubyte TGAcompare[12]; //Used to compare TGA header
GLubyte header[6]; //The first six useful bytes from the header
GLuint bytesPerPixel; //Holds the bpp of the TGA
GLuint imageSize; //Used to store image size while in RAM
GLuint temp; //Temp variable
GLuint type=GL_RGBA; //Set the default OpenGL mode to RBGA (32 BPP)
FILE* file = fopen(filename, "rb"); // Open The TGA File
if(file==NULL || // Does File Even Exist?
fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) || // Are There 12 Bytes To Read?
memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 || // Does The Header Match What We Want?
fread(header,1,sizeof(header),file)!=sizeof(header)) // If So Read Next 6 Header Bytes
{
if(file==NULL) // Did The File Even Exist? *Added Jim Strong*
{
return false;
}
else
{
fclose(file); // If anything failed, close the file
return false;
}
}
width = header[1] * 256 + header[0]; // Determine The TGA Width (highbyte*256+lowbyte)
height= header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte)
if(width <=0 || // Is The Width Less Than Or Equal To Zero
height<=0 || // Is The Height Less Than Or Equal To Zero
(header[4]!=24 && header[4]!=32)) // Is The TGA 24 or 32 Bit?
{
fclose(file); // If Anything Failed, Close The File
return false;
}
bpp = header[4]; // Grab The TGA's Bits Per Pixel (24 or 32)
bytesPerPixel= bpp/8; // Divide By 8 To Get The Bytes Per Pixel
imageSize = width*height*bytesPerPixel; // Calculate The Memory Required For The TGA Data
data= new GLubyte [imageSize]; // Reserve Memory To Hold The TGA Data
if(data==NULL || // Does The Storage Memory Exist?
fread(data, 1, imageSize, file)!=imageSize) // Does The Image Size Match The Memory Reserved?
{
if(data!=NULL) // Was Image Data Loaded
free(data); // If So, Release The Image Data
fclose(file); // Close The File
return false; // Return False
}
for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel) // Loop Through The Image Data
{ // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
temp =data[i]; // Temporarily Store The Value At Image Data 'i'
data[i] = data[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
data[i+2]= temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
}
fclose (file); //Close the file
glGenTextures(1, &ID); //Generate OpenGL texture IDs
glBindTexture(GL_TEXTURE_2D, ID); //Bind the texture to a texture object
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width,
height, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter); //Filtering for if texture is bigger than should be
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxFilter); //Filtering for if texture is smaller than it should be
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
if(bpp==24) //Was the TGA 24 bpp?
type=GL_RGB;
// glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, data);
return true;
}
int LoadSkyTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator
AUX_RGBImageRec *TextureImage[6]; // Create Storage Space For The Texture
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
TextureImage[0]=LoadBMP("Art/skybox/Back.bmp");
TextureImage[1]=LoadBMP("Art/skybox/Front.bmp");
TextureImage[2]=LoadBMP("Art/skybox/Bottom.bmp");
TextureImage[3]=LoadBMP("Art/skybox/Top.bmp");
TextureImage[4]=LoadBMP("Art/skybox/Right.bmp");
TextureImage[5]=LoadBMP("Art/skybox/Left.bmp");
for(int count=0; count < 6; count++)
{
Status=TRUE; // Set The Status To TRUE
glGenTextures(1, &texture[count]); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[count]);
// glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[count]->sizeX, TextureImage[count]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[count]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[count]->sizeX,
TextureImage[count]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[count]->data);
if (TextureImage[count]) // If Texture Exists
{
if (TextureImage[count]->data) // If Texture Image Exists
{
free(TextureImage[count]->data); // Free The Texture Image Memory
}
free(TextureImage[count]); // Free The Image Structure
}
}
return Status; // Return The Status
}
int DrawSkyBox(GLvoid) // Here's Where We Do All The Drawing
{
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
// glLoadIdentity(); // Reset The View
// glTranslatef(0.0f,0.0f,z);
glScalef(2000.0, 2000.0, 2000.0);
float x, y, z;
x = 1.0f;
y = 1.0f;
z = 1.0f;
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f( x, -y, -z);
glTexCoord2f(1.0f, 1.0f); glVertex3f( x, y, -z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-x, y, -z);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-x, -y, -z);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-x, -y, z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-x, y, z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y, z);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, -y, z);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-x, -y, -z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-x, -y, z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, -y, z);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, -y, -z);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[3]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y, -z);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-x,y, z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-x,y, -z);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[5]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-x, y, -z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-x, y, z);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-x, -y, z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-x, -y, -z);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[4]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, -y, -z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, -y, z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y, z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y, -z);
glEnd();
glScalef(0.0005, 0.0005, 0.0005);
return TRUE; // Keep Going
}