From ac6a01d5bed92c772f0d3a1a4aa488784c3271ac Mon Sep 17 00:00:00 2001 From: dompia5 Date: Wed, 7 Feb 2024 21:58:19 +0100 Subject: [PATCH] instancing but scale is messed up --- PlanetCreator/cw 6/src/Render_Utils.cpp | 64 +++++++++++++++++-------- PlanetCreator/cw 6/src/ex_6_1.hpp | 27 +++++------ 2 files changed, 57 insertions(+), 34 deletions(-) diff --git a/PlanetCreator/cw 6/src/Render_Utils.cpp b/PlanetCreator/cw 6/src/Render_Utils.cpp index b880ab1..7d449a4 100644 --- a/PlanetCreator/cw 6/src/Render_Utils.cpp +++ b/PlanetCreator/cw 6/src/Render_Utils.cpp @@ -214,22 +214,58 @@ void Core::DrawContext(Core::RenderContext& context) -void Core::DrawContextInstanced(Core::RenderContext& context, std::vector transformations,std::vector Modelmatrices,int numberOfInstances, GLuint program) +void Core::DrawContextInstanced(Core::RenderContext& context, std::vector transformations, std::vector Modelmatrices, int numberOfInstances,GLuint program) { + + GLuint VAO = context.vertexArray; + glBindVertexArray(VAO); GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, transformations.size() * sizeof(glm::mat4), transformations.data(), GL_STATIC_DRAW); - GLuint VAO = context.vertexArray; - glBindVertexArray(VAO); std::size_t mat4Size = sizeof(glm::mat4); - glEnableVertexAttribArray(6); - glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, mat4Size, (void*)0); - glVertexAttribDivisor(6, 1); + for (int i = 0; i < 4; i++) { + glEnableVertexAttribArray(6+i); + glVertexAttribPointer(6 + i, 4, GL_FLOAT, GL_FALSE, mat4Size, (void*)(sizeof(glm::vec4) * i)); + glVertexAttribDivisor(6 + i, 1); + } + GLuint matricesBuffer; + glGenBuffers(1, &matricesBuffer); + glBindBuffer(GL_ARRAY_BUFFER, matricesBuffer); + glBufferData(GL_ARRAY_BUFFER, Modelmatrices.size() * sizeof(glm::mat4), Modelmatrices.data(), GL_STATIC_DRAW); + for (int i = 0; i < 4; i++) { + glEnableVertexAttribArray(10 + i); + glVertexAttribPointer(10 + i, 4, GL_FLOAT, GL_FALSE, mat4Size, (void*)(sizeof(glm::vec4) * i)); + glVertexAttribDivisor(10 + i, 1); + } + glUniform3f(glGetUniformLocation(program, "lightPos"), 0, 0, 0); + //Material + glUniform1f(glGetUniformLocation(program, "shininess"), 0); + glUniform3f(glGetUniformLocation(program, "ambientColor"), 0, 0,0 ); + glUniform3f(glGetUniformLocation(program, "specularColor"), 0,0 , 0); + glUniform3f(glGetUniformLocation(program, "emissiveColor"), 0,0 ,0 ); + glUniform1f(glGetUniformLocation(program, "opticalDensity"),0 ); + //glUniform1f(glGetUniformLocation(program, "dissolve"), material.d); + glUniform1i(glGetUniformLocation(program, "illuminationModel"), 0); + glUniform1f(glGetUniformLocation(program, "metallic"), 0.05); + glUniform1f(glGetUniformLocation(program, "roughness"), 0.2); - /* std::size_t vec4Size = sizeof(glm::vec4); + glDrawElementsInstanced( + GL_TRIANGLES, // mode + context.size, // count + GL_UNSIGNED_INT, // type + (void*)0, numberOfInstances // element array buffer offset + ); + + glBindVertexArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + +} + +/* std::size_t vec4Size = sizeof(glm::vec4); glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 4 * vec4Size, (void*)0);* glEnableVertexAttribArray(4); @@ -250,16 +286,4 @@ void Core::DrawContextInstanced(Core::RenderContext& context, std::vector objA, std::tuple objB) PlanetParams populatePlanet(PlanetParams planet, std::vector plants) { float probability=0.5f; - int HOW_MANY_PLANTS = 10000; + int HOW_MANY_PLANTS = 100; int PRECISION = 10000; float NOTHING_SPAWNS_CUTOFF = 0.0; int sum = 0; @@ -320,7 +320,7 @@ glm::mat4 createPerspectiveMatrix() glm::mat4 perspectiveMatrix; float n = 0.05; - float f = 15.; + float f = 100.; float a1 = glm::min(aspectRatio, 1.f); float a2 = glm::min(1 / aspectRatio, 1.f); perspectiveMatrix = glm::mat4({ @@ -417,18 +417,17 @@ void drawObjectTexture_plantInstanced(Core::RenderContext& context, std::vector< glm::mat4 transformation = viewProjectionMatrix* matrix; transformations.push_back(transformation); } - - glUniform3f(glGetUniformLocation(program, "lightPos"), 0, 0, 0); - //Material - glUniform1f(glGetUniformLocation(program, "shininess"), material.Ns); - glUniform3f(glGetUniformLocation(program, "ambientColor"), material.Ka.r, material.Ka.g, material.Ka.b); - glUniform3f(glGetUniformLocation(program, "specularColor"), material.Ks.r, material.Ks.g, material.Ks.b); - glUniform3f(glGetUniformLocation(program, "emissiveColor"), material.Ke.r, material.Ke.g, material.Ke.b); - glUniform1f(glGetUniformLocation(program, "opticalDensity"), material.Ni); - //glUniform1f(glGetUniformLocation(program, "dissolve"), material.d); - glUniform1i(glGetUniformLocation(program, "illuminationModel"), material.illum); - glUniform1f(glGetUniformLocation(program, "metallic"), 0.05); - glUniform1f(glGetUniformLocation(program, "roughness"), 0.2); + //glUniform3f(glGetUniformLocation(program, "lightPos"), 0, 0, 0); + ////Material + //glUniform1f(glGetUniformLocation(program, "shininess"), material.Ns); + //glUniform3f(glGetUniformLocation(program, "ambientColor"), material.Ka.r, material.Ka.g, material.Ka.b); + //glUniform3f(glGetUniformLocation(program, "specularColor"), material.Ks.r, material.Ks.g, material.Ks.b); + //glUniform3f(glGetUniformLocation(program, "emissiveColor"), material.Ke.r, material.Ke.g, material.Ke.b); + //glUniform1f(glGetUniformLocation(program, "opticalDensity"), material.Ni); + ////glUniform1f(glGetUniformLocation(program, "dissolve"), material.d); + //glUniform1i(glGetUniformLocation(program, "illuminationModel"), material.illum); + //glUniform1f(glGetUniformLocation(program, "metallic"), 0.05); + //glUniform1f(glGetUniformLocation(program, "roughness"), 0.2); Core::DrawContextInstanced(context, transformations,modelMatrices,count,program); glUseProgram(0);