2017-03-01 03:05:54 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// 1D dynamic shadow maps
|
|
|
|
// Copyright(C) 2017 Magnus Norddahl
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// This program 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.
|
|
|
|
//
|
|
|
|
// This program 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 this program. If not, see http://www.gnu.org/licenses/
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "gl/system/gl_system.h"
|
|
|
|
#include "gl/shaders/gl_shader.h"
|
|
|
|
#include "gl/dynlights/gl_shadowmap.h"
|
|
|
|
#include "gl/dynlights/gl_dynlight.h"
|
|
|
|
#include "gl/system/gl_interface.h"
|
|
|
|
#include "gl/system/gl_debug.h"
|
|
|
|
#include "gl/renderer/gl_renderer.h"
|
|
|
|
#include "gl/renderer/gl_postprocessstate.h"
|
2017-03-01 16:17:33 +00:00
|
|
|
#include "gl/renderer/gl_renderbuffers.h"
|
2017-03-01 03:05:54 +00:00
|
|
|
#include "gl/shaders/gl_shadowmapshader.h"
|
|
|
|
#include "r_state.h"
|
|
|
|
|
|
|
|
void FShadowMap::Clear()
|
|
|
|
{
|
2017-03-01 16:17:33 +00:00
|
|
|
if (mLightList != 0)
|
|
|
|
{
|
|
|
|
glDeleteBuffers(1, (GLuint*)&mLightList);
|
|
|
|
mLightList = 0;
|
|
|
|
}
|
|
|
|
|
2017-03-01 03:05:54 +00:00
|
|
|
mLightBSP.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FShadowMap::Update()
|
|
|
|
{
|
2017-03-01 16:17:33 +00:00
|
|
|
UploadLights();
|
2017-03-01 03:05:54 +00:00
|
|
|
|
|
|
|
FGLDebug::PushGroup("ShadowMap");
|
|
|
|
FGLPostProcessState savedState;
|
|
|
|
|
2017-03-01 16:17:33 +00:00
|
|
|
GLRenderer->mBuffers->BindShadowMapFB();
|
|
|
|
|
2017-03-01 03:05:54 +00:00
|
|
|
GLRenderer->mShadowMapShader->Bind();
|
2017-03-02 17:07:47 +00:00
|
|
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, mLightList);
|
2017-03-01 03:05:54 +00:00
|
|
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, mLightBSP.GetNodesBuffer());
|
2017-03-07 14:58:22 +00:00
|
|
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, mLightBSP.GetLinesBuffer());
|
2017-03-01 03:05:54 +00:00
|
|
|
|
|
|
|
glViewport(0, 0, 1024, 1024);
|
|
|
|
GLRenderer->RenderScreenQuad();
|
|
|
|
|
|
|
|
const auto &viewport = GLRenderer->mScreenViewport;
|
|
|
|
glViewport(viewport.left, viewport.top, viewport.width, viewport.height);
|
|
|
|
|
2017-03-02 17:07:47 +00:00
|
|
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, 0);
|
2017-03-01 03:05:54 +00:00
|
|
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, 0);
|
|
|
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, 0);
|
|
|
|
|
2017-03-02 18:10:57 +00:00
|
|
|
GLRenderer->mBuffers->BindShadowMapTexture(16);
|
|
|
|
|
2017-03-01 03:05:54 +00:00
|
|
|
FGLDebug::PopGroup();
|
|
|
|
}
|
2017-03-01 16:17:33 +00:00
|
|
|
|
|
|
|
void FShadowMap::UploadLights()
|
|
|
|
{
|
2017-03-02 17:07:47 +00:00
|
|
|
mLights.Clear();
|
|
|
|
mLightToShadowmap.Clear(mLightToShadowmap.CountUsed() * 2); // To do: allow clearing a TMap while building up a reserve
|
2017-03-01 16:17:33 +00:00
|
|
|
|
|
|
|
TThinkerIterator<ADynamicLight> it(STAT_DLIGHT);
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
ADynamicLight *light = it.Next();
|
|
|
|
if (!light) break;
|
|
|
|
|
2017-03-04 08:14:01 +00:00
|
|
|
mLightToShadowmap[light] = mLights.Size() / 4;
|
2017-03-01 16:17:33 +00:00
|
|
|
|
2017-03-02 17:07:47 +00:00
|
|
|
mLights.Push(light->X());
|
|
|
|
mLights.Push(light->Y());
|
|
|
|
mLights.Push(light->Z());
|
|
|
|
mLights.Push(light->GetRadius());
|
|
|
|
|
|
|
|
if (mLights.Size() == 1024) // Only 1024 lights for now
|
2017-03-01 16:17:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-02 17:07:47 +00:00
|
|
|
while (mLights.Size() < 1024 * 4)
|
|
|
|
mLights.Push(0.0f);
|
2017-03-01 16:17:33 +00:00
|
|
|
|
|
|
|
if (mLightList == 0)
|
|
|
|
glGenBuffers(1, (GLuint*)&mLightList);
|
|
|
|
|
|
|
|
int oldBinding = 0;
|
|
|
|
glGetIntegerv(GL_SHADER_STORAGE_BUFFER_BINDING, &oldBinding);
|
|
|
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, mLightList);
|
2017-03-02 17:07:47 +00:00
|
|
|
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(float) * mLights.Size(), &mLights[0], GL_STATIC_DRAW);
|
2017-03-01 16:17:33 +00:00
|
|
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, oldBinding);
|
2017-03-07 23:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FShadowMap::ShadowTest(ADynamicLight *light, const DVector3 &pos)
|
|
|
|
{
|
|
|
|
return mLightBSP.ShadowTest(light->Pos(), pos);
|
|
|
|
}
|