2012-02-12 14:20:49 +01:00
|
|
|
/* Copyright (C) 2012 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* Shadow mapping related texture and matrix management
|
2006-01-07 02:04:26 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
2012-03-19 22:10:14 +01:00
|
|
|
#include "gui/GUIutil.h"
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
#include "lib/bits.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "lib/ogl.h"
|
|
|
|
#include "ps/CLogger.h"
|
2011-02-19 00:41:27 +01:00
|
|
|
#include "ps/Profile.h"
|
2006-01-07 02:04:26 +01:00
|
|
|
|
|
|
|
#include "graphics/LightEnv.h"
|
2012-02-12 14:20:49 +01:00
|
|
|
#include "graphics/ShaderManager.h"
|
2006-01-07 02:04:26 +01:00
|
|
|
|
2011-11-25 07:36:13 +01:00
|
|
|
#include "maths/BoundingBoxAligned.h"
|
2006-01-07 02:04:26 +01:00
|
|
|
#include "maths/MathUtil.h"
|
|
|
|
#include "maths/Matrix3D.h"
|
|
|
|
|
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
#include "renderer/ShadowMap.h"
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ShadowMap implementation
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Struct ShadowMapInternals: Internal data for the ShadowMap implementation
|
|
|
|
*/
|
|
|
|
struct ShadowMapInternals
|
|
|
|
{
|
2011-04-07 00:08:07 +02:00
|
|
|
// bit depth for the depth texture
|
2006-02-16 00:50:24 +01:00
|
|
|
int DepthTextureBits;
|
2011-04-07 00:08:51 +02:00
|
|
|
// the EXT_framebuffer_object framebuffer
|
2006-03-26 19:36:33 +02:00
|
|
|
GLuint Framebuffer;
|
2006-01-07 02:04:26 +01:00
|
|
|
// handle of shadow map
|
|
|
|
GLuint Texture;
|
|
|
|
// width, height of shadow map
|
2007-02-09 18:04:55 +01:00
|
|
|
int Width, Height;
|
2006-03-26 19:36:33 +02:00
|
|
|
// used width, height of shadow map
|
2007-02-09 18:04:55 +01:00
|
|
|
int EffectiveWidth, EffectiveHeight;
|
2006-02-11 01:26:40 +01:00
|
|
|
// transform light space into projected light space
|
|
|
|
// in projected light space, the shadowbound box occupies the [-1..1] cube
|
|
|
|
// calculated on BeginRender, after the final shadow bounds are known
|
2006-01-07 02:04:26 +01:00
|
|
|
CMatrix3D LightProjection;
|
2006-02-11 01:26:40 +01:00
|
|
|
// Transform world space into light space; calculated on SetupFrame
|
2006-01-07 02:04:26 +01:00
|
|
|
CMatrix3D LightTransform;
|
2006-02-11 01:26:40 +01:00
|
|
|
// Transform world space into texture space of the shadow map;
|
|
|
|
// calculated on BeginRender, after the final shadow bounds are known
|
2006-01-07 02:04:26 +01:00
|
|
|
CMatrix3D TextureMatrix;
|
2006-01-29 18:34:45 +01:00
|
|
|
|
|
|
|
// transform light space into world space
|
|
|
|
CMatrix3D InvLightTransform;
|
|
|
|
// bounding box of shadowed objects in light space
|
2011-11-25 07:36:13 +01:00
|
|
|
CBoundingBoxAligned ShadowBound;
|
2006-01-29 18:34:45 +01:00
|
|
|
|
2006-03-26 23:58:48 +02:00
|
|
|
// Camera transformed into light space
|
|
|
|
CCamera LightspaceCamera;
|
|
|
|
|
2011-03-07 01:34:03 +01:00
|
|
|
// Some drivers (at least some Intel Mesa ones) appear to handle alpha testing
|
|
|
|
// incorrectly when the FBO has only a depth attachment.
|
|
|
|
// When m_ShadowAlphaFix is true, we use DummyTexture to store a useless
|
|
|
|
// alpha texture which is attached to the FBO as a workaround.
|
|
|
|
GLuint DummyTexture;
|
|
|
|
|
2012-03-19 22:10:14 +01:00
|
|
|
// Copy of renderer's standard view camera, saved between
|
|
|
|
// BeginRender and EndRender while we replace it with the shadow camera
|
|
|
|
CCamera SavedViewCamera;
|
|
|
|
|
2006-01-07 02:04:26 +01:00
|
|
|
// Helper functions
|
2006-02-11 01:26:40 +01:00
|
|
|
void CalcShadowMatrices();
|
|
|
|
void CreateTexture();
|
2006-01-07 02:04:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Construction/Destruction
|
|
|
|
ShadowMap::ShadowMap()
|
|
|
|
{
|
|
|
|
m = new ShadowMapInternals;
|
2006-03-26 19:36:33 +02:00
|
|
|
m->Framebuffer = 0;
|
2006-01-07 02:04:26 +01:00
|
|
|
m->Texture = 0;
|
2011-03-07 01:34:03 +01:00
|
|
|
m->DummyTexture = 0;
|
2006-02-11 19:04:32 +01:00
|
|
|
m->Width = 0;
|
|
|
|
m->Height = 0;
|
2006-03-26 19:36:33 +02:00
|
|
|
m->EffectiveWidth = 0;
|
|
|
|
m->EffectiveHeight = 0;
|
2007-02-10 21:53:48 +01:00
|
|
|
m->DepthTextureBits = 0;
|
|
|
|
// DepthTextureBits: 24/32 are very much faster than 16, on GeForce 4 and FX;
|
|
|
|
// but they're very much slower on Radeon 9800.
|
|
|
|
// In both cases, the default (no specified depth) is fast, so we just use
|
|
|
|
// that by default and hope it's alright. (Otherwise, we'd probably need to
|
|
|
|
// do some kind of hardware detection to work out what to use.)
|
2011-03-14 22:54:44 +01:00
|
|
|
|
|
|
|
// Avoid using uninitialised values in AddShadowedBound if SetupFrame wasn't called first
|
|
|
|
m->LightTransform.SetIdentity();
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ShadowMap::~ShadowMap()
|
|
|
|
{
|
|
|
|
if (m->Texture)
|
|
|
|
glDeleteTextures(1, &m->Texture);
|
2011-03-07 01:34:03 +01:00
|
|
|
if (m->DummyTexture)
|
|
|
|
glDeleteTextures(1, &m->DummyTexture);
|
2006-03-26 19:36:33 +02:00
|
|
|
if (m->Framebuffer)
|
|
|
|
pglDeleteFramebuffersEXT(1, &m->Framebuffer);
|
2006-01-07 02:04:26 +01:00
|
|
|
|
|
|
|
delete m;
|
|
|
|
}
|
|
|
|
|
2006-05-31 18:42:50 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Force the texture/buffer/etc to be recreated, particularly when the renderer's
|
|
|
|
// size has changed
|
|
|
|
void ShadowMap::RecreateTexture()
|
|
|
|
{
|
|
|
|
if (m->Texture)
|
|
|
|
glDeleteTextures(1, &m->Texture);
|
2011-03-07 01:34:03 +01:00
|
|
|
if (m->DummyTexture)
|
|
|
|
glDeleteTextures(1, &m->DummyTexture);
|
2006-05-31 18:42:50 +02:00
|
|
|
if (m->Framebuffer)
|
|
|
|
pglDeleteFramebuffersEXT(1, &m->Framebuffer);
|
|
|
|
|
|
|
|
m->Texture = 0;
|
2011-03-07 01:34:03 +01:00
|
|
|
m->DummyTexture = 0;
|
2006-05-31 18:42:50 +02:00
|
|
|
m->Framebuffer = 0;
|
|
|
|
|
|
|
|
// (Texture will be constructed in next SetupFrame)
|
|
|
|
}
|
|
|
|
|
2006-01-29 18:34:45 +01:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2006-03-26 19:36:33 +02:00
|
|
|
// SetupFrame: camera and light direction for this frame
|
2006-02-11 01:26:40 +01:00
|
|
|
void ShadowMap::SetupFrame(const CCamera& camera, const CVector3D& lightdir)
|
2006-01-29 18:34:45 +01:00
|
|
|
{
|
2006-02-11 01:26:40 +01:00
|
|
|
if (!m->Texture)
|
|
|
|
m->CreateTexture();
|
|
|
|
|
2006-01-29 18:34:45 +01:00
|
|
|
CVector3D z = lightdir;
|
|
|
|
CVector3D y;
|
|
|
|
CVector3D x = camera.m_Orientation.GetIn();
|
|
|
|
CVector3D eyepos = camera.m_Orientation.GetTranslation();
|
|
|
|
|
|
|
|
z.Normalize();
|
|
|
|
x -= z * z.Dot(x);
|
2007-05-02 14:07:08 +02:00
|
|
|
if (x.Length() < 0.001)
|
2006-01-29 18:34:45 +01:00
|
|
|
{
|
|
|
|
// this is invoked if the camera and light directions almost coincide
|
|
|
|
// assumption: light direction has a significant Z component
|
|
|
|
x = CVector3D(1.0, 0.0, 0.0);
|
|
|
|
x -= z * z.Dot(x);
|
|
|
|
}
|
|
|
|
x.Normalize();
|
|
|
|
y = z.Cross(x);
|
|
|
|
|
|
|
|
// X axis perpendicular to light direction, flowing along with view direction
|
2006-02-11 01:26:40 +01:00
|
|
|
m->LightTransform._11 = x.X;
|
|
|
|
m->LightTransform._12 = x.Y;
|
|
|
|
m->LightTransform._13 = x.Z;
|
2006-01-29 18:34:45 +01:00
|
|
|
|
|
|
|
// Y axis perpendicular to light and view direction
|
2006-02-11 01:26:40 +01:00
|
|
|
m->LightTransform._21 = y.X;
|
|
|
|
m->LightTransform._22 = y.Y;
|
|
|
|
m->LightTransform._23 = y.Z;
|
2006-01-29 18:34:45 +01:00
|
|
|
|
|
|
|
// Z axis is in direction of light
|
2006-02-11 01:26:40 +01:00
|
|
|
m->LightTransform._31 = z.X;
|
|
|
|
m->LightTransform._32 = z.Y;
|
|
|
|
m->LightTransform._33 = z.Z;
|
2006-01-29 18:34:45 +01:00
|
|
|
|
|
|
|
// eye is at the origin of the coordinate system
|
2006-02-11 01:26:40 +01:00
|
|
|
m->LightTransform._14 = -x.Dot(eyepos);
|
|
|
|
m->LightTransform._24 = -y.Dot(eyepos);
|
|
|
|
m->LightTransform._34 = -z.Dot(eyepos);
|
2006-01-29 18:34:45 +01:00
|
|
|
|
2006-02-11 01:26:40 +01:00
|
|
|
m->LightTransform._41 = 0.0;
|
|
|
|
m->LightTransform._42 = 0.0;
|
|
|
|
m->LightTransform._43 = 0.0;
|
|
|
|
m->LightTransform._44 = 1.0;
|
2006-01-29 18:34:45 +01:00
|
|
|
|
2006-02-11 01:26:40 +01:00
|
|
|
m->LightTransform.GetInverse(m->InvLightTransform);
|
|
|
|
m->ShadowBound.SetEmpty();
|
2006-03-26 23:58:48 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
m->LightspaceCamera = camera;
|
|
|
|
m->LightspaceCamera.m_Orientation = m->LightTransform * camera.m_Orientation;
|
|
|
|
m->LightspaceCamera.UpdateFrustum();
|
2006-01-29 18:34:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// AddShadowedBound: add a world-space bounding box to the bounds of shadowed
|
|
|
|
// objects
|
2011-11-25 07:36:13 +01:00
|
|
|
void ShadowMap::AddShadowedBound(const CBoundingBoxAligned& bounds)
|
2006-01-29 18:34:45 +01:00
|
|
|
{
|
2011-11-25 07:36:13 +01:00
|
|
|
CBoundingBoxAligned lightspacebounds;
|
2006-01-29 18:34:45 +01:00
|
|
|
|
2006-02-11 01:26:40 +01:00
|
|
|
bounds.Transform(m->LightTransform, lightspacebounds);
|
|
|
|
m->ShadowBound += lightspacebounds;
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CalcShadowMatrices: calculate required matrices for shadow map generation - the light's
|
|
|
|
// projection and transformation matrices
|
2006-02-11 01:26:40 +01:00
|
|
|
void ShadowMapInternals::CalcShadowMatrices()
|
2006-01-07 02:04:26 +01:00
|
|
|
{
|
2006-02-11 01:26:40 +01:00
|
|
|
CRenderer& renderer = g_Renderer;
|
2006-03-26 23:58:48 +02:00
|
|
|
float minZ = ShadowBound[0].Z;
|
|
|
|
|
|
|
|
ShadowBound.IntersectFrustumConservative(LightspaceCamera.GetFrustum());
|
|
|
|
|
2012-04-06 00:29:01 +02:00
|
|
|
// ShadowBound might have been empty to begin with, producing an empty result
|
|
|
|
if (ShadowBound.IsEmpty())
|
|
|
|
{
|
|
|
|
// no-op
|
|
|
|
LightProjection.SetIdentity();
|
|
|
|
TextureMatrix = LightTransform;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-13 01:48:05 +02:00
|
|
|
// round off the shadow boundaries to sane increments to help reduce swim effect
|
|
|
|
float boundInc = 16.0f;
|
|
|
|
ShadowBound[0].X = floor(ShadowBound[0].X / boundInc) * boundInc;
|
|
|
|
ShadowBound[0].Y = floor(ShadowBound[0].Y / boundInc) * boundInc;
|
|
|
|
ShadowBound[1].X = ceil(ShadowBound[1].X / boundInc) * boundInc;
|
|
|
|
ShadowBound[1].Y = ceil(ShadowBound[1].Y / boundInc) * boundInc;
|
|
|
|
|
2006-03-26 23:58:48 +02:00
|
|
|
// minimum Z bound must not be clipped too much, because objects that lie outside
|
|
|
|
// the shadow bounds cannot cast shadows either
|
2012-03-19 22:10:14 +01:00
|
|
|
// the 2.0 is rather arbitrary: it should be big enough so that we won't accidentally miss
|
2006-03-26 23:58:48 +02:00
|
|
|
// a shadow generator, and small enough not to affect Z precision
|
|
|
|
ShadowBound[0].Z = minZ - 2.0;
|
|
|
|
|
2006-02-11 01:26:40 +01:00
|
|
|
// Setup orthogonal projection (lightspace -> clip space) for shadowmap rendering
|
|
|
|
CVector3D scale = ShadowBound[1] - ShadowBound[0];
|
|
|
|
CVector3D shift = (ShadowBound[1] + ShadowBound[0]) * -0.5;
|
2006-01-07 02:04:26 +01:00
|
|
|
|
2006-02-11 01:26:40 +01:00
|
|
|
if (scale.X < 1.0)
|
|
|
|
scale.X = 1.0;
|
|
|
|
if (scale.Y < 1.0)
|
|
|
|
scale.Y = 1.0;
|
|
|
|
if (scale.Z < 1.0)
|
|
|
|
scale.Z = 1.0;
|
2006-01-07 02:04:26 +01:00
|
|
|
|
2006-02-11 01:26:40 +01:00
|
|
|
scale.X = 2.0 / scale.X;
|
|
|
|
scale.Y = 2.0 / scale.Y;
|
|
|
|
scale.Z = 2.0 / scale.Z;
|
2006-01-07 02:04:26 +01:00
|
|
|
|
2011-07-13 01:48:05 +02:00
|
|
|
// make sure a given world position falls on a consistent shadowmap texel fractional offset
|
|
|
|
float offsetX = fmod(ShadowBound[0].X - LightTransform._14, 2.0f/(scale.X*EffectiveWidth));
|
|
|
|
float offsetY = fmod(ShadowBound[0].Y - LightTransform._24, 2.0f/(scale.Y*EffectiveHeight));
|
|
|
|
|
2006-02-11 01:26:40 +01:00
|
|
|
LightProjection.SetZero();
|
|
|
|
LightProjection._11 = scale.X;
|
2011-07-13 01:48:05 +02:00
|
|
|
LightProjection._14 = (shift.X + offsetX) * scale.X;
|
2006-02-11 01:26:40 +01:00
|
|
|
LightProjection._22 = scale.Y;
|
2011-07-13 01:48:05 +02:00
|
|
|
LightProjection._24 = (shift.Y + offsetY) * scale.Y;
|
2006-02-11 01:26:40 +01:00
|
|
|
LightProjection._33 = scale.Z;
|
2006-02-13 15:18:20 +01:00
|
|
|
LightProjection._34 = shift.Z * scale.Z + renderer.m_ShadowZBias;
|
2006-02-11 01:26:40 +01:00
|
|
|
LightProjection._44 = 1.0;
|
|
|
|
|
|
|
|
// Calculate texture matrix by creating the clip space to texture coordinate matrix
|
|
|
|
// and then concatenating all matrices that have been calculated so far
|
2012-04-06 00:29:01 +02:00
|
|
|
|
2011-07-13 01:48:05 +02:00
|
|
|
float texscalex = scale.X * 0.5f * (float)EffectiveWidth / (float)Width;
|
|
|
|
float texscaley = scale.Y * 0.5f * (float)EffectiveHeight / (float)Height;
|
|
|
|
float texscalez = scale.Z * 0.5f;
|
2006-02-13 15:18:20 +01:00
|
|
|
|
2012-04-06 00:29:01 +02:00
|
|
|
CMatrix3D lightToTex;
|
2006-02-13 15:18:20 +01:00
|
|
|
lightToTex.SetZero();
|
|
|
|
lightToTex._11 = texscalex;
|
2011-07-13 01:48:05 +02:00
|
|
|
lightToTex._14 = (offsetX - ShadowBound[0].X) * texscalex;
|
2006-02-13 15:18:20 +01:00
|
|
|
lightToTex._22 = texscaley;
|
2011-07-13 01:48:05 +02:00
|
|
|
lightToTex._24 = (offsetY - ShadowBound[0].Y) * texscaley;
|
2006-02-13 15:18:20 +01:00
|
|
|
lightToTex._33 = texscalez;
|
|
|
|
lightToTex._34 = -ShadowBound[0].Z * texscalez;
|
|
|
|
lightToTex._44 = 1.0;
|
|
|
|
|
|
|
|
TextureMatrix = lightToTex * LightTransform;
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-05-31 18:42:50 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2006-02-11 01:26:40 +01:00
|
|
|
// Create the shadow map
|
|
|
|
void ShadowMapInternals::CreateTexture()
|
2006-01-07 02:04:26 +01:00
|
|
|
{
|
2006-03-26 19:36:33 +02:00
|
|
|
// Cleanup
|
2006-02-11 01:26:40 +01:00
|
|
|
if (Texture)
|
2006-03-26 19:36:33 +02:00
|
|
|
{
|
2006-02-11 01:26:40 +01:00
|
|
|
glDeleteTextures(1, &Texture);
|
2006-03-26 19:36:33 +02:00
|
|
|
Texture = 0;
|
|
|
|
}
|
2011-03-07 01:34:03 +01:00
|
|
|
if (DummyTexture)
|
|
|
|
{
|
|
|
|
glDeleteTextures(1, &DummyTexture);
|
|
|
|
DummyTexture = 0;
|
|
|
|
}
|
2006-03-26 19:36:33 +02:00
|
|
|
if (Framebuffer)
|
|
|
|
{
|
|
|
|
pglDeleteFramebuffersEXT(1, &Framebuffer);
|
|
|
|
Framebuffer = 0;
|
|
|
|
}
|
|
|
|
|
2011-04-07 00:08:51 +02:00
|
|
|
pglGenFramebuffersEXT(1, &Framebuffer);
|
2006-02-11 01:26:40 +01:00
|
|
|
|
2007-02-09 18:04:55 +01:00
|
|
|
if (g_Renderer.m_ShadowMapSize != 0)
|
|
|
|
{
|
|
|
|
// non-default option to override the size
|
|
|
|
Width = Height = g_Renderer.m_ShadowMapSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-19 22:10:14 +01:00
|
|
|
// get shadow map size as next power of two up from view width/height
|
|
|
|
Width = Height = (int)round_up_to_pow2((unsigned)std::max(g_Renderer.GetWidth(), g_Renderer.GetHeight()));
|
2007-02-09 18:04:55 +01:00
|
|
|
}
|
|
|
|
// Clamp to the maximum texture size
|
|
|
|
Width = std::min(Width, (int)ogl_max_tex_size);
|
|
|
|
Height = std::min(Height, (int)ogl_max_tex_size);
|
2006-03-26 19:36:33 +02:00
|
|
|
|
2011-04-07 00:08:51 +02:00
|
|
|
// Since we're using a framebuffer object, the whole texture is available
|
|
|
|
EffectiveWidth = Width;
|
|
|
|
EffectiveHeight = Height;
|
2006-02-11 01:26:40 +01:00
|
|
|
|
2011-04-07 00:08:07 +02:00
|
|
|
const char* formatname;
|
2006-02-16 00:50:24 +01:00
|
|
|
|
2011-04-07 00:08:07 +02:00
|
|
|
switch(DepthTextureBits)
|
2006-02-16 00:50:24 +01:00
|
|
|
{
|
2011-04-07 00:08:07 +02:00
|
|
|
case 16: formatname = "DEPTH_COMPONENT16"; break;
|
|
|
|
case 24: formatname = "DEPTH_COMPONENT24"; break;
|
|
|
|
case 32: formatname = "DEPTH_COMPONENT32"; break;
|
|
|
|
default: formatname = "DEPTH_COMPONENT"; break;
|
2006-02-16 00:50:24 +01:00
|
|
|
}
|
|
|
|
|
2011-04-07 00:08:51 +02:00
|
|
|
LOGMESSAGE(L"Creating shadow texture (size %dx%d) (format = %hs)",
|
|
|
|
Width, Height, formatname);
|
2006-02-11 19:04:32 +01:00
|
|
|
|
2006-02-11 01:26:40 +01:00
|
|
|
|
2011-04-07 00:08:07 +02:00
|
|
|
if (g_Renderer.m_Options.m_ShadowAlphaFix)
|
2006-02-11 19:04:32 +01:00
|
|
|
{
|
2011-04-07 00:08:07 +02:00
|
|
|
glGenTextures(1, &DummyTexture);
|
|
|
|
g_Renderer.BindTexture(0, DummyTexture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
2012-02-13 17:41:17 +01:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
2011-04-07 00:08:07 +02:00
|
|
|
}
|
2006-02-16 00:50:24 +01:00
|
|
|
|
2011-04-07 00:08:07 +02:00
|
|
|
glGenTextures(1, &Texture);
|
|
|
|
g_Renderer.BindTexture(0, Texture);
|
2006-02-16 00:50:24 +01:00
|
|
|
|
2011-04-07 00:08:07 +02:00
|
|
|
GLenum format;
|
2006-02-14 21:38:51 +01:00
|
|
|
|
2012-03-19 22:10:14 +01:00
|
|
|
#if CONFIG2_GLES
|
|
|
|
format = GL_DEPTH_COMPONENT;
|
|
|
|
#else
|
|
|
|
switch (DepthTextureBits)
|
2006-02-11 19:04:32 +01:00
|
|
|
{
|
2011-04-07 00:08:07 +02:00
|
|
|
case 16: format = GL_DEPTH_COMPONENT16; break;
|
|
|
|
case 24: format = GL_DEPTH_COMPONENT24; break;
|
|
|
|
case 32: format = GL_DEPTH_COMPONENT32; break;
|
|
|
|
default: format = GL_DEPTH_COMPONENT; break;
|
2006-02-11 19:04:32 +01:00
|
|
|
}
|
2012-03-19 22:10:14 +01:00
|
|
|
#endif
|
2006-02-11 01:26:40 +01:00
|
|
|
|
2012-03-19 22:10:14 +01:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, format, Width, Height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, NULL);
|
|
|
|
// GLES requires type == UNSIGNED_SHORT or UNSIGNED_INT
|
|
|
|
|
|
|
|
// set texture parameters
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2011-04-07 00:08:07 +02:00
|
|
|
|
2012-02-13 17:41:17 +01:00
|
|
|
#if CONFIG2_GLES
|
2012-03-19 22:10:14 +01:00
|
|
|
// GLES doesn't do depth comparisons, so treat it as a
|
|
|
|
// basic unfiltered depth texture
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
2012-02-13 17:41:17 +01:00
|
|
|
#else
|
2012-03-19 22:10:14 +01:00
|
|
|
// Enable automatic depth comparisons
|
2011-07-13 01:48:05 +02:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
|
2011-04-07 00:08:07 +02:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
|
|
|
|
|
2012-03-19 22:10:14 +01:00
|
|
|
// Use GL_LINEAR to trigger automatic PCF on some devices
|
2012-02-13 17:41:17 +01:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
2012-03-19 22:10:14 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ogl_WarnIfError();
|
2006-03-26 19:36:33 +02:00
|
|
|
|
|
|
|
// bind to framebuffer object
|
2011-04-07 00:08:51 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
pglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, Framebuffer);
|
2006-03-26 19:36:33 +02:00
|
|
|
|
2011-04-07 00:08:51 +02:00
|
|
|
pglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, Texture, 0);
|
2011-03-07 01:34:03 +01:00
|
|
|
|
2011-04-07 00:08:51 +02:00
|
|
|
if (g_Renderer.m_Options.m_ShadowAlphaFix)
|
|
|
|
{
|
|
|
|
pglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, DummyTexture, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-13 17:41:17 +01:00
|
|
|
#if CONFIG2_GLES
|
|
|
|
#warning TODO: figure out whether the glDrawBuffer/glReadBuffer stuff is needed, since it is not supported by GLES
|
|
|
|
#else
|
2011-04-07 00:08:51 +02:00
|
|
|
glDrawBuffer(GL_NONE);
|
2012-02-13 17:41:17 +01:00
|
|
|
#endif
|
2011-04-07 00:08:51 +02:00
|
|
|
}
|
2006-03-26 19:36:33 +02:00
|
|
|
|
2012-02-13 17:41:17 +01:00
|
|
|
#if !CONFIG2_GLES
|
2011-04-07 00:08:51 +02:00
|
|
|
glReadBuffer(GL_NONE);
|
2012-02-13 17:41:17 +01:00
|
|
|
#endif
|
2006-03-26 19:36:33 +02:00
|
|
|
|
2012-03-19 22:10:14 +01:00
|
|
|
ogl_WarnIfError();
|
|
|
|
|
2011-04-07 00:08:51 +02:00
|
|
|
GLenum status = pglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
|
2006-03-26 19:36:33 +02:00
|
|
|
|
2011-04-07 00:08:51 +02:00
|
|
|
pglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
2006-03-26 19:36:33 +02:00
|
|
|
|
2011-04-07 00:08:51 +02:00
|
|
|
if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
|
|
|
|
{
|
2012-01-01 17:43:10 +01:00
|
|
|
LOGWARNING(L"Framebuffer object incomplete: 0x%04X", status);
|
2006-03-26 19:36:33 +02:00
|
|
|
|
2011-04-07 00:08:51 +02:00
|
|
|
// Disable shadow rendering (but let the user try again if they want)
|
|
|
|
g_Renderer.m_Options.m_Shadows = false;
|
2006-03-26 19:36:33 +02:00
|
|
|
}
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Set up to render into shadow map texture
|
|
|
|
void ShadowMap::BeginRender()
|
|
|
|
{
|
|
|
|
// HACK HACK: this depends in non-obvious ways on the behaviour of the caller
|
2006-01-29 18:34:45 +01:00
|
|
|
|
2006-02-11 01:26:40 +01:00
|
|
|
// Calc remaining shadow matrices
|
|
|
|
m->CalcShadowMatrices();
|
|
|
|
|
2006-03-26 19:36:33 +02:00
|
|
|
{
|
2011-02-19 00:41:27 +01:00
|
|
|
PROFILE("bind framebuffer");
|
2006-03-26 19:36:33 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
pglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m->Framebuffer);
|
|
|
|
}
|
|
|
|
|
2006-01-07 02:04:26 +01:00
|
|
|
// clear buffers
|
2006-02-11 19:04:32 +01:00
|
|
|
{
|
2011-02-19 00:41:27 +01:00
|
|
|
PROFILE("clear depth texture");
|
2012-03-19 22:10:14 +01:00
|
|
|
// In case we used m_ShadowAlphaFix, we ought to clear the unused
|
|
|
|
// color buffer too, else Mali 400 drivers get confused.
|
|
|
|
// Might as well clear stencil too for completeness.
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
2006-02-11 19:04:32 +01:00
|
|
|
glColorMask(0,0,0,0);
|
|
|
|
}
|
2006-01-07 02:04:26 +01:00
|
|
|
|
|
|
|
// setup viewport
|
2006-03-26 19:36:33 +02:00
|
|
|
glViewport(0, 0, m->EffectiveWidth, m->EffectiveHeight);
|
2006-01-07 02:04:26 +01:00
|
|
|
|
2012-03-19 22:10:14 +01:00
|
|
|
m->SavedViewCamera = g_Renderer.GetViewCamera();
|
|
|
|
|
|
|
|
CCamera c = m->SavedViewCamera;
|
|
|
|
c.SetProjection(m->LightProjection);
|
|
|
|
c.GetOrientation() = m->InvLightTransform;
|
|
|
|
g_Renderer.SetViewCamera(c);
|
|
|
|
|
|
|
|
#if !CONFIG2_GLES
|
2006-01-07 02:04:26 +01:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadMatrixf(&m->LightProjection._11);
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadMatrixf(&m->LightTransform._11);
|
2012-02-13 17:41:17 +01:00
|
|
|
#endif
|
2006-01-07 02:04:26 +01:00
|
|
|
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2006-03-26 19:36:33 +02:00
|
|
|
glScissor(1,1, m->EffectiveWidth-2, m->EffectiveHeight-2);
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Finish rendering into shadow map texture
|
|
|
|
void ShadowMap::EndRender()
|
|
|
|
{
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
|
2012-03-19 22:10:14 +01:00
|
|
|
g_Renderer.SetViewCamera(m->SavedViewCamera);
|
|
|
|
|
2006-03-26 19:36:33 +02:00
|
|
|
{
|
2011-02-19 00:41:27 +01:00
|
|
|
PROFILE("unbind framebuffer");
|
2006-03-26 19:36:33 +02:00
|
|
|
pglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
|
|
|
}
|
2006-01-07 02:04:26 +01:00
|
|
|
|
2006-03-26 19:36:33 +02:00
|
|
|
glViewport(0, 0, g_Renderer.GetWidth(), g_Renderer.GetHeight());
|
|
|
|
|
2011-04-07 00:08:07 +02:00
|
|
|
glColorMask(1,1,1,1);
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Retrieve the texture handle and texture matrix for shadowing
|
2006-03-26 01:54:20 +01:00
|
|
|
GLuint ShadowMap::GetTexture() const
|
2006-01-07 02:04:26 +01:00
|
|
|
{
|
|
|
|
return m->Texture;
|
|
|
|
}
|
|
|
|
|
2006-03-26 01:54:20 +01:00
|
|
|
const CMatrix3D& ShadowMap::GetTextureMatrix() const
|
2006-01-07 02:04:26 +01:00
|
|
|
{
|
|
|
|
return m->TextureMatrix;
|
|
|
|
}
|
2006-01-29 18:34:45 +01:00
|
|
|
|
|
|
|
|
2006-02-16 00:50:24 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Depth texture bits
|
|
|
|
int ShadowMap::GetDepthTextureBits() const
|
|
|
|
{
|
|
|
|
return m->DepthTextureBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShadowMap::SetDepthTextureBits(int bits)
|
|
|
|
{
|
|
|
|
if (bits != m->DepthTextureBits)
|
|
|
|
{
|
|
|
|
if (m->Texture)
|
|
|
|
{
|
|
|
|
glDeleteTextures(1, &m->Texture);
|
|
|
|
m->Texture = 0;
|
|
|
|
}
|
|
|
|
m->Width = m->Height = 0;
|
|
|
|
|
|
|
|
m->DepthTextureBits = bits;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-21 21:23:14 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Depth texture size
|
|
|
|
int ShadowMap::GetWidth() const
|
|
|
|
{
|
|
|
|
return m->Width;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ShadowMap::GetHeight() const
|
2011-07-13 01:48:05 +02:00
|
|
|
{
|
2012-05-21 21:23:14 +02:00
|
|
|
return m->Height;
|
2011-07-13 01:48:05 +02:00
|
|
|
}
|
2006-02-16 00:50:24 +01:00
|
|
|
|
2006-01-29 18:34:45 +01:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2012-03-19 22:10:14 +01:00
|
|
|
|
|
|
|
void ShadowMap::RenderDebugBounds()
|
2006-01-29 18:34:45 +01:00
|
|
|
{
|
2012-02-25 18:29:27 +01:00
|
|
|
CShaderTechniquePtr shaderTech = g_Renderer.GetShaderManager().LoadEffect("gui_solid");
|
2012-02-12 21:45:31 +01:00
|
|
|
shaderTech->BeginPass();
|
|
|
|
CShaderProgramPtr shader = shaderTech->GetShader();
|
2012-02-12 14:20:49 +01:00
|
|
|
|
2006-02-11 01:26:40 +01:00
|
|
|
glDepthMask(0);
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
|
|
|
|
// Render shadow bound
|
2012-02-12 14:20:49 +01:00
|
|
|
shader->Uniform("transform", g_Renderer.GetViewCamera().GetViewProjection() * m->InvLightTransform);
|
2006-01-29 18:34:45 +01:00
|
|
|
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2012-02-12 14:20:49 +01:00
|
|
|
shader->Uniform("color", 0.0f, 0.0f, 1.0f, 0.25f);
|
|
|
|
m->ShadowBound.Render(shader);
|
2006-01-29 18:34:45 +01:00
|
|
|
glDisable(GL_BLEND);
|
|
|
|
|
2012-02-12 14:20:49 +01:00
|
|
|
shader->Uniform("color", 0.0f, 0.0f, 1.0f, 1.0f);
|
|
|
|
m->ShadowBound.RenderOutline(shader);
|
2006-01-29 18:34:45 +01:00
|
|
|
|
2012-02-12 14:20:49 +01:00
|
|
|
// Draw a funny line/triangle direction indicator thing for unknown reasons
|
|
|
|
float shadowLineVerts[] = {
|
|
|
|
0.0, 0.0, 0.0,
|
|
|
|
0.0, 0.0, 50.0,
|
|
|
|
|
|
|
|
0.0, 0.0, 50.0,
|
|
|
|
50.0, 0.0, 50.0,
|
|
|
|
|
|
|
|
50.0, 0.0, 50.0,
|
|
|
|
0.0, 50.0, 50.0,
|
|
|
|
|
|
|
|
0.0, 50.0, 50.0,
|
|
|
|
0.0, 0.0, 50.0
|
|
|
|
};
|
|
|
|
shader->VertexPointer(3, GL_FLOAT, 0, shadowLineVerts);
|
|
|
|
shader->AssertPointersBound();
|
|
|
|
glDrawArrays(GL_LINES, 0, 8);
|
|
|
|
|
2012-02-12 21:45:31 +01:00
|
|
|
shaderTech->EndPass();
|
2006-02-11 01:26:40 +01:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
CMatrix3D InvTexTransform;
|
|
|
|
|
|
|
|
m->TextureMatrix.GetInverse(InvTexTransform);
|
|
|
|
|
|
|
|
// Render representative texture rectangle
|
|
|
|
glPushMatrix();
|
|
|
|
glMultMatrixf(&InvTexTransform._11);
|
|
|
|
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glColor4ub(255,0,0,64);
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glVertex3f(0.0, 0.0, 0.0);
|
|
|
|
glVertex3f(1.0, 0.0, 0.0);
|
|
|
|
glVertex3f(1.0, 1.0, 0.0);
|
|
|
|
glVertex3f(0.0, 1.0, 0.0);
|
|
|
|
glEnd();
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
|
|
|
glColor3ub(255,0,0);
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glVertex3f(0.0, 0.0, 0.0);
|
|
|
|
glVertex3f(1.0, 0.0, 0.0);
|
|
|
|
glVertex3f(1.0, 1.0, 0.0);
|
|
|
|
glVertex3f(0.0, 1.0, 0.0);
|
|
|
|
glEnd();
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
|
|
glPopMatrix();
|
|
|
|
#endif
|
|
|
|
|
2012-03-19 22:10:14 +01:00
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glDepthMask(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShadowMap::RenderDebugTexture()
|
|
|
|
{
|
|
|
|
glDepthMask(0);
|
2006-02-11 01:26:40 +01:00
|
|
|
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
2012-03-19 22:10:14 +01:00
|
|
|
|
|
|
|
#if !CONFIG2_GLES
|
2006-02-11 01:26:40 +01:00
|
|
|
g_Renderer.BindTexture(0, m->Texture);
|
2011-04-07 00:08:07 +02:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
|
2012-03-19 22:10:14 +01:00
|
|
|
#endif
|
2006-02-11 01:26:40 +01:00
|
|
|
|
2012-03-19 22:10:14 +01:00
|
|
|
CShaderTechniquePtr texTech = g_Renderer.GetShaderManager().LoadEffect("gui_basic");
|
|
|
|
texTech->BeginPass();
|
|
|
|
CShaderProgramPtr texShader = texTech->GetShader();
|
2006-01-29 18:34:45 +01:00
|
|
|
|
2012-03-19 22:10:14 +01:00
|
|
|
texShader->Uniform("transform", GetDefaultGuiMatrix());
|
|
|
|
texShader->BindTexture("tex", m->Texture);
|
|
|
|
|
|
|
|
float s = 256.f;
|
|
|
|
float boxVerts[] = {
|
|
|
|
0,0, 0,s, s,0,
|
|
|
|
s,0, 0,s, s,s
|
|
|
|
};
|
|
|
|
float boxUV[] = {
|
|
|
|
0,0, 0,1, 1,0,
|
|
|
|
1,0, 0,1, 1,1
|
|
|
|
};
|
|
|
|
|
|
|
|
texShader->VertexPointer(2, GL_FLOAT, 0, boxVerts);
|
|
|
|
texShader->TexCoordPointer(GL_TEXTURE0, 2, GL_FLOAT, 0, boxUV);
|
|
|
|
texShader->AssertPointersBound();
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
|
|
|
|
|
texTech->EndPass();
|
|
|
|
|
|
|
|
#if !CONFIG2_GLES
|
|
|
|
g_Renderer.BindTexture(0, m->Texture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
|
2012-02-13 17:41:17 +01:00
|
|
|
#endif
|
2006-02-11 01:26:40 +01:00
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthMask(1);
|
2006-01-29 18:34:45 +01:00
|
|
|
}
|