-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADSDA.cpp
More file actions
368 lines (280 loc) · 10.1 KB
/
Copy pathADSDA.cpp
File metadata and controls
368 lines (280 loc) · 10.1 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*//GLEW
#define GLEW_STATIC
#include "../include/glew/include/GL/glew.h"
//GLFW
#include "../include/glfw/include/GLFW/glfw3.h"
#include "../include/soil/src/SOIL.h"
#include "../include/glm/gtc/type_ptr.hpp"
#include "../include/glm/gtc/matrix_transform.hpp"
#include "../include/glm/gtc/type_ptr.hpp"
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include "../Shader.h"
#include <chrono>
using namespace std;
const GLint WIDTH = 800, HEIGHT = 600;
bool WIDEFRAME = false;
GLsizei numBuffers = 3;
float switcher = 0.0;
float angle = 0.0;
bool rotateleft;
bool rotateRight;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode); // capta keyPresses, ect
int main() {
auto t_start = std::chrono::high_resolution_clock::now();
//initGLFW
glEnable(GL_DEPTH_TEST);
if (!glfwInit())
::exit(EXIT_FAILURE);
//set GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
//create a window
GLFWwindow* window;
window = glfwCreateWindow(WIDTH, HEIGHT, "Trasform.", nullptr, nullptr);
if (!window) {
cout << "Error al crear la ventana" << endl;
glfwTerminate();
::exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
//set GLEW and inicializate
glewExperimental = GL_TRUE;
if (GLEW_OK != glewInit()) {
std::cout << "Error al inicializar glew" << std::endl;
glfwTerminate();
return NULL;
}
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
//set function when callback
glfwSetKeyCallback(window, key_callback);
//fondo
glClearColor(0.5, 0, 0.5, 1.0);
//cargamos los shader
Shader squareShader("./src/SimpleVertexShader.vertexshader", "./src/SimpleFragmentShader.fragmentshader");
Shader textureShader("./src/textureVertex.vertexshader", "./src/textureFragment.fragmentshader");
Shader cubeShader("./src/cubeVertex.v", "./src/cubeFragment.f");
//square
GLfloat vertexBufferObject[] = {
0.5f, 0.5f, 0.f, // 0. top right
0.5f, -0.75f, 0.f,// 1. bot right
-0.5f, -0.75f, 0.f,// 2. bot left
-0.5f, 0.5f, 0.f, // 3. top left
};
GLuint indexBufferObject[] = {
0, 1, 3,
1, 2, 3
};
//texture
GLfloat vertices[] = {
// Position Color Texcoords
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
0.5f, 0.5f,0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right
0.5f, -0.75f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
-0.5f, -0.75f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f // Bottom-left
};
GLfloat textureBufferObject[] = {
0.5f, 0.5f, 0.f, // 0. top right
0.5f, -0.5f, 0.f,// 1. bot right
-0.5f, -0.5f, 0.f,// 2. bot left
-0.5f, 0.5f, 0.f, // 3. top left
};
//cube
GLfloat VertexBufferCube[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f , -0.5f, -0.5f, 1.0f, 0.0f,
0.5f , 0.5f, -0.5f, 1.0f, 1.0f,
0.5f , 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f , -0.5f, 0.5f, 1.0f, 0.0f,
0.5f , 0.5f, 0.5f, 1.0f, 1.0f,
0.5f , 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f , 0.5f, 0.5f, 1.0f, 0.0f,
0.5f , 0.5f, -0.5f, 1.0f, 1.0f,
0.5f , -0.5f, -0.5f, 0.0f, 1.0f,
0.5f , -0.5f, -0.5f, 0.0f, 1.0f,
0.5f , -0.5f, 0.5f, 0.0f, 0.0f,
0.5f , 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f , -0.5f, -0.5f, 1.0f, 1.0f,
0.5f , -0.5f, 0.5f, 1.0f, 0.0f,
0.5f , -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f , 0.5f, -0.5f, 1.0f, 1.0f,
0.5f , 0.5f, 0.5f, 1.0f, 0.0f,
0.5f , 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
glm::vec3 CubesPositionBuffer[] = {
glm::vec3(0.0f , 0.0f, 0.0f),
glm::vec3(2.0f , 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f , -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3(1.3f , -2.0f, -2.5f),
glm::vec3(1.5f , 2.0f, -2.5f),
glm::vec3(1.5f , 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
// crear vao, vbo i ebo
GLuint VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
GLuint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // 1= sizeof(verterxbuffer ibkect), 0 = vertexbufferobject
//delete[] vertexBufferObject;
GLuint EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexBufferObject), indexBufferObject, GL_STATIC_DRAW); // 1 = SIZEOF INDEXBUFFEROBJECT 0 = INDEXBUFFEROBJECT
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GL_FLOAT), (GLvoid*)0);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GL_FLOAT), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(2);
glBindVertexArray(0);
//load textures
int width, height;
unsigned char *img;
GLint texAttrib = glGetAttribLocation(squareShader.Program, "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE,
8 * sizeof(float), (void*)(6 * sizeof(float)));
GLuint texture1;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img = SOIL_load_image("gr8.png", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
SOIL_free_image_data(img);
GLuint texture2;
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img = SOIL_load_image("fire.jpg", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
SOIL_free_image_data(img);
GLint variableShader = glGetUniformLocation(squareShader.Program, "Sion");// variable indicada anteriorment jeje
GLint imgSwitcher = glGetUniformLocation(textureShader.Program, "alternador"); // alternador entre imatges
while (!glfwWindowShouldClose(window)) {
// Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
glViewport(0, 0, screenWidth, screenHeight);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Establecer el color de fondo
glClearColor(0.5, 0.2, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10, -10.f, 10.f, -1.0f, 10.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//establecer el shader
squareShader.USE();
//textureShader.USE();
//cubeShader.USE();
//pasasr valors als shaders
glUniform1f(variableShader, abs(sin(glfwGetTime()) / 2)); // pasa un float a sa variable indicada anteriorment
glUniform1f(imgSwitcher, switcher);
// rotar sa foto. valor
if (rotateleft)
angle -= 0.01f;
if (rotateRight)
angle += 0.01f;
if (!rotateRight && !rotateleft) {
angle = 0;
}
//mesclar foto. valor
if (switcher > 1)
switcher = 1;
if (switcher < 0.1)
switcher = 0;
//activar texturas
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glUniform1i(glGetUniformLocation(textureShader.Program, "tex1"), 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glUniform1i(glGetUniformLocation(textureShader.Program, "tex2"), 1);
glBindVertexArray(VAO);
//pitar es VAO
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (GLvoid*)0);
//square things
if (WIDEFRAME) {
//pintar con lineas
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawElements(GL_LINE_STRIP, 7, GL_UNSIGNED_INT, (GLvoid*)0);
glBindVertexArray(0);
}
else {
//pintar con triangulos
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (GLvoid*)0);
glBindVertexArray(0);
}
// Swap the screen buffers. Essencial
glfwSwapBuffers(window);
//comprueba que algun disparador se haya activado (tales como el teclado, raton, etc)
glfwSetKeyCallback(window, key_callback);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
// Terminate GLFW, clearing any resources allocated by GLFW.
::exit(EXIT_SUCCESS);
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
//TODO, comprobar que la tecla pulsada es escape para cerrar la aplicación y la tecla w para cambiar a modo widwframe
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
else if (key == GLFW_KEY_W && action == GLFW_PRESS && WIDEFRAME == false) {
WIDEFRAME = true;
}
else if (key == GLFW_KEY_W && action == GLFW_PRESS && WIDEFRAME == true) {
WIDEFRAME = false;
}
else if ((key == GLFW_KEY_UP && action == GLFW_PRESS)) {
switcher += 0.1;
}
else if ((key == GLFW_KEY_DOWN && action == GLFW_PRESS)) {
switcher -= 0.1;
}
else if ((key == GLFW_KEY_RIGHT && action == GLFW_PRESS)) {
rotateRight = true;
}
else if ((key == GLFW_KEY_RIGHT && action == GLFW_RELEASE)) {
rotateRight = false;
}
else if ((key == GLFW_KEY_LEFT && action == GLFW_PRESS)) {
rotateleft = true;
}
else if ((key == GLFW_KEY_LEFT && action == GLFW_RELEASE)) {
rotateleft = false;
}
}*/