instance was not the right thing for me,

now it's working
This commit is contained in:
Anakin 2016-11-07 17:07:16 +01:00
parent de76f7182a
commit 42c94f290c
3 changed files with 23 additions and 69 deletions

View File

@ -53,7 +53,6 @@ private:
//object data //object data
GLuint gluiVertexArrayID = 0; GLuint gluiVertexArrayID = 0;
GLuint gluiVertexBufferID = 0; GLuint gluiVertexBufferID = 0;
GLuint gluiInstanceBufferID = 0;
//obj color //obj color
GLuint gluiTextureID = 0; GLuint gluiTextureID = 0;

View File

@ -3,10 +3,6 @@
// Input vertex data, different for all executions of this shader. // Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace; layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV; layout(location = 1) in vec2 vertexUV;
layout(location = 2) in mat4 mvp;
//layout(location = 3) in mat4 mvp;
//layout(location = 4) in mat4 mvp;
//layout(location = 5) in mat4 mvp;
// Output // Output
out vec2 UV; out vec2 UV;

View File

@ -24,7 +24,6 @@ OpenGLController* OpenGLController::getInstance(int oglMajor, int oglMinor)
OpenGLController::~OpenGLController() OpenGLController::~OpenGLController()
{ {
glDeleteBuffers(1, &gluiInstanceBufferID);
glDeleteBuffers(1, &gluiVertexBufferID); glDeleteBuffers(1, &gluiVertexBufferID);
glDeleteVertexArrays(1, &gluiVertexArrayID); glDeleteVertexArrays(1, &gluiVertexArrayID);
glDeleteProgram(gluiShaderPrgmID); glDeleteProgram(gluiShaderPrgmID);
@ -83,7 +82,6 @@ void OpenGLController::processInit()
glBindVertexArray(gluiVertexArrayID); glBindVertexArray(gluiVertexArrayID);
glGenBuffers(1, &gluiVertexBufferID); glGenBuffers(1, &gluiVertexBufferID);
glGenBuffers(1, &gluiInstanceBufferID);
// open attribute position // open attribute position
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID); glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
@ -91,28 +89,10 @@ void OpenGLController::processInit()
glVertexAttribPointer(VERTEX_INDEX_UV, VERTEX_COMPONENTS_UV, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)VERTEX_OFFSET_UV); glVertexAttribPointer(VERTEX_INDEX_UV, VERTEX_COMPONENTS_UV, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)VERTEX_OFFSET_UV);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, gluiInstanceBufferID);
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 // enable position, UV and mvp
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1); 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 // get the painter ready
try try
{ {
@ -271,21 +251,17 @@ void OpenGLController::updateScene()
int instanceOffset(0); int instanceOffset(0);
for (unsigned int modelIndex = 0; modelIndex < vModels.size(); modelIndex++) for (int modelIndex = 0; modelIndex < vModels.size(); modelIndex++)
{ {
// give the MVPs to the shader glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix(modelIndex)[0][0]);
//glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix(modelIndex)[0][0]); glDrawArrays(GL_TRIANGLES, instanceOffset, vModels[modelIndex]->meshSize);
//draw objects hardcoded only 1 instance for testing
glDrawArraysInstanced(GL_TRIANGLES, instanceOffset, vModels[modelIndex]->meshSize, vModels.size());
instanceOffset += vModels[modelIndex]->meshSize; instanceOffset += vModels[modelIndex]->meshSize;
} }
glfwSwapBuffers(pWindow); glfwSwapBuffers(pWindow);
glfwPollEvents(); glfwPollEvents();
} }
void OpenGLController::loadMsh(const char * path) void OpenGLController::loadMsh(const char * path)
@ -350,26 +326,29 @@ void OpenGLController::loadMsh(const char * path)
////TODO: for all ////TODO: for all
std::vector<Vertex> tempBufferData; std::vector<Vertex> tempBufferData;
for (unsigned int i = 0; i < vModels.front()->meshSize; i++) for (auto& it : vModels)
{ {
Vertex tempVertex; for (unsigned int i = 0; i < it->meshSize; i++)
tempVertex.position[0] = (GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3];
tempVertex.position[1] = (GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3 + 1];
tempVertex.position[2] = (GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3 + 2];
if (vModels.front()->uv == NULL)
{ {
tempVertex.uv[0] = 1.0; Vertex tempVertex;
tempVertex.uv[1] = 1.0; tempVertex.position[0] = (GLfloat)it->vertex[it->mesh[i] * 3];
} tempVertex.position[1] = (GLfloat)it->vertex[it->mesh[i] * 3 + 1];
else tempVertex.position[2] = (GLfloat)it->vertex[it->mesh[i] * 3 + 2];
{
tempVertex.uv[0] = (GLfloat)vModels.front()->uv[vModels.front()->mesh[i] * 2];
tempVertex.uv[1] = (GLfloat)vModels.front()->uv[vModels.front()->mesh[i] * 2 + 1];
}
tempBufferData.push_back(tempVertex); if (it->uv == NULL)
{
tempVertex.uv[0] = 1.0;
tempVertex.uv[1] = 1.0;
}
else
{
tempVertex.uv[0] = (GLfloat)it->uv[it->mesh[i] * 2];
tempVertex.uv[1] = (GLfloat)it->uv[it->mesh[i] * 2 + 1];
}
tempBufferData.push_back(tempVertex);
}
} }
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID); glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
@ -381,24 +360,4 @@ void OpenGLController::loadMsh(const char * path)
); );
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
// set instance buffer data
std::vector<glm::mat4> tempMVPs;
for (int i = 0; i < vModels.size(); i++)
tempMVPs.push_back(getMVPMatrix(i));
glBindBuffer(GL_ARRAY_BUFFER, gluiInstanceBufferID);
glBufferData(
GL_ARRAY_BUFFER,
vModels.size() > MAX_MODEL_COUNT? MAX_MODEL_COUNT * sizeof(glm::mat4) : vModels.size() * sizeof(glm::mat4),
tempMVPs.data(),
GL_STREAM_DRAW
);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//glBindBuffer(GL_UNIFORM_BUFFER, gluiInstanceBufferID);
//glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::mat4) * vModels.size(), NULL, tempMVPs.data());
//glBindBuffer(GL_UNIFORM_BUFFER, 0);
} }