-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkybox.cpp
More file actions
executable file
·103 lines (81 loc) · 2.46 KB
/
Skybox.cpp
File metadata and controls
executable file
·103 lines (81 loc) · 2.46 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
#include "Skybox.h"
Skybox::Skybox()
{
}
Skybox::Skybox(std::vector<std::string> faceLocations)
{
// Shader Setup
skyShader = new Shader();
skyShader->CreateFromFiles("Shaders/skybox.vert", "Shaders/skybox.frag");
uniformProjection = skyShader->GetProjectionLocation();
uniformView = skyShader->GetViewLocation();
// Texture Setup
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);
int width, height, bitDepth;
for (size_t i = 0; i < 6; i++)
{
unsigned char *texData = stbi_load(faceLocations[i].c_str(), &width, &height, &bitDepth, 0);
if (!texData)
{
printf("Failed to find: %s\n", faceLocations[i].c_str());
return;
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texData);
stbi_image_free(texData);
}
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);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Mesh Setup
unsigned int skyboxIndices[] = {
// front
0, 1, 2,
2, 1, 3,
// right
2, 3, 5,
5, 3, 7,
// back
5, 7, 4,
4, 7, 6,
// left
4, 6, 0,
0, 6, 1,
// top
4, 0, 5,
5, 0, 2,
// bottom
1, 6, 3,
3, 6, 7
};
float skyboxVertices[] = {
-1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
};
skyMesh = new Mesh();
skyMesh->CreateMesh(skyboxVertices, skyboxIndices, 64, 36);
}
void Skybox::DrawSkybox(glm::mat4 viewMatrix, glm::mat4 projectionMatrix)
{
viewMatrix = glm::mat4(glm::mat3(viewMatrix));
glDepthMask(GL_FALSE);
skyShader->UseShader();
glUniformMatrix4fv(uniformProjection, 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniformMatrix4fv(uniformView, 1, GL_FALSE, glm::value_ptr(viewMatrix));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);
skyShader->Validate();
skyMesh->RenderMesh();
glDepthMask(GL_TRUE);
}
Skybox::~Skybox()
{
}