-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexturedCube.cpp
445 lines (341 loc) · 11 KB
/
TexturedCube.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//////////////////////////////////////////////////////////////////////////////
//
// Triangles.cpp
//
//////////////////////////////////////////////////////////////////////////////
#include "GL/glew.h"
#include "GL/freeglut.h"
#include "GLFW/glfw3.h"
#include "LoadShaders.h"
#include <glm/glm.hpp> //includes GLM
#include "glm/ext/vector_float3.hpp"
#include "glm/fwd.hpp"
#include <glm/ext/matrix_transform.hpp> // GLM: translate, rotate
#include <glm/ext/matrix_clip_space.hpp> // GLM: perspective and ortho
#include <glm/gtc/type_ptr.hpp> // GLM: access to the value_ptr
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <iostream>
#include<vector>
#include "TexturedCube.h"
// to use this example you will need to download the header files for GLM put them into a folder which you will reference in
// properties -> VC++ Directories -> libraries
enum VAO_IDs { Cube, NumVAOs = 1 };
enum Buffer_IDs { Triangles, Colours, Normals, Textures, Indices, NumBuffers = 5 };
GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];
GLuint texture1;
GLuint shader;
//----------------------------------------------------------------------------
//
// init
//
#define BUFFER_OFFSET(a) ((void*)(a))
void
init(void)
{
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Cube]);
ShaderInfo shaders[] =
{
{ GL_VERTEX_SHADER, "media/triangles.vert" },
{ GL_FRAGMENT_SHADER, "media/triangles.frag" },
{ GL_NONE, NULL }
};
shader = LoadShaders(shaders);
glUseProgram(shader);
//
// configuring lighting
//
// ambient light
glm::vec4 ambient = glm::vec4(0.1f, 0.1f, 0.1f, 1.0f);
//adding the Uniform to the shader
GLuint aLoc = glGetUniformLocation(shader, "ambient");
glUniform4fv(aLoc, 1, glm::value_ptr(ambient));
// light object
glm::vec3 lightPos = glm::vec3(100.0f, 25.0f, 100.0f);
GLuint dLightPosLoc = glGetUniformLocation(shader, "lightPos");
glUniform3fv(dLightPosLoc, 1, glm::value_ptr(lightPos));
// diffuse light
glm::vec3 diffuseLight = glm::vec3(0.5f, 0.2f, 0.7f);
GLuint dLightLoc = glGetUniformLocation(shader, "dLight");
glUniform3fv(dLightLoc, 1, glm::value_ptr(diffuseLight));
// specular light
glm::vec3 specularLight = glm::vec3(0.7f);
GLfloat shininess = 256; //128 is max value
GLuint sLightLoc = glGetUniformLocation(shader, "sLight");
GLuint sShineLoc = glGetUniformLocation(shader, "sShine");
glUniform3fv(sLightLoc, 1, glm::value_ptr(specularLight));
glUniform1fv(sShineLoc, 1, &shininess);
// setting up the cube
GLfloat vertices[] = {
-0.5f, -0.5f, -0.5f, //0 b l
0.5f, -0.5f, -0.5f, //1 b r
0.5f, 0.5f, -0.5f, //2 t r
0.5f, 0.5f, -0.5f, //3 t r
-0.5f, 0.5f, -0.5f, //4 t l
-0.5f, -0.5f, -0.5f, //5 b l
-0.5f, -0.5f, 0.5f, //6 b l
0.5f, -0.5f, 0.5f, //7 b r
0.5f, 0.5f, 0.5f, //8 t r
0.5f, 0.5f, 0.5f, //9 t r
-0.5f, 0.5f, 0.5f, //10 t l
-0.5f, -0.5f, 0.5f, //11 b l
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f, // b l b
0.5f, -0.5f, -0.5f, // b r b
0.5f, -0.5f, 0.5f, // b r f
0.5f, -0.5f, 0.5f, // b r b
-0.5f, -0.5f, 0.5f, // b l f
-0.5f, -0.5f, -0.5f, // b l b
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
};
GLfloat normals[]{
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f };
GLuint indices[][3] = { // note that we start from 0!
{0, 1, 2}, // first Triangle front
{3, 4, 5}, // second Triangle
{8, 7, 6 },
{11, 10, 9 },
{14, 13, 12 },
{17, 16, 15 },
{18, 19, 20 },
{21, 22, 23 },
{26, 25, 24 },
{29, 28, 27 },
{30, 31, 32 }, // first Triangle back
{33, 34, 35 } // second Triangle
};
GLfloat colours[][4] = {
{ 1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f },
{ 1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f },
{ 0.0f, 1.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f },
{ 0.0f, 1.0f, 0.0f, 1.0f }, { 0.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0f,
0.0f, 1.0f,
0.0f, 0.0f,
0.0f, 0.0f,
1.0f,0.0f,
1.0f, 1.0f,
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f
};
glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[Triangles]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Buffers[Indices]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(Triangles, 3, GL_FLOAT,
GL_FALSE, 0, BUFFER_OFFSET(0));
//Colour Binding
glBindBuffer(GL_ARRAY_BUFFER, Buffers[Colours]);
glBufferStorage(GL_ARRAY_BUFFER, sizeof(colours), colours, 0);
glVertexAttribPointer(Colours, 4, GL_FLOAT,
GL_FALSE, 0, BUFFER_OFFSET(0));
//Colour Binding
glBindBuffer(GL_ARRAY_BUFFER, Buffers[Normals]);
glBufferStorage(GL_ARRAY_BUFFER, sizeof(normals), normals, 0);
glVertexAttribPointer(Normals, 3, GL_FLOAT,
GL_FALSE, 0, BUFFER_OFFSET(0));
//Texture Binding
glBindBuffer(GL_ARRAY_BUFFER, Buffers[Textures]);
glBufferData(GL_ARRAY_BUFFER, sizeof(texture_coords), texture_coords, GL_STATIC_DRAW);
glVertexAttribPointer(Textures, 2, GL_FLOAT,
GL_FALSE, 0, BUFFER_OFFSET(0));
// load and create a texture
// -------------------------
// texture 1
// ---------
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
GLint width, height, nrChannels;
stbi_set_flip_vertically_on_load(false); // tell stb_image.h to flip loaded texture's on the y-axis.
unsigned char* data = stbi_load("media/textures/awesomeface.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
glUniform1i(glGetUniformLocation(shader, "texture1"), 0);
// creating the model matrix
glm::mat4 model = glm::mat4(1.0f);
model = glm::scale(model, glm::vec3(2.0f, 2.0f, 2.0f));
// creating the view matrix
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -4.0f));
// creating the projection matrix
glm::mat4 projection = glm::perspective(45.0f, 4.0f / 3, 0.1f, 20.0f);
// Adding all matrices up to create combined matrix
glm::mat4 mv = view * model;
//adding the Uniform to the shader
int mvLoc = glGetUniformLocation(shader, "mv_matrix");
glUniformMatrix4fv(mvLoc, 1, GL_FALSE, glm::value_ptr(mv));
//adding the Uniform to the shader
int pLoc = glGetUniformLocation(shader, "p_matrix");
glUniformMatrix4fv(pLoc, 1, GL_FALSE, glm::value_ptr(projection));
glEnableVertexAttribArray(Triangles);
glEnableVertexAttribArray(Colours);
glEnableVertexAttribArray(Textures);
glEnableVertexAttribArray(Normals);
}
//----------------------------------------------------------------------------
//
// display
//
void
display(GLfloat delta)
{
static const float black[] = { 0.0f, 0.0f, 0.0f, 0.0f };
glClearBufferfv(GL_COLOR, 0, black);
glClear(GL_COLOR_BUFFER_BIT);
// bind textures on corresponding texture units
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
// creating the model matrix
glm::mat4 model = glm::mat4(1.0f);
model = glm::scale(model, glm::vec3(2.0f, 2.0f, 2.0f));
model = glm::rotate(model, glm::radians(delta*2.0f), glm::vec3(0.0f, 1.0f, 0.0f));
// creating the view matrix
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -4.0f));
// creating the projection matrix
glm::mat4 projection = glm::perspective(45.0f, 4.0f / 3, 0.1f, 20.0f);
// Adding all matrices up to create combined matrix
glm::mat4 mv = view * model;
//adding the Uniform to the shader
int mvLoc = glGetUniformLocation(shader, "mv_matrix");
glUniformMatrix4fv(mvLoc, 1, GL_FALSE, glm::value_ptr(mv));
//adding the Uniform to the shader
int pLoc = glGetUniformLocation(shader, "p_matrix");
glUniformMatrix4fv(pLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(VAOs[Cube]);
glBindTexture(GL_TEXTURE_2D, texture1);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
}
//----------------------------------------------------------------------------
//
// main
//
int
main(int argc, char** argv)
{
glfwInit();
GLFWwindow* window = glfwCreateWindow(800, 600, "Shaded Cube", NULL, NULL);
glfwMakeContextCurrent(window);
glewInit();
init();
GLfloat timer = 0.0f;
while (!glfwWindowShouldClose(window))
{
// uncomment to draw only wireframe
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
display(timer);
glfwSwapBuffers(window);
glfwPollEvents();
timer += 0.4f;
}
glfwDestroyWindow(window);
glfwTerminate();
}