Basic working deferred rendering

This commit is contained in:
Tleety
2014-04-27 02:44:04 +02:00
parent 9c329145c4
commit 87cac66cd8
17 changed files with 344 additions and 325 deletions
-35
View File
@@ -1,35 +0,0 @@
#version 130
uniform sampler2D firstTexture;
in vec3 fragmentNormal;
in vec2 fragmentTexCoord;
in vec3 position;
layout (location = 0) out vec4 diffuseOutput;
layout (location = 1) out vec4 posOutput;
layout (location = 2) out vec4 normOutput;
layout (location = 3) out vec4 blendOutput;
void main(void)
{
posOutput.xyz = position;
normOutPut = vec4(fragmentNormal, 0);
vec4 clr = texture(firstTexture, fragmentTexCoord);
float alpha = clr.a;
if(alpha < 0.1)
discard; //Some optimizing
blendOutput.rgb = clr.rgb * clr.a; //Pre multiplied alpha
blendOutput.a = clr.a;
diffuseOutput = clr;
}
/*#version 400
in vec3 p_eye;
in vec3 n_eye;
layout (location = 0) out vec4 def_p; // "go to GL_COLOR_ATTACHMENT0"
layout (location = 1) out vec4 def_n; // "go to GL_COLOR_ATTACHMENT1"
void main () {
def_p = vec4(p_eye, 1.0);
def_n = vec4(n_eye, 1.0);
}*/
-46
View File
@@ -1,46 +0,0 @@
#version 130
precision mediump float;
uniform mat4 projectionMatrix;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
in vec3 normal;
in vec2 texCoord;
in vec3 vertex;
in float intensity;
in float ambientLight;
out vec3 fragmentNormal;
out vec2 fragmentTexCoord;
out float extIntensity;
out float extAmbientLight;
void main(void)
{
fragmentTexCoord = texCoord;
fragmentNormal = normalize((modelMatrix*vec4(normal, 0.0).xyz);
gl_Position = vec3(modelMatrix * vertex); //Copy position to the fragment shader
extIntensity = intensity/255.0;
extAmbientLight = ambientLight/255.0;
}
/*#version 400
layout(location = 0) in vec3 vp;
layout(location = 1) in vec3 vn;
layout(location = 2) in vec2 TextureCoord;
uniform mat4 P, V, M;
out vec3 p_eye;
out vec3 n_eye;
void main () {
p_eye = (V * M * vec4 (vp, 1.0)).xyz;
n_eye = (V * M * vec4 (vn, 0.0)).xyz;
gl_Position = P * vec4 (p_eye, 1.0);
}*/
+10 -97
View File
@@ -1,113 +1,26 @@
#version 430
uniform mat4 model;
uniform mat4 view;
layout(binding=0) uniform sampler2D texture0;
layout(binding=1) uniform sampler2D shadowMap;
const int maxNumberOfLights = 82;
uniform int numberOfLights;
uniform vec3 position[maxNumberOfLights];
uniform vec3 specular[maxNumberOfLights];
uniform vec3 diffuse[maxNumberOfLights];
uniform float constantAttenuation[maxNumberOfLights];
uniform float linearAttenuation[maxNumberOfLights];
uniform float quadraticAttenuation[maxNumberOfLights];
uniform float spotExponent[maxNumberOfLights];
layout (binding=0) uniform sampler2D DiffuseTexture;
in VertexData
{
vec3 Position;
vec3 Normal;
vec2 TextureCoord;
vec3 ShadowCoord;
} Input;
vec3 scene_ambient = vec3(0.5, 0.5, 0.5);
out vec4 fragmentColor;
out vec4 frag_Diffuse;
out vec4 frag_Position;
out vec4 frag_Normal;
void main()
{
// Diffuse Texture
frag_Diffuse = texture2D(DiffuseTexture, Input.TextureCoord);
// Texture
vec4 texel = texture2D(texture0, Input.TextureCoord);
//vec4 texel = (blend.x * texel0) + (blend.y * texel1) + (blend.z * texel2);
// G-buffer Position
frag_Position = vec4(Input.Position.xy, 0.0, 0.0);
//
// Phong shading
//
// Ambient light
vec3 La = scene_ambient; // Ambient light
vec3 Ks = vec3(0.3, 0.3, 0.3); // Specular reflectance
vec3 Kd = vec3(1.0, 1.0, 1.0); // Diffuse reflectance
vec3 Ka = vec3(1.0, 1.0, 1.0); // Ambient reflectance
vec3 Is;
vec3 Id;
// Shadows
//float cosTheta = clamp(dot(Input.Normal, vec3(0, 1, 0)), 0.0, 1.0);
//float bias = 0.001 * tan(acos(cosTheta)); // cosTheta is dot( n,l ), clamped between 0 and 1
//bias = clamp(bias, 0.0, 0.01);
float visibility = 1.0;
if (Input.ShadowCoord.x >= 0.0 && Input.ShadowCoord.x <= 1.0 && Input.ShadowCoord.y >= 0.0 && Input.ShadowCoord.y <= 1.0)
{
float bias = 0.00005;
vec4 shadowMapValue = texture(shadowMap, Input.ShadowCoord.xy);
if (shadowMapValue.z < clamp(Input.ShadowCoord.z - bias, 0, 1))
{
visibility = 0.3;
}
}
vec3 totalLighting = La * Ka * visibility;
float attenuation;
for(int i = 0; i < numberOfLights && i < maxNumberOfLights; i++)
{
// Light
//vec3 lightPosition = vec3(0, 0, 2);
vec3 Ls = specular[i]; // Specular light
vec3 Ld = diffuse[i]; // Diffuse light
vec3 lightPosView = vec3(view * vec4(position[i], 1.0));
vec3 surfacePosition = vec3(model * vec4(Input.Position, 1.0));
vec3 surfacePosView = vec3(view * vec4(surfacePosition, 1.0));
vec3 surfaceToLight = normalize(lightPosView - surfacePosView);
mat3 normalMatrix = transpose(inverse(mat3(view * model)));
vec3 surfaceNormal = normalize(normalMatrix * Input.Normal);
float dist = length(position[i] - surfacePosition);
attenuation = 1.0 / (constantAttenuation[i]
+ linearAttenuation[i] * dist
+ quadraticAttenuation[i] * pow(dist, 2.0));
//attenuation = attenuation * pow(clampedCosine, spotExponent[i]);
// Diffuse light
float dotProd = dot(surfaceToLight, surfaceNormal);
dotProd = max(dotProd, 0.0);
Id = Ld * Kd * abs(dotProd) * attenuation;
// Specular light
vec3 reflection = reflect(-surfaceToLight, surfaceNormal);
float dotSpecular = dot(reflection, normalize(-surfacePosView));
dotSpecular = max(dotSpecular, 0.0);
float specularFactor = pow(dotSpecular, 30.0); // Specular factor
Is = attenuation * Ls * Ks * specularFactor;
totalLighting = totalLighting + Id + Is;
}
fragmentColor = vec4(totalLighting, 1.0) * texel;
//fragmentColor = vec4(Id, 1.0) * texel;
//fragmentColor = texel;
// G-buffer Normal
frag_Normal = vec4(Input.Normal, 0.0);
}
+19
View File
@@ -0,0 +1,19 @@
#version 430
layout (binding=0) uniform sampler2D DiffuseTexture;
layout (binding=1) uniform sampler2D PositionTexture;
layout (binding=2) uniform sampler2D NormalTexture;
in VertexData
{
vec3 Position;
vec3 Normal;
vec2 TextureCoord;
} Input;
out vec4 FragColor;
void main()
{
FragColor = texture2D(DiffuseTexture, Input.TextureCoord);
}
-32
View File
@@ -1,32 +0,0 @@
#version 130
uniform sampler2D diffuseTex; // The color information
uniform sampler2D posTex; // World position
uniform sampler2D normalTex; // Normals
uniform sampler2D blendTex; // A bitmap with colors to blend with.
uniform vec3 camera; // The coordinate of the camera
in vec2 position; // The world position
layout (location = 0) out vec4 fragColor;
void main(void)
{
// Load data, stored in textures, from the first stage rendering.
vec4 diffuse = texture2D(diffuseTex, position.xy);
vec4 blend = texture2D(blendTex, position.xy);
vec4 worldPos = texture2D(posTex, position.xy);
vec4 normal = texture2D(normalTex, position.xy);
// Use information about lamp coordinate (not shown here), the pixel
// coordinate (worldpos.xyz), the normal of this pixel (normal.xyz)
// to compute a lighting effect.
// Use this lighting effect to update 'diffuse'
vec4 preBlend = diffuse * lamp + specularGlare;
// manual blending, using premultiplied alpha.
fragColor = blend + preBlend*(1-blend.a);
// Some debug features. Enable any of them to get a visual representation
// of an internal buffer.
// fragColor = (normal+1)/2;
// fragColor = diffuse;
// fragColor = blend;
// fragColor = worldPos; // Scaling may be needed to range [0,1]
// fragColor = lamp*vec4(1,1,1,1);
}
-9
View File
@@ -1,9 +0,0 @@
#version 130
in vec4 vertex; out vec2 position;
void main(void)
{
gl_Position = vertex*2-1;
gl_Position.z = 0.0;
position = vertex.xy;
}
+4 -7
View File
@@ -1,26 +1,23 @@
#version 430
uniform mat4 MVP;
uniform mat4 DepthMVP;
layout(location = 0) in vec3 Position;
layout(location = 1) in vec3 Normal;
layout(location = 2) in vec2 TextureCoord;
layout (location = 0) in vec3 Position;
layout (location = 1) in vec3 Normal;
layout (location = 2) in vec2 TextureCoord;
out VertexData
{
vec3 Position;
vec3 Normal;
vec2 TextureCoord;
vec3 ShadowCoord;
} Output;
void main()
{
gl_Position = MVP * vec4(Position, 1.0);
Output.Position = Position;
Output.Position = gl_Position.xyz;
Output.Normal = Normal;
Output.TextureCoord = TextureCoord;
Output.ShadowCoord = vec3(DepthMVP * vec4(Position, 1.0));
}
+17
View File
@@ -0,0 +1,17 @@
#version 430
layout (location = 0) in vec3 Position;
layout (location = 2) in vec2 TextureCoord;
out VertexData
{
vec3 Position;
vec2 TextureCoord;
} Output;
void main()
{
gl_Position = vec4(Position, 1.0);
Output.Position = Position;
Output.TextureCoord = TextureCoord;
}