-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskybox.hpp
118 lines (97 loc) · 3.13 KB
/
skybox.hpp
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
#pragma once
#include <glad/glad.h>
#include <initializer_list>
#include <memory>
#include <glm/gtc/type_ptr.hpp>
#include "shader.hpp"
#include "texture.hpp"
#include "camera.hpp"
struct SkyBox {
// right, left, up, down, back, front
SkyBox(std::array<const char*, 6> &&filenames) {
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), skyboxVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)(0));
glEnableVertexAttribArray(0);
Shader vert(vertex_shader, GL_VERTEX_SHADER, 0);
Shader frag(fragment_shader, GL_FRAGMENT_SHADER, 0);
shader_program = std::make_shared<ShaderProgram>(vert, frag);
texture = std::make_shared<CubeMapTexture>(std::move(filenames));
}
void draw(const Camera &cam) {
shader_program->use();
GLuint location = glGetUniformLocation(shader_program->handle, "rotation");
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(cam.project() * cam.view()));
location = glGetUniformLocation(shader_program->handle, "tex");
glUniform1i(location, 0);
texture->use();
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
GLuint vao, vbo;
std::shared_ptr<ShaderProgram> shader_program = nullptr;
std::shared_ptr<CubeMapTexture> texture = nullptr;
const char *vertex_shader = R"(
#version 430 core
layout (location = 0) in vec3 aPos;
out vec3 texCoord;
uniform mat4 rotation;
void main() {
texCoord = aPos;
float scale = 10.0;
gl_Position = rotation * mat4(scale, 0.0, 0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0, 0.0, 1.0) * vec4(aPos, 1.0);
}
)";
const char *fragment_shader = R"(
#version 430 core
in vec3 texCoord;
out vec4 color;
uniform samplerCube tex;
void main() {
color = texture(tex, texCoord);
}
)";
static constexpr float 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
};
};