From a4843e277ade4ab83a35c033b3698b60cb4debe8 Mon Sep 17 00:00:00 2001 From: Anselme Date: Tue, 5 Sep 2017 20:34:13 +0200 Subject: [PATCH] lights and meshes are now almost fully serializables --- src/light.cpp | 16 +++++------- src/light.h | 36 +++++++++++++++++++------- src/mesh.cpp | 72 +++++++++++++++++++++++++-------------------------- src/mesh.h | 53 +++++++++++++++++++++---------------- 4 files changed, 99 insertions(+), 78 deletions(-) diff --git a/src/light.cpp b/src/light.cpp index 77453ac..7e1a3d0 100644 --- a/src/light.cpp +++ b/src/light.cpp @@ -59,11 +59,10 @@ void AmbientLight::bindAttributes(Shader *shader, Camera *camera) int DirectionnalLight::m_shaderRefCounter = 0; Shader* DirectionnalLight::m_shaders[4] = {NULL}; -DirectionnalLight::DirectionnalLight(glm::vec3 dir, glm::vec3 lightColor) : - m_direction(dir), - m_shadowCaster(false) - +DirectionnalLight::DirectionnalLight(glm::vec3 dir, glm::vec3 lightColor) { + m_direction = dir; + m_shadowCaster = false; m_color = lightColor; m_shadowMap = NULL; } @@ -252,12 +251,11 @@ void DirectionnalLight::updateShadowMap(Scene* scene) int PointLight::m_shaderRefCounter = 0; Shader* PointLight::m_shaders[2] = {NULL}; -PointLight::PointLight(glm::vec3 pos, float range, glm::vec3 lightColor) : - m_position(pos), - m_range(range), - m_shadowCaster(false) - +PointLight::PointLight(glm::vec3 pos, float range, glm::vec3 lightColor) { + m_position = pos; + m_range = range; + m_shadowCaster = false; m_color = lightColor; m_shadowMap = NULL; } diff --git a/src/light.h b/src/light.h index 0b46be6..6911293 100644 --- a/src/light.h +++ b/src/light.h @@ -5,6 +5,7 @@ #include #include #include +#include class FrameBuffer; class Shader; @@ -12,7 +13,7 @@ class Scene; class Texture; class Camera; -class Light +class Light : public Serializable { public: enum LightType { @@ -47,7 +48,7 @@ public: virtual unsigned int getFlags() = 0; protected: - glm::vec3 m_color; + P_VEC3(m_color) }; class AmbientLight : public Light @@ -56,6 +57,8 @@ class AmbientLight : public Light Texture* reflect; static Texture* brdfLUT; public: + SERIALIZABLE(AmbientLight, CAST(m_color)) + AmbientLight(Texture* ambientTex = nullptr, Texture* reflectTex = nullptr) : ambient(ambientTex), reflect(reflectTex) {} virtual LightType getType() { return AMBIENT; } virtual void bindAttributes(Shader *shader, Camera *camera); @@ -70,6 +73,13 @@ public: class DirectionnalLight : public Light { public: + SERIALIZABLE(DirectionnalLight, + CAST(m_color), + CAST(m_direction), + CAST(m_center), + CAST(m_shadowCaster), + CAST(m_shadowMapResolution)) + DirectionnalLight(glm::vec3 dir = glm::vec3(0, -1, 0), glm::vec3 lightColor = glm::vec3(1)); void setDir(const glm::vec3 &dir) { m_direction = dir; } @@ -87,12 +97,12 @@ public: void updateShadowMap(Scene* scene); private: - glm::vec3 m_direction; + P_VEC3(m_direction) - glm::vec3 m_center; + P_VEC3(m_center) - bool m_shadowCaster; - int m_shadowMapResolution; + P_BOOL(m_shadowCaster) + P_INT(m_shadowMapResolution) FrameBuffer* m_shadowMap; std::vector m_viewMatrices; std::vector m_projectionMatrices; @@ -104,6 +114,12 @@ private: class PointLight : public Light { public: + SERIALIZABLE(PointLight, + CAST(m_color), + CAST(m_position), + CAST(m_range), + CAST(m_shadowCaster), + CAST(m_shadowMapResolution)) PointLight(glm::vec3 pos = glm::vec3(0), float attenuation = 1, glm::vec3 lightColor = glm::vec3(1)); glm::vec3 getPos() { return m_position; } @@ -125,11 +141,11 @@ public: private: void updateTransform(); - glm::vec3 m_position; - float m_range; + P_VEC3(m_position) + P_FLOAT(m_range) - bool m_shadowCaster; - int m_shadowMapResolution; + P_BOOL(m_shadowCaster) + P_INT(m_shadowMapResolution) FrameBuffer* m_shadowMap; std::vector m_shadowTransforms; diff --git a/src/mesh.cpp b/src/mesh.cpp index 64381f6..505d427 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -36,18 +36,18 @@ const char* const Mesh::flagStr[Mesh::NB_FLAGS] = "BUMP_MAP" }; -Mesh::Mesh() : - m_name("mesh"), - material(NULL), - isDoubleSided(false), - isBillboard(false), - isWireframe(false), - isShadowCaster(true), - depth(0), - m_flags(0), - primitive_type(GL_TRIANGLES), - vao(0) +Mesh::Mesh() { + m_name = "mesh"; + m_material = NULL; + m_isDoubleSided = false; + m_isBillboard = false; + m_isWireframe = false; + m_isShadowCaster = true; + m_depth = 0; + m_flags = 0; + m_primitive_type = GL_TRIANGLES; + m_vao = 0; clearBuffers(); } @@ -94,19 +94,19 @@ unsigned int Mesh::updateFlags() if(!m_tangents.empty()) m_flags |= 1 << MESH_TANGENT_SPACE; - if(isDoubleSided) + if(m_isDoubleSided) m_flags |= 1 << MESH_DOUBLE_SIDED; - if(isBillboard) + if(m_isBillboard) m_flags |= 1 << MESH_BILLBOARD; - if(isWireframe) + if(m_isWireframe) m_flags |= 1 << MESH_WIREFRAME; - if(isShadowCaster) + if(m_isShadowCaster) m_flags |= 1 << MESH_SHADOWED; } else m_flags |= 1 << MESH_2D; - return m_flags | (material ? material->getFlags() : 0); + return m_flags | (m_material ? m_material->getFlags() : 0); } void Mesh::initGL() @@ -115,8 +115,8 @@ void Mesh::initGL() destroyGL(); // create VAO - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); + glGenVertexArrays(1, &m_vao); + glBindVertexArray(m_vao); // init positions VBO if(!m_positions3D.empty()) @@ -181,26 +181,26 @@ void Mesh::initGL() void Mesh::draw(Shader* shader) { - if(isWireframe) + if(m_isWireframe) glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO); else glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); - if(isDoubleSided) + if(m_isDoubleSided) glDisable(GL_CULL_FACE); - if(material != NULL) - material->bindAttributes(shader); - glBindVertexArray(vao); + if(m_material != NULL) + m_material->bindAttributes(shader); + glBindVertexArray(m_vao); if(m_indices.empty()) { int size = m_positions3D.empty() ? m_positions2D.size() : m_positions3D.size(); if(!m_instances_offsets.empty()) { - glDrawArraysInstanced(primitive_type, 0, size, m_instances_offsets.size()); + glDrawArraysInstanced(m_primitive_type, 0, size, m_instances_offsets.size()); } else { - glDrawArrays(primitive_type, 0, size); + glDrawArrays(m_primitive_type, 0, size); } } else @@ -209,26 +209,26 @@ void Mesh::draw(Shader* shader) b->bind(); if(!m_instances_offsets.empty()) { - glDrawElementsInstanced(primitive_type, m_indices.size(), GL_UNSIGNED_INT, NULL, m_instances_offsets.size()); + glDrawElementsInstanced(m_primitive_type, m_indices.size(), GL_UNSIGNED_INT, NULL, m_instances_offsets.size()); } else { - glDrawElements(primitive_type, m_indices.size(), GL_UNSIGNED_INT, NULL); + glDrawElements(m_primitive_type, m_indices.size(), GL_UNSIGNED_INT, NULL); } b->unbind(); } glBindVertexArray(0); - if(isDoubleSided) + if(m_isDoubleSided) glEnable(GL_CULL_FACE); } void Mesh::destroyGL() { - if(vao != 0) + if(m_vao != 0) { - glDeleteVertexArrays(1, &vao); - vao = 0; + glDeleteVertexArrays(1, &m_vao); + m_vao = 0; } clearBuffers(); } @@ -246,7 +246,7 @@ void Mesh::clearData() unsigned int Mesh::getFlags() { - return m_flags | material->getFlags(); + return m_flags | m_material->getFlags(); } void Mesh::addRectangle2D(const glm::vec2 &pos, const glm::vec2 &dim, const glm::vec2 &texCoord, const glm::vec2 &texRange, bool indexed) @@ -275,7 +275,7 @@ void Mesh::addRectangle2D(const glm::vec2 &pos, const glm::vec2 &dim, const glm: void Mesh::setIsDoubleSided(bool val) { - isDoubleSided = val; + m_isDoubleSided = val; if(val) m_flags |= 1 << MESH_DOUBLE_SIDED; else @@ -284,7 +284,7 @@ void Mesh::setIsDoubleSided(bool val) void Mesh::setIsBillboard(bool val) { - isBillboard = val; + m_isBillboard = val; if(val) m_flags |= 1 << MESH_BILLBOARD; else @@ -293,7 +293,7 @@ void Mesh::setIsBillboard(bool val) void Mesh::setWireframe(bool val) { - isWireframe = val; + m_isWireframe = val; if(val) m_flags |= 1 << MESH_WIREFRAME; else @@ -302,7 +302,7 @@ void Mesh::setWireframe(bool val) void Mesh::setIsShadowCaster(bool val) { - isShadowCaster = val; + m_isShadowCaster = val; if(val) m_flags |= 1 << MESH_SHADOWED; else diff --git a/src/mesh.h b/src/mesh.h index f274257..be2d85f 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -89,12 +89,19 @@ public: /*************************************************************/ SERIALIZABLE(Mesh, - CAST(m_positions3D), - CAST(m_normals), - CAST(m_positions2D), - CAST(m_texCoords), - CAST(m_instances_offsets), - CAST(m_indices)) + CAST(m_positions3D), + CAST(m_normals), + CAST(m_positions2D), + CAST(m_texCoords), + CAST(m_instances_offsets), + CAST(m_indices), + CAST(m_name), + CAST(m_isDoubleSided), + CAST(m_isBillboard), + CAST(m_isWireframe), + CAST(m_isShadowCaster), + CAST(m_depth), + CAST(m_primitive_type)) /** * @brief Mesh builds an empty mesh, to be renderable, a mesh must have vertices and a material. @@ -149,8 +156,8 @@ public: void addLine(int i1, int i2) {m_indices.push_back(i1), m_indices.push_back(i2);} // Material accessers - void setMaterial(Material* mat) {material = mat;} - Material *getMaterial() {return material;} + void setMaterial(Material* mat) {m_material = mat;} + Material *getMaterial() {return m_material;} // other accessers void addNormal(float x, float y, float z) {addNormal(glm::vec3(x, y, z));} @@ -165,8 +172,8 @@ public: /** * @brief setDepth allows to set the depth of a 2D mesh, the depth must be between -1 and 1, -1 being the closest to the camera */ - void setDepth(float d) { depth = d; } - float getDepth() const { return depth; } + void setDepth(float d) { m_depth = d; } + float getDepth() const { return m_depth; } /*************************************************************/ /* 3D MESH PROPERTIES */ @@ -236,7 +243,7 @@ public: * * default is GL_TRIANGLES */ - void setPrimitiveType(GLenum type) {primitive_type = type;} + void setPrimitiveType(GLenum type) {m_primitive_type = type;} /** * @brief updateFlags forces the refresh of the mesh flags, @@ -246,19 +253,19 @@ public: unsigned int updateFlags(); protected: - std::string m_name; // yup + P_STRING(m_name) - Material* material; // nop - bool isDoubleSided; // yup - bool isBillboard; // yup - bool isWireframe; // yup - bool isShadowCaster; // yup - float depth; // yup - unsigned int m_flags; // nop GL + Material* m_material; + P_BOOL(m_isDoubleSided) + P_BOOL(m_isBillboard) + P_BOOL(m_isWireframe) + P_BOOL(m_isShadowCaster) + P_FLOAT(m_depth) + unsigned int m_flags; - GLenum primitive_type; // yup + P_UINT(m_primitive_type) - std::vector buffers; // nop GL + std::vector buffers; enum { // required buffer @@ -279,12 +286,12 @@ protected: NB_BUFFERS }; - int buffersId[NB_BUFFERS]; // nop GL + int buffersId[NB_BUFFERS]; void addBuffer(Buffer *b, int bufferType); void clearBuffers(); - GLuint vao; // nop GL + GLuint m_vao; }; #endif // MESH_H