Now render water through it's own buffer. Not currently visable through finaltexture. Also added a white texture to render the water.

This commit is contained in:
tleety
2015-09-23 18:01:15 +02:00
parent 8e2eb63c5e
commit d473e3711c
9 changed files with 303 additions and 26 deletions
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

BIN
View File
Binary file not shown.
+16 -4
View File
@@ -347,8 +347,6 @@ public:
}
}
//TODO: Add LightLoadShit
auto pointLightComponent = m_World->GetComponent<Components::PointLight>(entity);
if (pointLightComponent)
{
@@ -359,6 +357,19 @@ public:
pointLightComponent->Radius);
}
auto parent = m_World->GetEntityParent(entity);
if(parent != 0) {
auto waterParticleComponent = m_World->GetComponent<Components::WaterVolume>(parent);
if (waterParticleComponent) {
//TODO: Remove hardcoded color.
//TODO: Do i even need modelMatrix?
Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity);
glm::mat4 modelMatrix = glm::translate(absoluteTransform.Position)
* glm::scale(absoluteTransform.Scale);
EnqueueWaterParticles(absoluteTransform.Position, glm::vec4(1.f, 1.f, 1.f, 1.f), modelMatrix);
}
}
auto spriteComponent = m_World->GetComponent<Components::Sprite>(entity);
if (spriteComponent)
@@ -440,13 +451,14 @@ public:
}
void EnqueueWaterParticles(glm::vec3 position, glm::vec4 color)
void EnqueueWaterParticles(glm::vec3 position, glm::vec4 color, glm::mat4 modelMatrix)
{
WaterParticleJob job;
job.Position = position;
job.Color = color;
job.ModelMatrix = modelMatrix;
m_RendererQueue.Deferred.Add(job);
m_RendererQueue.Water.Add(job);
}
private:
+13
View File
@@ -119,6 +119,12 @@ struct WaterParticleJob : RenderJob
{
glm::vec3 Position;
glm::vec4 Color;
glm::mat4 ModelMatrix;
void CalculateHash() override
{
Hash = 0;
}
};
class RenderQueue
@@ -129,6 +135,7 @@ public:
{
job.CalculateHash();
Jobs.push_front(std::shared_ptr<T>(new T(job)));
m_Size++;
}
void Sort()
@@ -139,8 +146,11 @@ public:
void Clear()
{
Jobs.clear();
m_Size = 0;
}
int Size() const { return m_Size; }
std::forward_list<std::shared_ptr<RenderJob>>::const_iterator begin()
{
return Jobs.begin();
@@ -152,6 +162,9 @@ public:
}
std::forward_list<std::shared_ptr<RenderJob>> Jobs;
private:
int m_Size = 0;
};
struct RenderQueueCollection
+6 -3
View File
@@ -79,6 +79,7 @@ private:
Model* m_UnitQuad = nullptr;
Texture* m_StandardNormal;
Texture* m_StandardSpecular;
Texture* t_m_WhiteSphereTexture;
GLuint m_rbDepthBuffer = 0;
GLuint m_fbDeferred1 = 0;
@@ -90,21 +91,23 @@ private:
GLuint m_tLighting = 0;
GLuint m_fbDeferred3 = 0;
GLuint m_tFinal = 0;
//Water "Diffuse" texture
GLuint t_m_GWater = 0;
//Water GTexture
GLuint t_m_Gwater = 0;
//Water blur texture
GLuint t_m_tWater;
//WAterpass Framebuffer
GLuint t_m_fbWater;
GLuint m_CurrentScreenBuffer = 0;
void LoadShaders();
void CreateBuffers();
GLuint CreateQuad();
GLuint CreateWaterParticleVAO(RenderQueue &particles);
static bool DepthSort(const std::shared_ptr<RenderJob> &i, const std::shared_ptr<RenderJob> &j) { return (i->Depth < j->Depth); }
void DrawDeferred(RenderQueue &objects, RenderQueue &lights);
void DrawDeferred(RenderQueue &objects, RenderQueue &lights, RenderQueue &waterParticles);
void DrawForward(RenderQueue &objects, RenderQueue &lights);
void DrawScene(RenderQueue &objects, ShaderProgram &program);
void DrawLightSpheres(RenderQueue &lights);
+93 -17
View File
@@ -91,6 +91,10 @@ void dd::Renderer::LoadShaders()
//glBindFragDataLocation(m_SPDeferred2, 0, "FragmentLighting");
m_spDeferred2->Link();
//Water Pass
t_m_spWater = ResourceManager::Load<ShaderProgram>("Shaders/Deferred/water/");
t_m_spWater->Link();
// Pass #3: Combining into final image
m_spDeferred3 = ResourceManager::Load<ShaderProgram>("Shaders/Deferred/3/");
m_spDeferred3->Link();
@@ -115,6 +119,7 @@ void dd::Renderer::CreateBuffers()
m_UnitSphere = ResourceManager::Load<Model>("Models/Core/UnitSphere.obj");
m_StandardNormal = ResourceManager::Load<Texture>("Textures/Core/NeutralNormalMap.png");
m_StandardSpecular = ResourceManager::Load<Texture>("Textures/Core/NeutralSpecularMap.png");
t_m_WhiteSphereTexture = ResourceManager::Load<Texture>("Textures/Test/Water.png");
glGenRenderbuffers(1, &m_rbDepthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_rbDepthBuffer);
@@ -149,16 +154,14 @@ void dd::Renderer::CreateBuffers()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//WaterTest
glGenTextures(1, &t_m_GWater);
glBindTexture(GL_TEXTURE_2D, t_m_GWater);
glGenTextures(1, &t_m_Gwater);
glBindTexture(GL_TEXTURE_2D, t_m_Gwater);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_Resolution.Width, m_Resolution.Height, 0, GL_RGBA, GL_FLOAT, 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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Create first pass framebuffer
glGenFramebuffers(1, &m_fbDeferred1);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbDeferred1);
@@ -167,10 +170,8 @@ void dd::Renderer::CreateBuffers()
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_GPosition, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, m_GNormal, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_GSpecular, 0);
//WaterTest
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, GL_TEXTURE_2D, t_m_GWater, 0);
GLenum firstPassDrawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3, GL_COLOR_ATTACHMENT4 };
glDrawBuffers(5, firstPassDrawBuffers);
GLenum firstPassDrawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, firstPassDrawBuffers);
if (GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
LOG_ERROR("m_fbDeferred1 incomplete: 0x%x\n", fbStatus);
exit(EXIT_FAILURE);
@@ -196,19 +197,19 @@ void dd::Renderer::CreateBuffers()
exit(EXIT_FAILURE);
}
//Blur the water
glGenTextures(1, &t_m_tWater);
glBindTexture(GL_TEXTURE_2D, t_m_tWater);
//Fill Water Texture
glGenTextures(1, &t_m_Gwater);
glBindTexture(GL_TEXTURE_2D, t_m_Gwater);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Resolution.Width, m_Resolution.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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//Blur water pass
//Fill water pass
glGenFramebuffers(1, &t_m_fbWater);
glBindFramebuffer(GL_FRAMEBUFFER, t_m_fbWater);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, t_m_tWater, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, t_m_Gwater, 0);
GLenum waterPassDrawBuffers[] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, secondPassDrawBuffers);
if (GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
@@ -241,7 +242,7 @@ void dd::Renderer::CreateBuffers()
void dd::Renderer::Draw(RenderQueueCollection& rq)
{
DrawDeferred(rq.Deferred, rq.Lights);
DrawDeferred(rq.Deferred, rq.Lights, rq.Water);
rq.Forward.Jobs.sort(dd::Renderer::DepthSort);
DrawForward(rq.Forward, rq.Lights);
@@ -264,7 +265,7 @@ void dd::Renderer::Draw(RenderQueueCollection& rq)
DebugKeys();
}
void dd::Renderer::DrawDeferred(RenderQueue &objects, RenderQueue &lights)
void dd::Renderer::DrawDeferred(RenderQueue &objects, RenderQueue &lights, RenderQueue &waterParticles)
{
// Pass #1: Fill G-buffers
glDisable(GL_CULL_FACE);
@@ -279,6 +280,18 @@ void dd::Renderer::DrawDeferred(RenderQueue &objects, RenderQueue &lights)
m_spDeferred1->Bind();
DrawScene(objects, *m_spDeferred1);
//WaterPass
glDisable(GL_CULL_FACE);
glCullFace(GL_BACK);
glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBindFramebuffer(GL_FRAMEBUFFER, t_m_fbWater);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
t_m_spWater->Bind();
DrawWater(waterParticles);
// Pass #2: Lighting
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
@@ -349,7 +362,6 @@ void dd::Renderer::DrawScene(RenderQueue &objects, ShaderProgram &program)
glUniform3fv(glGetUniformLocation(shaderProgramHandle, "LightSpecular"), 1, glm::value_ptr(glm::vec3(1.f)));
glUniform3fv(glGetUniformLocation(shaderProgramHandle, "LightDiffuse"), 1, glm::value_ptr(glm::vec3(1.f)));
glUniform1f(glGetUniformLocation(shaderProgramHandle, "LightRadius"), 40.0f);
glUniform1f(glGetUniformLocation(shaderProgramHandle, "MaterialShininess"), modelJob->Shininess);
glActiveTexture(GL_TEXTURE0);
@@ -370,7 +382,6 @@ void dd::Renderer::DrawScene(RenderQueue &objects, ShaderProgram &program)
glBindTexture(GL_TEXTURE_2D, *m_StandardSpecular);
}
glBindVertexArray(modelJob->VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->ElementBuffer);
glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, 0, modelJob->StartIndex);
@@ -440,6 +451,68 @@ void dd::Renderer::DrawLightSpheres(RenderQueue &lights)
}
}
void dd::Renderer::DrawWater(RenderQueue &waterParticles)
{
GLuint shaderProgramHandle = *t_m_spWater;
glm::mat4 projectionMatrix = m_Camera->ProjectionMatrix();
glm::mat4 viewMatrix = m_Camera->ViewMatrix();
glm::mat4 PV = projectionMatrix * viewMatrix;
glm::mat4 MVP;
for ( auto &job : waterParticles ) {
LOG_INFO("1");
auto waterJob = std::dynamic_pointer_cast<WaterParticleJob>(job);
if (waterJob) {
LOG_INFO("2");
glm::mat4 modelMatrix = waterJob->ModelMatrix;
MVP = PV * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "M"), 1, GL_FALSE,
glm::value_ptr(modelMatrix));
glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "V"), 1, GL_FALSE,
glm::value_ptr(viewMatrix));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, *t_m_WhiteSphereTexture);
glBindVertexArray(m_UnitQuad->VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_UnitQuad->ElementBuffer);
glDrawElementsBaseVertex(GL_TRIANGLES, m_UnitQuad->m_Indices.size(), GL_UNSIGNED_INT, 0, 0);
}
}
}
GLuint dd::Renderer::CreateWaterParticleVAO(RenderQueue &particles)
{
float waterVerts[particles.Size()];
int i = 0;
for ( auto &job : particles ) {
auto waterJob = std::dynamic_pointer_cast<WaterParticleJob>(job);
if (!waterJob)
continue;
waterVerts[i ] = waterJob->Position.x;
waterVerts[i+1] = waterJob->Position.y;
waterVerts[i+2] = waterJob->Position.z;
i+3;
}
GLuint vbo[1], vao;
glGenBuffers(1, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, 3 * particles.Size() * sizeof(float), waterVerts, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
return vao;
}
GLuint dd::Renderer::CreateQuad()
{
float quadVertices[] =
@@ -503,4 +576,7 @@ void dd::Renderer::DebugKeys()
if (glfwGetKey(m_Window, GLFW_KEY_F6)) {
m_CurrentScreenBuffer = m_tLighting;
}
if (glfwGetKey(m_Window, GLFW_KEY_F7)) {
m_CurrentScreenBuffer = t_m_Gwater;
}
}
+90
View File
@@ -0,0 +1,90 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#version 440
layout (binding=0) uniform sampler2D DiffuseTexture;
layout (binding=1) uniform sampler2D NormalMap;
layout (binding=2) uniform sampler2D SpecularMap;
uniform mat4 MVP;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
uniform vec3 LightPosition;
uniform float LightRadius;
uniform vec3 LightSpecular;
uniform vec3 LightDiffuse;
in VertexData
{
vec3 Position;
vec3 Normal;
vec3 Tangent;
vec3 BiTangent;
vec2 TextureCoord;
vec4 DiffuseColor;
vec4 SpecularColor;
vec4 BoneIndices1;
vec4 BoneIndices2;
vec4 BoneWeights1;
vec4 BoneWeights2;
} Input;
out vec4 frag_Diffuse;
vec4 phong(vec3 position, vec3 normal, vec3 specular, float specularExponent)
{
// Diffuse
vec3 lightPos = vec3(V * vec4(LightPosition, 1.0));
vec3 distanceToLight = lightPos - position;
vec3 directionToLight = normalize(distanceToLight);
float dotProd = dot(directionToLight, normal);
dotProd = max(dotProd, 0.0);
vec3 Idiffuse = LightDiffuse * dotProd;
// Specular
//vec3 reflection = reflect(-directionToLight, normal);
vec3 surfaceToViewer = normalize(-position);
vec3 halfWay = normalize(surfaceToViewer + directionToLight);
float dotSpecular = max(dot(halfWay, normal), 0.0);
float specularFactor = pow(dotSpecular, specularExponent);
vec3 Ispecular = specular * LightSpecular * specularFactor;
//Attenuation
float dist = distance(lightPos, position);
//float attenuation = 1.0 - pow(dist / LightRadius, 2);
float attenuation = pow(max(0.0f, 1.0 - (dist / LightRadius)), 2);
return vec4((Idiffuse + Ispecular) * attenuation, 1.0);
}
void main()
{
vec4 normalTexel = texture(NormalMap, Input.TextureCoord);
mat3 TBN = mat3(Input.Tangent, Input.BiTangent, Input.Normal);
vec3 normal = normalize(vec4(TBN * normalTexel.xyz, 0.0).xyz);
vec4 specularTexel = texture(SpecularMap, Input.TextureCoord);
vec4 diffuseTexel = texture(DiffuseTexture, Input.TextureCoord);
vec4 La = vec4(0.3, 0.3, 0.3, 1.0);
vec4 light = phong(Input.Position, normalize(normal), specularTexel.rgb, specularTexel.a);
//frag_Diffuse = light;
frag_Diffuse = diffuseTexel;
}
+83
View File
@@ -0,0 +1,83 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#version 440
uniform mat4 MVP;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
uniform mat4 Bones[100];
layout (location = 0) in vec3 Position;
layout (location = 1) in vec3 Normal;
layout (location = 2) in vec3 Tangent;
layout (location = 3) in vec3 BiTangent;
layout (location = 4) in vec2 TextureCoord;
layout (location = 5) in vec4 DiffuseColor;
layout (location = 6) in vec4 SpecularColor;
layout (location = 7) in vec4 BoneIndices1;
layout (location = 8) in vec4 BoneIndices2;
layout (location = 9) in vec4 BoneWeights1;
layout (location = 10) in vec4 BoneWeights2;
out VertexData
{
vec3 Position;
vec3 Normal;
vec3 Tangent;
vec3 BiTangent;
vec2 TextureCoord;
vec4 DiffuseColor;
vec4 SpecularColor;
vec4 BoneIndices1;
vec4 BoneIndices2;
vec4 BoneWeights1;
vec4 BoneWeights2;
} Output;
void main()
{
mat4 boneTransform = mat4(1);
if (length(BoneWeights1 + BoneWeights2) > 0) {
boneTransform = BoneWeights1[0] * Bones[int(BoneIndices1[0])]
+ BoneWeights1[1] * Bones[int(BoneIndices1[1])]
+ BoneWeights1[2] * Bones[int(BoneIndices1[2])]
+ BoneWeights1[3] * Bones[int(BoneIndices1[3])]
+ BoneWeights2[0] * Bones[int(BoneIndices2[0])]
+ BoneWeights2[1] * Bones[int(BoneIndices2[1])]
+ BoneWeights2[2] * Bones[int(BoneIndices2[2])]
+ BoneWeights2[3] * Bones[int(BoneIndices2[3])];
}
//gl_Position = MVP * boneTransform * vec4(Position, 1.0);
gl_Position = MVP * vec4(Position, 1.0);
//TODO: Make sure that boneTransform works here.
Output.Position = (V * M * boneTransform * vec4(Position, 1.0)).xyz;
Output.Normal = (inverse(transpose(V * M)) * boneTransform * vec4(Normal, 0.0)).xyz;
Output.Tangent = Tangent;
Output.BiTangent = BiTangent;
Output.TextureCoord = TextureCoord;
Output.DiffuseColor = DiffuseColor;
Output.SpecularColor = SpecularColor;
Output.BoneIndices1 = BoneIndices1;
Output.BoneIndices2 = BoneIndices2;
Output.BoneWeights1 = BoneWeights1;
Output.BoneWeights2 = BoneWeights2;
}
+2 -2
View File
@@ -264,13 +264,13 @@ void dd::Systems::PhysicsSystem::CreateParticleGroup(EntityID e)
{
auto t_waterparticle = m_World->CreateEntity(e);
auto transformChild = m_World->AddComponent<Components::Transform>(t_waterparticle);
auto sprite = m_World->AddComponent<Components::Sprite>(t_waterparticle);
//auto sprite = m_World->AddComponent<Components::Sprite>(t_waterparticle);
transformChild->Position = glm::vec3(t_ParticlePositions[i].x - transform->Position.x, t_ParticlePositions[i].y - transform->Position.y, -9.5f);
transformChild->Scale = glm::vec3(m_ParticleSystem->GetRadius())/transform->Scale;
sprite->SpriteFile = "Textures/Ball.png";
//sprite->SpriteFile = "Textures/Ball.png";
m_World->CommitEntity(t_waterparticle);
m_EntitiesToParticleHandle.insert(std::make_pair(t_waterparticle, m_ParticleSystem->GetParticleHandleFromIndex(i)));