-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathskybox.cpp
More file actions
221 lines (182 loc) · 7.88 KB
/
Copy pathskybox.cpp
File metadata and controls
221 lines (182 loc) · 7.88 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
#include "skybox.h"
#include "SOIL/SOIL.h"
SkyBox::SkyBox(QOpenGLShaderProgram *prog)
{
initializeOpenGLFunctions();
program = prog;
GLfloat skyboxVertices[] = {
// Positions
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f
};
QVector<QString> faces;
faces.push_back("../earth_10/miramar_rt.tga");
faces.push_back("../earth_10/miramar_lf.tga");
faces.push_back("../earth_10/miramar_up1.tga");
faces.push_back("../earth_10/miramar_dn.tga");
faces.push_back("../earth_10/miramar_bk.tga");
faces.push_back("../earth_10/miramar_ft.tga");
cubemapTexture = loadCubemap(faces);
qDebug() << QString("ID cubemapTexture = %1").arg(cubemapTexture);
glGenVertexArrays(1, &skyboxVAO);
glGenBuffers(1, &skyboxVBO);
glBindVertexArray(skyboxVAO);
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glBindVertexArray(0);
}
SkyBox::~SkyBox()
{
}
void SkyBox::render(QMatrix4x4 projection, QMatrix4x4 modelview, QMatrix4x4 rot)
{
// Draw skybox first
glDepthMask(GL_FALSE);// Remember to turn depth writing off
//QMatrix4x4 view = modelview.
//glm::mat4 view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // Remove any translation component of the view matrix
//glm::mat4 projection = glm::perspective(camera.Zoom, (float)screenWidth/(float)screenHeight, 0.1f, 100.0f);
//glUniformMatrix4fv(glGetUniformLocation(skyboxShader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));
program->setUniformValue("view", rot);
//glUniformMatrix4fv(glGetUniformLocation(skyboxShader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
program->setUniformValue("projection", projection);
// skybox cube
glBindVertexArray(skyboxVAO);
glActiveTexture(GL_TEXTURE0);
//glUniform1i(glGetUniformLocation(shader.Program, "skybox"), 0);
program->setUniformValue("skybox", 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthMask(GL_TRUE);
}
GLuint SkyBox::loadCubemap(QVector<QString> faces)
{
GLuint textureID;
glGenTextures(1, &textureID);
glActiveTexture(GL_TEXTURE0);
int width,height;
unsigned char* image;
// glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
// for(GLuint i = 0; i < faces.size(); i++)
// {
// image = SOIL_load_image(faces.at(i).toLatin1(), &width, &height, 0, SOIL_LOAD_RGB);
// glTexImage2D(
// GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0,
// GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image
// );
// }
/**
Loads 6 images from memory into an OpenGL cubemap texture.
\param x_pos_buffer the image data in RAM to upload as the +x cube face
\param x_pos_buffer_length the size of the above buffer
\param x_neg_buffer the image data in RAM to upload as the +x cube face
\param x_neg_buffer_length the size of the above buffer
\param y_pos_buffer the image data in RAM to upload as the +x cube face
\param y_pos_buffer_length the size of the above buffer
\param y_neg_buffer the image data in RAM to upload as the +x cube face
\param y_neg_buffer_length the size of the above buffer
\param z_pos_buffer the image data in RAM to upload as the +x cube face
\param z_pos_buffer_length the size of the above buffer
\param z_neg_buffer the image data in RAM to upload as the +x cube face
\param z_neg_buffer_length the size of the above buffer
\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
\return 0-failed, otherwise returns the OpenGL texture handle
**/
// QImage x_pos_buffer(faces.at(0));
// QImage x_neg_buffer(faces.at(1));
// QImage y_pos_buffer(faces.at(2));
// QImage y_neg_buffer(faces.at(3));
// QImage z_neg_buffer(faces.at(4));
// QImage z_pos_buffer(faces.at(5));
// GLuint tex_cube = SOIL_load_OGL_cubemap_from_memory
// (
// x_pos_buffer.bits(),
// x_pos_buffer.byteCount(),
// x_neg_buffer.bits(),
// x_neg_buffer.byteCount(),
// y_pos_buffer.bits(),
// y_pos_buffer.byteCount(),
// y_neg_buffer.bits(),
// y_neg_buffer.byteCount(),
// z_pos_buffer.bits(),
// z_pos_buffer.byteCount(),
// z_neg_buffer.bits(),
// z_neg_buffer.byteCount(),
// 4,
// 0,
// SOIL_FLAG_MIPMAPS
// );
// const unsigned char *const x_pos_buffer,
// int x_pos_buffer_length,
// const unsigned char *const x_neg_buffer,
// int x_neg_buffer_length,
// const unsigned char *const y_pos_buffer,
// int y_pos_buffer_length,
// const unsigned char *const y_neg_buffer,
// int y_neg_buffer_length,
// const unsigned char *const z_pos_buffer,
// int z_pos_buffer_length,
// const unsigned char *const z_neg_buffer,
// int z_neg_buffer_length,
// int force_channels,
// unsigned int reuse_texture_ID,
// unsigned int flags
// );
GLuint tex_cube = SOIL_load_OGL_cubemap
(
faces.at(0).toLatin1(),
faces.at(1).toLatin1(),
faces.at(2).toLatin1(),
faces.at(3).toLatin1(),
faces.at(4).toLatin1(),
faces.at(5).toLatin1(),
SOIL_LOAD_RGB,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS
);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
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);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
return tex_cube;
}