140 lines
2.9 KiB
C++
140 lines
2.9 KiB
C++
#include "imesh.h"
|
|
|
|
#include "glassert.h"
|
|
|
|
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
|
|
|
|
IMesh::~IMesh()
|
|
{
|
|
destroyGL();
|
|
}
|
|
|
|
void IMesh::addVertex(float x, float y, float z)
|
|
{
|
|
addVertex((glm::vec3(x, y, z));
|
|
}
|
|
|
|
void IMesh::addVertex(glm::vec3 &position)
|
|
{
|
|
positions.push_back(position);
|
|
}
|
|
|
|
void IMesh::addVertex(glm::vec3 &position, glm::vec3 &normal)
|
|
{
|
|
addVertex(position);
|
|
normals.push_back(normal);
|
|
}
|
|
|
|
void IMesh::addVertex(glm::vec3 &position, glm::vec2 &texCoord)
|
|
{
|
|
addVertex(position);
|
|
texCoords.push_back(texCoord);
|
|
}
|
|
|
|
void IMesh::addVertex(glm::vec3 &position, glm::vec3 &normal, glm::vec2 &texCoord)
|
|
{
|
|
addVertex(position, normal);
|
|
texCoords.push_back(texCoord);
|
|
}
|
|
|
|
|
|
void IMesh::addFace(int i1, int i2, int i3)
|
|
{
|
|
if(!locked)
|
|
{
|
|
indices.push_back(i1);
|
|
indices.push_back(i2);
|
|
indices.push_back(i3);
|
|
}
|
|
}
|
|
|
|
void IMesh::computeFaceNormals()
|
|
{
|
|
|
|
}
|
|
|
|
void IMesh::computeVertexNormals()
|
|
{
|
|
|
|
}
|
|
|
|
void IMesh::computeTangents()
|
|
{
|
|
|
|
}
|
|
|
|
bool IMesh::hasNormals()
|
|
{
|
|
return normals.size() != 0;
|
|
}
|
|
|
|
bool IMesh::hasTexCoords()
|
|
{
|
|
return texCoords.size() != 0;
|
|
}
|
|
|
|
bool IMesh::hasTangents()
|
|
{
|
|
return tangents.size() != 0;
|
|
}
|
|
|
|
void IMesh::initGL(bool isDynamic)
|
|
{
|
|
if(locked)
|
|
destroyGL();
|
|
|
|
GLenum buffer_type = isDynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW;
|
|
|
|
// create VAO
|
|
glAssert(glGenVertexArrays(1, &vao));
|
|
glAssert(glBindVertexArray(vao));
|
|
|
|
// create VBOs
|
|
glAssert(glGenBuffers(NB_BUFFERS, vbo));
|
|
|
|
// init positions vbo
|
|
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[POSITION_BUFFER]));
|
|
glAssert(glBufferData(GL_ARRAY_BUFFER, positions.size() * sizeof(glm::vec3), positions.data(), buffer_type));
|
|
|
|
// init normals vbo
|
|
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_BUFFER]));
|
|
glAssert(glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), normals.data(), buffer_type));
|
|
|
|
// init texCoords vbo
|
|
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]));
|
|
glAssert(glBufferData(GL_ARRAY_BUFFER, texCoords.size() * sizeof(glm::vec2), texCoords.data(), buffer_type));
|
|
|
|
// init tangents vbo
|
|
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TANGENT_BUFFER]));
|
|
glAssert(glBufferData(GL_ARRAY_BUFFER, tangents.size() * sizeof(glm::vec3)*2, tangents.data(), buffer_type));
|
|
|
|
// init indices vbo
|
|
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[INDICES_BUFFER]));
|
|
glAssert(glBufferData(GL_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), buffer_type));
|
|
|
|
// unbind vao
|
|
glAssert(glBindVertexArray(0));
|
|
|
|
locked = true;
|
|
}
|
|
|
|
void IMesh::destroyGL()
|
|
{
|
|
if(locked)
|
|
{
|
|
locked = false;
|
|
glAssert(glDeleteVertexArrays(1, &vao));
|
|
glAssert(glDeleteBuffers(2, vbo));
|
|
}
|
|
}
|
|
|
|
void IMesh::draw()
|
|
{
|
|
if(locked)
|
|
{
|
|
glAssert(glBindVertexArray(vao));
|
|
glAssert(glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, indices.data()));
|
|
}
|
|
}
|
|
|