Skip to content

Commit eeef7e8

Browse files
committed
Tutorial 17 - Sky Box
Usamos los Cube Maps para crear un sky box.
1 parent 111a8df commit eeef7e8

14 files changed

+459
-0
lines changed

Data/image/skybox/bk.jpg

157 KB
Loading

Data/image/skybox/dn.jpg

89.6 KB
Loading

Data/image/skybox/ft.jpg

122 KB
Loading

Data/image/skybox/lt.jpg

135 KB
Loading

Data/image/skybox/readme.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Skybox textures courtesy of The Mighty Pete.
2+
Obtained from The Wadfather (http://www.planethalflife.com/wadfather/).
3+
Used with permission.

Data/image/skybox/rt.jpg

169 KB
Loading

Data/image/skybox/up.jpg

109 KB
Loading

Tutorial-17/CMakeLists.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
project(17-SkyBox)
2+
3+
file(COPY skybox.vs.glsl terrain.vs.glsl terrain.fs.glsl skybox.fs.glsl DESTINATION shaders)
4+
file(COPY ../data/image/skybox DESTINATION image)
5+
file(COPY ../data/image/heightmap.png ../data/image/terreno.jpg DESTINATION image)
6+
7+
add_executable( ${PROJECT_NAME} skybox.cpp
8+
terrain.hpp
9+
shaders/skybox.fs.glsl
10+
shaders/skybox.vs.glsl
11+
../common/openglwindow.hpp
12+
../common/openglshader.hpp
13+
../common/openglcamera.hpp )
14+
15+
target_link_libraries( ${PROJECT_NAME} ${GRAPHIC_LIBS} )

Tutorial-17/skybox.cpp

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#include "OpenGLWindow.hpp"
2+
#include "OpenGLShader.hpp"
3+
#include "OpenGLCamera.hpp"
4+
5+
#include "terrain.hpp"
6+
7+
#include <glm\gtc\matrix_inverse.hpp>
8+
#include <glm\gtc\matrix_transform.hpp>
9+
#include <glm\gtc\type_ptr.hpp>
10+
11+
#include <string>
12+
#include <vector>
13+
14+
class Tutorial_OpenGL_Moderno : public OpenGLWindow {
15+
16+
public:
17+
Tutorial_OpenGL_Moderno() { }
18+
19+
OpenGLShader shader;
20+
OpenGLCamera camera;
21+
Terrain terreno;
22+
23+
GLuint skyboxTextureID;
24+
GLuint boxVAO;
25+
26+
private:
27+
void onstart() override {
28+
glEnable(GL_DEPTH_TEST);
29+
30+
glClearColor(0.0f, 0.5f, 0.5f, 1.0f);
31+
32+
camera.setWindow(this->window);
33+
shader.compile("shaders/skybox.vs.glsl", "shaders/skybox.fs.glsl");
34+
35+
terreno.init();
36+
37+
std::vector<std::string> textures = {
38+
"image/skybox/rt.jpg",
39+
"image/skybox/lt.jpg",
40+
"image/skybox/up.jpg",
41+
"image/skybox/dn.jpg",
42+
"image/skybox/ft.jpg",
43+
"image/skybox/bk.jpg"
44+
};
45+
46+
loadSkyBoxTextures(textures);
47+
createBox();
48+
49+
glfwSetInputMode(this->window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
50+
glfwSetCursorPos(this->window, 1280 / 2, 720 / 2);
51+
}
52+
53+
void loadSkyBoxTextures(std::vector<std::string> textures) {
54+
glGenTextures(1, &skyboxTextureID);
55+
56+
glActiveTexture(GL_TEXTURE0);
57+
glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTextureID);
58+
59+
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
60+
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
61+
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
62+
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);
63+
64+
for (int i = 0; i < 6; i++) {
65+
int channels, width, height;
66+
67+
unsigned char *pData = stbi_load(textures[i].c_str(), &width, &height, &channels, STBI_rgb);
68+
69+
if (pData == nullptr) cout << "Error al cargar: " << textures[i] << endl;
70+
71+
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pData);
72+
stbi_image_free(pData);
73+
}
74+
75+
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
76+
}
77+
78+
void createBox() {
79+
glGenVertexArrays(1, &boxVAO);
80+
glBindVertexArray(boxVAO);
81+
82+
// vetices para generar un cubo 3D
83+
static const float vertex[] =
84+
{
85+
1.0f, -1.0f, -1.0f, 1.0f,
86+
1.0f, -1.0f, 1.0f, 1.0f,
87+
-1.0f, -1.0f, 1.0f, 1.0f,
88+
-1.0f, -1.0f, -1.0f, 1.0f,
89+
1.0f, 1.0f, -1.0f, 1.0f,
90+
1.0f, 1.0f, 1.0f, 1.0f,
91+
-1.0f, 1.0f, 1.0f, 1.0f,
92+
-1.0f, 1.0f, -1.0f, 1.0f
93+
};
94+
95+
// indices usados para unir los vertices que componen el cubo
96+
static const GLushort indices[] =
97+
{
98+
0, 1, 2, 3, 0, 2,
99+
4, 5, 1, 0, 4, 1,
100+
4, 7, 5, 5, 6, 2,
101+
6, 7, 3, 0, 3, 7,
102+
7, 6, 5, 1, 5, 2,
103+
2, 6, 3, 4, 0, 7
104+
};
105+
106+
GLuint index_buffer = 0;
107+
glGenBuffers(1, &index_buffer);
108+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
109+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
110+
111+
GLuint buffer = 0;
112+
glGenBuffers(1, &buffer);
113+
glBindBuffer(GL_ARRAY_BUFFER, buffer);
114+
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
115+
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
116+
glEnableVertexAttribArray(0);
117+
118+
glBindVertexArray(0);
119+
}
120+
121+
void onrender(double time) override {
122+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
123+
124+
terreno.render(camera);
125+
126+
shader.use();
127+
128+
glm::mat4 M;
129+
M = glm::translate(M, camera.getPosition());
130+
M = glm::scale(M, glm::vec3(100.0f));
131+
132+
glm::mat4 MV = camera.getViewMatrix() * M;
133+
glm::mat4 MVP = camera.getProjectionMatrix() * MV;
134+
135+
glUniformMatrix4fv(shader.getUniformLocation("MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
136+
glUniformMatrix4fv(shader.getUniformLocation("MV"), 1, GL_FALSE, glm::value_ptr(MV));
137+
138+
glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTextureID);
139+
140+
glBindVertexArray(boxVAO);
141+
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
142+
glBindVertexArray(0);
143+
144+
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
145+
146+
shader.unUse();
147+
}
148+
149+
void onkey(int key, int scancode, int action, int mods) override {
150+
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
151+
glfwSetWindowShouldClose(window, GL_TRUE);
152+
}
153+
154+
};
155+
156+
int main() {
157+
Tutorial_OpenGL_Moderno win_app;
158+
159+
if (win_app.init("OpenGL Moderno - SkyBox", 1280, 720)) {
160+
win_app.info();
161+
162+
cout << "TOTURIALES OPENGL MODERNO - SKYBOX" << endl << endl;
163+
cout << "Usar la tecla [Esc] para salir." << endl << endl;
164+
165+
win_app.run();
166+
}
167+
168+
return 0;
169+
}

Tutorial-17/skybox.fs.glsl

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#version 330 core
2+
3+
out vec4 color;
4+
uniform samplerCube cubeMap;
5+
in vec3 uv;
6+
7+
void main()
8+
{
9+
color = texture(cubeMap, uv);
10+
}

Tutorial-17/skybox.vs.glsl

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 330 core
2+
3+
layout (location = 0) in vec4 position;
4+
uniform mat4 MVP, MV;
5+
out vec3 uv;
6+
7+
void main()
8+
{
9+
gl_Position = MVP * position;
10+
uv = position.xyz;
11+
}

Tutorial-17/terrain.fs.glsl

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#version 330 core
2+
3+
out vec4 color;
4+
5+
in vec3 POS, NORMAL;
6+
in vec2 UV;
7+
8+
uniform vec3 light_position ;
9+
uniform vec3 diffuse_color;
10+
uniform sampler2D terrain;
11+
12+
void main() {
13+
vec3 N = normalize(NORMAL);
14+
vec3 L = normalize(light_position - POS);
15+
16+
float diffuse = max(0, dot(N, L));
17+
18+
//color = diffuse * vec4(diffuse_color, 1.0);
19+
color = diffuse * texture(terrain, UV);
20+
//color = vec4(1.0);
21+
}

0 commit comments

Comments
 (0)