Messing around with when to do glGen, solving a memory leak because of that, and rewriting Scene::spawn to use the instancing stuff.
This commit is contained in:
parent
a199d41890
commit
6ffc6cb4ca
9 changed files with 23 additions and 31 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define INSTANCE_COUNT 100
|
#define INSTANCE_COUNT 5
|
||||||
|
|
||||||
const unsigned int SCR_WIDTH = 1792;
|
const unsigned int SCR_WIDTH = 1792;
|
||||||
const unsigned int SCR_HEIGHT = 1008;
|
const unsigned int SCR_HEIGHT = 1008;
|
||||||
|
|
|
||||||
|
|
@ -76,8 +76,8 @@ void process_input(GLFWwindow *window, Scene& scene) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) {
|
if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) {
|
||||||
components::Rotation rotation{90.0f, {1.0f, 0.0f, 0.0f}};
|
components::Rotation rotation{0.0f, {1.0f, 1.0f, 1.0f}};
|
||||||
scene.spawn("grass", scene.camera.position, rotation);
|
scene.spawn("marble_cube", scene.camera.position, rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
|
if(glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,13 @@
|
||||||
#include "dbc.hpp"
|
#include "dbc.hpp"
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
|
|
||||||
void Mesh::draw(Shader &shader, bool with_textures) {
|
void Mesh::draw(Shader &shader, bool with_textures, unsigned int count) {
|
||||||
if(with_textures) {
|
if(with_textures) {
|
||||||
apply_textures(shader);
|
apply_textures(shader);
|
||||||
}
|
}
|
||||||
|
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
glDrawElementsInstanced(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0, INSTANCE_COUNT);
|
glDrawElementsInstanced(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0, count);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ struct Mesh {
|
||||||
setup_mesh();
|
setup_mesh();
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw(Shader &shader, bool with_textures);
|
void draw(Shader &shader, bool with_textures, unsigned int count);
|
||||||
void setup_mesh();
|
void setup_mesh();
|
||||||
void apply_textures(const Shader& shader);
|
void apply_textures(const Shader& shader);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -83,9 +83,9 @@ unsigned int texture_from_file(const std::string& directory, const std::string&
|
||||||
return textureID;
|
return textureID;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Model::draw(Shader &shader, bool with_textures) {
|
void Model::draw(Shader &shader, bool with_textures, unsigned int count) {
|
||||||
for(auto& mesh : meshes) {
|
for(auto& mesh : meshes) {
|
||||||
mesh.draw(shader, with_textures);
|
mesh.draw(shader, with_textures, count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ struct Model {
|
||||||
load_model();
|
load_model();
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw(Shader &shader, bool with_textures=true);
|
void draw(Shader &shader, bool with_textures, unsigned int count);
|
||||||
|
|
||||||
void load_model();
|
void load_model();
|
||||||
void process_node(const aiScene *scene, aiNode *node);
|
void process_node(const aiScene *scene, aiNode *node);
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ void Scene::draw_thing(Shader& with_shader, Thing& thing)
|
||||||
with_shader.setMat4("model", model);
|
with_shader.setMat4("model", model);
|
||||||
|
|
||||||
// !reflect_on turns it on? yeah weird
|
// !reflect_on turns it on? yeah weird
|
||||||
thing.model.draw(with_shader, !reflect_on);
|
thing.model.draw(with_shader, !reflect_on, translations.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::render(GLFWwindow* window) {
|
void Scene::render(GLFWwindow* window) {
|
||||||
|
|
@ -72,13 +72,7 @@ void Scene::cleanup() {
|
||||||
|
|
||||||
void Scene::spawn(const std::string& name, components::Position& position, components::Rotation& rotation) {
|
void Scene::spawn(const std::string& name, components::Position& position, components::Rotation& rotation) {
|
||||||
if(models.contains(name)) {
|
if(models.contains(name)) {
|
||||||
things.emplace_back(
|
translations.push_back(position);
|
||||||
name,
|
|
||||||
models.at(name),
|
|
||||||
materials.at("default"),
|
|
||||||
position,
|
|
||||||
rotation,
|
|
||||||
1.0f);
|
|
||||||
} else {
|
} else {
|
||||||
dbc::log($F("No model named: {}", name));
|
dbc::log($F("No model named: {}", name));
|
||||||
}
|
}
|
||||||
|
|
@ -86,7 +80,7 @@ void Scene::spawn(const std::string& name, components::Position& position, compo
|
||||||
|
|
||||||
void Scene::time_warp() {
|
void Scene::time_warp() {
|
||||||
float time = glfwGetTime();
|
float time = glfwGetTime();
|
||||||
for(size_t i = 0; i < INSTANCE_COUNT; i++) {
|
for(size_t i = 0; i < translations.size(); i++) {
|
||||||
auto& t = translations[i];
|
auto& t = translations[i];
|
||||||
float factor = (float)i / 3.0f;
|
float factor = (float)i / 3.0f;
|
||||||
t.y = cos((time + i)) * factor;
|
t.y = cos((time + i)) * factor;
|
||||||
|
|
@ -94,9 +88,8 @@ void Scene::time_warp() {
|
||||||
t.x = sin((time + i)) * factor;
|
t.x = sin((time + i)) * factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
glGenBuffers(1, &instanceVBO);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
|
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * INSTANCE_COUNT, &translations[0], GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * translations.size(), translations.data(), GL_STATIC_DRAW);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
|
||||||
for(auto& mesh : things[0].model.meshes) {
|
for(auto& mesh : things[0].model.meshes) {
|
||||||
|
|
@ -111,17 +104,13 @@ void Scene::time_warp() {
|
||||||
|
|
||||||
void Scene::setup_instancing() {
|
void Scene::setup_instancing() {
|
||||||
for(size_t i = 0; i < INSTANCE_COUNT; i++) {
|
for(size_t i = 0; i < INSTANCE_COUNT; i++) {
|
||||||
translations[i].x = (float)i;
|
translations.emplace_back((float)i, (float)i, (float)i);
|
||||||
translations[i].z = (float)i;
|
|
||||||
translations[i].y = (float)i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
time_warp();
|
time_warp();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::init_skybox() {
|
void Scene::init_skybox() {
|
||||||
glGenVertexArrays(1, &skyboxVAO);
|
|
||||||
glGenBuffers(1, &skyboxVBO);
|
|
||||||
glBindVertexArray(skyboxVAO);
|
glBindVertexArray(skyboxVAO);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
|
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
|
||||||
|
|
@ -138,7 +127,6 @@ void Scene::load_skybox_textures() {
|
||||||
int nrChannels = 0;
|
int nrChannels = 0;
|
||||||
size_t face_i = 0;
|
size_t face_i = 0;
|
||||||
|
|
||||||
glGenTextures(1, &skybox_texture_id);
|
|
||||||
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture_id);
|
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture_id);
|
||||||
|
|
||||||
for(const auto& face : skybox_faces) {
|
for(const auto& face : skybox_faces) {
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,6 @@ struct Scene {
|
||||||
Screen screen;
|
Screen screen;
|
||||||
FrameBuffer framebuffer;
|
FrameBuffer framebuffer;
|
||||||
|
|
||||||
|
|
||||||
std::unordered_map<std::string, components::Material> materials;
|
std::unordered_map<std::string, components::Material> materials;
|
||||||
Camera camera;
|
Camera camera;
|
||||||
components::Lighting light;
|
components::Lighting light;
|
||||||
|
|
@ -47,7 +46,7 @@ struct Scene {
|
||||||
unsigned int skybox_texture_id = 0;
|
unsigned int skybox_texture_id = 0;
|
||||||
bool reflect_on = false;
|
bool reflect_on = false;
|
||||||
|
|
||||||
glm::vec3 translations[INSTANCE_COUNT];
|
std::vector<glm::vec3> translations;
|
||||||
unsigned int instanceVBO = 0;
|
unsigned int instanceVBO = 0;
|
||||||
|
|
||||||
Scene(components::Scene& config):
|
Scene(components::Scene& config):
|
||||||
|
|
@ -80,6 +79,11 @@ struct Scene {
|
||||||
thing.scale);
|
thing.scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glGenVertexArrays(1, &skyboxVAO);
|
||||||
|
glGenBuffers(1, &skyboxVBO);
|
||||||
|
glGenTextures(1, &skybox_texture_id);
|
||||||
|
glGenBuffers(1, &instanceVBO);
|
||||||
|
|
||||||
shader.use();
|
shader.use();
|
||||||
shader.setInt("texture1", 0);
|
shader.setInt("texture1", 0);
|
||||||
shader.apply_lighting(light);
|
shader.apply_lighting(light);
|
||||||
|
|
|
||||||
|
|
@ -14,13 +14,13 @@ namespace model_tests {
|
||||||
Shader shader({"shaders/14-vert.glsl", "shaders/14-frag.glsl", ""});
|
Shader shader({"shaders/14-vert.glsl", "shaders/14-frag.glsl", ""});
|
||||||
|
|
||||||
Model popcorn{"assets", "popcorn_model_a.glb"};
|
Model popcorn{"assets", "popcorn_model_a.glb"};
|
||||||
popcorn.draw(shader);
|
popcorn.draw(shader, true, 1);
|
||||||
|
|
||||||
Model crate_glb{"assets", "crate.glb"};
|
Model crate_glb{"assets", "crate.glb"};
|
||||||
crate_glb.draw(shader);
|
crate_glb.draw(shader, true, 1);
|
||||||
|
|
||||||
Model crate_obj{"assets", "crate.obj"};
|
Model crate_obj{"assets", "crate.obj"};
|
||||||
crate_obj.draw(shader);
|
crate_obj.draw(shader, true, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_failures() {
|
void test_failures() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue