-
Notifications
You must be signed in to change notification settings - Fork 0
/
SkyBox.cpp
88 lines (80 loc) · 2.55 KB
/
SkyBox.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
#include "SkyBox.h"
SkyBox::SkyBox()
{
SkyShader = new Shader();
//SkyShader->CreateShadersFromFiles((char*)"Shaders/skybox.vert", (char*)"Shaders/skybox.frag");
}
SkyBox::SkyBox(std::vector<std::string> FaceLocations)
{
SkyShader = new Shader();
SkyShader->CreateShadersFromFiles((char*)"Shaders/skybox.vert", (char*)"Shaders/skybox.frag");
UniformProjection = SkyShader->GetUniformProjection();
UniformView = SkyShader->GetUniformView();
glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, TextureID);
int width, height, bitDepth;
for (size_t i = 0; i < 6; i++)
{
unsigned char* TextureData = stbi_load(FaceLocations[i].c_str(), &width, &height,&bitDepth,0);
if (!TextureData)
{
std::cout << "Failed to find: " << FaceLocations[i] << std::endl;
return;
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureData);
stbi_image_free(TextureData);
}
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);
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.f,1.f,-1.f, 0.f,0.f, 0.f,0.f,0.f,
-1.f,-1.f,-1.f, 0.f,0.f, 0.f,0.f,0.f,
1.f,1.f,-1.f, 0.f,0.f, 0.f,0.f,0.f,
1.f,-1.f,-1.f, 0.f,0.f, 0.f,0.f,0.f,
-1.f,1.f,1.f, 0.f,0.f, 0.f,0.f,0.f,
1.f,1.f,1.f, 0.f,0.f, 0.f,0.f,0.f,
-1.f,-1.f, 1.f, 0.f,0.f, 0.f,0.f,0.f,
1.f,-1.f,1.f, 0.f,0.f, 0.f,0.f,0.f,
};
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->EnableShader();
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->ValidateShaders();
SkyMesh->RenderMesh();
glDepthMask(GL_TRUE);
}
SkyBox::~SkyBox()
{
}