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
+1 -1
View File
@@ -67,7 +67,7 @@ void GameWorld::Initialize()
model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj";
}
for(int i = 0; i < 500; i++)
for(int i = 0; i < 0; i++)
{
auto ball = CreateEntity();
auto transform = AddComponent<Components::Transform>(ball, "Transform");
+184 -93
View File
@@ -72,13 +72,11 @@ void Renderer::Initialize()
glEnable(GL_DEPTH_TEST);
LoadContent();
FrameBufferTextures();
}
void Renderer::LoadContent()
{
auto standardVS = std::shared_ptr<Shader>(new VertexShader("Shaders/Vertex.glsl"));
/*auto standardVS = std::shared_ptr<Shader>(new VertexShader("Shaders/Vertex.glsl"));
auto standardFS = std::shared_ptr<Shader>(new FragmentShader("Shaders/Fragment.glsl"));
m_ShaderProgram.AddShader(standardVS);
@@ -110,52 +108,26 @@ void Renderer::LoadContent()
m_ShaderProgramSkybox.AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/Skybox.vert.glsl")));
m_ShaderProgramSkybox.AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/Skybox.frag.glsl")));
m_ShaderProgramSkybox.Compile();
m_ShaderProgramSkybox.Link();
m_ShaderProgramSkybox.Link();*/
m_FirstPassProgram.AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/First_pass.vert.glsl")));
m_FirstPassProgram.AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/First_pass.frag.glsl")));
m_FirstPassProgram.AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/Vertex.glsl")));
m_FirstPassProgram.AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/Fragment.glsl")));
m_FirstPassProgram.Compile();
BindFragDataLocation();
glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 0, "frag_Diffuse");
glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 1, "frag_Position");
glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 2, "frag_Normal");
m_FirstPassProgram.Link();
m_SecondPassProgram.AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/Second_pass.vert.glsl")));
m_SecondPassProgram.AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/Second_pass.frag.glsl")));
m_SecondPassProgram.AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/Vertex2.glsl")));
m_SecondPassProgram.AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/Fragment2.glsl")));
m_SecondPassProgram.Compile();
m_SecondPassProgram.Link();
m_Skybox = std::make_shared<Skybox>("Textures/Skybox/Sunset", "jpg");
m_DebugAABB = CreateAABB();
m_ScreenQuad = CreateQuad();
CreateShadowMap(m_ShadowMapRes);
FrameBufferTextures();
}
void Renderer::CreateShadowMap(int resolution)
{
glGenFramebuffers(1, &m_ShadowFrameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_ShadowFrameBuffer);
// Depth texture
glGenTextures(1, &m_ShadowDepthTexture);
glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, resolution, resolution, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
//glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE );
//glTexParameteri( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY );
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_ShadowDepthTexture, 0);
glDrawBuffer(GL_NONE);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
LOG_ERROR("Framebuffer incomplete!");
return;
}
}
void Renderer::Draw(double dt)
{
glDisable(GL_BLEND);
@@ -550,7 +522,7 @@ void Renderer::FrameBufferTextures()
glGenRenderbuffers(1, &m_fDepthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_fDepthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, WIDTH, HEIGHT);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, WIDTH, HEIGHT);
//Generate and bind diffuse texture
glGenTextures(1, &m_fDiffuseTexture);
@@ -579,15 +551,6 @@ void Renderer::FrameBufferTextures()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//Generate and bind blend texture
glGenTextures(1, &m_fBlendTexture);
glBindTexture(GL_TEXTURE_2D, m_fBlendTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//Bind fb
glBindFramebuffer(GL_FRAMEBUFFER, m_fb);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_fDepthBuffer);
@@ -596,73 +559,201 @@ void Renderer::FrameBufferTextures()
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fDiffuseTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_fPositionTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, m_fNormalsTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_fBlendTexture, 0);
GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(fbStatus != GL_FRAMEBUFFER_COMPLETE)
{
printf("DeferredLighting:Init: FrameBuffer incomplete: 0x%x\n", fbStatus);
exit(1);
LOG_ERROR("DeferredLighting:Init: FrameBuffer incomplete: 0x%x\n", fbStatus);
//exit(1);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
//void Renderer::FrameBufferTextures()
//{
// m_fb = 0;
// m_fDepthBuffer = 0;
//
// glGenFramebuffers(1, &m_fb);
// glGenRenderbuffers(1, &m_fDepthBuffer);
//
// glBindRenderbuffer(GL_RENDERBUFFER, m_fDepthBuffer);
// glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, WIDTH, HEIGHT);
//
// //Generate and bind diffuse texture
// glGenTextures(1, &m_fDiffuseTexture);
// glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//
// //Generate and bind position texture
// glGenTextures(1, &m_fPositionTexture);
// glBindTexture(GL_TEXTURE_2D, m_fPositionTexture);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//
// //Generate and bind normal texture
// glGenTextures(1, &m_fNormalsTexture);
// glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//
// //Generate and bind blend texture
// glGenTextures(1, &m_fBlendTexture);
// glBindTexture(GL_TEXTURE_2D, m_fBlendTexture);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//
// //Bind fb
// glBindFramebuffer(GL_FRAMEBUFFER, m_fb);
// glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_fDepthBuffer);
//
// //Attach textures to the FB
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fDiffuseTexture, 0);
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_fPositionTexture, 0);
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, m_fNormalsTexture, 0);
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_fBlendTexture, 0);
//
// GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
// if(fbStatus != GL_FRAMEBUFFER_COMPLETE)
// {
// printf("DeferredLighting:Init: FrameBuffer incomplete: 0x%x\n", fbStatus);
// exit(1);
// }
//
// glBindFramebuffer(GL_FRAMEBUFFER, 0);
//}
void Renderer::DrawFBO()
{
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fb);
GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, windowBuffClear);
// Clear G-buffer
GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, windowBuffClear);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Execute the first render stage which will fill out the internal buffers with data(??)
//EnableRenderProgramStage1;
GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_NONE };
glDrawBuffers(4, windowBuffOpaque);
//DrawTheWorld();
m_FirstPassProgram.Bind();
GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, windowBuffOpaque);
DrawFBOScene();
GLenum windowBuffTransp[] = { GL_NONE, GL_NONE, GL_NONE, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, windowBuffTransp);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
//Depth buffer shall not be updated
glDepthMask(GL_FALSE);
//DrawTransparent items
glDepthMask(GL_TRUE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_BLEND);
// Draw to screen
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
m_SecondPassProgram.Bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Probably means to use the second_pass shader
//EnableRenderProgramDeferredStage();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//SetupDefferedStageUniforms(); // Probably what we do in FrameBufferTextures();
//glEnableVertexAttribArray(fVertexIndex); // VertexIndex?
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, m_fBlendTexture);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture);
////SetupDefferedStageUniforms(); // Probably what we do in FrameBufferTextures();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_fPositionTexture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture);
//DrawSimpleSquare(); //I guess this draw a square and put the textures on it
//glDisableVertexAttribArray(fVertexIndex); //VertexIndex?
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture);
glBindVertexArray(m_ScreenQuad);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(2);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
void Renderer::BindFragDataLocation()
{
glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 0, "diffuseOutput");
glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 1, "posOutput");
glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 2, "normOutput");
glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 3, "blendOutput");
//void Renderer::DrawFBO()
//{
// glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fb);
//
// GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
// glDrawBuffers(4, windowBuffClear);
// glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//
// // Execute the first render stage which will fill out the internal buffers with data(??)
// //EnableRenderProgramStage1;
// m_FirstPassProgram.Bind();
// GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_NONE };
// glDrawBuffers(4, windowBuffOpaque);
// DrawFBOScene();
//
// GLenum windowBuffTransp[] = { GL_NONE, GL_NONE, GL_NONE, GL_COLOR_ATTACHMENT3 };
// glDrawBuffers(4, windowBuffTransp);
// glEnable(GL_BLEND);
// glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
// //Depth buffer shall not be updated
// glDepthMask(GL_FALSE);
// //DrawTransparent items
// glDepthMask(GL_TRUE);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// glDisable(GL_BLEND);
//
//
// glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
// //Probably means to use the second_pass shader
// //EnableRenderProgramDeferredStage();
// m_SecondPassProgram.Bind();
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// //SetupDefferedStageUniforms(); // Probably what we do in FrameBufferTextures();
// glEnableVertexAttribArray(0);
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture);
//
// glActiveTexture(GL_TEXTURE1);
// glBindTexture(GL_TEXTURE_2D, m_fPositionTexture);
//
// glActiveTexture(GL_TEXTURE2);
// glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture);
//
// glActiveTexture(GL_TEXTURE3);
// glBindTexture(GL_TEXTURE_2D, m_fBlendTexture);
//
//
//
//
//
//
//
// //DrawSimpleSquare(); //I guess this draw a square and put the textures on it
// glBindVertexArray(m_ScreenQuad);
// glDrawArrays(GL_TRIANGLES, 0, 6);
// glDisableVertexAttribArray(0);
//
//}
void Renderer::DrawFBOScene()
{
glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix();
glm::mat4 MVP;
for (auto tuple : ModelsToRender)
{
Model* model;
glm::mat4 modelMatrix;
bool visible;
std::tie(model, modelMatrix, visible, std::ignore) = tuple;
if (!visible)
continue;
MVP = cameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
glBindVertexArray(model->VAO);
for (auto texGroup : model->TextureGroups)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, *texGroup.Texture);
glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1);
}
}
}
+2 -1
View File
@@ -84,7 +84,6 @@ private:
glm::mat4 m_SunProjection;
GLuint m_DebugAABB;
GLuint m_ScreenQuad;
GLuint m_ShadowFrameBuffer;
GLuint m_ShadowDepthTexture;
@@ -95,6 +94,7 @@ private:
GLuint m_fb;
GLuint m_fDepthBuffer;
GLenum draw_bufs[2];
GLuint m_ScreenQuad;
std::shared_ptr<Camera> m_Camera;
@@ -114,6 +114,7 @@ private:
void CreateShadowMap(int resolution);
void FrameBufferTextures();
void DrawFBO();
void DrawFBOScene();
void BindFragDataLocation();
GLuint CreateQuad();
+6 -1
View File
@@ -103,6 +103,11 @@ void ShaderProgram::AddShader(std::shared_ptr<Shader> shader)
void ShaderProgram::Compile()
{
if (m_ShaderProgramHandle == 0)
{
m_ShaderProgramHandle = glCreateProgram();
}
for (auto &shader : m_Shaders)
{
if (!shader->IsCompiled())
@@ -121,7 +126,7 @@ GLuint ShaderProgram::Link()
}
LOG_INFO("Linking shader program");
m_ShaderProgramHandle = glCreateProgram();
for (auto &shader : m_Shaders)
{
glAttachShader(m_ShaderProgramHandle, shader->GetHandle());
+1 -1
View File
@@ -61,7 +61,7 @@ class ShaderProgram
{
public:
ShaderProgram()
: m_ShaderProgramHandle(0) { }
: m_ShaderProgramHandle(0) { }
~ShaderProgram();
void AddShader(std::shared_ptr<Shader> shader);
-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;
}