-
Notifications
You must be signed in to change notification settings - Fork 11
/
ex23_sampler_objects.cpp
248 lines (192 loc) · 9.13 KB
/
ex23_sampler_objects.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <application.hpp>
#include <shader.hpp>
#include <utility>
#include <imgui-utils/utils.hpp>
#include <mesh/mesh.hpp>
#include <mesh/mesh-utils.hpp>
#include <texture/texture-utils.h>
#include <camera/camera.hpp>
#include <camera/controllers/fly_camera_controller.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <json/json.hpp>
#include <fstream>
#include <unordered_map>
namespace glm {
template<length_t L, typename T, qualifier Q>
void from_json(const nlohmann::json& j, vec<L, T, Q>& v){
for(length_t index = 0; index < L; ++index)
v[index] = j[index].get<T>();
}
}
struct Transform {
glm::vec4 tint;
glm::vec3 translation, rotation, scale;
std::optional<std::string> mesh;
std::string texture;
std::unordered_map<std::string, std::shared_ptr<Transform>> children;
explicit Transform(
const glm::vec4& tint = {1,1,1,1},
const glm::vec3& translation = {0,0,0},
const glm::vec3& rotation = {0,0,0},
const glm::vec3& scale = {1,1,1},
std::optional<std::string> mesh = std::nullopt,
std::string texture = ""
): tint(tint), translation(translation), rotation(rotation), scale(scale), mesh(std::move(mesh)), texture(std::move(texture)) {}
[[nodiscard]] glm::mat4 to_mat4() const {
return glm::translate(glm::mat4(1.0f), translation) *
glm::yawPitchRoll(rotation.y, rotation.x, rotation.z) *
glm::scale(glm::mat4(1.0f), scale);
}
};
// This example shows how to use Sampler objects to separate the sample state from the texture object.
class SamplerObjectsApplication : public our::Application {
our::ShaderProgram program;
std::unordered_map<std::string, std::unique_ptr<our::Mesh>> meshes;
std::unordered_map<std::string, GLuint> textures;
// Samplers are OpenGL objects so we identify them using a GLuint.
GLuint sampler = 0;
// These sampling parameters will be stored in the Sampler object.
GLenum magnification_filter = GL_LINEAR, minification_filter = GL_LINEAR_MIPMAP_LINEAR;
GLenum wrap_s = GL_REPEAT, wrap_t = GL_REPEAT;
glm::vec4 border_color = {1,1,1,1};
GLfloat max_anisotropy = 1.0f;
GLenum polygon_mode = GL_FILL;
std::shared_ptr<Transform> root;
our::Camera camera;
our::FlyCameraController camera_controller;
our::WindowConfiguration getWindowConfiguration() override {
return { "Sampler Objects", {1280, 720}, false };
}
void onInitialize() override {
program.create();
// These are the same shaders as the ones we used in the previous example; nothing needs to be changed.
program.attach("assets/shaders/ex22_texture_sampling/transform.vert", GL_VERTEX_SHADER);
program.attach("assets/shaders/ex22_texture_sampling/texture.frag", GL_FRAGMENT_SHADER);
program.link();
GLuint texture;
glGenTextures(1, &texture);
our::texture_utils::checkerBoard(texture, {256,256}, {128,128}, {255, 255, 255, 255}, {16, 16, 16, 255});
textures["checkerboard"] = texture;
glGenTextures(1, &texture);
our::texture_utils::loadImage(texture, "assets/models/House/House.jpeg");
textures["house"] = texture;
glGenTextures(1, &texture);
our::texture_utils::loadImage(texture, "assets/images/common/moon.jpg");
textures["moon"] = texture;
meshes["house"] = std::make_unique<our::Mesh>();
our::mesh_utils::loadOBJ(*(meshes["house"]), "assets/models/House/House.obj");
meshes["plane"] = std::make_unique<our::Mesh>();
our::mesh_utils::Plane(*(meshes["plane"]), {1, 1}, false, {0, 0, 0}, {1, 1}, {0, 0}, {100, 100});
meshes["sphere"] = std::make_unique<our::Mesh>();
our::mesh_utils::Sphere(*(meshes["sphere"]), {32, 16}, false);
// Generate one sampler
glGenSamplers(1, &sampler);
int width, height;
glfwGetFramebufferSize(window, &width, &height);
camera.setEyePosition({10, 10, 10});
camera.setTarget({0, 0, 0});
camera.setUp({0, 1, 0});
camera.setupPerspective(glm::pi<float>()/2, static_cast<float>(width)/height, 0.1f, 100.0f);
camera_controller.initialize(this, &camera);
camera_controller.setFieldOfViewSensitivity(0.05f );
std::ifstream file_in("assets/data/ex23_sampler_objects/scene.json");
nlohmann::json json;
file_in >> json;
file_in.close();
root = loadNode(json);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glClearColor(0.88,0.65,0.15, 1);
}
std::shared_ptr<Transform> loadNode(const nlohmann::json& json){
auto node = std::make_shared<Transform>(
json.value<glm::vec4>("tint", {1,1,1,1}),
json.value<glm::vec3>("translation", {0, 0, 0}),
json.value<glm::vec3>("rotation", {0, 0, 0}),
json.value<glm::vec3>("scale", {1, 1, 1})
);
if(json.contains("mesh")){
node->mesh = json["mesh"].get<std::string>();
}
if(json.contains("texture")){
node->texture = json["texture"].get<std::string>();
}
if(json.contains("children")){
for(auto& [name, child]: json["children"].items()){
node->children[name] = loadNode(child);
}
}
return node;
}
void drawNode(const std::shared_ptr<Transform>& node, const glm::mat4& parent_transform_matrix){
glm::mat4 transform_matrix = parent_transform_matrix * node->to_mat4();
if(node->mesh.has_value()){
if(auto mesh_it = meshes.find(node->mesh.value()); mesh_it != meshes.end()) {
GLuint texture = 0;
if(auto tex_it = textures.find(node->texture); tex_it != textures.end())
texture = tex_it->second;
glBindTexture(GL_TEXTURE_2D, texture);
program.set("tint", node->tint);
program.set("transform", transform_matrix);
mesh_it->second->draw();
}
}
for(auto& [name, child]: node->children){
drawNode(child, transform_matrix);
}
}
void onDraw(double deltaTime) override {
camera_controller.update(deltaTime);
root->children["moon-axis"]->children["moon"]->rotation.y += deltaTime;
glUseProgram(program);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Since we will always draw one texture per object, we set the active unit to be 0 and kept it active for the rest of the frame
glActiveTexture(GL_TEXTURE0);
// To tell OpenGL which sampler object we will use for this unit, we bind the sampler to unit 0 (which is specified by the 1st parameter of the following function).
glBindSampler(0, sampler);
program.set("sampler", 0);
// Now, instead of setting the parameters for each texture, we just set it to the sampler and each unit that uses that sampler will automatically use these parameters.
glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, magnification_filter);
glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, minification_filter);
glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, wrap_s);
glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, wrap_t);
glSamplerParameterfv(sampler, GL_TEXTURE_BORDER_COLOR, glm::value_ptr(border_color));
glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
glPolygonMode(GL_FRONT_AND_BACK, polygon_mode);
drawNode(root, camera.getVPMatrix());
}
void onDestroy() override {
program.destroy();
glDeleteSamplers(1, &sampler);
for(auto& [name, texture]: textures){
glDeleteTextures(1, &texture);
}
textures.clear();
for(auto& [name, mesh]: meshes){
mesh->destroy();
}
meshes.clear();
}
void onImmediateGui(ImGuiIO &io) override {
ImGui::Begin("Controls");
our::OptionMapCombo("Magnification Filter", magnification_filter, our::gl_enum_options::texture_magnification_filters);
our::OptionMapCombo("Minification Filter", minification_filter, our::gl_enum_options::texture_minification_filters);
our::OptionMapCombo("Wrap S", wrap_s, our::gl_enum_options::texture_wrapping_modes);
our::OptionMapCombo("Wrap T", wrap_t, our::gl_enum_options::texture_wrapping_modes);
ImGui::ColorEdit4("Border Color", glm::value_ptr(border_color));
ImGui::Separator();
GLfloat max_anisotropy_upper_bound;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy_upper_bound);
ImGui::DragFloat("Maximum Anisotropy", &max_anisotropy, 0.1f, 1.0f, max_anisotropy_upper_bound);
ImGui::Text("Maximum Anisotropy Upper Bound: %f", max_anisotropy_upper_bound);
ImGui::Separator();
our::OptionMapCombo("Polygon Mode", polygon_mode, our::gl_enum_options::polygon_modes);
ImGui::End();
}
};
int main(int argc, char** argv) {
return SamplerObjectsApplication().run();
}