Skip to content

Commit

Permalink
Gloom: Setting up lights for rendering shadow maps
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent dd857c6 commit 73b0587
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 7 deletions.
13 changes: 9 additions & 4 deletions doomsday/tests/test_gloom/gloom/render/light.cpp
Expand Up @@ -26,9 +26,9 @@ namespace gloom {

DENG2_PIMPL(Light)
{
Vec3d origin;
Vec3f dir;
Vec3f intensity;
Vec3d origin { -100, 100, 30 };
Vec3f dir { 100, -100, -30 };
Vec3f intensity { 10, 10, 10 };
GLTexture shadowMap;
GLFramebuffer framebuf;

Expand All @@ -48,9 +48,14 @@ Light::Light()
: d(new Impl(this))
{}

GLFramebuffer &Light::framebuf()
{
return d->framebuf;
}

Mat4f Light::lightMatrix() const
{
return Mat4f::ortho(-10, 10, -10, 10, 1, 10) *
return Mat4f::ortho(-50, 50, -50, 50, 1, 200) *
Mat4f::lookAt(d->origin + d->dir, d->origin, Vec3f(0, 1, 0));
}

Expand Down
6 changes: 4 additions & 2 deletions doomsday/tests/test_gloom/gloom/render/light.h
Expand Up @@ -19,6 +19,7 @@
#ifndef GLOOM_RENDER_LIGHT_H
#define GLOOM_RENDER_LIGHT_H

#include <de/GLFramebuffer>
#include <de/GLTexture>
#include <de/Matrix>
#include <de/Vector>
Expand Down Expand Up @@ -49,8 +50,9 @@ class Light
float fovY() const;
float aspectRatio() const;

de::GLTexture &shadowMap();
de::Mat4f lightMatrix() const;
de::GLTexture & shadowMap();
de::GLFramebuffer &framebuf();
de::Mat4f lightMatrix() const;

private:
DENG2_PRIVATE(d)
Expand Down
35 changes: 34 additions & 1 deletion doomsday/tests/test_gloom/gloom/render/lightrender.cpp
Expand Up @@ -19,19 +19,33 @@
#include "gloom/render/lightrender.h"
#include "gloom/render/light.h"

using namespace de;

namespace gloom {

DENG2_PIMPL(LightRender)
{
std::unique_ptr<Light> skyLight;
Drawable * shadowGeometry = nullptr;
GLState state;
GLProgram program;
std::unique_ptr<Light> skyLight;
QHash<ID, std::shared_ptr<Light>> lights;
GLUniform uLightMatrix{"uLightMatrix", GLUniform::Mat4};

Impl(Public *i) : Base(i)
{}

void glInit()
{
state.setBlend(false);
state.setDepthTest(true);
state.setDepthWrite(true);
state.setColorMask(gl::WriteNone);
state.setCull(gl::Front);

skyLight.reset(new Light);

self().context().shaders->build(program, "gloom.shadow") << uLightMatrix;
}

void glDeinit()
Expand All @@ -58,7 +72,26 @@ void LightRender::glDeinit()

void LightRender::render()
{
auto &sg = *d->shadowGeometry;

sg.setProgram(d->program);
sg.setState(d->state);

for (auto *light : {d->skyLight.get()})
{
d->state.setTarget(light->framebuf())
.setViewport(Rectangleui::fromSize(light->framebuf().size()));
d->uLightMatrix = light->lightMatrix();
sg.draw();
}

sg.unsetState();
sg.setProgram(d->shadowGeometry->program());
}

void gloom::LightRender::setShadowGeometry(Drawable &sg)
{
d->shadowGeometry = &sg;
}

void LightRender::createLights()
Expand Down
3 changes: 3 additions & 0 deletions doomsday/tests/test_gloom/gloom/render/lightrender.h
Expand Up @@ -21,6 +21,8 @@

#include "gloom/render/render.h"

#include <de/Drawable>

namespace gloom {

/**
Expand All @@ -35,6 +37,7 @@ class LightRender : public Render
void glDeinit() override;
void render() override;

void setShadowGeometry(de::Drawable &);
void createLights();

private:
Expand Down
4 changes: 4 additions & 0 deletions doomsday/tests/test_gloom/gloom/render/maprender.cpp
Expand Up @@ -142,6 +142,7 @@ DENG2_PIMPL(MapRender)
buildMap();
ents.createEntities();
lights.createLights();
lights.setShadowGeometry(drawable);
}

void glDeinit()
Expand Down Expand Up @@ -216,6 +217,9 @@ void MapRender::advanceTime(TimeSpan)

void MapRender::render()
{
d->lights.render();

//d->drawable.setState(d->state);
d->drawable.draw();
d->ents.render();
}
Expand Down
Expand Up @@ -3,6 +3,7 @@

group gloom {
shader surface { path = "surface" }
shader shadow { path = "shadow"}
shader ssao { path = "ssao" }
shader ssao_denoise {
path.vertex = "ssao.vsh"
Expand Down
@@ -0,0 +1,3 @@
void main(void) {
// nothing to do
}
@@ -0,0 +1,7 @@
uniform mat4 uLightMatrix;

DENG_ATTRIB vec4 aVertex;

void main(void) {
gl_Position = uLightMatrix * aVertex;
}

0 comments on commit 73b0587

Please sign in to comment.