This repository was archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCubeMap.cpp
More file actions
163 lines (126 loc) · 4.36 KB
/
CubeMap.cpp
File metadata and controls
163 lines (126 loc) · 4.36 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
#include "CubeMap.hpp"
CubeMap::~CubeMap() {
glDeleteBuffers(1, &cubeVertexBuffer);
glDeleteBuffers(1, &cubeUVbuffer);
}
void CubeMap::load() {
// load Textures
texture = (GLuint*) malloc(6 * sizeof(GLuint));
texture[1] = loadBMP_custom("images/cubemap/bottom.bmp");
texture[0] = loadBMP_custom("images/cubemap/top.bmp");
texture[4] = loadBMP_custom("images/cubemap/front.bmp");
texture[5] = loadBMP_custom("images/cubemap/back.bmp");
texture[2] = loadBMP_custom("images/cubemap/left.bmp");
texture[3] = loadBMP_custom("images/cubemap/right.bmp");
// ORDER -> top - left - front - right - back - bottom
float cubeVertices[] = {
-30.0f, -30.0f, -30.0f, // top
30.0f, -30.0f, -30.0f,
30.0f, 30.0f, -30.0f,
30.0f, 30.0f, -30.0f,
-30.0f, 30.0f, -30.0f,
-30.0f, -30.0f, -30.0f,
-30.0f, -30.0f, 30.0f, // bottom
30.0f, -30.0f, 30.0f,
30.0f, 30.0f, 30.0f,
30.0f, 30.0f, 30.0f,
-30.0f, 30.0f, 30.0f,
-30.0f, -30.0f, 30.0f,
-30.0f, 30.0f, 30.0f, // left
-30.0f, 30.0f, -30.0f,
-30.0f, -30.0f, -30.0f,
-30.0f, -30.0f, -30.0f,
-30.0f, -30.0f, 30.0f,
-30.0f, 30.0f, 30.0f,
30.0f, 30.0f, 30.0f, //right
30.0f, 30.0f, -30.0f,
30.0f, -30.0f, -30.0f,
30.0f, -30.0f, -30.0f,
30.0f, -30.0f, 30.0f,
30.0f, 30.0f, 30.0f,
-30.0f, -30.0f, -30.0f, //front
30.0f, -30.0f, -30.0f,
30.0f, -30.0f, 30.0f,
30.0f, -30.0f, 30.0f,
-30.0f, -30.0f, 30.0f,
-30.0f, -30.0f, -30.0f,
-30.0f, 30.0f, -30.0f, // back
30.0f, 30.0f, -30.0f,
30.0f, 30.0f, 30.0f,
30.0f, 30.0f, 30.0f,
-30.0f, 30.0f, 30.0f,
-30.0f, 30.0f, -30.0f,
};
float cubeUVs[] = {
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
1.0f, 1.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,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.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, 0.0f,
0.0f, 1.0f
};
GLubyte indices[36];
// Move vertex data to video memory; specifically to VBO called vertexbuffer
glGenBuffers(1, &cubeVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, cubeVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
glGenBuffers(1, &cubeUVbuffer);
glBindBuffer(GL_ARRAY_BUFFER, cubeUVbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeUVs), cubeUVs, GL_STATIC_DRAW);
}
void CubeMap::draw(glm::mat4 MVP, GLuint shader) {
glUseProgram(shader);
glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(-15.0f, 15.0f, -15.0f));
glm::mat4 MVP2 = MVP * translate;
for (int i = 0; i < 6; i++) {
// Send our transformation to the currently bound shader,
// in the "MVP" uniform, which is now MVP
glUniformMatrix4fv(glGetUniformLocation(shader, "MVP"), 1, GL_FALSE, &MVP2[0][0]);
// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture[i]);
// Set our "myTextureSampler" sampler to use Texture Unit 0
glUniform1i(glGetUniformLocation(shader, "myTextureSampler"), 0);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, cubeVertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// 2nd attribute buffer : UVs
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, cubeUVbuffer);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, (i*6), (2*3));
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
}