From 57c6d122a8779c69ac87323d1f86adf3ab8c18df Mon Sep 17 00:00:00 2001 From: Anselme Date: Mon, 16 Nov 2015 13:27:50 +0100 Subject: [PATCH 1/6] fixed error messages showing in release mode --- CMakeLists.txt | 6 +++++- glassert.h | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f320646..bd71da6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,8 +38,12 @@ set(LIB_ROOT ${DEPENDENCIES_ROOT}/lib/${SYSTEM_LIB_PATH}) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIB_ROOT}) #for SHARED set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIB_ROOT}) #for STATIC +if(CMAKE_BUILD_TYPE MATCHES "Debug") + set(CPP_DEFINES -DRENDER_DEBUG) +endif() + add_library(${LIBRARY_NAME} STATIC ${LIB_SRC_LIST}) -add_definitions(-std=c++11) +add_definitions(-std=c++11 ${CPP_DEFINES}) include_directories( ${INCLUDE_ROOT} diff --git a/glassert.h b/glassert.h index 3153cca..4be8ad0 100644 --- a/glassert.h +++ b/glassert.h @@ -10,6 +10,7 @@ #include #include +#ifdef RENDER_DEBUG #define STR(x) #x #define glAssert(code) \ code; \ @@ -19,6 +20,9 @@ std::cerr<<"Erreur OpenGL ("<<__FILE__<<":"<<__LINE__<<", "< Date: Wed, 18 Nov 2015 09:37:24 +0100 Subject: [PATCH 2/6] fixed clone bug --- phongentity.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/phongentity.cpp b/phongentity.cpp index a52af2c..51196b8 100644 --- a/phongentity.cpp +++ b/phongentity.cpp @@ -193,4 +193,5 @@ PhongEntity* PhongEntity::clone() myClone->nb_buffers = nb_buffers; myClone->vao = vao; myClone->vbo = vbo; + return myClone; } From 8f0d9d5fc7e6f291e7e45d804a7d1370be69822a Mon Sep 17 00:00:00 2001 From: Anselme Date: Wed, 18 Nov 2015 22:36:03 +0100 Subject: [PATCH 3/6] added normal computation from positions and faces --- meshbuilder.cpp | 19 +++++++++++++++++++ meshbuilder.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/meshbuilder.cpp b/meshbuilder.cpp index ba8b53b..0640898 100644 --- a/meshbuilder.cpp +++ b/meshbuilder.cpp @@ -1,4 +1,5 @@ #include "meshbuilder.h" +#include void MeshBuilder::addPosition(float x, float y, float z) { @@ -80,6 +81,24 @@ int MeshBuilder::getNbGroups() return indiceGroups.size(); } +void MeshBuilder::computeNormals() +{ + normals.resize(positions.size()); + for(const Group &g : indiceGroups) + for (int i=0; i < g.indices.size(); i += 3) + { + int v0 = g.indices[i]; + int v1 = g.indices[i+1]; + int v2 = g.indices[i+2]; + glm::vec3 n = glm::cross(positions[v1] - positions[v0], positions[v2] - positions[v0]); + normals[v0] += n; + normals[v1] += n; + normals[v2] += n; + } + for(glm::vec3 &n : normals) + n = glm::normalize(n); +} + void MeshBuilder::computeTangents() { if(!hasTexCoords()) diff --git a/meshbuilder.h b/meshbuilder.h index 0f9b978..90334fe 100644 --- a/meshbuilder.h +++ b/meshbuilder.h @@ -26,6 +26,8 @@ public: void setCurrentGroupMaterial(Material* myMaterial); int getNbGroups(); + // require positions and indices + void computeNormals(); // require normals and texCoord void computeTangents(); }; From 4aaf417cde6b9f7dd459aef1b9be6b6ce2b52af4 Mon Sep 17 00:00:00 2001 From: Anselme Date: Sun, 22 Nov 2015 21:07:33 +0100 Subject: [PATCH 4/6] added gbuffer class and shader --- CMakeLists.txt | 11 +++++--- deferredmodule.cpp | 1 + deferredmodule.h | 34 +++++++++++++++++++++++++ entityloader.cpp | 7 ------ entityloader.h | 10 -------- gbuffer.cpp | 56 +++++++++++++++++++++++++++++++++++++++++ gbuffer.h | 26 +++++++++++++++++++ gbuffermodule.cpp | 13 ---------- gbuffermodule.h | 23 ----------------- shadersource.cpp | 29 ++++++++++++++++++++++ shadersource.h | 29 ++++++++++++++++++++++ togbuffer.frag | 62 ++++++++++++++++++++++++++++++++++++++++++++++ togbuffer.vert | 41 ++++++++++++++++++++++++++++++ 13 files changed, 285 insertions(+), 57 deletions(-) create mode 100644 deferredmodule.cpp create mode 100644 deferredmodule.h delete mode 100644 entityloader.cpp delete mode 100644 entityloader.h create mode 100644 gbuffer.cpp create mode 100644 gbuffer.h delete mode 100644 gbuffermodule.cpp delete mode 100644 gbuffermodule.h create mode 100644 shadersource.cpp create mode 100644 shadersource.h create mode 100644 togbuffer.frag create mode 100644 togbuffer.vert diff --git a/CMakeLists.txt b/CMakeLists.txt index bd71da6..73c93fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ set(LIB_SRC_LIST asciientity.cpp asciimodule.cpp framebuffer.cpp - gbuffermodule.cpp + gbuffer.cpp lights.cpp meshbuilder.cpp phongentity.cpp @@ -25,8 +25,9 @@ set(LIB_SRC_LIST skyboxmodule.cpp sparrowrenderer.cpp parametricmesh.cpp - texture.cpp - entityloader.cpp + texture.cpp + deferredmodule.cpp + shadersource.cpp ) set(LIBRARY_NAME ${PROJECT_NAME}) @@ -42,7 +43,9 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug") set(CPP_DEFINES -DRENDER_DEBUG) endif() -add_library(${LIBRARY_NAME} STATIC ${LIB_SRC_LIST}) +file(GLOB LIBRARY_RES_FILES *.h *.frag *.vert) + +add_library(${LIBRARY_NAME} STATIC ${LIB_SRC_LIST} ${LIBRARY_RES_FILES}) add_definitions(-std=c++11 ${CPP_DEFINES}) include_directories( diff --git a/deferredmodule.cpp b/deferredmodule.cpp new file mode 100644 index 0000000..2c1342a --- /dev/null +++ b/deferredmodule.cpp @@ -0,0 +1 @@ +#include "deferredmodule.h" diff --git a/deferredmodule.h b/deferredmodule.h new file mode 100644 index 0000000..8900b44 --- /dev/null +++ b/deferredmodule.h @@ -0,0 +1,34 @@ +#ifndef DEFERREDMODULE_H +#define DEFERREDMODULE_H + +#include "module.h" +#include +#include +#include +#include "lights.h" + +class Shader; +class PhongEntity; + +class DeferredModule : public Module +{ +public: + DeferredModule(Lights::Light* myDirLight, Lights* myPointLights); + + //void addEntity(PhongEntity* myEntity); + //void clearEntities(); + + virtual void renderGL(Camera* myCamera) = 0; + virtual bool requiresModernOpenGL() {return true;} +private: + /*Lights::Light* dirLight; + Lights* pointLights; + GLuint dirLightLocation; + GLuint nbPointLightsLocation; + GLuint pointLightsLocation; + std::vector entities; + + static Shader* shaders[NB_SHADERS];*/ +}; + +#endif // DEFERREDMODULE_H diff --git a/entityloader.cpp b/entityloader.cpp deleted file mode 100644 index 12f79c8..0000000 --- a/entityloader.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "entityloader.h" - -EntityLoader::EntityLoader() -{ - -} - diff --git a/entityloader.h b/entityloader.h deleted file mode 100644 index ce015fb..0000000 --- a/entityloader.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef ENTITYLOADER_H -#define ENTITYLOADER_H - -class EntityLoader -{ -public: - EntityLoader(); -}; - -#endif // ENTITYLOADER_H diff --git a/gbuffer.cpp b/gbuffer.cpp new file mode 100644 index 0000000..8a3bf0c --- /dev/null +++ b/gbuffer.cpp @@ -0,0 +1,56 @@ +#include "gbuffer.h" +#include "sparrowrenderer.h" +#include "glassert.h" + +GBuffer::GBuffer(int width, int height) +{ + glAssert(glGenFramebuffers(1, &fbo)); + glAssert(glBindFramebuffer(GL_FRAMEBUFFER, fbo)); + + // - Normal buffer + glAssert(glGenTextures(1, textures + NORMAL)); + glAssert(glBindTexture(GL_TEXTURE_2D, textures[NORMAL])); + glAssert(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, NULL)); + glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)); + glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)); + glAssert(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[NORMAL], 0)); + + // - Color + Specular exponent buffer + glAssert(glGenTextures(1, textures + COLOR)); + glAssert(glBindTexture(GL_TEXTURE_2D, textures[COLOR])); + glAssert(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL)); + glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)); + glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)); + glAssert(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, textures[COLOR], 0)); + + // - Specular color + objectId buffer + glAssert(glGenTextures(1, textures + SPECULAR)); + glAssert(glBindTexture(GL_TEXTURE_2D, textures[SPECULAR])); + glAssert(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL)); + glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)); + glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)); + glAssert(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, textures[SPECULAR], 0)); + + // - depth buffer + glAssert(glGenTextures(1, textures + DEPTH)); + glAssert(glBindTexture(GL_TEXTURE_2D, textures[DEPTH])); + glAssert(glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL)); + glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)); + glAssert(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)); + glAssert(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textures[DEPTH], 0)); + + // - Tell OpenGL which color attachments we'll use (of this framebuffer) for rendering + GLuint attachments[4] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_DEPTH_ATTACHMENT}; + glAssert(glDrawBuffers(4, attachments)); +} + +void GBuffer::bind() +{ + glAssert(glBindFramebuffer(GL_FRAMEBUFFER, fbo)); +} + +void GBuffer::bindTexture(TextureType type, int slot) +{ + glAssert(glActiveTexture(GL_TEXTURE0 + slot)); + glAssert(glBindTexture(GL_TEXTURE_2D, textures[type])); +} diff --git a/gbuffer.h b/gbuffer.h new file mode 100644 index 0000000..137a808 --- /dev/null +++ b/gbuffer.h @@ -0,0 +1,26 @@ +#ifndef GBUFFER_H +#define GBUFFER_H + +#include + +class GBuffer +{ + // fbo + GLuint fbo; + // textures + enum TextureType + { + NORMAL, + COLOR, + SPECULAR, + DEPTH, + NB_TEXTURES + }; + GLuint textures[NB_TEXTURES]; +public: + GBuffer(int width, int height); + void bind(); + void bindTexture(TextureType type, int slot); +}; + +#endif // GBUFFER_H diff --git a/gbuffermodule.cpp b/gbuffermodule.cpp deleted file mode 100644 index 68e7c92..0000000 --- a/gbuffermodule.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "gbuffermodule.h" -#include "camera.h" -#include "mesh.h" - -void GBufferModule::addMesh(Mesh* myMesh) -{ - -} - -void GBufferModule::renderGL(Camera* myCamera) -{ - -} diff --git a/gbuffermodule.h b/gbuffermodule.h deleted file mode 100644 index e9b2b55..0000000 --- a/gbuffermodule.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef GBUFFERMODULE_H -#define GBUFFERMODULE_H - -#include "module.h" -#include -#include - -class Shader; -class Mesh; - -class GBufferModule : public Module -{ -public: - Shader* shader; - std::vector meshes; - - GBufferModule(Shader* myShader = NULL) : shader(myShader) {} - - void addMesh(Mesh* myMesh); - void virtual renderGL(Camera* myCamera); -}; - -#endif // GBUFFERMODULE_H diff --git a/shadersource.cpp b/shadersource.cpp new file mode 100644 index 0000000..95b722f --- /dev/null +++ b/shadersource.cpp @@ -0,0 +1,29 @@ +#include "shadersource.h" +#include +#include "shader.h" + +ShaderSource::ShaderSource(); + +void ShaderSource::addSource(const char *source, SourceType type) +{ + +} + +Shader* ShaderSource::compile(int nbDefines = 0, const char** defines = NULL) +{ + if(sources[VERTEX] == NULL || sources[FRAGMENT] == NULL) + return NULL; + std::string plop(sources[VERTEX]); + plop.find("#if") + { + + } + + // read lines + + else if(sources[GEOMETRY] != NULL) + return new Shader(sources[VERTEX], sources[GEOMETRY], sources[FRAGMENT]); + else + return new Shader(sources[VERTEX], sources[FRAGMENT]); +} + diff --git a/shadersource.h b/shadersource.h new file mode 100644 index 0000000..949db4e --- /dev/null +++ b/shadersource.h @@ -0,0 +1,29 @@ +#ifndef SHADERSOURCE_H +#define SHADERSOURCE_H + +#include + +class Shader; + +class ShaderSource +{ +public: + enum SourceType + { + VERTEX, + GEOMETRY, + FRAGMENT, + NB_TYPES + }; + + ShaderSource(); + + void addSource(const char *source, SourceType type); + + Shader* compile(int nbDefines = 0, const char** defines = NULL); + +private: + char* sources[NB_TYPES]; +}; + +#endif // SHADERSOURCE_H diff --git a/togbuffer.frag b/togbuffer.frag new file mode 100644 index 0000000..4260fe9 --- /dev/null +++ b/togbuffer.frag @@ -0,0 +1,62 @@ +#version 330 core +layout (location = 0) out vec3 outNormal; +layout (location = 1) out vec4 outColor; +layout (location = 2) out vec4 outSpecular; + +uniform float materialNs; +uniform int objectId; + +#ifdef ALPHA_MASK +uniform sampler2D alphaMask; +#endif + +#ifdef DIFFUSE_TEXTURE +uniform sampler2D diffuseTexture; +#else +uniform vec3 materialKd; +#endif + +#ifdef SPECULAR_TEXTURE +uniform sampler2D specularTexture; +#else +uniform vec3 materialKs; +#endif + +#ifdef NORMAL_MAP +uniform sampler2D normalMap; + +in mat3 tangentSpace; +#else +in vec3 varNormal; +#endif + +in vec2 varTexCoord; + +void main() +{ +#ifdef ALPHA_MASK + if(texture(alphaMask, varTexCoord).r < 0.5) + discard; +#endif + +#ifdef NORMAL_MAP + vec3 varNormal = texture(normalMap, varTexCoord).xyz * tangentSpace; +#endif + outNormal = normalize(varNormal); + +#ifdef DIFFUSE_TEXTURE + outColor.rgb = texture(diffuseTexture, varTexCoord).rgb; +#else + outColor.rgb = materialKd; +#endif + + outColor.a = materialNs; + +#ifdef SPECULAR_TEXTURE + outSpecular.rgb = texture(specularTexture, varTexCoord).rgb; +#else + outSpecular.rgb = materialKs; +#endif + + outSpecular.a = objectId; +} diff --git a/togbuffer.vert b/togbuffer.vert new file mode 100644 index 0000000..fec3961 --- /dev/null +++ b/togbuffer.vert @@ -0,0 +1,41 @@ +#version 330 core + +#ifdef NORMAL_MAP +out mat3 tangentSpace; +#else +out vec3 varNormal; +#endif + +out vec2 varTexCoord; + +// Matrices +uniform mat4 modelViewMatrix; +uniform mat4 MVP; +uniform mat4 viewMatrix; +uniform mat3 normalMatrix; + +layout(location = 0)in vec3 inPosition; +layout(location = 1)in vec2 inTexCoord; +layout(location = 2)in vec3 inNormal; +#ifdef NORMAL_MAP +layout(location = 3)in vec3 inTangent; +layout(location = 4)in vec3 inBinormal; +#endif + +out vec3 varNormal; +out vec2 varTexCoord; + +void main(void) +{ +#ifdef NORMAL_MAP + tangentSpace = mat3(normalize(normalMatrix*inNormal), + normalize(normalMatrix*inTangent), + normalize(normalMatrix*inBinormal)); +#else + varNormal = normalize(normalMatrix*inNormal); +#endif + + varTexCoord = inTexCoord.xy; + + gl_Position = MVP * vec4(inPosition, 1.0); +} From 5305fdeac2ad39b87cf4dab333bfe3c315c636f9 Mon Sep 17 00:00:00 2001 From: Anselme Date: Mon, 23 Nov 2015 18:27:59 +0100 Subject: [PATCH 5/6] ninja commit on shader source class --- shadersource.cpp | 29 ++++++++++++++++++++--------- shadersource.h | 2 ++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/shadersource.cpp b/shadersource.cpp index 95b722f..4910d10 100644 --- a/shadersource.cpp +++ b/shadersource.cpp @@ -13,17 +13,28 @@ Shader* ShaderSource::compile(int nbDefines = 0, const char** defines = NULL) { if(sources[VERTEX] == NULL || sources[FRAGMENT] == NULL) return NULL; - std::string plop(sources[VERTEX]); - plop.find("#if") + std::string compiledSources[NB_TYPES]; + for(int i=0; i Date: Mon, 23 Nov 2015 20:49:00 +0100 Subject: [PATCH 6/6] shader preprocessing is working --- shadersource.cpp | 66 ++++++++++++++++++++++++++++++++++++++---------- shadersource.h | 5 ++-- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/shadersource.cpp b/shadersource.cpp index 4910d10..5ec0e80 100644 --- a/shadersource.cpp +++ b/shadersource.cpp @@ -1,15 +1,31 @@ #include "shadersource.h" #include +#include #include "shader.h" -ShaderSource::ShaderSource(); +#include + +ShaderSource::ShaderSource() +{ + for(int i=0; i 0 && line.at(0) == '#') + { + if(line.compare(0, 8, "#version") == 0) + compiled.append(line+'\n'); + else if(line.compare(0, 7, "#ifdef ") == 0) + allowed = isDefined(line.substr(7), nbDefines, defines); + else if(line.compare(0, 8, "#ifndef ") == 0) + allowed = !isDefined(line.substr(8), nbDefines, defines); + else if(line.compare("#endif") == 0) + allowed = true; + else if(line.compare("#else") == 0) + allowed = !allowed; + } + else if(allowed) + compiled.append(line+'\n'); + } + std::cout << compiled << std::endl; + return compiled; } diff --git a/shadersource.h b/shadersource.h index 3ea302d..78512be 100644 --- a/shadersource.h +++ b/shadersource.h @@ -17,15 +17,16 @@ public: }; ShaderSource(); + ~ShaderSource(); void addSource(const char *source, SourceType type); Shader* compile(int nbDefines = 0, const char** defines = NULL); private: - char* sources[NB_TYPES]; + std::string* sources[NB_TYPES]; - void preprocess(std::string &source, int nbDefines, const char** defines); + std::string preprocess(std::string source, int nbDefines, const char** defines); }; #endif // SHADERSOURCE_H