Merge commit 'e3aab96a750d6e5d6f8ff42ffdb5a23a47ab929d' into render_queue

Conflicts:
	assets
	src/GameWorld.cpp
	src/Renderer.cpp
	src/Renderer.h
	src/Systems/RenderSystem.cpp
This commit is contained in:
2014-05-26 04:21:08 +02:00
17 changed files with 351 additions and 87 deletions
+4 -4
View File
@@ -12,10 +12,10 @@ Camera::Camera(float yFOV, float nearClip, float farClip)
UpdateViewMatrix();
}
//glm::vec3 Camera::Forward()
//{
// return glm::rotate(glm::vec3(0.f, 0.f, -1.f), -m_Yaw, glm::vec3(0.f, 1.f, 0.f));
//}
glm::vec3 Camera::Forward()
{
return m_Orientation * glm::vec3(0, 0, -1);
}
//
//glm::vec3 Camera::Right()
//{
+2
View File
@@ -16,9 +16,11 @@ struct PointLight : Component
, ConstantAttenuation(1.0f)
, LinearAttenuation(0.f)
, QuadraticAttenuation(3.f)
, Radius(5.f)
{ }
float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation;
float Radius;
Color color;
glm::vec3 Specular;
+33 -6
View File
@@ -23,11 +23,23 @@ Model::Model(std::shared_ptr<ResourceManager> rm, OBJ &obj)
// TODO: Load normal map
std::shared_ptr<Texture> normalMap = nullptr;
if (!currentMaterial->NormalMap.FileName.empty())
{
normalMap = std::shared_ptr<Texture>(rm->Load<Texture>("Texture", currentMaterial->NormalMap.FileName));
}
else
{
normalMap = std::shared_ptr<Texture>(rm->Load<Texture>("Texture", "Textures/NeutralNormalMap.png"));
}
// Load specular map
std::shared_ptr<Texture> specularMap = nullptr;
if (!currentMaterial->SpecularMap.FileName.empty())
{
specularMap = std::shared_ptr<Texture>(rm->Load<Texture>("Texture", currentMaterial->SpecularMap.FileName));
}
else
{
specularMap = std::shared_ptr<Texture>(rm->Load<Texture>("Texture", "Textures/NeutralSpecularMap.png"));
}
// TODO: Load material parameters
// Create new texture group (start index of new group is upcoming index)
@@ -36,6 +48,21 @@ Model::Model(std::shared_ptr<ResourceManager> rm, OBJ &obj)
currentTexGroup = &TextureGroups.back();
}
/*std::unordered_map<int, glm::vec3> similarNormals;
std::unordered_map<int, int> normalCount;
for (auto &faceDef : face.Definitions)
{
if (faceDef.NormalIndex == 0)
continue;
similarNormals[faceDef.VertexIndex - 1] += normal;
normalCount[faceDef.VertexIndex - 1]++;
int index = pair.first;
glm::vec3 averagedNormal = ;
Normals[]
}*/
// Face definitions
for (auto faceDef : face.Definitions)
{
@@ -178,7 +205,7 @@ void Model::CreateBuffers( std::vector<glm::vec3> vertices, std::vector<glm::vec
bool Model::IsNear( float v1, float v2 )
{
return fabs(v1 - v2) < 0.01f;
return fabs(v1 - v2) < 0.001f;
}
void Model::getSimilarVertexIndex()
@@ -190,15 +217,15 @@ void Model::getSimilarVertexIndex()
if(i != t)
{
if(IsNear(Vertices[i].x, Vertices[t].x)
& IsNear(Vertices[i].y, Vertices[t].y)
& IsNear(Vertices[i].z, Vertices[t].z)
&& IsNear(Vertices[i].y, Vertices[t].y)
&& IsNear(Vertices[i].z, Vertices[t].z)
)
{
glm::vec3 tempNormal, tempTangent, tempBiTangent;
tempNormal = glm::normalize(Normals[i] + Normals[t]);
tempTangent = glm::normalize(TangentNormals[i] + TangentNormals[t]);
tempBiTangent = glm::normalize(BiTangentNormals[i] + BiTangentNormals[t]);
tempNormal = Normals[i] + Normals[t];
tempTangent = TangentNormals[i] + TangentNormals[t];
tempBiTangent = BiTangentNormals[i] + BiTangentNormals[t];
Normals[i] = tempNormal;
Normals[t] = tempNormal;
+127 -42
View File
@@ -13,14 +13,17 @@ Renderer::Renderer()
m_DrawWireframe = false;
m_DrawBounds = false;
#endif
Gamma = 2.2f;
Gamma = 0.85f;
CAtt = 1.0f;
LAtt = 0.0f;
QAtt = 3.0f;
m_ShadowMapRes = 2048*6;
m_SunPosition = glm::vec3(0, 3.5f, 10);
m_ShadowMapRes = 2048*8;
m_SunPosition = glm::vec3(0.f, 1.0f, 0.5f);
m_SunTarget = glm::vec3(0, 0, 0);
m_SunProjection = glm::ortho<float>(10.f, -10.f, 10.f, -10.f, 10.f, -10.f);
m_SunProjection_height = glm::vec2(-40.f, 40.f);
m_SunProjection_width = glm::vec2(-40.f, 40.f);
m_SunProjection_length = glm::vec2(-50.f, 50.f);
m_SunProjection = glm::ortho<float>(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y);
/* Lights = 0;*/
}
@@ -108,6 +111,11 @@ void Renderer::LoadContent()
m_ShaderProgramSkybox.Compile();
m_ShaderProgramSkybox.Link();*/
m_ForwardRendering.AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/ForwardRendering.vert.glsl")));
m_ForwardRendering.AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/ForwardRendering.frag.glsl")));
m_ForwardRendering.Compile();
m_ForwardRendering.Link();
m_ShaderProgramShadows.AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/ShadowMap.vert.glsl")));
m_ShaderProgramShadows.AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/ShadowMap.frag.glsl")));
m_ShaderProgramShadows.Compile();
@@ -152,17 +160,44 @@ void Renderer::Draw(double dt)
m_QuadView = true;
}
if(glfwGetKey(m_Window, GLFW_KEY_KP_1))
//if(glfwGetKey(m_Window, GLFW_KEY_KP_1))
//{
// Gamma -= 0.3f * dt;
// LOG_INFO("Gamma_UP: %f", Gamma);
//}
//if(glfwGetKey(m_Window, GLFW_KEY_KP_4))
//{
// Gamma += 0.3f * dt;
// LOG_INFO("Gamma_DOWN: %f", Gamma);
//}
if(glfwGetKey(m_Window, GLFW_KEY_KP_7))
{
Gamma -= 0.3f * dt;
LOG_INFO("Gamma_UP: %f", Gamma);
m_SunProjection_height.x += 10.f * dt;
LOG_INFO("Heightx+: %f", m_SunProjection_height);
m_SunProjection = glm::ortho<float>(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y);
}
if(glfwGetKey(m_Window, GLFW_KEY_KP_4))
else if(glfwGetKey(m_Window, GLFW_KEY_KP_4))
{
Gamma += 0.3f * dt;
LOG_INFO("Gamma_DOWN: %f", Gamma);
m_SunProjection_height.x -= 10.f * dt;
LOG_INFO("Heightx-: %f", m_SunProjection_height);
m_SunProjection = glm::ortho<float>(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y);
}
if(glfwGetKey(m_Window, GLFW_KEY_KP_8))
{
m_SunProjection_height.y += 10.f * dt;
LOG_INFO("Heightx+: %f", m_SunProjection_height);
m_SunProjection = glm::ortho<float>(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y);
}
else if(glfwGetKey(m_Window, GLFW_KEY_KP_5))
{
m_SunProjection_height.y -= 10.f * dt;
LOG_INFO("Heightx-: %f", m_SunProjection_height);
m_SunProjection = glm::ortho<float>(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y);
}
if(glfwGetKey(m_Window, GLFW_KEY_1))
{
if(glfwGetKey(m_Window, GLFW_KEY_KP_ADD))
@@ -240,8 +275,8 @@ void Renderer::CreateShadowMap(int resolution)
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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
@@ -262,7 +297,7 @@ void Renderer::DrawShadowMap()
{
glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly
glEnable(GL_CULL_FACE); //removes triangles on the wrong side of the object
glCullFace(GL_BACK); //Make it so that only the back faces are rendered
glCullFace(GL_FRONT); //Make it so that only the back faces are rendered
//Binds the FBO and sets the veiwport, witch in effect is how large the shadowmap is and what resolution it has.
glBindFramebuffer(GL_FRAMEBUFFER, m_ShadowFrameBuffer);
@@ -272,7 +307,7 @@ void Renderer::DrawShadowMap()
//glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//Creates the "camera" for the shadowmap from the direction of the sun.
glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0));
glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate((-m_Camera->Position() + (glm::vec3(40.0) * -m_Camera->Forward())) * glm::vec3(1,0,1));
glm::mat4 depthCamera = m_SunProjection * depthViewMatrix;
glm::mat4 MVP;
@@ -299,7 +334,6 @@ void Renderer::DrawShadowMap()
}
}
}
void Renderer::DrawDebugShadowMap()
@@ -383,7 +417,8 @@ void Renderer::AddPointLightToDraw(
float _specularExponent,
float _ConstantAttenuation,
float _LinearAttenuation,
float _QuadraticAttenuation
float _QuadraticAttenuation,
float _radius
)
{
Light light;
@@ -394,6 +429,7 @@ void Renderer::AddPointLightToDraw(
light.ConstantAttenuation = _ConstantAttenuation;
light.LinearAttenuation = _LinearAttenuation;
light.QuadraticAttenuation = _QuadraticAttenuation;
light.Radius = _radius;
light.SphereModelMatrix = CreateLightMatrix(light);
Lights.push_back(light);
}
@@ -553,7 +589,7 @@ void Renderer::FrameBufferTextures()
//Generate and bind diffuse texture
glGenTextures(1, &m_fDiffuseTexture);
glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, m_Width, m_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_REPEAT);
@@ -562,7 +598,7 @@ void Renderer::FrameBufferTextures()
//Generate and bind position texture
glGenTextures(1, &m_fPositionTexture);
glBindTexture(GL_TEXTURE_2D, m_fPositionTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_Width, m_Height, 0, GL_RGB, 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_REPEAT);
@@ -571,7 +607,7 @@ void Renderer::FrameBufferTextures()
//Generate and bind normal texture
glGenTextures(1, &m_fNormalsTexture);
glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_Width, m_Height, 0, GL_RGB, 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_REPEAT);
@@ -580,7 +616,7 @@ void Renderer::FrameBufferTextures()
//Generate and bind normal texture
glGenTextures(1, &m_fSpecularTexture);
glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, m_Width, m_Height, 0, GL_RED, 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_REPEAT);
@@ -617,7 +653,7 @@ void Renderer::FrameBufferTextures()
glGenTextures(1, &m_fLightingTexture);
glBindTexture(GL_TEXTURE_2D, m_fLightingTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, m_Width, m_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_REPEAT);
@@ -659,15 +695,15 @@ void Renderer::DrawFBO()
glViewport(0, 0, m_Width, m_Height);
// Clear G-buffer
GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, windowBuffClear);
glClearColor(0.f, 0.f, 0.f, 0.f);
GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, windowBuffClear);
glClearColor(0.0f, 0.3f, 0.7f, 0.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Execute the first render stage which will fill out the internal buffers with data(??)
m_FirstPassProgram.Bind();
GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, windowBuffOpaque);
GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, windowBuffOpaque);
glCullFace(GL_BACK);
@@ -688,6 +724,8 @@ void Renderer::DrawFBO()
glBindTexture(GL_TEXTURE_2D, m_fPositionTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture);
glCullFace(GL_FRONT);
DrawLightScene(viewport);
@@ -702,7 +740,7 @@ void Renderer::DrawFBO()
m_FinalPassProgram.Bind();
// Ambient light
glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.1f, 0.1f, 0.1f)));
glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.7f)));
glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma);
glActiveTexture(GL_TEXTURE0);
@@ -717,6 +755,11 @@ void Renderer::DrawFBO()
}
}
void Renderer::DrawFBO2()
{
ForwardRendering();
}
void Renderer::DrawFBOScene(Viewport &viewport)
{
// glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly
@@ -733,11 +776,14 @@ void Renderer::DrawFBOScene(Viewport &viewport)
0.5, 0.5, 0.5, 1.0
);
glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0));
glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate((-m_Camera->Position() + (glm::vec3(40.0) * -m_Camera->Forward())) * glm::vec3(1,0,1));
glm::mat4 depthCamera = m_SunProjection * depthViewMatrix;
glm::mat4 depthCameraMatrix = biasMatrix * depthCamera;
glm::mat4 depthMVP;
glm::vec3 sunDirection = m_SunTarget - m_SunPosition;
glm::vec3 sunDirection_cameraview = glm::vec3(m_Camera->ProjectionMatrix((float)m_Width / m_Height) * m_Camera->ViewMatrix() * glm::vec4(sunDirection, 1.0));
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture);
@@ -757,6 +803,8 @@ void Renderer::DrawFBOScene(Viewport &viewport)
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ViewMatrix()));
glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ProjectionMatrix((float)m_Width / m_Height)));
glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "SunDirection_cameraspace"), 1, glm::value_ptr(sunDirection_cameraview));
glBindVertexArray(model->VAO);
for (auto texGroup : model->TextureGroups)
{
@@ -767,6 +815,11 @@ void Renderer::DrawFBOScene(Viewport &viewport)
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, *texGroup.NormalMap);
}
if (texGroup.SpecularMap)
{
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, *texGroup.SpecularMap);
}
glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1);
}
}
@@ -830,6 +883,7 @@ void Renderer::DrawLightScene(Viewport &viewport)
glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), CAtt);
glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), LAtt);
glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), QAtt);
glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LightRadius"), light.Radius);
glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size());
};
@@ -848,14 +902,14 @@ glm::mat4 Renderer::CreateLightMatrix(Light &_light)
// float c = _light.ConstantAttenuation;
// float l = _light.LinearAttenuation;
// float q = _light.QuadraticAttenuation;
float c = CAtt;
float l = LAtt;
float q = QAtt;
float cutOffRadius = abs(sqrt((-4*c*q) + pow(l, 2) + (1024*q) - l) / (2*q));
//float c = CAtt;
//float l = LAtt;
//float q = QAtt;
//float cutOffRadius = abs(sqrt((-4*c*q) + pow(l, 2) + (1024*q) - l) / (2*q));
glm::mat4 model;
model *= glm::translate(_light.Position);
model *= glm::scale(glm::vec3(cutOffRadius));
model *= glm::scale(glm::vec3(_light.Radius*2));
return model;
}
@@ -886,15 +940,46 @@ void Renderer::UpdateSunProjection()
//Pass the bounding box's extents to glOrtho or similar to set up the orthographic projection matrix for the shadow map.
}
void Renderer::RegisterViewport(int identifier, float left, float top, float right, float bottom)
void Renderer::ForwardRendering()
{
/*Viewport v;
v.Left = left;
v.Top = top;
v.Right = right;
v.Bottom = bottom;
v.Camera = nullptr;
m_Viewports[identifier] = v;*/
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, m_Width, m_Height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.5f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix((float)m_Width / m_Height) * m_Camera->ViewMatrix();
glm::mat4 MVP;
m_ForwardRendering.Bind();
for (auto tuple : ModelsToRender) //// Todo: Add so it's TransparentModelsToRender
{
Model* model;
glm::mat4 modelMatrix;
bool visible;
std::tie(model, modelMatrix, visible, std::ignore) = tuple;
if (!visible)
continue;
MVP = cameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix()));
glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(m_Camera->ProjectionMatrix((float)m_Width / m_Height)));
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);
}
}
}
void Renderer::RegisterCamera(int identifier, float FOV, float nearClip, float farClip)
@@ -917,4 +1002,4 @@ void Renderer::UpdateCamera(int cameraIdentifier, glm::vec3 position, glm::quat
m_Cameras[cameraIdentifier]->FOV(FOV);
m_Cameras[cameraIdentifier]->NearClip(nearClip);
m_Cameras[cameraIdentifier]->FarClip(farClip);*/
}
}
+10 -2
View File
@@ -67,7 +67,8 @@ public:
float _specularExponent,
float _ConstantAttenuation,
float _LinearAttenuation,
float _QuadraticAttenuation
float _QuadraticAttenuation,
float _radius
);
void AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding);
@@ -111,7 +112,7 @@ private:
glm::vec3 Diffuse;
float SpecularExponent;
glm::mat4 SphereModelMatrix;
float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation;
float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation, Radius;
};
float Gamma;
@@ -133,6 +134,10 @@ private:
glm::vec3 m_SunPosition;
glm::vec3 m_SunTarget;
glm::mat4 m_SunProjection;
glm::vec2 m_SunProjection_width;
glm::vec2 m_SunProjection_height;
glm::vec2 m_SunProjection_length;
GLuint m_DebugAABB;
GLuint m_ShadowFrameBuffer;
@@ -160,6 +165,7 @@ private:
ShaderProgram m_SecondPassProgram;
ShaderProgram m_SecondPassProgram_Debug;
ShaderProgram m_FinalPassProgram;
ShaderProgram m_ForwardRendering;
ShaderProgram m_ShaderProgramNormals;
ShaderProgram m_ShaderProgramShadows;
@@ -176,12 +182,14 @@ private:
void CreateShadowMap(int resolution);
void FrameBufferTextures();
void DrawFBO();
void DrawFBO2();
void DrawFBOScene(Viewport &viewport);
void DrawLightScene(Viewport &viewport);
void BindFragDataLocation();
glm::mat4 CreateLightMatrix(Light &_light);
void UpdateSunProjection();
void CreateNormalMapTangent();
void ForwardRendering();
GLuint CreateQuad();
+5 -3
View File
@@ -21,9 +21,11 @@ void main()
vec4 LightingTexel = texture(LightingTexture, Input.TextureCoord);
vec4 ShadowTexel = texture(ShadowTexture, Input.TextureCoord);
vec4 _FragmentColor = DiffuseTexel * vec4(La, 1.0) + LightingTexel;
FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a);
//FragmentColor = DiffuseTexel;
FragmentColor = DiffuseTexel * (vec4(La, 0.0) + vec4(LightingTexel.rgb, 0.0)) + vec4(LightingTexel.a, LightingTexel.a, LightingTexel.a, 0.0);
//FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a);
}
+18
View File
@@ -0,0 +1,18 @@
#version 430
layout(binding=0) uniform sampler2D texture0;
in VertexData {
vec3 Position;
vec3 Normal;
vec2 TextureCoord;
} Input;
out vec4 fragmentColor;
void main() {
// Texture
vec4 texel = texture(texture0, Input.TextureCoord);
fragmentColor = texel;
}
+25
View File
@@ -0,0 +1,25 @@
#version 430
uniform mat4 MVP;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
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;
} Output;
void main()
{
gl_Position = MVP * vec4(Position, 1.0);
Output.Position = Position;
Output.Normal = Normal;
Output.TextureCoord = TextureCoord;
}
+21 -8
View File
@@ -5,6 +5,8 @@ layout (binding=1) uniform sampler2D ShadowTexture;
layout (binding=2) uniform sampler2D NormalMapTexture;
layout (binding=3) uniform sampler2D SpecularMapTexture;
uniform vec3 SunDirection_cameraspace;
uniform mat4 V;
in VertexData
{
@@ -19,16 +21,26 @@ in VertexData
out vec4 frag_Diffuse;
out vec4 frag_Position;
out vec4 frag_Normal;
out vec4 frag_specular;
out vec4 frag_Specular;
float Shadow(vec4 ShadowCoord)
{
//float cosTheta = clamp(dot(Input.Normal, 1.0), 0.0, 1.0);
float bias = 0.0005; // cosTheta is dot( n,l ), clamped between 0 and 1
bias = clamp(bias, 0.0, 0.01);
if( texture(ShadowTexture, Input.ShadowCoord.xy).z < ShadowCoord.z - bias)
if (Input.ShadowCoord.x < 0.0 || Input.ShadowCoord.x > 1.0 || Input.ShadowCoord.y < 0.0 || Input.ShadowCoord.y > 1.0)
return 0.9;
//Fixed bias
//float bias = 0.00005;
//Variable bias
vec3 n = normalize(Input.Normal);
vec3 l = normalize(SunDirection_cameraspace);
float cosTheta = clamp(dot(n, l), 0.0, 1.0);
float bias = tan(acos(cosTheta));
bias = clamp(bias, 0.0, 0.00005);
if( texture(ShadowTexture, Input.ShadowCoord.xy).z < ShadowCoord.z + bias)
{
return 0.3;
return 0.6;
}
else
{
@@ -46,10 +58,11 @@ void main()
frag_Position = vec4(Input.Position.xyz, 1.0);
// G-buffer Normal
mat3 TBN = transpose(mat3(Input.Tangent, Input.BiTangent, Input.Normal));
mat3 TBN = mat3(Input.Tangent, Input.BiTangent, Input.Normal);
frag_Normal = normalize(vec4(TBN * vec3(texture(NormalMapTexture, Input.TextureCoord)), 0.0));
//frag_Diffuse = normalize(vec4(TBN * vec3(texture(NormalMapTexture, Input.TextureCoord)), 0.0));
//frag_Normal = vec4(Input.Normal, 0.0);
//G-buffer Specular
frag_specular = texture(SpecularMapTexture, Input.TextureCoord);
frag_Specular = texture(SpecularMapTexture, Input.TextureCoord);
}
+9 -22
View File
@@ -2,6 +2,7 @@
layout (binding=0) uniform sampler2D PositionTexture;
layout (binding=1) uniform sampler2D NormalsTexture;
layout (binding=2) uniform sampler2D SpecularTexture;
uniform vec2 ViewportSize;
uniform mat4 MVP;
@@ -17,12 +18,14 @@ uniform vec3 CameraPosition;
uniform float ConstantAttenuation;
uniform float LinearAttenuation;
uniform float QuadraticAttenuation;
uniform float LightRadius;
const vec3 ks = vec3(1.0, 1.0, 1.0);
const vec3 kd = vec3(1.0, 1.0, 1.0);
const vec3 ka = vec3(1.0, 1.0, 1.0);
const float kshine = 1.0;
in VertexData
{
vec3 Position;
@@ -31,7 +34,7 @@ in VertexData
out vec4 FragColor;
vec4 phong(vec3 position, vec3 normal)
vec4 phong(vec3 position, vec3 normal, vec3 specular)
{
// Diffuse
vec3 lightPos = vec3(V * vec4(lp, 1.0));
@@ -47,31 +50,14 @@ vec4 phong(vec3 position, vec3 normal)
vec3 halfWay = normalize(surfaceToViewer + directionToLight);
float dotSpecular = max(dot(halfWay, normal), 0.0);
float specularFactor = pow(dotSpecular, specularExponent * 2.0);
vec3 Is = ks * ls * specularFactor;
vec3 Is = specular.r * ls * specularFactor;
//Attenuation
float dist = distance(lightPos, position);
//float attenuation = -log(min(1.0, dist / LightRadius));
float attenuation = 1.0 / (ConstantAttenuation + (LinearAttenuation * dist) + (QuadraticAttenuation * dist * dist));
float attenuation = pow(max(0.0f, 1.0 - (dist / LightRadius)), 2);
//float attenuation = 1.0 / (1.0 - 0.0001 * pow(dist, 2));
//float attenuation = clamp(0.0, 1.0, 1.0 / (0.001 + (0.001 * dist) + (0.001 * dist * dist)));
//float attenuation = 1.0 / dot(directionToLight, directionToLight);
//float att_s = 5;
//float attenuation = pow(dist, 2) / pow(5.0, 2);
//attenuation = 1.0 / (1.0 + attenuation * att_s);
//att_s = 1.0 / (1.0 + att_s);
//attenuation = attenuation / (1.0 - att_s);
//float radius = 5.0;
//float alpha = dist / radius;
//float dampingFactor = 1.0 - pow(alpha, 3);
return vec4((Id + Is) * attenuation, 1.0);
return vec4((Id) * attenuation, Is.r * attenuation);
}
void main()
@@ -79,7 +65,8 @@ void main()
vec2 TextureCoord = gl_FragCoord.xy / ViewportSize;
vec4 PositionTexel = texture(PositionTexture, TextureCoord);
vec4 NormalTexel = texture(NormalsTexture, TextureCoord);
vec4 SpecularTexel = texture(SpecularTexture, TextureCoord);
FragColor = phong(vec3(PositionTexel), vec3(NormalTexel));
FragColor = phong(vec3(PositionTexel), vec3(NormalTexel), vec3(SpecularTexel));
//FragColor = NormalTexel;
}