-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathskybox.cpp
More file actions
202 lines (157 loc) · 5.49 KB
/
skybox.cpp
File metadata and controls
202 lines (157 loc) · 5.49 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
/*
Author: Lucas Parzych
This class creates a skybox with its own VAO.
A cube map texture is the primary mechanism by which this was accomplished.
cube maps r cewl.
This code is essentially a mash up of my own efforts, in combination with these articles:
http://antongerdelan.net/opengl/cubemaps.html
http://learnopengl.com/#!Advanced-OpenGL/Cubemaps
*/
using namespace std;
class skybox
{
//ID for appropriate shader
GLuint skyboxID;
//cube map texture itself
GLuint box;
//location of unform variable in fragment shader to identify the texture unit
GLuint textureID;
//location of VP matrix in vertex shader.
GLuint projectionID;
GLuint viewID;
//vertex array object
//holds all of the vertex info necessary for rendering
GLuint VAO;
GLuint loadCubemap(vector<const GLchar*> faces);
public:
skybox(void);
void load(glm::mat4 projection);
void update(glm::mat4 view);
void draw(void);
};
skybox::skybox(void)
{
}
void skybox::update(glm::mat4 view)
{
glUseProgram(skyboxID);
//remove the translation dimension from the camera's view matrix.
view = glm::mat4(glm::mat3(view));
//Inform the vertex shader of the new view matrix
glUniformMatrix4fv(viewID, 1, GL_FALSE, &view[0][0]);
}
void skybox::load(glm::mat4 projection)
{
//Load cube map shaders
skyboxID = loadShaders("skybox_fragment_shader.glsl", "skybox_vertex_shader.glsl");
glUseProgram(skyboxID);
//Get location of uniform variables in the shader program
textureID = glGetUniformLocation(skyboxID, "textureID"); //texture unit (fragment shader)
projectionID = glGetUniformLocation(skyboxID, "projection"); //projection matrix (vertex shader)
viewID = glGetUniformLocation(skyboxID, "view"); //view matrix (vertex shader)
//Set the uniform variable indicating the active texture unit (found in fragment shader) to 0
glUniform1i(textureID, 0);
//Pass projection matrix to shader, This isn't going to change so we can set it up now.
glUniformMatrix4fv(projectionID, 1, GL_FALSE, &projection[0][0]);
//Load the cube map, paint each side
vector<const GLchar*> faces;
faces.push_back("skyboxes/galaxy/right.png");
faces.push_back("skyboxes/galaxy/left.png");
faces.push_back("skyboxes/galaxy/top.png");
faces.push_back("skyboxes/galaxy/bottom.png");
faces.push_back("skyboxes/galaxy/back.png");
faces.push_back("skyboxes/galaxy/front.png");
box = loadCubemap(faces);
//Create unit cube to splat cube-map texture onto
GLfloat D =1.0f;
GLfloat vertices[] = {
-D, D, -D,
-D, -D, -D,
D, -D, -D,
D, -D, -D,
D, D, -D,
-D, D, -D,
-D, -D, D,
-D, -D, -D,
-D, D, -D,
-D, D, -D,
-D, D, D,
-D, -D, D,
D, -D, -D,
D, -D, D,
D, D, D,
D, D, D,
D, D, -D,
D, -D, -D,
-D, -D, D,
-D, D, D,
D, D, D,
D, D, D,
D, -D, D,
-D, -D, D,
-D, D, -D,
D, D, -D,
D, D, D,
D, D, D,
-D, D, D,
-D, D, -D,
-D, -D, -D,
-D, -D, D,
D, -D, -D,
D, -D, -D,
-D, -D, D,
D, -D, D
};
//using explicit AA 2 to pass vetex info to vertex shader
int AA_index=2;
//Generate VBO
GLuint VBO = createBuffer(GL_ARRAY_BUFFER, vertices, sizeof(GLfloat) * (36*3), GL_STATIC_DRAW); //buffer_tools.cpp
//Generate VAO
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glEnableVertexAttribArray(AA_index);
attributeBind(VBO, AA_index, 3); //buffer_tools.cpp
glBindVertexArray(0);
}
GLuint skybox::loadCubemap(vector<const GLchar*> faces)
{
GLuint tempTexture;
glGenTextures(1, &tempTexture);
glBindTexture(GL_TEXTURE_CUBE_MAP, tempTexture);
int width,height;
unsigned char* image;
for(GLuint i = 0; i < faces.size(); i++)
{
image = SOIL_load_image(faces[i], &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
return tempTexture;
}
void skybox::draw()
{
glUseProgram(skyboxID);
//don't write to Z buffer
glDepthMask(GL_FALSE);
//bind the skybox VAO (sets up all vertex data)
glBindVertexArray(VAO);
//bind texture object to cube-map target on texture unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, box);
//tell OpenGL to fill polygons
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//draw the sky box
glDrawArrays(GL_TRIANGLES, 0, 36);
//turn depth writing on again
glDepthMask(GL_TRUE);
//unbind VAO
glBindVertexArray(0);
//switch back to normal shaders
glUseProgram(programID);
}