-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkybox.cpp
More file actions
122 lines (100 loc) · 3.61 KB
/
Copy pathSkybox.cpp
File metadata and controls
122 lines (100 loc) · 3.61 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
#include "Skybox.h"
#include "stb_image.h"
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
using namespace std;
using namespace glm;
// Cube vertices — just positions, no UVs needed
static float skyboxVertices[] = {
-1.f, -1.f, 1.f, // 0
1.f, -1.f, 1.f, // 1
1.f, -1.f, -1.f, // 2
-1.f, -1.f, -1.f, // 3
-1.f, 1.f, 1.f, // 4
1.f, 1.f, 1.f, // 5
1.f, 1.f, -1.f, // 6
-1.f, 1.f, -1.f // 7
};
static unsigned int skyboxIndices[] = {
1,2,6, 6,5,1, // right
0,4,7, 7,3,0, // left
4,5,6, 6,7,4, // top
0,3,2, 2,1,0, // bottom
0,1,5, 5,4,0, // front
3,7,6, 6,2,3 // back
};
// ---- Load 6 faces into a cubemap texture ----
void Skybox::loadCubemap(const string faces[6]) {
glGenTextures(1, &cubemapTexture);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
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_R, GL_CLAMP_TO_EDGE);
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);
stbi_set_flip_vertically_on_load(false);
for (unsigned int i = 0; i < 6; i++) {
int w, h, ch;
unsigned char* data = stbi_load(faces[i].c_str(), &w, &h, &ch, 0);
if (data) {
GLenum fmt = (ch == 4) ? GL_RGBA : GL_RGB;
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0, fmt, w, h, 0, fmt, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
cout << "[Skybox] Loaded face " << i << ": " << faces[i] << endl;
} else {
cerr << "[Skybox] Failed to load face: " << faces[i] << endl;
cerr << " STB: " << stbi_failure_reason() << endl;
}
}
stbi_set_flip_vertically_on_load(true);
}
// ---- Setup VAO/VBO/EBO ----
void Skybox::setupMesh() {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices),
skyboxVertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(skyboxIndices),
skyboxIndices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
}
// ---- Constructor ----
Skybox::Skybox(const string faces[6]) {
setupMesh();
loadCubemap(faces);
}
// ---- Destructor ----
Skybox::~Skybox() {
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteTextures(1, &cubemapTexture);
}
// ---- Draw ----
void Skybox::draw(Shader& skyboxShader, mat4 view, mat4 projection) {
// Draw skybox first, disable depth writing
glDepthMask(GL_FALSE);
glDepthFunc(GL_LEQUAL);
skyboxShader.use();
// Strip translation from view matrix so skybox stays fixed
mat4 skyView = mat4(mat3(view));
skyboxShader.setMat4("view", value_ptr(skyView));
skyboxShader.setMat4("projection", value_ptr(projection));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
skyboxShader.setInt("skybox", 0);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
// Restore depth state
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
}