-
Notifications
You must be signed in to change notification settings - Fork 0
/
skybox.cpp
106 lines (95 loc) · 4.08 KB
/
skybox.cpp
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
// Taylor Seale
// skybox.cpp
// implementation for a skybox class
#include "skybox.h"
#include "texture.h"
#include <iostream>
using namespace std;
skybox::skybox(string topFile, string leftFile, string rightFile, string frontFile, string backFile, string bottomFile){
int texWidth,texHeight;
unsigned char *texData;
// read and register the top texture
readPPM(topFile, texWidth, texHeight, texData);
registerOpenGLTexture(texData, texWidth, texHeight, topTexHandle);
// read and register the left texture
readPPM(leftFile, texWidth, texHeight, texData);
registerOpenGLTexture(texData, texWidth, texHeight, leftTexHandle);
// read and register the right texture
readPPM(rightFile, texWidth, texHeight, texData);
registerOpenGLTexture(texData, texWidth, texHeight, rightTexHandle);
// read and register the front texture
readPPM(frontFile, texWidth, texHeight, texData);
registerOpenGLTexture(texData, texWidth, texHeight, frontTexHandle);
// read and register the back texture
readPPM(backFile, texWidth, texHeight, texData);
registerOpenGLTexture(texData, texWidth, texHeight, backTexHandle);
// read and register the bottom texture
readPPM(bottomFile, texWidth, texHeight, texData);
registerOpenGLTexture(texData, texWidth, texHeight, bottomTexHandle);
}
void skybox::drawSkybox(int length, int width, int height){
glPushMatrix();
// Enable/Disable features
glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_BLEND);
// Just in case we set all vertices to white.
glColor4f(1,1,1,1);
// Render the front quad
glBindTexture(GL_TEXTURE_2D, frontTexHandle);
glBegin(GL_QUADS);
glTexCoord2f(1, 1); glVertex3f( width, -height, -length );
glTexCoord2f(0, 1); glVertex3f( -width, -height, -length);
glTexCoord2f(0, 0); glVertex3f( -width, height, -length );
glTexCoord2f(1, 0); glVertex3f( width, height, -length );
glEnd();
// Render the left quad
glBindTexture(GL_TEXTURE_2D, rightTexHandle);
glBegin(GL_QUADS);
glTexCoord2f(1, 1); glVertex3f( width, -height, length );
glTexCoord2f(0, 1); glVertex3f( width, -height, -length );
glTexCoord2f(0, 0); glVertex3f( width, height, -length );
glTexCoord2f(1, 0); glVertex3f( width, height, length );
glEnd();
// Render the back quad
glBindTexture(GL_TEXTURE_2D, backTexHandle);
glBegin(GL_QUADS);
glTexCoord2f(1, 1); glVertex3f( -width, -height, length );
glTexCoord2f(0, 1); glVertex3f( width, -height, length );
glTexCoord2f(0, 0); glVertex3f( width, height, length );
glTexCoord2f(1, 0); glVertex3f( -width, height, length );
glEnd();
// Render the right quad
glBindTexture(GL_TEXTURE_2D, leftTexHandle);
glBegin(GL_QUADS);
glTexCoord2f(1, 1); glVertex3f( -width, -height, -length );
glTexCoord2f(0, 1) ; glVertex3f( -width, -height, length );
glTexCoord2f(0, 0); glVertex3f( -width, height, length );
glTexCoord2f(1, 0); glVertex3f( -width, height, -length );
glEnd();
// Render the top quad
glBindTexture(GL_TEXTURE_2D, topTexHandle);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex3f( -width, height, -length );
glTexCoord2f(0, 0); glVertex3f( -width, height, length );
glTexCoord2f(1, 0); glVertex3f( width, height, length );
glTexCoord2f(1, 1); glVertex3f( width, height, -length );
glEnd();
// Render the bottom quad
glBindTexture(GL_TEXTURE_2D, bottomTexHandle);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex3f( -width, -height, -length );
glTexCoord2f(0, 0); glVertex3f( -width, -height, length );
glTexCoord2f(1, 0); glVertex3f( width, -height, length );
glTexCoord2f(1, 1); glVertex3f( width, -height, -length );
glEnd();
// Restore enable bits and matrix
glPopAttrib();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_BLEND);
}