69 VertexAttribute(
string in_name,
int in_num_elements,
int in_element_size, GLenum in_type, GLenum in_array_type = GL_ARRAY_BUFFER) :
70 name(in_name), size(in_num_elements), element_size(in_element_size), type(in_type), array_type (in_array_type)
73 glGenBuffers(1, &bufferID);
75 bufferID_sptr = std::shared_ptr < GLuint > (
76 new GLuint (bufferID),
78 glDeleteBuffers(1, p);
148 glBindBuffer(array_type, *bufferID_sptr);
163 glBindBuffer(array_type, *bufferID_sptr);
164 glVertexAttribPointer(location, element_size, type, GL_FALSE, 0, NULL);
165 glEnableVertexAttribArray(location);
173 glBindBuffer(array_type, 0);
181 glDisableVertexAttribArray(location);
201 unsigned int numberOfVertices = 0;
204 unsigned int numberOfNormals = 0;
207 unsigned int numberOfElements = 0;
210 unsigned int numberOfTexCoords = 0;
213 unsigned int numberOfColors = 0;
216 GLuint index_buffer_id = 0;
222 bool willTessellate =
false;
225 std::shared_ptr < GLuint > index_buffer_sptr = 0;
228 std::shared_ptr < GLuint > vao_sptr = 0;
240 glGenBuffers(1, &index_buffer_id);
241 index_buffer_sptr = std::shared_ptr < GLuint > (
242 new GLuint (index_buffer_id),
244 glDeleteBuffers(1, p);
249 glGenVertexArrays(1, &vao_id);
250 vao_sptr = std::shared_ptr < GLuint > (
253 glDeleteVertexArrays(1, p);
271 return numberOfElements;
280 return numberOfVertices;
288 for (
unsigned int i = 0; i < vertex_attributes.size(); ++i)
290 vertex_attributes[i].setLocation(-1);
296 for (
unsigned int i = 0; i < vertex_attributes.size(); ++i)
298 vertex_attributes[i].disable();
315 numberOfVertices = vert.size();
318 createAttribute(
"in_Position", vert);
322 float xMax = 0;
float xMin = 0;
float yMax = 0;
float yMin = 0;
float zMax = 0;
float zMin = 0;
325 for(
unsigned int i = 0; i < numberOfVertices*4; i+=4) {
328 if(vert[temp][0] > xMax) {
329 xMax = vert[temp][0];
331 if(vert[temp][0] < xMin) {
332 xMin = vert[temp][0];
336 if(vert[temp][1] > yMax) {
337 yMax = vert[temp][1];
339 if(vert[temp][1] < yMin) {
340 yMin = vert[temp][1];
344 if(vert[temp][2] > zMax) {
345 zMax = vert[temp][2];
347 if(vert[temp][2] < zMin) {
348 zMin = vert[temp][2];
355 scale = 1.0/max(max(xMax-xMin, yMax-yMin), zMax-zMin);
357 float centerX = (xMax+xMin)/2.0;
358 float centerY = (yMax+yMin)/2.0;
359 float centerZ = (zMax+zMin)/2.0;
360 objectCenter = Eigen::Vector3f(centerX, centerY, centerZ);
363 centroid = Eigen::Vector3f::Zero();
364 for(
unsigned int i = 0; i < numberOfVertices; i++) {
365 centroid = centroid + vert[i].head(3);
367 centroid = centroid / numberOfVertices;
372 for(
unsigned int i = 0; i < numberOfVertices; i++) {
373 radius = max(radius, ( vert[i].head(3) - centroid ).norm());
386 numberOfNormals = norm.size();
388 createAttribute(
"in_Normal", norm);
399 numberOfTexCoords = tex.size();
403 vector<Eigen::Vector2f> tex_normalized;
405 float texXmax = tex[0][0];
406 float texXmin = tex[0][0];
407 float texYmax = tex[0][1];
408 float texYmin = tex[0][1];
411 for(
unsigned int i = 0; i < numberOfTexCoords; i++)
413 if (tex[i][0] < texXmin) texXmin = tex[i][0];
414 if (tex[i][0] > texXmax) texXmax = tex[i][0];
415 if (tex[i][1] < texYmin) texYmin = tex[i][1];
416 if (tex[i][1] > texYmax) texYmax = tex[i][1];
419 for(
unsigned int i = 0; i < numberOfTexCoords; i++)
421 tex_normalized.push_back ( Eigen::Vector2f(
422 (tex[i][0] - texXmin) / (texXmax - texXmin),
423 (tex[i][1] - texYmin) / (texYmax - texYmin) ) );
425 createAttribute(
"in_TexCoords", tex_normalized);
429 createAttribute(
"in_TexCoords", tex);
440 createAttribute(
"in_Color", clrs);
450 numberOfElements = ind.size();
452 GLuint *indices =
new GLuint[ind.size()];
455 for(
unsigned int i = 0; i < ind.size(); i++)
460 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *index_buffer_sptr);
461 glBufferData(GL_ELEMENT_ARRAY_BUFFER, ind.size()*
sizeof(GLuint), indices, GL_STATIC_DRAW);
462 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
477 for (
unsigned int i = 0; i < vertex_attributes.size(); ++i)
479 if (!vertex_attributes[i].
getName().compare(
"in_Position"))
481 vertex_attributes[i].setLocation(0);
483 else if (!vertex_attributes[i].
getName().compare(
"in_Normal"))
485 vertex_attributes[i].setLocation(1);
487 else if (!vertex_attributes[i].
getName().compare(
"in_Color"))
489 vertex_attributes[i].setLocation(2);
491 else if (!vertex_attributes[i].
getName().compare(
"in_TexCoord"))
493 vertex_attributes[i].setLocation(3);
505 for (
unsigned int i = 0; i < vertex_attributes.size(); ++i)
507 if (!vertex_attributes[i].
getName().compare(name))
524 for (
unsigned int i = 0; i < vertex_attributes.size(); ++i)
526 if (!vertex_attributes[i].
getName().compare(name))
528 return &vertex_attributes[i];
543 for (
unsigned int i = 0; i < vertex_attributes.size(); ++i)
546 vertex_attributes[i].setLocation(loc);
549 willTessellate =
true;
551 willTessellate =
false;
556 setAttributeLocation((
Shader*)(&shader));
567 for (
unsigned int i = 0; i < vertex_attributes.size(); ++i)
569 if (!name.compare(vertex_attributes[i].getName()))
571 vertex_attributes[i].setLocation(loc);
588 float * attrib_array =
new float[va.getSize()*va.getElementSize()];
590 for(
int i = 0; i < va.getSize()*va.getElementSize(); i+=4) {
591 attrib_array[i] = attrib[temp][0];
592 attrib_array[i+1] = attrib[temp][1];
593 attrib_array[i+2] = attrib[temp][2];
594 attrib_array[i+3] = attrib[temp][3];
600 glBufferData(va.getArrayType(), va.getSize()*va.getElementSize()*
sizeof(va.getType()), attrib_array, GL_STATIC_DRAW);
603 vertex_attributes.push_back(va);
604 delete [] attrib_array;
605 return &vertex_attributes[vertex_attributes.size()-1];
620 float * attrib_array =
new float[va.getSize()*va.getElementSize()];
623 for(
int i = 0; i < va.getSize()*va.getElementSize(); i+=3) {
624 attrib_array[i] = attrib[temp][0];
625 attrib_array[i+1] = attrib[temp][1];
626 attrib_array[i+2] = attrib[temp][2];
632 glBufferData(va.getArrayType(), va.getSize()*va.getElementSize()*
sizeof(va.getType()), attrib_array, GL_STATIC_DRAW);
635 vertex_attributes.push_back(va);
636 delete [] attrib_array;
637 return &vertex_attributes[vertex_attributes.size()-1];
651 float * attrib_array =
new float[va.getSize()*va.getElementSize()];
654 for(
int i = 0; i < va.getSize()*va.getElementSize(); i+=2) {
655 attrib_array[i] = attrib[temp][0];
656 attrib_array[i+1] = attrib[temp][1];
662 glBufferData(va.getArrayType(), va.getSize()*va.getElementSize()*
sizeof(va.getType()), attrib_array, GL_STATIC_DRAW);
665 vertex_attributes.push_back(va);
666 delete [] attrib_array;
667 return &vertex_attributes[vertex_attributes.size()-1];
679 assert (vao_sptr != 0);
681 glBindVertexArray(*vao_sptr);
683 if (*index_buffer_sptr)
685 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *index_buffer_sptr);
689 for (
unsigned int i = 0; i < vertex_attributes.size(); ++i)
691 vertex_attributes[i].enable();
704 glBindVertexArray(0);
705 glBindBuffer(GL_ARRAY_BUFFER,0);
706 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
708 for (
unsigned int i = 0; i < vertex_attributes.size(); ++i)
710 vertex_attributes[i].disable();
719 glDrawArrays(GL_POINTS, 0, numberOfVertices);
729 glDrawElements(GL_TRIANGLES, numberOfElements, GL_UNSIGNED_INT, (GLvoid*)0);
737 glDrawArrays(GL_LINE_LOOP, 0, numberOfVertices);
747 glPatchParameteri(GL_PATCH_VERTICES, 3);
748 glDrawElements(GL_PATCHES, numberOfElements,GL_UNSIGNED_INT, 0);
759 if (numberOfElements == 0)
786 numberOfVertices = 32;
787 numberOfElements = 36;
790 vector<Eigen::Vector4f> vert;
795 if (y > x) scale = y;
796 if (z > scale) scale = z;
798 vert.push_back( Eigen::Vector4f (-.5f*x/scale, -.5f*y/scale, .5f*z/scale, 1) );
799 vert.push_back( Eigen::Vector4f (-.5f*x/scale, .5f*y/scale, .5f*z/scale, 1) );
800 vert.push_back( Eigen::Vector4f (.5f*x/scale, .5f*y/scale, .5f*z/scale, 1) );
801 vert.push_back( Eigen::Vector4f (.5f*x/scale, -.5f*y/scale, .5f*z/scale, 1) );
802 vert.push_back( Eigen::Vector4f (-.5f*x/scale, -.5f*y/scale, -.5f*z/scale, 1) );
803 vert.push_back( Eigen::Vector4f (-.5f*x/scale, .5f*y/scale, -.5f*z/scale, 1) );
804 vert.push_back( Eigen::Vector4f (.5f*x/scale, .5f*y/scale, -.5f*z/scale, 1) );
805 vert.push_back( Eigen::Vector4f (.5f*x/scale, -.5f*y/scale, -.5f*z/scale, 1) );
807 GLuint tempIndices[36] = {
816 for(
unsigned int i = 0; i < numberOfElements;i++) {
817 ind.push_back( tempIndices[i] );
835 setDefaultAttribLocations();
843 vector<Eigen::Vector4f> vert;
844 vector<Eigen::Vector2f> texCoord;
845 vector<GLuint> elementsVertices;
847 vert.push_back ( Eigen::Vector4f(-1.0, -1.0, 0.0, 1.0) );
848 vert.push_back ( Eigen::Vector4f( 1.0, -1.0, 0.0, 1.0) );
849 vert.push_back ( Eigen::Vector4f( 1.0, 1.0, 0.0, 1.0) );
850 vert.push_back ( Eigen::Vector4f(-1.0, 1.0, 0.0, 1.0) );
852 elementsVertices.push_back(0);
853 elementsVertices.push_back(1);
854 elementsVertices.push_back(2);
855 elementsVertices.push_back(2);
856 elementsVertices.push_back(3);
857 elementsVertices.push_back(0);
859 texCoord.push_back ( Eigen::Vector2f(0.0, 0.0) );
860 texCoord.push_back ( Eigen::Vector2f(1.0, 0.0) );
861 texCoord.push_back ( Eigen::Vector2f(1.0, 1.0) );
862 texCoord.push_back ( Eigen::Vector2f(0.0, 1.0) );
865 loadTexCoords(texCoord);
866 loadIndices(elementsVertices);
868 setDefaultAttribLocations();
void setAttributeLocation(string name, GLint loc)
Sets the location of a generic vertex attribute.
Definition: mesh.hpp:565
int getElementSize(void) const
Returns the number of elements per attribute For example, returns 3 if attribute is of type vec3...
Definition: mesh.hpp:104
void createQuad(void)
Sets the mesh as Unit Quad.
Definition: mesh.hpp:841
int getNumberOfElements(void)
Returns the number of elements (primitives such as triangles) of the mesh.
Definition: mesh.hpp:269
void loadNormals(vector< Eigen::Vector3f > &norm)
Load normals (x,y,z) as a vertex attribute.
Definition: mesh.hpp:384
VertexAttribute * createAttribute(string name, vector< Eigen::Vector4f > &attrib)
Creates and loads a new mesh attribute of 4 floats.
Definition: mesh.hpp:583
virtual void renderLineLoop(void)
Render all vertices as a continous line loop.
Definition: mesh.hpp:735
void createParallelepiped(float x, float y, float z)
Sets the mesh as a Parallelpiped with given dimensions, scales so larger side is equal to 1...
Definition: mesh.hpp:783
GLuint getTessellationEvaluationShader(void)
Returns a handle to the tessellation evaluation shader.
Definition: shader.hpp:405
void loadColors(vector< Eigen::Vector4f > &clrs)
Load colors (r,g,b,a) as a vertex attribute.
Definition: mesh.hpp:438
int element_size
number of elements per attribute
Definition: mesh.hpp:52
GLint getAttributeLocation(const GLchar *name) const
Definition: shader.hpp:1240
Definition: bufferobject.hpp:34
GLuint getBufferID(void)
Returns the id of the vertex array of this attribute.
Definition: mesh.hpp:143
void setAttributeLocation(const Shader &shader)
Definition: mesh.hpp:554
virtual void renderPatches(void)
Call the draw method for rendering patches. This method requires that a index buffer has been created...
Definition: mesh.hpp:745
int getSize(void) const
Returns the number of elements of this attribute (usually number of vertices in VAO) ...
Definition: mesh.hpp:97
int getNumberOfVertices(void)
Returns the number of vertices in the mesh.
Definition: mesh.hpp:278
The Model class is a holder for any kind of model, such as meshes, point clouds, surfaces ...
Definition: model.hpp:16
virtual void renderPoints(void)
Render only points without index buffer.
Definition: mesh.hpp:717
A Shader object represents one GLSL program.
Definition: shader.hpp:45
void errorCheckFunc(std::string file, int line, std::string message="")
GL error check method.
Definition: misc.hpp:53
void disable(void)
Disables associated location if attribute has one.
Definition: mesh.hpp:177
GLenum type
Type of attribute element (ex. GL_FLOAT)
Definition: mesh.hpp:58
Mesh(void)
Default Constructor.
Definition: mesh.hpp:235
void loadIndices(vector< GLuint > &ind)
Load indices into indices array.
Definition: mesh.hpp:448
GLenum array_type
Type of attribute array (GL_ARRAY_BUFFER for most cases), indices are GL_ELEMENT_ARRAY_BUFFER.
Definition: mesh.hpp:60
void setLocation(GLint loc)
Sets the location of the attribute for a shader.
Definition: mesh.hpp:137
bool hasAttribute(const string &name) const
Returns wether an attribute exists or not.
Definition: mesh.hpp:503
GLenum getType(void) const
Returns the type of the attribute (ex. GL_FLOAT)
Definition: mesh.hpp:110
VertexAttribute * createAttribute(string name, vector< Eigen::Vector2f > &attrib)
Creates and loads a new mesh attribute of 2 floats.
Definition: mesh.hpp:646
GLint getLocation(void) const
Returns the location of the attribute. The location is usually set by the shader, since it can be use...
Definition: mesh.hpp:131
A vertex attribute of a mesh.
Definition: mesh.hpp:42
VertexAttribute(string in_name, int in_num_elements, int in_element_size, GLenum in_type, GLenum in_array_type=GL_ARRAY_BUFFER)
Definition: mesh.hpp:69
void setDefaultAttribLocations(void)
Sets default attribute locations. vertex coords -> location 0 normals -> location 1 colors -> locatio...
Definition: mesh.hpp:474
void reset(void)
Definition: mesh.hpp:294
int size
size of attribute array (usually number of vertices)
Definition: mesh.hpp:50
void bind(void)
Bind the attribute.
Definition: mesh.hpp:146
GLint location
this is set by the shader using the attribute
Definition: mesh.hpp:54
void resetLocations(void)
Resets all vertex attributes locations to -1.
Definition: mesh.hpp:286
void setArrayType(GLenum at)
Sets the type of array (default is GL_ARRAY_BUFFER)
Definition: mesh.hpp:122
GLuint bufferID
Vertex array Buffer ID.
Definition: mesh.hpp:56
std::shared_ptr< GLuint > bufferID_sptr
Definition: mesh.hpp:62
VertexAttribute * getAttribute(const string &name)
Returns a pointer to an attribute Given an attribute name, searches to see if it exists, if so, returns a pointer to it.
Definition: mesh.hpp:522
virtual void unbindBuffers(void)
Unbinds all buffers.
Definition: mesh.hpp:702
virtual void render(void)
Render the mesh triangles. The method binds the buffers, calls the method to render triangles...
Definition: mesh.hpp:756
GLenum getArrayType(void) const
Returns the type of array (ex. GL_ARRAY_BUFFER)
Definition: mesh.hpp:116
vector< Tucano::VertexAttribute > vertex_attributes
Array of generic attributes.
Definition: mesh.hpp:198
void loadVertices(vector< Eigen::Vector4f > &vert)
Load vertices (x,y,z,w) and creates appropriate vertex attribute. The default attribute name is "in_P...
Definition: mesh.hpp:312
string getName(void) const
Returns the name of the attribute.
Definition: mesh.hpp:91
VertexAttribute * createAttribute(string name, vector< Eigen::Vector3f > &attrib)
Creates and loads a new mesh attribute of 3 floats.
Definition: mesh.hpp:615
void enable()
Binds the attribute to its location.
Definition: mesh.hpp:159
void loadTexCoords(vector< Eigen::Vector2f > &tex, bool normalize=false)
Load tex coords (u,v) as a vertex attribute. Optionally normalizes coords in range [0...
Definition: mesh.hpp:397
virtual void renderElements(void)
Call the draw method for rendering triangles. This method requires that a index buffer has been creat...
Definition: mesh.hpp:726
virtual void bindBuffers(void)
Binds all buffers.
Definition: mesh.hpp:677
void enable(GLint loc)
Bind the attribute to a given location.
Definition: mesh.hpp:152
A common Mesh, usually containing triagles or points.
Definition: mesh.hpp:194
void setAttributeLocation(Shader *shader)
Automatically sets the attribute locations for a given Shader.
Definition: mesh.hpp:541
string name
Attribute's name.
Definition: mesh.hpp:48
void unbind(void)
Unbind the attribute.
Definition: mesh.hpp:171