-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskybox.cpp
More file actions
126 lines (110 loc) · 3.76 KB
/
skybox.cpp
File metadata and controls
126 lines (110 loc) · 3.76 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
#include "skybox.h"
#include "Window.h"
#define STB_IMAGE_IMPLEMENTATION
#include "packages//stb-master//stb_image.h"
float skyboxVertices[] = {
// positions
-1000.0f, 1000.0f, -1000.0f,
-1000.0f, -1000.0f, -1000.0f,
1000.0f, -1000.0f, -1000.0f,
1000.0f, -1000.0f, -1000.0f,
1000.0f, 1000.0f, -1000.0f,
-1000.0f, 1000.0f, -1000.0f,
-1000.0f, -1000.0f, 1000.0f,
-1000.0f, -1000.0f, -1000.0f,
-1000.0f, 1000.0f, -1000.0f,
-1000.0f, 1000.0f, -1000.0f,
-1000.0f, 1000.0f, 1000.0f,
-1000.0f, -1000.0f, 1000.0f,
1000.0f, -1000.0f, -1000.0f,
1000.0f, -1000.0f, 1000.0f,
1000.0f, 1000.0f, 1000.0f,
1000.0f, 1000.0f, 1000.0f,
1000.0f, 1000.0f, -1000.0f,
1000.0f, -1000.0f, -1000.0f,
-1000.0f, -1000.0f, 1000.0f,
-1000.0f, 1000.0f, 1000.0f,
1000.0f, 1000.0f, 1000.0f,
1000.0f, 1000.0f, 1000.0f,
1000.0f, -1000.0f, 1000.0f,
-1000.0f, -1000.0f, 1000.0f,
-1000.0f, 1000.0f, -1000.0f,
1000.0f, 1000.0f, -1000.0f,
-1000.0f, 1000.0f, 1000.0f,
-1000.0f, 1000.0f, 1000.0f,
1000.0f, 1000.0f, -1000.0f,
1000.0f, 1000.0f, 1000.0f,
-1000.0f, -1000.0f, -1000.0f,
-1000.0f, -1000.0f, 1000.0f,
1000.0f, -1000.0f, -1000.0f,
1000.0f, -1000.0f, -1000.0f,
-1000.0f, -1000.0f, 1000.0f,
1000.0f, -1000.0f, 1000.0f
};
std::vector<std::string> faces = {
"C:\\Users\\Lingfeng\\Desktop\\arrakisday\\right.ppm",
"C:\\Users\\Lingfeng\\Desktop\\arrakisday\\left.ppm",
"C:\\Users\\Lingfeng\\Desktop\\arrakisday\\up.ppm",
"C:\\Users\\Lingfeng\\Desktop\\arrakisday\\down.ppm",
"C:\\Users\\Lingfeng\\Desktop\\arrakisday\\back.ppm",
"C:\\Users\\Lingfeng\\Desktop\\arrakisday\\front.ppm"
};
Skybox::Skybox()
{
loadCubemap(faces);
}
Skybox::~Skybox()
{
glDeleteVertexArrays(1, &skyboxVAO);
glDeleteBuffers(1, &skyboxVBO);
}
void Skybox::loadCubemap(std::vector<std::string> faces){
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
int width, height, nrChannels;
for (unsigned int i = 0; i < faces.size(); i++)
{
unsigned char *data = stbi_load(faces[i].c_str(), &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data
);
stbi_image_free(data);
}
else
{
std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl;
stbi_image_free(data);
}
}
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);
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(float), (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Skybox::draw(GLuint skyboxShader) {
glDepthMask(GL_FALSE);
//glm::mat4 view = glm::mat4(glm::mat3(Window::V)); // remove translation from the view matrix
uProjection = glGetUniformLocation(skyboxShader, "projection");
uModelview = glGetUniformLocation(skyboxShader, "modelview");
glUniformMatrix4fv(uProjection, 1, GL_FALSE, &Window::P[0][0]);
glUniformMatrix4fv(uModelview, 1, GL_FALSE, &Window::V[0][0]);
glUniform1i(glGetUniformLocation(skyboxShader, "skybox"), 0);
// skybox cube
glBindVertexArray(skyboxVAO);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthMask(GL_TRUE); // set depth function back to default
}