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
+ }
0 commit comments