From 501ec75dd896925e4a1bd61098033da1dfe7d13d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anselme=20FRAN=C3=87OIS?= Date: Sun, 15 May 2016 15:21:57 +0200 Subject: [PATCH] multiple fixes and shadows shaders are now in files --- shaders/forward.frag.glsl | 4 ++-- shaders/forward.vert.glsl | 4 ++-- shaders/shadow.frag.glsl | 15 +++++++++++++++ shaders/shadow.vert.glsl | 16 ++++++++++++++++ src/buffer.h | 17 +++++++++-------- src/chunk.cpp | 2 +- src/forwardmodule.h | 25 ++++++++++++++++++++++++ src/light.cpp | 40 +++++++-------------------------------- src/light.h | 2 -- src/shader.cpp | 5 +++++ src/shader.h | 1 + 11 files changed, 83 insertions(+), 48 deletions(-) create mode 100644 shaders/shadow.frag.glsl create mode 100644 shaders/shadow.vert.glsl diff --git a/shaders/forward.frag.glsl b/shaders/forward.frag.glsl index 6ffc830..27d5ed9 100644 --- a/shaders/forward.frag.glsl +++ b/shaders/forward.frag.glsl @@ -3,7 +3,7 @@ uniform vec3 lightColor; uniform float materialNs; uniform uint objectId; -#ifdef INSTANCING +#ifdef INSTANCED flat in int instanceId; #endif @@ -118,7 +118,7 @@ void main(void) { outColor = vec4(light*shadow*(1+cos(1.57 + att*1.57)), 1); #endif -#ifdef INSTANCING +#ifdef INSTANCED pickData = vec3(gl_FragCoord.z, gl_FragCoord.w, float(int(objectId) + instanceId)); #else pickData = vec3(gl_FragCoord.z, gl_FragCoord.w, float(objectId)); diff --git a/shaders/forward.vert.glsl b/shaders/forward.vert.glsl index 38a409a..7a130cc 100644 --- a/shaders/forward.vert.glsl +++ b/shaders/forward.vert.glsl @@ -27,7 +27,7 @@ layout(location = 3)in vec3 inTangent; layout(location = 4)in vec3 inBinormal; #endif -#ifdef INSTANCING +#ifdef INSTANCED layout(location = 5)in vec3 inInstanceOffset; flat out int instanceId; @@ -48,7 +48,7 @@ out vec4 posInLightSpace; #endif void main(void) { -#ifdef INSTANCING +#ifdef INSTANCED instanceId = gl_InstanceID; vec4 pos = vec4(inPosition + inInstanceOffset, 1.0); #else diff --git a/shaders/shadow.frag.glsl b/shaders/shadow.frag.glsl new file mode 100644 index 0000000..4a5b750 --- /dev/null +++ b/shaders/shadow.frag.glsl @@ -0,0 +1,15 @@ +#ifdef ALPHA_MASK +uniform sampler2D alphaMask; +in vec2 varTexCoord; +#endif + +out float fragmentdepth; + +void main() +{ + #ifdef ALPHA_MASK + if(texture(alphaMask, varTexCoord).r < 0.5) + discard; + #endif + fragmentdepth = gl_FragCoord.z; +} \ No newline at end of file diff --git a/shaders/shadow.vert.glsl b/shaders/shadow.vert.glsl new file mode 100644 index 0000000..ad6ae36 --- /dev/null +++ b/shaders/shadow.vert.glsl @@ -0,0 +1,16 @@ +layout(location = 0) in vec3 inPosition; + +#ifdef ALPHA_MASK +layout(location = 2) in vec2 inTexCoord; +out vec2 varTexCoord; +#endif + +uniform mat4 MVP; + +void main() +{ +#ifdef ALPHA_MASK + varTexCoord = inTexCoord.xy; +#endif + gl_Position = MVP * vec4(inPosition, 1.0); +} \ No newline at end of file diff --git a/src/buffer.h b/src/buffer.h index a9381bb..dc5437c 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -52,7 +52,7 @@ public: glBindBuffer(m_typeEnum, 0); } - T* getPointer(); + T* getPointer() { return ptr; } }; TBuffer(const std::vector &data, BufferType type, bool isDynamic = false) : @@ -105,19 +105,20 @@ private: GLenum getGLEnum() { - GLenum typeEnum; + GLenum typeEnum; switch(m_type) { + default: case VBO : - typeEnum = GL_ARRAY_BUFFER; - break; + typeEnum = GL_ARRAY_BUFFER; + break; case EBO : - typeEnum = GL_ELEMENT_ARRAY_BUFFER; - break; + typeEnum = GL_ELEMENT_ARRAY_BUFFER; + break; case UBO : - typeEnum = GL_UNIFORM_BUFFER; - break; + typeEnum = GL_UNIFORM_BUFFER; + break; } return typeEnum; diff --git a/src/chunk.cpp b/src/chunk.cpp index 38cabd8..6e0c00c 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -48,7 +48,7 @@ void Chunk::generate(glm::vec3 pos) // calling terrainFunction only once per cube corner float* top = new float[162]; float* bottom = top + 81; - //INFO: 9*9*9*3 = 2187 (only 9*9*8*3 = 1944 are used, but the hash algorithm is easier to write with 2187 ids) + // INFO: 9*9*9*3 = 2187 (only 9*9*8*3 = 1944 are used, but the hash algorithm is easier to write with 2187 ids) m_vertexHashTable = new short[2187]; glm::ivec3 p; diff --git a/src/forwardmodule.h b/src/forwardmodule.h index 8a9acd7..1356897 100644 --- a/src/forwardmodule.h +++ b/src/forwardmodule.h @@ -59,4 +59,29 @@ private: int height; }; +class PassScheduler +{ + struct Pass + { + Shader* shader; + const std::vector &geometry; + const std::vector &lights; + + Pass(Shader *myShader, + const std::vector &geomIt, + const std::vector &lightIt) : + shader(myShader), + geometry(geomIt), + lights(lightIt) + {} + }; + + std::unordered_map> geometry; + std::unordered_map> lights; + ShaderSource* shaderSources; + +public: + void compileShaders(Scene *scene, std::vector &passes); +}; + #endif // FORWARDMODULE_H diff --git a/src/light.cpp b/src/light.cpp index 50ff93f..690e3ad 100644 --- a/src/light.cpp +++ b/src/light.cpp @@ -6,9 +6,11 @@ #include "shadersource.h" #include "phongmaterial.h" #include "mesh.h" - +#include #include +RESOURCE_PACK(shaders) + const char* Light::flagStr[] = { "AMBIENT_LIGHT", "DIRECTIONNAL_LIGHT", @@ -97,8 +99,10 @@ void Light::initShadowMap(int resWidth, int resHeight, glm::vec3 dim) shadowMap->initColorAttachments(); ShaderSource source; - source.setSource(shadowVertSource, ShaderSource::VERTEX); - source.setSource(shadowFragSource, ShaderSource::FRAGMENT); + Resource::ResourceMap shaderMap; + Resource::getResourcePack_shaders(shaderMap); + source.setSource(shaderMap["shaders/shadow.vert.glsl"], ShaderSource::VERTEX); + source.setSource(shaderMap["shaders/shadow.frag.glsl"], ShaderSource::FRAGMENT); shaders[0] = source.compile(Mesh::MESH_3D, getFlags()); shaders[1] = source.compile(Mesh::MESH_3D & Mesh::MATERIAL_ALPHA_MASK, getFlags()); } @@ -163,33 +167,3 @@ unsigned int Light::getFlags() flags |= 1 << SHADOWMAP_FLAG; return flags; } - -const char* Light::shadowVertSource = - "layout(location = 0)in vec3 inPosition;\n\ - #ifdef ALPHA_MASK\n\ - layout(location = 2)in vec2 inTexCoord;\n\ - out vec2 varTexCoord;\n\ - #endif\n\ - uniform mat4 MVP;\n\ - void main()\n\ - {\n\ - #ifdef ALPHA_MASK\n\ - varTexCoord = inTexCoord.xy;\n\ - #endif\n\ - gl_Position = MVP * vec4(inPosition, 1.0);\n\ - }\n"; - -const char* Light::shadowFragSource = - "#ifdef ALPHA_MASK\n\ - uniform sampler2D alphaMask;\n\ - in vec2 varTexCoord;\n\ - #endif\n\ - out float fragmentdepth;\n\ - void main()\n\ - {\n\ - #ifdef ALPHA_MASK\n\ - if(texture(alphaMask, varTexCoord).r < 0.5)\n\ - discard;\n\ - #endif\n\ - fragmentdepth = gl_FragCoord.z;\n\ - }\n"; diff --git a/src/light.h b/src/light.h index 21ee895..76a4451 100644 --- a/src/light.h +++ b/src/light.h @@ -84,8 +84,6 @@ private: Shader* shaders[2]; glm::mat4 viewMatrix; glm::mat4 projectionMatrix; - static const char* shadowVertSource; - static const char* shadowFragSource; }; #endif // LIGHT_H diff --git a/src/shader.cpp b/src/shader.cpp index 7374e9d..9c084db 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -173,6 +173,11 @@ void Shader::bindMat4(GLuint location, glm::mat4 mat) glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(mat)); } +void Shader::bindVec2(GLuint location, glm::vec2 vec) +{ + glUniform2fv(location, 1, glm::value_ptr(vec)); +} + void Shader::bindVec3(GLuint location, glm::vec3 vec) { glUniform3fv(location, 1, glm::value_ptr(vec)); diff --git a/src/shader.h b/src/shader.h index e2d2784..389a9bb 100644 --- a/src/shader.h +++ b/src/shader.h @@ -23,6 +23,7 @@ public: void bindFloat(GLuint location, float val); void bindMat3(GLuint location, glm::mat3 mat); void bindMat4(GLuint location, glm::mat4 mat); + void bindVec2(GLuint location, glm::vec2 vec); void bindVec3(GLuint location, glm::vec3 vec); void bindVec4(GLuint location, glm::vec4 vec); void bindVec3Array(GLuint location, glm::vec3* vec, int nb_elements);