added imesh, a unique mesh class that will replace all others

This commit is contained in:
Anselme 2015-07-17 16:38:06 +02:00
parent 92c40cff48
commit c20ad01d73
7 changed files with 210 additions and 6 deletions

View File

@ -8,8 +8,8 @@ class GridMesh : public StandardMesh
public:
GridMesh(int width, int height, bool alternate);
private:
int m_height;
int m_width;
int m_height;
int getVertexId(int i, int j);
};

139
imesh.cpp Normal file
View File

@ -0,0 +1,139 @@
#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()));
}
}

59
imesh.h Normal file
View File

@ -0,0 +1,59 @@
#ifndef IMESH_H
#define IMESH_H
#include <glew/glew.h>
#include <vector>
#include <glm/vec3.hpp>
#include <glm/vec2.hpp>
class IMesh
{
protected:
enum {POSITION_BUFFER, NORMAL_BUFFER, TEXCOORD_BUFFER, TANGENT_BUFFER, INDICES_BUFFER, NB_BUFFERS};
typedef struct
{
glm::vec3 tangent;
glm::vec3 bitangent;
} Tangents;
std::vector<glm::vec3> positions;
std::vector<glm::vec3> normals;
std::vector<glm::vec2> texCoords;
std::vector<Tangents> tangents;
std::vector<unsigned int> indices;
GLuint vao;
GLuint vbo[NB_BUFFERS];
bool locked;
public:
~IMesh();
void addTriangle(int i1, int i2, int i3);
void addVertex(float x, float y, float z);
void addVertex(glm::vec3 &position);
void addVertex(glm::vec3 &position, glm::vec3 &normal);
void addVertex(glm::vec3 &position, glm::vec2 &texCoord);
void addVertex(glm::vec3 &position, glm::vec3 &normal, glm::vec2 &texCoord);
void computeFaceNormals();
void computeVertexNormals();
void computeTangents();
bool hasNormals();
bool hasTexCoords();
bool hasTangents();
/*
* When the mesh is locked, addFace and addVertex do nothing, but the mesh can be drawn
* initGL locks the mesh
* destroyGL unlocks the mesh
*/
void initGL(bool isDynamic = false);
void destroyGL();
void draw();
bool isLocked(){return locked;}
};
#endif // IMESH_H

View File

@ -1,8 +1,8 @@
#include "phongmodule.h"
#include "lights.h"
PhongModule::PhongModule(Lights* myDirLights, Lights* myPointLights, Shader* phongShader) :
BasicModule(phongShader), dirLights(myDirLights), pointLights(myPointLights)
PhongModule::PhongModule(Lights* myDirLights, Lights* myPointLights) :
BasicModule(ResourceBase::getShader("phong")), dirLights(myDirLights), pointLights(myPointLights)
{
nbDirLightsLocation = shader->getLocation("nbDirLights");
dirLightsLocation = shader->getLocation("dirLights");

View File

@ -17,7 +17,7 @@ class PhongModule : public BasicModule
GLuint nbPointLightsLocation;
GLuint pointLightsLocation;
public:
PhongModule(Lights* myDirLights, Lights* myPointLights, Shader* phongShader);
PhongModule(Lights* myDirLights, Lights* myPointLights);
virtual void bindModule();
};

View File

@ -11,6 +11,7 @@ class Lights;
#include <unordered_map>
#include <string>
#include <vector>
#include <stdio.h>
class ResourceBase
{
@ -48,7 +49,10 @@ protected:
if(data.count(name))
return data[name];
else
{
fprintf(stderr, "Requesting unloaded resource : %s\n", name.c_str());
return NULL;
}
}
};

View File

@ -43,7 +43,8 @@ SOURCES += shader.cpp \
framebuffer.cpp \
standardmesh.cpp \
mesh.cpp \
bumpmappedmesh.cpp
bumpmappedmesh.cpp \
imesh.cpp
HEADERS += shader.h \
camera.h \
@ -69,7 +70,8 @@ HEADERS += shader.h \
standardmesh.h \
mesh.h \
bumpmappedmesh.h \
vertex.h
vertex.h \
imesh.h
OTHER_FILES += *.frag *.vert *.glsl *.todo