-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSkybox.cpp
175 lines (137 loc) · 7.49 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
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
#define _CRT_SECURE_NO_DEPRECATE
#include "Skybox.h"
#include "Window.h"
#include "Project3s16-texture.cpp"
Skybox::Skybox()
{
initialize();
}
Skybox::Skybox(float mult) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 3; j++) {
this->vertices[i][j] *= mult;
}
}
initialize();
}
Skybox::~Skybox()
{
// Delete previously generated buffers. Note that forgetting to do this can waste GPU memory in a
// large project! This could crash the graphics driver due to memory leaks, or slow down application performance!
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
}
void Skybox::initialize() {
toWorld = glm::mat4(1.0f);
// Create array object and buffers. Remember to delete your buffers when the object is destroyed!
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// Bind the Vertex Array Object (VAO) first, then bind the associated buffers to it.
// Consider the VAO as a container for all your buffers.
glBindVertexArray(VAO);
// Now bind a VBO to it as a GL_ARRAY_BUFFER. The GL_ARRAY_BUFFER is an array containing relevant data to what
// you want to draw, such as vertices, normals, colors, etc.
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// glBufferData populates the most recently bound buffer with data starting at the 3rd argument and ending after
// the 2nd argument number of indices. How does OpenGL know how long an index spans? Go to glVertexAttribPointer.
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW);
// Enable the usage of layout location 0 (check the vertex shader to see what this is)
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,// This first parameter x should be the same as the number passed into the line "layout (location = x)" in the vertex shader. In this case, it's 0. Valid values are 0 to GL_MAX_UNIFORM_LOCATIONS.
3, // This second line tells us how any components there are per vertex. In this case, it's 3 (we have an x, y, and z component)
GL_FLOAT, // What type these components are
GL_FALSE, // GL_TRUE means the values should be normalized. GL_FALSE means they shouldn't
3 * sizeof(GLfloat), // Offset between consecutive indices. Since each of our vertices have 3 floats, they should have the size of 3 floats in between
(GLvoid*)0); // Offset of the first vertex's component. In our case it's 0 since we don't pad the vertices array with anything.
// We've sent the vertex data over to OpenGL, but there's still something missing.
// In what order should it draw those vertices? That's why we'll need a GL_ELEMENT_ARRAY_BUFFER for this.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Unbind the currently bound buffer so that we don't accidentally make unwanted changes to it.
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Unbind the VAO now so we don't accidentally tamper with it.
// NOTE: You must NEVER unbind the element array buffer associated with a VAO!
glBindVertexArray(0);
}
GLuint Skybox::loadCubemap() {
glGenTextures(1, &textureID);
int width, height;
unsigned char* image;
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
// Make sure no bytes are padded:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Select GL_MODULATE to mix texture with polygon color for shading:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
// Load front
image = loadPPM("skybox/ppm/front.ppm", width, height);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
//glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
// Load back
image = loadPPM("skybox/ppm/back.ppm", width, height);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
//glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
// Load bottom
image = loadPPM("skybox/ppm/bottom.ppm", width, height);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
// Load left
image = loadPPM("skybox/ppm/left.ppm", width, height);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
//glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
// Load right
image = loadPPM("skybox/ppm/right.ppm", width, height);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
//glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
// Load top
image = loadPPM("skybox/ppm/top.ppm", width, height);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
// Sets texture parameters
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);
// Unbinds texture
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
return textureID;
}
void Skybox::sendLight(GLuint shaderProgram) {
glUniform3f(glGetUniformLocation(shaderProgram, "dirLight.direction"), -0.2f, -1.0f, -0.3f);
glUniform3f(glGetUniformLocation(shaderProgram, "dirLight.ambient"), 0.9f, 0.6f, 0.7f);
glUniform3f(glGetUniformLocation(shaderProgram, "dirLight.diffuse"), 0.7f, 0.42f, 0.46f);
glUniform3f(glGetUniformLocation(shaderProgram, "dirLight.specular"), 0.5f, 0.5f, 0.5f);
}
void Skybox::draw(GLuint shaderProgram)
{
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_FALSE);
// Calculate the combination of the model and view (camera inverse) matrices
glm::mat4 model = toWorld;
glm::mat4 view(Window::V);
glm::mat4 projection = Window::P;
// Make it seem infinitely far away
for(int i = 0; i < 3; i++)
view[3][i] = 0;
// We need to calculate this because modern OpenGL does not keep track of any matrix other than the viewport (D)
// Consequently, we need to forward the projection, view, and model matrices to the shader programs
// Get the location of the uniform variables "projection" and "modelview"
uProjection = glGetUniformLocation(shaderProgram, "projection");
uView = glGetUniformLocation(shaderProgram, "view");
uModel = glGetUniformLocation(shaderProgram, "model");
// Now send these values to the shader program
glUniformMatrix4fv(uProjection, 1, GL_FALSE, &Window::P[0][0]);
glUniformMatrix4fv(uModel, 1, GL_FALSE, &model[0][0]);
glUniformMatrix4fv(uView, 1, GL_FALSE, &view[0][0]);
// Now draw the cube. We simply need to bind the VAO associated with it.
glBindVertexArray(VAO);
glActiveTexture(GL_TEXTURE0);
glUniform1i(glGetUniformLocation(shaderProgram, "skybox"), 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
// Tell OpenGL to draw with triangles, using 36 indices, the type of the indices, and the offset to start from
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
// Unbind the VAO when we're done so we don't accidentally draw extra stuff or tamper with its bound buffers
glBindVertexArray(0);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
}