diff --git a/gridmesh.h b/gridmesh.h index b24ff5d..f9542fe 100644 --- a/gridmesh.h +++ b/gridmesh.h @@ -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); }; diff --git a/imesh.cpp b/imesh.cpp new file mode 100644 index 0000000..6314d68 --- /dev/null +++ b/imesh.cpp @@ -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())); + } +} + diff --git a/imesh.h b/imesh.h new file mode 100644 index 0000000..530d539 --- /dev/null +++ b/imesh.h @@ -0,0 +1,59 @@ +#ifndef IMESH_H +#define IMESH_H + +#include +#include +#include +#include + +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 positions; + std::vector normals; + std::vector texCoords; + std::vector tangents; + std::vector 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 diff --git a/phongmodule.cpp b/phongmodule.cpp index 564090a..6bc73c3 100644 --- a/phongmodule.cpp +++ b/phongmodule.cpp @@ -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"); diff --git a/phongmodule.h b/phongmodule.h index c16d37d..2081867 100644 --- a/phongmodule.h +++ b/phongmodule.h @@ -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(); }; diff --git a/resourcebase.h b/resourcebase.h index bcc6ab5..a8c1c62 100644 --- a/resourcebase.h +++ b/resourcebase.h @@ -11,6 +11,7 @@ class Lights; #include #include #include +#include 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; + } } }; diff --git a/sparrowRenderer.pro b/sparrowRenderer.pro index a15f103..71c6da9 100644 --- a/sparrowRenderer.pro +++ b/sparrowRenderer.pro @@ -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