Commit d93e3150 authored by Dario Seyb's avatar Dario Seyb

started on stochastic transparency

parent d6f5692c
......@@ -83,7 +83,7 @@ void main()
float specularFactor = dot( reflect(lightDir, -normal), -viewDir);
if(specularFactor < 0) specularFactor = 0;
vec3 specularColor = pow(specularFactor, 210) * color.rgb;
vec3 specularColor = pow(specularFactor, 210) * uLightColor.rgb;
oColor = vec4((diffuseColor + specularColor) * uLightColor.a/(lightDistance*lightDistance) * shadowFactor(worldPosition), 1);
oColor = vec4((diffuseColor + specularColor) * color.rgb * uLightColor.a/(lightDistance*lightDistance) * shadowFactor(worldPosition), 1);
}
......@@ -2,16 +2,28 @@
in vec3 vNormal;
in vec2 vTexCoord;
in vec3 vPosition;
uniform sampler2D uTexture;
uniform vec4 uTintColor;
uniform float uTime;
out vec4 oColor;
out vec4 oNormal;
float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
void main()
{
vec3 color = texture(uTexture, vTexCoord).rgb;
oColor = vec4(color, 1.0);
vec4 color = texture(uTexture, vTexCoord).rgba * uTintColor;
float sample = rand(gl_FragCoord.xy * vPosition.xy * vPosition.z * uTime);
if(sample > color.a) discard;
oColor = color;
oNormal = vec4(normalize(vNormal) * 0.5 + 0.5, 0.0);
}
......@@ -9,11 +9,13 @@ in vec2 aTexCoord;
out vec3 vNormal;
out vec2 vTexCoord;
out vec3 vPosition;
void main()
{
vNormal = inverse(transpose(mat3(uModelMatrix))) * aNormal;
vTexCoord = aTexCoord;
vec4 worldPosition = uModelMatrix * vec4(aPosition, 1.0);
vPosition = worldPosition.xyz;
gl_Position = uViewProjectionMatrix * worldPosition;
}
......@@ -19,6 +19,14 @@ struct Light {
glm::mat4 projMatrix;
};
#undef OPAQUE
#undef TRANSPARENT
enum class RenderQueue {
OPAQUE,
TRANSPARENT
};
class RendererSystem : public System {
private:
EventSystem *m_events;
......@@ -35,9 +43,9 @@ private:
SharedShaderProgram m_blitProgram;
SharedShaderProgram m_ShadowMapProg;
std::vector<Light> m_lights = {
{ { 0, 10, 10 },{ 0.2, 0.7, 0.2, 6 }, nullptr, nullptr },
{ { 0, 10, 10 }, { 0.2, 0.7, 0.2, 6 }, nullptr, nullptr },
{ { 10, 10, 10 },{ .5, .1, .1, 4 }, nullptr, nullptr },
//{ { -10, 10, 10 },{ 1, 1, 1, 1 }, nullptr, nullptr },
{ { 0, 5, 0 },{ 1, .5, .5, 1 }, nullptr, nullptr },
};
public:
......@@ -47,7 +55,7 @@ public:
void shutdown() override;
void setMainCamera(Entity cam) { m_mainCamera = cam; }
inline void submit(DrawCall drawCall) {
inline void submit(DrawCall drawCall, RenderQueue queue) {
m_submittedDrawCalls.push(drawCall);
}
......
......@@ -19,7 +19,7 @@ using namespace ACGL::OpenGL;
using namespace ACGL::Base;
using namespace ACGL::Utils;
const glm::vec2 SHADOW_MAP_RESOLUTION = { 512, 512 };
const glm::vec2 SHADOW_MAP_RESOLUTION = {512, 512};
bool RendererSystem::startup() {
RESOLVE_DEPENDENCY(m_events);
......@@ -62,9 +62,12 @@ bool RendererSystem::startup() {
m_compositingBuffer->attachColorTexture("oColor", m_gBufferTextures[3]);
m_compositingBuffer->validate();
m_deferredCombineProgram = ShaderProgramFileManager::the()->get(ShaderProgramCreator("DeferredCombine"));
m_blitProgram = ShaderProgramFileManager::the()->get(ShaderProgramCreator("Blit"));
m_ShadowMapProg = ShaderProgramFileManager::the()->get(ShaderProgramCreator("ShadowMap"));
m_deferredCombineProgram = ShaderProgramFileManager::the()->get(
ShaderProgramCreator("DeferredCombine"));
m_blitProgram =
ShaderProgramFileManager::the()->get(ShaderProgramCreator("Blit"));
m_ShadowMapProg =
ShaderProgramFileManager::the()->get(ShaderProgramCreator("ShadowMap"));
m_events->subscribe<ResizeWindowEvent>([this](const ResizeWindowEvent &e) {
glViewport(0, 0, e.newSize.x, e.newSize.y);
......@@ -74,8 +77,8 @@ bool RendererSystem::startup() {
});
for (auto &light : m_lights) {
light.shadowMap =
SharedTexture2D(new Texture2D(SHADOW_MAP_RESOLUTION, GL_DEPTH24_STENCIL8));
light.shadowMap = SharedTexture2D(
new Texture2D(SHADOW_MAP_RESOLUTION, GL_DEPTH24_STENCIL8));
light.shadowMap->setMinFilter(GL_LINEAR);
light.shadowMap->setMagFilter(GL_LINEAR);
light.shadowMap->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
......@@ -89,7 +92,7 @@ bool RendererSystem::startup() {
m_events->subscribe<"DrawUI"_sh>([this]() {
ImGui::Begin("Reload");
if (ImGui::Button("Reload Shaders", { 100, 20 })) {
if (ImGui::Button("Reload Shaders", {100, 20})) {
ShaderProgramFileManager::the()->updateAll();
}
ImGui::End();
......@@ -101,7 +104,6 @@ bool RendererSystem::startup() {
ImGui::End();*/
});
return true;
}
......@@ -111,7 +113,6 @@ glm::mat4 interpolate(TransformData a, TransformData b, float t) {
return glm::translate(pos) * glm::mat4_cast(rot);
}
void RendererSystem::frame(double interp, double totalTime) {
if (!m_mainCamera.valid()) {
return;
......@@ -134,12 +135,14 @@ void RendererSystem::frame(double interp, double totalTime) {
auto gBufferRes = m_gBufferObject->getSize();
glViewport(0, 0, gBufferRes.x, gBufferRes.y);
auto camTransform = interpolate({ trans->lastPosition, glm::quat_cast(trans->lastRotation) }, { trans->position, glm::quat_cast(trans->rotation) }, interp);
auto camTransform =
interpolate({trans->lastPosition, glm::quat_cast(trans->lastRotation)},
{trans->position, glm::quat_cast(trans->rotation)}, interp);
auto windowSize = m_window->getSize();
auto projectionMatrix =
glm::perspectiveFov<float>(glm::radians(cam->fov), windowSize.x, windowSize.y,
cam->near, cam->far) *
glm::perspectiveFov<float>(glm::radians(cam->fov), windowSize.x,
windowSize.y, cam->near, cam->far) *
glm::inverse(camTransform);
for (size_t i = 0; i < m_submittedDrawCalls.size(); i++) {
......@@ -152,30 +155,37 @@ void RendererSystem::frame(double interp, double totalTime) {
auto bunnyTexture =
Texture2DFileManager::the()->get(Texture2DCreator("checkerboard.png"));
drawCall.material.prog->setTexture("uTexture", bunnyTexture, 0);
drawCall.material.prog->setUniform("uTime", (float)totalTime);
drawCall.material.prog->setUniform("uTintColor", glm::vec4{ 1,1,1, 0.5 });
drawCall.material.prog->setUniform("uModelMatrix", interpolate(drawCall.lastFrameTransform, drawCall.thisFrameTransform, interp));
drawCall.material.prog->setUniform(
"uModelMatrix", interpolate(drawCall.lastFrameTransform,
drawCall.thisFrameTransform, interp));
drawCall.material.prog->setUniform("uViewProjectionMatrix",
projectionMatrix);
drawCall.geometry.vao->render();
}
m_lights[0].position = {sin(totalTime) * 10, 10, cos(totalTime) * 10};
m_ShadowMapProg->use();
glCullFace(GL_FRONT);
for (auto& light : m_lights) {
light.shadowFbo->setClearColor(glm::vec4{ 0, 0, 0, 0 });
for (auto &light : m_lights) {
light.shadowFbo->setClearColor(glm::vec4{0, 0, 0, 0});
light.shadowFbo->bind();
light.shadowFbo->clearBuffers();
auto shadowRes = light.shadowFbo->getSize();
glViewport(0, 0, shadowRes.x, shadowRes.y);
auto viewMatrix = glm::lookAt<float>(light.position, light.position + glm::normalize(light.position), glm::vec3(0.0f, 1.0f, 0.0f));
auto lightVP = glm::ortho<float>(-15.0f, + 15.0f, 15.0f, -15.0f, 0.0f, -40.0f) * viewMatrix;
auto viewMatrix = glm::lookAt<float>(
light.position, light.position + glm::normalize(light.position),
glm::vec3(0.0f, 1.0f, 0.0f));
auto lightVP =
glm::ortho<float>(-15.0f, +15.0f, 15.0f, -15.0f, 0.0f, -40.0f) *
viewMatrix;
light.projMatrix = lightVP;
......@@ -183,7 +193,9 @@ void RendererSystem::frame(double interp, double totalTime) {
for (size_t i = 0; i < m_submittedDrawCalls.size(); i++) {
auto drawCall = m_submittedDrawCalls[i];
m_ShadowMapProg->setUniform("uModelMatrix", interpolate(drawCall.lastFrameTransform, drawCall.thisFrameTransform, interp));
m_ShadowMapProg->setUniform(
"uModelMatrix", interpolate(drawCall.lastFrameTransform,
drawCall.thisFrameTransform, interp));
drawCall.geometry.vao->render();
}
}
......@@ -211,8 +223,9 @@ void RendererSystem::frame(double interp, double totalTime) {
m_deferredCombineProgram->setUniform("uNear", cam->near);
m_deferredCombineProgram->setUniform("uFar", cam->far);
auto camPos = camTransform * glm::vec4{ 0, 0, 0, 1 };
m_deferredCombineProgram->setUniform("cameraPosition", glm::vec3{ camPos.x, camPos.y, camPos.z });
auto camPos = camTransform * glm::vec4{0, 0, 0, 1};
m_deferredCombineProgram->setUniform("cameraPosition",
glm::vec3{camPos.x, camPos.y, camPos.z});
m_deferredCombineProgram->setUniform("uViewProjectionInverseMatrix",
glm::inverse(projectionMatrix));
......@@ -220,20 +233,22 @@ void RendererSystem::frame(double interp, double totalTime) {
// attribute-less rendering:
VertexArrayObject vao;
vao.bind(); // 'empty' VAO -> no attributes are defined
glm::mat4 biasMatrix(
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0);
for (auto light : m_lights) {
m_deferredCombineProgram->setUniform("uLightProjMatrix", biasMatrix * light.projMatrix);
m_deferredCombineProgram->setTexture("uSamplerShadowMap", light.shadowMap, 3);
m_deferredCombineProgram->setTexture("uSamplerShadowMap", light.shadowMap, 3);
glm::mat4 biasMatrix(0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0);
auto size = light.shadowMap->getSize();
m_deferredCombineProgram->setUniform("uOneOverShadowTexSize", glm::vec2{ 1.0f/size.x, 1.0f/size.y });
for (auto light : m_lights) {
m_deferredCombineProgram->setUniform("uLightProjMatrix",
biasMatrix * light.projMatrix);
m_deferredCombineProgram->setTexture("uSamplerShadowMap", light.shadowMap,
3);
auto size = light.shadowMap->getSize();
m_deferredCombineProgram->setUniform(
"uOneOverShadowTexSize", glm::vec2{1.0f / size.x, 1.0f / size.y});
m_deferredCombineProgram->setUniform("uLightPosition", light.position);
m_deferredCombineProgram->setUniform("uLightColor", light.color);
glDrawArrays(
......@@ -241,7 +256,7 @@ void RendererSystem::frame(double interp, double totalTime) {
4); // create 2 triangles (defined in shader) with no attributes
}
// Bit to backbuffer with tonemapping
// Blit to backbuffer with tonemapping
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_BLEND);
......
......@@ -41,7 +41,7 @@ void SceneGraphSystem::prepareDraw() {
e.unpack<Drawable, Transform>(drawable, transform);
m_renderer->submit({ drawable->material, drawable->geometry,
{ transform->lastPosition, glm::quat_cast(transform->lastRotation) },
{ transform->position, glm::quat_cast(transform->rotation) } });
{ transform->position, glm::quat_cast(transform->rotation) } }, RenderQueue::OPAQUE);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment