multiple fixes and shadows shaders are now in files

This commit is contained in:
Anselme FRANÇOIS 2016-05-15 15:21:57 +02:00
parent 2b191afd9f
commit 501ec75dd8
11 changed files with 83 additions and 48 deletions

View File

@ -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));

View File

@ -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

15
shaders/shadow.frag.glsl Normal file
View File

@ -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;
}

16
shaders/shadow.vert.glsl Normal file
View File

@ -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);
}

View File

@ -52,7 +52,7 @@ public:
glBindBuffer(m_typeEnum, 0);
}
T* getPointer();
T* getPointer() { return ptr; }
};
TBuffer(const std::vector<T> &data, BufferType type, bool isDynamic = false) :
@ -109,6 +109,7 @@ private:
switch(m_type)
{
default:
case VBO :
typeEnum = GL_ARRAY_BUFFER;
break;

View File

@ -59,4 +59,29 @@ private:
int height;
};
class PassScheduler
{
struct Pass
{
Shader* shader;
const std::vector<GeometryNode*> &geometry;
const std::vector<Light*> &lights;
Pass(Shader *myShader,
const std::vector<GeometryNode*> &geomIt,
const std::vector<Light*> &lightIt) :
shader(myShader),
geometry(geomIt),
lights(lightIt)
{}
};
std::unordered_map<unsigned int, std::vector<GeometryNode*>> geometry;
std::unordered_map<unsigned int, std::vector<Light*>> lights;
ShaderSource* shaderSources;
public:
void compileShaders(Scene *scene, std::vector<Pass> &passes);
};
#endif // FORWARDMODULE_H

View File

@ -6,9 +6,11 @@
#include "shadersource.h"
#include "phongmaterial.h"
#include "mesh.h"
#include <resource.h>
#include <glm/ext.hpp>
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";

View File

@ -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

View File

@ -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));

View File

@ -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);