PDA

View Full Version : Instancing rendering nothing



kaberdock
03-25-2014, 11:36 AM
Hello, I'm learning about instancing but my code is rendering nothing and I don't know what is the problem.

I'm creating a little library that help me to use opengl easily so it has a class Object that represent a mesh that will be rendered and a Class Instance that represent instances of this object that will be rendered. The class instance inherits methods attributes from Object class but it has one more buffer to store the transformation matrix to be used in vertex shader program.

So my Instance class code is:



/*
* Instance.cc
*
* Created on: 25/03/2014
* Author: luiz
*/

#include "../lib/Instance.h"

sgl::Instance::Instance(Object obj, int nInstances) {

this->nInstances = nInstances;
this->vertices = obj.getVertices();
this->indices = obj.getIndices();
transformationBuffer.createBuffer(GL_ARRAY_BUFFER, GL_STREAM_DRAW);
this->vertexBuffer.createBuffer(GL_ARRAY_BUFFER, GL_STATIC_DRAW);
this->vertexBuffer.setData(&(obj.getVertices()[0]),
obj.getVertices().size() * sizeof(glm::vec3));
this->elementbuffer.createBuffer(GL_ELEMENT_ARRAY_BUFFER , GL_STATIC_DRAW);
this->elementbuffer.setData(&(obj.getIndices()[0]),
obj.getIndices().size() * sizeof(unsigned short));
this->colorBuffer.createBuffer(GL_ARRAY_BUFFER, GL_STATIC_DRAW);
this->colorBuffer.setData(&(obj.getColor()[0]),
obj.getColor().size() * sizeof(glm::vec3));

for (int i = 0; i < nInstances; i++) {
this->transformations.push_back(glm::mat4(1.0f));
this->scales.push_back(glm::mat4(1.0f));
this->rotates.push_back(glm::mat4(1.0f));
this->translations.push_back(glm::mat4(1.0f));

}
}

sgl::Instance::~Instance() {

}

glm::mat4 sgl::Instance::translate(int index, glm::vec3 v) {

this->translations[index] = glm::translate(glm::mat4(1.0f), v);

return translations[index];
}

glm::mat4 sgl::Instance::rotate(int index, glm::vec3 eulerAngles) {

glm::quat quaternion = glm::quat(glm::radians(-eulerAngles));
this->rotates[index] = glm::toMat4(quaternion);
return rotates[index];

}

glm::mat4 sgl::Instance::scale(int index, glm::vec3 size) {

scales[index] = glm::scale(glm::mat4(1.0f), size);
return this->scales[index];

}

void sgl::Instance::link(int son, int father) {
links[son] = father;
}

glm::mat4 sgl::Instance::getTransformationsMatrix(int index) {
if (this->links.find(index) == this->links.end()) {
//cout<<"has no father"<<endl;
return this->translations[index] * this->rotates[index]
* this->scales[index];
} else {
//cout<<"has a father"<<endl;
return this->getTransformationsMatrix(links.find(index)->second)
* this->translations[index] * this->rotates[index]
* this->scales[index];

}
}

void sgl::Instance::render(GLenum mode) {

this->transformationBuffer.setData(&(this->transformations[0]),
transformations.size() * sizeof(glm::mat4));

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer.getBuffer());
glVertexAttribPointer(0, 3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*) 0 // array buffer offset
);
if (this->hasColor) {
//cout<<"existe cor"<<" "<<color.size()<<endl;
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, this->colorBuffer.getBuffer());
glVertexAttribPointer(1, // The attribute we want to configure
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*) 0 // array buffer offset
);

}
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, this->transformationBuffer.getBuffer());
glVertexAttribPointer(2, // The attribute we want to configure
4, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
sizeof(glm::mat4), // stride
(void*) 0 // array buffer offset
);

glVertexAttribDivisor(0, 0);
glVertexAttribDivisor(1, 0);
glVertexAttribDivisor(2, 1);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer.getBuffer());

glDrawElementsInstanced(GL_TRIANGLES, this->indices.size(),
GL_UNSIGNED_SHORT, &(this->indices[0]), this->nInstances);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);

}





and it is the vertex shader:



#version 330

layout (location = 0) in vec3 Position;
layout (location = 2) in mat4 trans;

uniform mat4 Camera;


void main()
{
mat4 MVP = Camera * trans;
gl_Position = MVP * vec4(Position, 1.0);
}



Thanks for your attention.