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
|
||||
|
||||
#define INSTANCE_COUNT 100
|
||||
#define INSTANCE_COUNT 5
|
||||
|
||||
const unsigned int SCR_WIDTH = 1792;
|
||||
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) {
|
||||
components::Rotation rotation{90.0f, {1.0f, 0.0f, 0.0f}};
|
||||
scene.spawn("grass", scene.camera.position, rotation);
|
||||
components::Rotation rotation{0.0f, {1.0f, 1.0f, 1.0f}};
|
||||
scene.spawn("marble_cube", scene.camera.position, rotation);
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
|
||||
|
|
|
|||
|
|
@ -2,13 +2,13 @@
|
|||
#include "dbc.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) {
|
||||
apply_textures(shader);
|
||||
}
|
||||
|
||||
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);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ struct Mesh {
|
|||
setup_mesh();
|
||||
}
|
||||
|
||||
void draw(Shader &shader, bool with_textures);
|
||||
void draw(Shader &shader, bool with_textures, unsigned int count);
|
||||
void setup_mesh();
|
||||
void apply_textures(const Shader& shader);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -83,9 +83,9 @@ unsigned int texture_from_file(const std::string& directory, const std::string&
|
|||
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) {
|
||||
mesh.draw(shader, with_textures);
|
||||
mesh.draw(shader, with_textures, count);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ struct 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 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);
|
||||
|
||||
// !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) {
|
||||
|
|
@ -72,13 +72,7 @@ void Scene::cleanup() {
|
|||
|
||||
void Scene::spawn(const std::string& name, components::Position& position, components::Rotation& rotation) {
|
||||
if(models.contains(name)) {
|
||||
things.emplace_back(
|
||||
name,
|
||||
models.at(name),
|
||||
materials.at("default"),
|
||||
position,
|
||||
rotation,
|
||||
1.0f);
|
||||
translations.push_back(position);
|
||||
} else {
|
||||
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() {
|
||||
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];
|
||||
float factor = (float)i / 3.0f;
|
||||
t.y = cos((time + i)) * factor;
|
||||
|
|
@ -94,9 +88,8 @@ void Scene::time_warp() {
|
|||
t.x = sin((time + i)) * factor;
|
||||
}
|
||||
|
||||
glGenBuffers(1, &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);
|
||||
|
||||
for(auto& mesh : things[0].model.meshes) {
|
||||
|
|
@ -111,17 +104,13 @@ void Scene::time_warp() {
|
|||
|
||||
void Scene::setup_instancing() {
|
||||
for(size_t i = 0; i < INSTANCE_COUNT; i++) {
|
||||
translations[i].x = (float)i;
|
||||
translations[i].z = (float)i;
|
||||
translations[i].y = (float)i;
|
||||
translations.emplace_back((float)i, (float)i, (float)i);
|
||||
}
|
||||
|
||||
time_warp();
|
||||
}
|
||||
|
||||
void Scene::init_skybox() {
|
||||
glGenVertexArrays(1, &skyboxVAO);
|
||||
glGenBuffers(1, &skyboxVBO);
|
||||
glBindVertexArray(skyboxVAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
|
||||
|
|
@ -138,7 +127,6 @@ void Scene::load_skybox_textures() {
|
|||
int nrChannels = 0;
|
||||
size_t face_i = 0;
|
||||
|
||||
glGenTextures(1, &skybox_texture_id);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture_id);
|
||||
|
||||
for(const auto& face : skybox_faces) {
|
||||
|
|
|
|||
|
|
@ -34,7 +34,6 @@ struct Scene {
|
|||
Screen screen;
|
||||
FrameBuffer framebuffer;
|
||||
|
||||
|
||||
std::unordered_map<std::string, components::Material> materials;
|
||||
Camera camera;
|
||||
components::Lighting light;
|
||||
|
|
@ -47,7 +46,7 @@ struct Scene {
|
|||
unsigned int skybox_texture_id = 0;
|
||||
bool reflect_on = false;
|
||||
|
||||
glm::vec3 translations[INSTANCE_COUNT];
|
||||
std::vector<glm::vec3> translations;
|
||||
unsigned int instanceVBO = 0;
|
||||
|
||||
Scene(components::Scene& config):
|
||||
|
|
@ -80,6 +79,11 @@ struct Scene {
|
|||
thing.scale);
|
||||
}
|
||||
|
||||
glGenVertexArrays(1, &skyboxVAO);
|
||||
glGenBuffers(1, &skyboxVBO);
|
||||
glGenTextures(1, &skybox_texture_id);
|
||||
glGenBuffers(1, &instanceVBO);
|
||||
|
||||
shader.use();
|
||||
shader.setInt("texture1", 0);
|
||||
shader.apply_lighting(light);
|
||||
|
|
|
|||
|
|
@ -14,13 +14,13 @@ namespace model_tests {
|
|||
Shader shader({"shaders/14-vert.glsl", "shaders/14-frag.glsl", ""});
|
||||
|
||||
Model popcorn{"assets", "popcorn_model_a.glb"};
|
||||
popcorn.draw(shader);
|
||||
popcorn.draw(shader, true, 1);
|
||||
|
||||
Model crate_glb{"assets", "crate.glb"};
|
||||
crate_glb.draw(shader);
|
||||
crate_glb.draw(shader, true, 1);
|
||||
|
||||
Model crate_obj{"assets", "crate.obj"};
|
||||
crate_obj.draw(shader);
|
||||
crate_obj.draw(shader, true, 1);
|
||||
}
|
||||
|
||||
void test_failures() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue