From 1cbc9336fe45547bde1e0fa6d4848d8564975738 Mon Sep 17 00:00:00 2001 From: Anakin Date: Sat, 5 Nov 2016 11:53:49 +0100 Subject: [PATCH] fixing some problems, trying to draw instanced, now it crashes --- MshViewer/Source/OpenGlController.cpp | 33 +++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp index ffefbef..1da567b 100644 --- a/MshViewer/Source/OpenGlController.cpp +++ b/MshViewer/Source/OpenGlController.cpp @@ -92,15 +92,27 @@ void OpenGLController::processInit() glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, gluiInstanceBufferID); - glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(GL_FLOAT) * 0)); - glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(GL_FLOAT) * 4)); - glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(GL_FLOAT) * 8)); - glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(GL_FLOAT) * 12)); + glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(GLfloat) * 0)); + glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(GLfloat) * 4)); + glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(GLfloat) * 8)); + glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(GLfloat) * 12)); glBindBuffer(GL_ARRAY_BUFFER, 0); + // enable position, UV and mvp glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); + glEnableVertexAttribArray(3); + glEnableVertexAttribArray(4); + glEnableVertexAttribArray(5); + + // MPV only once per instance + glVertexAttribDivisor(2, 1); + glVertexAttribDivisor(3, 1); + glVertexAttribDivisor(4, 1); + glVertexAttribDivisor(5, 1); + // get the painter ready try { @@ -251,10 +263,13 @@ void OpenGLController::updateScene() // use shader prgm glUseProgram(gluiShaderPrgmID); - // tell shader transformation - //glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix()[0][0]); + // fill vector with MVPs of all Models (only 1 for testing) + std::vector mvpMatrices; + mvpMatrices.push_back(getMVPMatrix()); + + // give the MVPs to the shader glBindBuffer(GL_UNIFORM_BUFFER, gluiInstanceBufferID); - glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::mat4) * 1, NULL, GL_STREAM_DRAW); + glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::mat4) * mvpMatrices.size(), NULL, mvpMatrices.data()); glBindBuffer(GL_UNIFORM_BUFFER, 0); // bind texture in texture unit 0 @@ -263,8 +278,8 @@ void OpenGLController::updateScene() // tell sampler to use texture unit 0 glUniform1i(gluiSamplerID, 0); - //draw objects - glDrawArrays(GL_TRIANGLES, 0, vModels.front()->meshSize); + //draw objects hardcoded only 1 instance for testing + glDrawArraysInstanced(GL_TRIANGLES, 0, vModels.front()->meshSize, 1); glfwSwapBuffers(pWindow); glfwPollEvents();