diff --git a/bumpmappedmesh.cpp b/bumpmappedmesh.cpp new file mode 100644 index 0000000..91fa3ec --- /dev/null +++ b/bumpmappedmesh.cpp @@ -0,0 +1,30 @@ +#include "bumpmappedmesh.h" +#include "glassert.h" + +void BumpMappedMesh::addVertex(const SRFVertex& v) +{ + if(!locked) + vertices.push_back(v); +} + +void BumpMappedMesh::setAttribPointer() +{ + glAssert(glBindVertexArray(vao)); + glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER])); + + // position attribute + glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(SRFVertex), BUFFER_OFFSET(0))); + glAssert(glEnableVertexAttribArray(0)); + // normal attribute + glAssert(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(SRFVertex), BUFFER_OFFSET(sizeof(glm::vec3)))); + glAssert(glEnableVertexAttribArray(1)); + // texCoord attribute + glAssert(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(SRFVertex), BUFFER_OFFSET(2*sizeof(glm::vec3)))); + glAssert(glEnableVertexAttribArray(2)); + // normal attribute + glAssert(glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(SRFVertex), BUFFER_OFFSET(2*sizeof(glm::vec3)+sizeof(glm::vec2)))); + glAssert(glEnableVertexAttribArray(3)); + // texCoord attribute + glAssert(glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(SRFVertex), BUFFER_OFFSET(3*sizeof(glm::vec3)+sizeof(glm::vec2)))); + glAssert(glEnableVertexAttribArray(4)); +} diff --git a/bumpmappedmesh.h b/bumpmappedmesh.h new file mode 100644 index 0000000..6f6cde0 --- /dev/null +++ b/bumpmappedmesh.h @@ -0,0 +1,34 @@ +#ifndef BUMPMAPPEDMESH_H +#define BUMPMAPPEDMESH_H + +#include "vertex.h" +#include "mesh.h" + +class BumpMappedMesh : public Mesh +{ +protected: + std::vector vertices; + + virtual int getNbVertices() + { + return vertices.size(); + } + + virtual int getSizeofVertexType() + { + return sizeof(SRFVertex); + } + + virtual void* getVertexData() + { + return (void*)(vertices.data()); + } + + virtual void setAttribPointer(); + +public: + + void addVertex(const SRFVertex& v); +}; + +#endif // BUMPMAPPEDMESH_H diff --git a/entity.cpp b/entity.cpp index 9e8cf3e..5b3b7ae 100644 --- a/entity.cpp +++ b/entity.cpp @@ -2,9 +2,9 @@ #include "shader.h" #include #include "material.h" -#include "mesh.h" +#include "standardmesh.h" -Entity::Entity(Mesh* myMesh, Material* myMat) : mesh(myMesh), mat(myMat) {} +Entity::Entity(StandardMesh* myMesh, Material* myMat) : mesh(myMesh), mat(myMat) {} void Entity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix) { diff --git a/entity.h b/entity.h index 5c580b9..e7acdb5 100644 --- a/entity.h +++ b/entity.h @@ -4,18 +4,18 @@ #include "glm/mat4x4.hpp" #include -class Mesh; +class StandardMesh; class Material; class Shader; class Entity { protected: - Mesh* mesh; + StandardMesh* mesh; Material* mat; glm::mat4 modelMatrix; public: - Entity(Mesh* myMesh, Material* myMat); + Entity(StandardMesh* myMesh, Material* myMat); virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix); glm::mat4* getTransform(); Shader* getShader(); diff --git a/gridmesh.cpp b/gridmesh.cpp index 93700c1..69a9cd5 100644 --- a/gridmesh.cpp +++ b/gridmesh.cpp @@ -1,12 +1,12 @@ #include "gridmesh.h" -GridMesh::GridMesh(int width, int height, bool alternate) : Mesh(), m_width(width), m_height(height) +GridMesh::GridMesh(int width, int height, bool alternate) : StandardMesh(), m_width(width), m_height(height) { for(int i=0; i<=width; ++i) { for(int j=0; j<=height; ++j) { - Vertex v; + TexturedVertex v; v.position = glm::vec3((float)i/(float)width, (float)j/(float)height, 0); v.normal = glm::vec3(0, 0, 1); v.texCoord = glm::vec2(v.position.x, v.position.y); diff --git a/gridmesh.h b/gridmesh.h index ff68368..b24ff5d 100644 --- a/gridmesh.h +++ b/gridmesh.h @@ -1,9 +1,9 @@ #ifndef GRIDMESH_H #define GRIDMESH_H -#include "mesh.h" +#include "standardmesh.h" -class GridMesh : public Mesh +class GridMesh : public StandardMesh { public: GridMesh(int width, int height, bool alternate); diff --git a/mesh.cpp b/mesh.cpp index ab83e07..4779f9a 100644 --- a/mesh.cpp +++ b/mesh.cpp @@ -1,4 +1,5 @@ #include "mesh.h" + #include "glassert.h" #define BUFFER_OFFSET(i) ((char *)NULL + (i)) @@ -8,12 +9,6 @@ Mesh::~Mesh() destroyGL(); } -void Mesh::addVertex(const Vertex& v) -{ - if(!locked) - vertices.push_back(v); -} - void Mesh::addFace(int i1, int i2, int i3) { if(!locked) @@ -36,19 +31,13 @@ void Mesh::initGL() // create VBOs glAssert(glGenBuffers(NB_BUFFERS, vbo)); + int size = getSizeofVertexType(); + // init vertex vbo glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER])); - glAssert(glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW)); + glAssert(glBufferData(GL_ARRAY_BUFFER, getNbVertices() * size, getVertexData(), GL_STATIC_DRAW)); - // position attribute - glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0))); - glAssert(glEnableVertexAttribArray(0)); - // normal attribute - glAssert(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(glm::vec3)))); - glAssert(glEnableVertexAttribArray(1)); - // texCoord attribute - glAssert(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(2*sizeof(glm::vec3)))); - glAssert(glEnableVertexAttribArray(2)); + setAttribPointer(); // init indices vbo glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[INDICES_BUFFER])); diff --git a/mesh.h b/mesh.h index 8da70d3..7b49023 100644 --- a/mesh.h +++ b/mesh.h @@ -3,34 +3,30 @@ #include #include -#include -class Mesh -{ +#define BUFFER_OFFSET(i) ((char *)NULL + (i)) + +class Mesh{ +protected: + enum {VERTEX_BUFFER, INDICES_BUFFER, NB_BUFFERS}; + + std::vector indices; + GLuint vao; + GLuint vbo[2]; + bool locked; + + virtual int getNbVertices() = 0; + virtual int getSizeofVertexType() = 0; + virtual void* getVertexData() = 0; + virtual void setAttribPointer() = 0; + public: - typedef struct - { - glm::vec3 position; - glm::vec3 normal; - glm::vec2 texCoord; - } Vertex; - ~Mesh(); - void addVertex(const Vertex& v); void addFace(int i1, int i2, int i3); void initGL(); void destroyGL(); void draw(); bool isLocked(){return locked;} - -protected: - enum {VERTEX_BUFFER, INDICES_BUFFER, NB_BUFFERS}; - - std::vector vertices; - std::vector indices; - GLuint vao; - GLuint vbo[2]; - bool locked; }; #endif // MESH_H diff --git a/phongmodule.h b/phongmodule.h index eecf6e9..c16d37d 100644 --- a/phongmodule.h +++ b/phongmodule.h @@ -4,7 +4,7 @@ #include #include "basicmodule.h" #include "phongmaterial.h" -#include "mesh.h" +#include "standardmesh.h" class Lights; diff --git a/resourcebase.cpp b/resourcebase.cpp index e409251..89539df 100644 --- a/resourcebase.cpp +++ b/resourcebase.cpp @@ -1,7 +1,7 @@ #include "resourcebase.h" ResourceBase::DataBase ResourceBase::textures; -ResourceBase::DataBase ResourceBase::meshes; +ResourceBase::DataBase ResourceBase::meshes; ResourceBase::DataBase ResourceBase::materials; ResourceBase::DataBase ResourceBase::shaders; ResourceBase::DataBase ResourceBase::entities; @@ -12,7 +12,7 @@ void ResourceBase::setTexture(const std::string &textureName, Texture* myTexture textures.add(textureName, myTexture); } -void ResourceBase::setMesh(const std::string &meshName, Mesh* myMesh ) +void ResourceBase::setMesh(const std::string &meshName, StandardMesh* myMesh ) { meshes.add(meshName, myMesh); } @@ -43,7 +43,7 @@ Texture* ResourceBase::getTexture(const std::string &textureName) return textures.get(textureName); } -Mesh* ResourceBase::getMesh(const std::string &meshName) +StandardMesh* ResourceBase::getMesh(const std::string &meshName) { return meshes.get(meshName); } diff --git a/resourcebase.h b/resourcebase.h index c1e2982..bcc6ab5 100644 --- a/resourcebase.h +++ b/resourcebase.h @@ -2,7 +2,7 @@ #define RESOURCEBASE_H class Texture; -class Mesh; +class StandardMesh; class Material; class Shader; class Entity; @@ -16,14 +16,14 @@ class ResourceBase { public: static void setTexture(const std::string &textureName, Texture* myTexture); - static void setMesh(const std::string &meshName, Mesh* myMesh); + static void setMesh(const std::string &meshName, StandardMesh* myMesh); static void setMaterial(const std::string &materialName, Material* myMaterial); static void setShader(const std::string &shaderName, Shader* myShader); static void setEntity(const std::string &entityName, Entity* myEntity); static void setLights(const std::string &lightsName, Lights* myLights); static Texture* getTexture(const std::string &textureName); - static Mesh* getMesh(const std::string &meshName); + static StandardMesh* getMesh(const std::string &meshName); static Material* getMaterial(const std::string &materialName); static Shader* getShader(const std::string &shaderName); static Entity* getEntity(const std::string &entityName); @@ -53,7 +53,7 @@ protected: }; static DataBase textures; - static DataBase meshes; + static DataBase meshes; static DataBase materials; static DataBase shaders; static DataBase entities; diff --git a/sparrowRenderer.pro b/sparrowRenderer.pro index 5fb8828..a15f103 100644 --- a/sparrowRenderer.pro +++ b/sparrowRenderer.pro @@ -25,7 +25,6 @@ unix { } SOURCES += shader.cpp \ - mesh.cpp \ camera.cpp \ gridmesh.cpp \ texture.cpp \ @@ -41,10 +40,12 @@ SOURCES += shader.cpp \ phongmodule.cpp \ skyboxmodule.cpp \ basicmodule.cpp \ - framebuffer.cpp + framebuffer.cpp \ + standardmesh.cpp \ + mesh.cpp \ + bumpmappedmesh.cpp HEADERS += shader.h \ - mesh.h \ camera.h \ glassert.h \ material.h \ @@ -64,7 +65,11 @@ HEADERS += shader.h \ basicmodule.h \ module.h \ resource.h \ - framebuffer.h + framebuffer.h \ + standardmesh.h \ + mesh.h \ + bumpmappedmesh.h \ + vertex.h OTHER_FILES += *.frag *.vert *.glsl *.todo diff --git a/sparrowrenderer.cpp b/sparrowrenderer.cpp index 2b141e2..8d5938a 100644 --- a/sparrowrenderer.cpp +++ b/sparrowrenderer.cpp @@ -62,101 +62,11 @@ void SparrowRenderer::addModule(Module* myModule, std::string name) modules.push_back(ModuleNode(myModule, name)); } -void SparrowRenderer::addModule(Module* myModule, std::string name, int index) -{ - if(index < 0) - index = 0; - if(index > modules.size()) - modules.push_back(ModuleNode(myModule, name)); - else - { - auto it = modules.begin(); - std::advance(it, index); - modules.insert(it, ModuleNode(myModule, name)); - } -} - -void SparrowRenderer::removeModule(int index) -{ - auto it = modules.begin(); - std::advance(it, index); - modules.erase(it); -} - -void SparrowRenderer::removeModule(std::string name) -{ - modules.erase(getModuleNode(name)); -} - -Module* SparrowRenderer::getModule(int index) -{ - auto it = modules.begin(); - std::advance(it, index); - return (*it).module; -} - -Module* SparrowRenderer::getModule(std::string name) -{ - return (*getModuleNode(name)).module; -} - -std::list::iterator SparrowRenderer::getModuleNode(std::string name) -{ - for(auto iterator = modules.begin(); iterator != modules.end(); ++iterator) - { - if(iterator->name == name) - return iterator; - } -} - int SparrowRenderer::getNbModules() { return modules.size(); } -std::string SparrowRenderer::getModuleName(int index) -{ - auto it = modules.begin(); - std::advance(it, index); - return (*it).name; -} - -void SparrowRenderer::enableModule(std::string name) -{ - (*getModuleNode(name)).isEnabled = true; -} - -void SparrowRenderer::enableModule(int index) -{ - auto it = modules.begin(); - std::advance(it, index); - (*it).isEnabled = true; -} - -void SparrowRenderer::disableModule(std::string name) -{ - (*getModuleNode(name)).isEnabled = false; -} - -void SparrowRenderer::disableModule(int index) -{ - auto it = modules.begin(); - std::advance(it, index); - (*it).isEnabled = false; -} - -bool SparrowRenderer::isModuleEnabled(std::string name) -{ - return (*getModuleNode(name)).isEnabled; -} - -bool SparrowRenderer::isModuleEnabled(int index) -{ - auto it = modules.begin(); - std::advance(it, index); - return (*it).isEnabled; -} - // camera methods Camera* SparrowRenderer::getCamera() diff --git a/sparrowrenderer.h b/sparrowrenderer.h index 66de07a..bc19f60 100644 --- a/sparrowrenderer.h +++ b/sparrowrenderer.h @@ -17,20 +17,8 @@ public: void render(); // modules methods - void addModule(Module* myModule, std::string name); - void addModule(Module* myModule, std::string name, int index); - void removeModule(std::string name); - void removeModule(int index); - Module* getModule(std::string name); - Module* getModule(int index); - int getNbModules(); - std::string getModuleName(int index); - void enableModule(std::string name); - void enableModule(int index); - void disableModule(std::string name); - void disableModule(int index); - bool isModuleEnabled(std::string name); - bool isModuleEnabled(int index); + void addModule(Module* myModule, std::string name); + int getNbModules(); // camera methods Camera* getCamera(); diff --git a/sphere.cpp b/sphere.cpp index 20dca52..fb13ab0 100644 --- a/sphere.cpp +++ b/sphere.cpp @@ -1,4 +1,5 @@ #include "sphere.h" +#include "glm/ext.hpp" #define M_PI 3.14159265358979323846 #define MAGIC_RATIO 0.37139f @@ -66,9 +67,9 @@ int Sphere::getEdge(int a, int b) else if(current->next == NULL) { // creating subdivision vertex - Vertex v; - Vertex v0 = vertices[a]; - Vertex v1 = vertices[b]; + TexturedVertex v; + TexturedVertex v0 = vertices[a]; + TexturedVertex v1 = vertices[b]; v.position = glm::normalize((v0.position + v1.position)/2.f); v.normal = v.position; @@ -98,7 +99,7 @@ int Sphere::getEdge(int a, int b) void Sphere::createVertex(float u, float v) { - Vertex vert; + TexturedVertex vert; vert.position = glm::vec3(cos(u*2*M_PI)*sin(v*M_PI), cos(v*M_PI), sin(u*2*M_PI)*sin(v*M_PI)); diff --git a/sphere.h b/sphere.h index 8d4f5f6..238cbba 100644 --- a/sphere.h +++ b/sphere.h @@ -1,9 +1,9 @@ #ifndef SPHERE_H #define SPHERE_H -#include "mesh.h" +#include "standardmesh.h" -class Sphere : public Mesh +class Sphere : public StandardMesh { private: class Edge{ diff --git a/standardmesh.cpp b/standardmesh.cpp new file mode 100644 index 0000000..48f2a24 --- /dev/null +++ b/standardmesh.cpp @@ -0,0 +1,24 @@ +#include "standardmesh.h" +#include "glassert.h" + +void StandardMesh::addVertex(const TexturedVertex& v) +{ + if(!locked) + vertices.push_back(v); +} + +void StandardMesh::setAttribPointer() +{ + glAssert(glBindVertexArray(vao)); + glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER])); + + // position attribute + glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), BUFFER_OFFSET(0))); + glAssert(glEnableVertexAttribArray(0)); + // normal attribute + glAssert(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), BUFFER_OFFSET(sizeof(glm::vec3)))); + glAssert(glEnableVertexAttribArray(1)); + // texCoord attribute + glAssert(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), BUFFER_OFFSET(2*sizeof(glm::vec3)))); + glAssert(glEnableVertexAttribArray(2)); +} diff --git a/standardmesh.h b/standardmesh.h new file mode 100644 index 0000000..4ac7998 --- /dev/null +++ b/standardmesh.h @@ -0,0 +1,34 @@ +#ifndef STANDARDMESH_H +#define STANDARDMESH_H + +#include "vertex.h" +#include "mesh.h" + +class StandardMesh : public Mesh{ +protected: + std::vector vertices; + + virtual int getNbVertices() + { + return vertices.size(); + } + + virtual int getSizeofVertexType() + { + return sizeof(TexturedVertex); + } + + virtual void* getVertexData() + { + return (void*)(vertices.data()); + } + + virtual void setAttribPointer(); + +public: + + void addVertex(const TexturedVertex& v); + +}; + +#endif // STANDARDMESH_H diff --git a/utils.cpp b/utils.cpp index 210b780..a4fed29 100644 --- a/utils.cpp +++ b/utils.cpp @@ -1,5 +1,5 @@ #include "utils.h" -#include "mesh.h" +#include "standardmesh.h" #include #include #include @@ -90,16 +90,16 @@ void Utils::loadMTL(const std::string &filename) } -Mesh* Utils::loadOBJ(const std::string &filename) +StandardMesh* Utils::loadOBJ(const std::string &filename) { - Mesh* mesh = new Mesh(); + StandardMesh* mesh = new StandardMesh(); std::vector pos; std::vector norm; std::vector tex; QString line; QStringList list; QStringList faceList; - Mesh::Vertex v; + TexturedVertex v; int nb_vertices = 0; QFile f(QString(filename.c_str())); if(!f.open(QFile::ReadOnly | QFile::Text)) diff --git a/utils.h b/utils.h index 4a27b78..0bee9e0 100644 --- a/utils.h +++ b/utils.h @@ -3,13 +3,13 @@ #include class QImage; -class Mesh; +class StandardMesh; class Utils { public: static std::string fileToString(const std::string &filename); - static Mesh* loadOBJ(const std::string &filename); + static StandardMesh* loadOBJ(const std::string &filename); //static void loadOBJ(const std::string &filename); static void loadMTL(const std::string &filename); class Image diff --git a/vertex.h b/vertex.h new file mode 100644 index 0000000..b63fd7a --- /dev/null +++ b/vertex.h @@ -0,0 +1,29 @@ +#ifndef VERTEX_H +#define VERTEX_H + +#include "glm/vec3.hpp" +#include "glm/vec2.hpp" + +struct Vertex +{ + glm::vec3 position; +}; + +struct BasicVertex : public Vertex +{ + glm::vec3 normal; +}; + +struct TexturedVertex : public BasicVertex +{ + glm::vec2 texCoord; +}; + +struct SRFVertex : public TexturedVertex +{ + glm::vec3 tangent; + glm::vec3 bitangent; +}; + +#endif // VERTEX_H +