Merge branch 'NDK-ShadowMapping'
Former-commit-id: 0fcd92b113069d2ee1edc74cf9be471cd6d97267
This commit is contained in:
217
src/Nazara/Graphics/DepthRenderQueue.cpp
Normal file
217
src/Nazara/Graphics/DepthRenderQueue.cpp
Normal file
@@ -0,0 +1,217 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Graphics/DepthRenderQueue.hpp>
|
||||
#include <Nazara/Graphics/AbstractViewer.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
#include <Nazara/Graphics/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
DepthRenderQueue::DepthRenderQueue()
|
||||
{
|
||||
// Material
|
||||
m_baseMaterial = Material::New();
|
||||
m_baseMaterial->Enable(RendererParameter_ColorWrite, false);
|
||||
m_baseMaterial->Enable(RendererParameter_FaceCulling, false);
|
||||
//m_baseMaterial->SetFaceCulling(FaceSide_Front);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddBillboard(int renderOrder, const Material* material, const Vector3f& position, const Vector2f& size, const Vector2f& sinCos, const Color& color)
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
NazaraUnused(renderOrder);
|
||||
|
||||
if (!IsMaterialSuitable(material))
|
||||
return;
|
||||
|
||||
if (material->HasDepthMaterial())
|
||||
material = material->GetDepthMaterial();
|
||||
else
|
||||
material = m_baseMaterial;
|
||||
|
||||
ForwardRenderQueue::AddBillboard(0, material, position, size, sinCos, color);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const Vector2f> sinCosPtr, SparsePtr<const Color> colorPtr)
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
NazaraUnused(renderOrder);
|
||||
|
||||
if (!IsMaterialSuitable(material))
|
||||
return;
|
||||
|
||||
if (material->HasDepthMaterial())
|
||||
material = material->GetDepthMaterial();
|
||||
else
|
||||
material = m_baseMaterial;
|
||||
|
||||
ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, sinCosPtr, colorPtr);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const Vector2f> sinCosPtr, SparsePtr<const float> alphaPtr)
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
NazaraUnused(renderOrder);
|
||||
|
||||
if (!IsMaterialSuitable(material))
|
||||
return;
|
||||
|
||||
if (material->HasDepthMaterial())
|
||||
material = material->GetDepthMaterial();
|
||||
else
|
||||
material = m_baseMaterial;
|
||||
|
||||
ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, sinCosPtr, alphaPtr);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const Color> colorPtr)
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
NazaraUnused(renderOrder);
|
||||
|
||||
if (!IsMaterialSuitable(material))
|
||||
return;
|
||||
|
||||
if (material->HasDepthMaterial())
|
||||
material = material->GetDepthMaterial();
|
||||
else
|
||||
material = m_baseMaterial;
|
||||
|
||||
ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, anglePtr, colorPtr);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const float> alphaPtr)
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
NazaraUnused(renderOrder);
|
||||
|
||||
if (!IsMaterialSuitable(material))
|
||||
return;
|
||||
|
||||
if (material->HasDepthMaterial())
|
||||
material = material->GetDepthMaterial();
|
||||
else
|
||||
material = m_baseMaterial;
|
||||
|
||||
ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, anglePtr, alphaPtr);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const Vector2f> sinCosPtr, SparsePtr<const Color> colorPtr)
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
NazaraUnused(renderOrder);
|
||||
|
||||
if (!IsMaterialSuitable(material))
|
||||
return;
|
||||
|
||||
if (material->HasDepthMaterial())
|
||||
material = material->GetDepthMaterial();
|
||||
else
|
||||
material = m_baseMaterial;
|
||||
|
||||
ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, sinCosPtr, colorPtr);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const Vector2f> sinCosPtr, SparsePtr<const float> alphaPtr)
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
NazaraUnused(renderOrder);
|
||||
|
||||
if (!IsMaterialSuitable(material))
|
||||
return;
|
||||
|
||||
if (material->HasDepthMaterial())
|
||||
material = material->GetDepthMaterial();
|
||||
else
|
||||
material = m_baseMaterial;
|
||||
|
||||
ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, sinCosPtr, alphaPtr);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const Color> colorPtr)
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
NazaraUnused(renderOrder);
|
||||
|
||||
if (!IsMaterialSuitable(material))
|
||||
return;
|
||||
|
||||
if (material->HasDepthMaterial())
|
||||
material = material->GetDepthMaterial();
|
||||
else
|
||||
material = m_baseMaterial;
|
||||
|
||||
ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, anglePtr, colorPtr);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const float> anglePtr, SparsePtr<const float> alphaPtr)
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
NazaraUnused(renderOrder);
|
||||
|
||||
if (!IsMaterialSuitable(material))
|
||||
return;
|
||||
|
||||
if (material->HasDepthMaterial())
|
||||
material = material->GetDepthMaterial();
|
||||
else
|
||||
material = m_baseMaterial;
|
||||
|
||||
ForwardRenderQueue::AddBillboards(0, material, count, positionPtr, sizePtr, anglePtr, alphaPtr);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddDirectionalLight(const DirectionalLight& light)
|
||||
{
|
||||
NazaraAssert(false, "Depth render queue doesn't handle lights");
|
||||
NazaraUnused(light);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddMesh(int renderOrder, const Material* material, const MeshData& meshData, const Boxf& meshAABB, const Matrix4f& transformMatrix)
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
NazaraUnused(renderOrder);
|
||||
NazaraUnused(meshAABB);
|
||||
|
||||
if (!IsMaterialSuitable(material))
|
||||
return;
|
||||
|
||||
if (material->HasDepthMaterial())
|
||||
material = material->GetDepthMaterial();
|
||||
else
|
||||
material = m_baseMaterial;
|
||||
|
||||
ForwardRenderQueue::AddMesh(0, material, meshData, meshAABB, transformMatrix);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddPointLight(const PointLight& light)
|
||||
{
|
||||
NazaraAssert(false, "Depth render queue doesn't handle lights");
|
||||
NazaraUnused(light);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddSpotLight(const SpotLight& light)
|
||||
{
|
||||
NazaraAssert(false, "Depth render queue doesn't handle lights");
|
||||
NazaraUnused(light);
|
||||
}
|
||||
|
||||
void DepthRenderQueue::AddSprites(int renderOrder, const Material* material, const VertexStruct_XYZ_Color_UV* vertices, unsigned int spriteCount, const Texture* overlay)
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
NazaraUnused(renderOrder);
|
||||
NazaraUnused(overlay);
|
||||
|
||||
if (!IsMaterialSuitable(material))
|
||||
return;
|
||||
|
||||
if (material->HasDepthMaterial())
|
||||
material = material->GetDepthMaterial();
|
||||
else
|
||||
material = m_baseMaterial;
|
||||
|
||||
ForwardRenderQueue::AddSprites(0, material, vertices, spriteCount, overlay);
|
||||
}
|
||||
}
|
||||
|
||||
539
src/Nazara/Graphics/DepthRenderTechnique.cpp
Normal file
539
src/Nazara/Graphics/DepthRenderTechnique.cpp
Normal file
@@ -0,0 +1,539 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Graphics/DepthRenderTechnique.hpp>
|
||||
#include <Nazara/Core/ErrorFlags.hpp>
|
||||
#include <Nazara/Core/OffsetOf.hpp>
|
||||
#include <Nazara/Graphics/AbstractBackground.hpp>
|
||||
#include <Nazara/Graphics/AbstractViewer.hpp>
|
||||
#include <Nazara/Graphics/Drawable.hpp>
|
||||
#include <Nazara/Graphics/Light.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
#include <Nazara/Graphics/Sprite.hpp>
|
||||
#include <Nazara/Renderer/Config.hpp>
|
||||
#include <Nazara/Renderer/Renderer.hpp>
|
||||
#include <Nazara/Utility/BufferMapper.hpp>
|
||||
#include <Nazara/Utility/StaticMesh.hpp>
|
||||
#include <Nazara/Utility/VertexStruct.hpp>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <Nazara/Graphics/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace
|
||||
{
|
||||
struct BillboardPoint
|
||||
{
|
||||
Color color;
|
||||
Vector3f position;
|
||||
Vector2f size;
|
||||
Vector2f sinCos; // must follow `size` (both will be sent as a Vector4f)
|
||||
Vector2f uv;
|
||||
};
|
||||
|
||||
unsigned int s_maxQuads = std::numeric_limits<UInt16>::max() / 6;
|
||||
unsigned int s_vertexBufferSize = 4 * 1024 * 1024; // 4 MiB
|
||||
}
|
||||
|
||||
DepthRenderTechnique::DepthRenderTechnique() :
|
||||
m_vertexBuffer(BufferType_Vertex)
|
||||
{
|
||||
ErrorFlags flags(ErrorFlag_ThrowException, true);
|
||||
|
||||
m_vertexBuffer.Create(s_vertexBufferSize, DataStorage_Hardware, BufferUsage_Dynamic);
|
||||
|
||||
m_billboardPointBuffer.Reset(&s_billboardVertexDeclaration, &m_vertexBuffer);
|
||||
m_spriteBuffer.Reset(VertexDeclaration::Get(VertexLayout_XYZ_Color_UV), &m_vertexBuffer);
|
||||
}
|
||||
|
||||
void DepthRenderTechnique::Clear(const SceneData& sceneData) const
|
||||
{
|
||||
Renderer::Enable(RendererParameter_DepthBuffer, true);
|
||||
Renderer::Enable(RendererParameter_DepthWrite, true);
|
||||
Renderer::Clear(RendererBuffer_Depth);
|
||||
|
||||
// Just in case the background does render depth
|
||||
//if (sceneData.background)
|
||||
// sceneData.background->Draw(sceneData.viewer);
|
||||
}
|
||||
|
||||
bool DepthRenderTechnique::Draw(const SceneData& sceneData) const
|
||||
{
|
||||
for (auto& pair : m_renderQueue.layers)
|
||||
{
|
||||
ForwardRenderQueue::Layer& layer = pair.second;
|
||||
|
||||
if (!layer.opaqueModels.empty())
|
||||
DrawOpaqueModels(sceneData, layer);
|
||||
|
||||
if (!layer.basicSprites.empty())
|
||||
DrawBasicSprites(sceneData, layer);
|
||||
|
||||
if (!layer.billboards.empty())
|
||||
DrawBillboards(sceneData, layer);
|
||||
|
||||
for (const Drawable* drawable : layer.otherDrawables)
|
||||
drawable->Draw();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
AbstractRenderQueue* DepthRenderTechnique::GetRenderQueue()
|
||||
{
|
||||
return &m_renderQueue;
|
||||
}
|
||||
|
||||
RenderTechniqueType DepthRenderTechnique::GetType() const
|
||||
{
|
||||
return RenderTechniqueType_Depth;
|
||||
}
|
||||
|
||||
bool DepthRenderTechnique::Initialize()
|
||||
{
|
||||
try
|
||||
{
|
||||
ErrorFlags flags(ErrorFlag_ThrowException, true);
|
||||
|
||||
s_quadIndexBuffer.Reset(false, s_maxQuads * 6, DataStorage_Hardware, BufferUsage_Static);
|
||||
|
||||
BufferMapper<IndexBuffer> mapper(s_quadIndexBuffer, BufferAccess_WriteOnly);
|
||||
UInt16* indices = static_cast<UInt16*>(mapper.GetPointer());
|
||||
|
||||
for (unsigned int i = 0; i < s_maxQuads; ++i)
|
||||
{
|
||||
*indices++ = i * 4 + 0;
|
||||
*indices++ = i * 4 + 2;
|
||||
*indices++ = i * 4 + 1;
|
||||
|
||||
*indices++ = i * 4 + 2;
|
||||
*indices++ = i * 4 + 3;
|
||||
*indices++ = i * 4 + 1;
|
||||
}
|
||||
|
||||
mapper.Unmap(); // Inutile de garder le buffer ouvert plus longtemps
|
||||
|
||||
// Quad buffer (utilisé pour l'instancing de billboard et de sprites)
|
||||
//Note: Les UV sont calculés dans le shader
|
||||
s_quadVertexBuffer.Reset(VertexDeclaration::Get(VertexLayout_XY), 4, DataStorage_Hardware, BufferUsage_Static);
|
||||
|
||||
float vertices[2 * 4] = {
|
||||
-0.5f, -0.5f,
|
||||
0.5f, -0.5f,
|
||||
-0.5f, 0.5f,
|
||||
0.5f, 0.5f,
|
||||
};
|
||||
|
||||
s_quadVertexBuffer.FillRaw(vertices, 0, sizeof(vertices));
|
||||
|
||||
// Déclaration lors du rendu des billboards par sommet
|
||||
s_billboardVertexDeclaration.EnableComponent(VertexComponent_Color, ComponentType_Color, NazaraOffsetOf(BillboardPoint, color));
|
||||
s_billboardVertexDeclaration.EnableComponent(VertexComponent_Position, ComponentType_Float3, NazaraOffsetOf(BillboardPoint, position));
|
||||
s_billboardVertexDeclaration.EnableComponent(VertexComponent_TexCoord, ComponentType_Float2, NazaraOffsetOf(BillboardPoint, uv));
|
||||
s_billboardVertexDeclaration.EnableComponent(VertexComponent_Userdata0, ComponentType_Float4, NazaraOffsetOf(BillboardPoint, size)); // Englobe sincos
|
||||
|
||||
// Declaration utilisée lors du rendu des billboards par instancing
|
||||
// L'avantage ici est la copie directe (std::memcpy) des données de la RenderQueue vers le buffer GPU
|
||||
s_billboardInstanceDeclaration.EnableComponent(VertexComponent_InstanceData0, ComponentType_Float3, NazaraOffsetOf(ForwardRenderQueue::BillboardData, center));
|
||||
s_billboardInstanceDeclaration.EnableComponent(VertexComponent_InstanceData1, ComponentType_Float4, NazaraOffsetOf(ForwardRenderQueue::BillboardData, size)); // Englobe sincos
|
||||
s_billboardInstanceDeclaration.EnableComponent(VertexComponent_InstanceData2, ComponentType_Color, NazaraOffsetOf(ForwardRenderQueue::BillboardData, color));
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
NazaraError("Failed to initialise: " + String(e.what()));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void DepthRenderTechnique::Uninitialize()
|
||||
{
|
||||
s_quadIndexBuffer.Reset();
|
||||
s_quadVertexBuffer.Reset();
|
||||
}
|
||||
|
||||
void DepthRenderTechnique::DrawBasicSprites(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const
|
||||
{
|
||||
const Shader* lastShader = nullptr;
|
||||
const ShaderUniforms* shaderUniforms = nullptr;
|
||||
|
||||
Renderer::SetIndexBuffer(&s_quadIndexBuffer);
|
||||
Renderer::SetMatrix(MatrixType_World, Matrix4f::Identity());
|
||||
Renderer::SetVertexBuffer(&m_spriteBuffer);
|
||||
|
||||
for (auto& matIt : layer.basicSprites)
|
||||
{
|
||||
const Material* material = matIt.first;
|
||||
auto& matEntry = matIt.second;
|
||||
|
||||
if (matEntry.enabled)
|
||||
{
|
||||
auto& overlayMap = matEntry.overlayMap;
|
||||
for (auto& overlayIt : overlayMap)
|
||||
{
|
||||
const Texture* overlay = overlayIt.first;
|
||||
auto& spriteChainVector = overlayIt.second.spriteChains;
|
||||
|
||||
unsigned int spriteChainCount = spriteChainVector.size();
|
||||
if (spriteChainCount > 0)
|
||||
{
|
||||
// On commence par appliquer du matériau (et récupérer le shader ainsi activé)
|
||||
UInt32 flags = 0;
|
||||
if (overlay)
|
||||
flags |= ShaderFlags_TextureOverlay;
|
||||
|
||||
UInt8 overlayUnit;
|
||||
const Shader* shader = material->Apply(flags, 0, &overlayUnit);
|
||||
|
||||
if (overlay)
|
||||
{
|
||||
overlayUnit++;
|
||||
Renderer::SetTexture(overlayUnit, overlay);
|
||||
Renderer::SetTextureSampler(overlayUnit, material->GetDiffuseSampler());
|
||||
}
|
||||
|
||||
// Les uniformes sont conservées au sein d'un programme, inutile de les renvoyer tant qu'il ne change pas
|
||||
if (shader != lastShader)
|
||||
{
|
||||
// Index des uniformes dans le shader
|
||||
shaderUniforms = GetShaderUniforms(shader);
|
||||
|
||||
// Overlay
|
||||
shader->SendInteger(shaderUniforms->textureOverlay, overlayUnit);
|
||||
// Position de la caméra
|
||||
shader->SendVector(shaderUniforms->eyePosition, Renderer::GetMatrix(MatrixType_ViewProj).GetTranslation());
|
||||
|
||||
lastShader = shader;
|
||||
}
|
||||
|
||||
unsigned int spriteChain = 0; // Quelle chaîne de sprite traitons-nous
|
||||
unsigned int spriteChainOffset = 0; // À quel offset dans la dernière chaîne nous sommes-nous arrêtés
|
||||
|
||||
do
|
||||
{
|
||||
// On ouvre le buffer en écriture
|
||||
BufferMapper<VertexBuffer> vertexMapper(m_spriteBuffer, BufferAccess_DiscardAndWrite);
|
||||
VertexStruct_XYZ_Color_UV* vertices = reinterpret_cast<VertexStruct_XYZ_Color_UV*>(vertexMapper.GetPointer());
|
||||
|
||||
unsigned int spriteCount = 0;
|
||||
unsigned int maxSpriteCount = std::min(s_maxQuads, m_spriteBuffer.GetVertexCount()/4);
|
||||
|
||||
do
|
||||
{
|
||||
ForwardRenderQueue::SpriteChain_XYZ_Color_UV& currentChain = spriteChainVector[spriteChain];
|
||||
unsigned int count = std::min(maxSpriteCount - spriteCount, currentChain.spriteCount - spriteChainOffset);
|
||||
|
||||
std::memcpy(vertices, currentChain.vertices + spriteChainOffset*4, 4*count*sizeof(VertexStruct_XYZ_Color_UV));
|
||||
vertices += count*4;
|
||||
|
||||
spriteCount += count;
|
||||
spriteChainOffset += count;
|
||||
|
||||
// Avons-nous traité la chaîne entière ?
|
||||
if (spriteChainOffset == currentChain.spriteCount)
|
||||
{
|
||||
spriteChain++;
|
||||
spriteChainOffset = 0;
|
||||
}
|
||||
}
|
||||
while (spriteCount < maxSpriteCount && spriteChain < spriteChainCount);
|
||||
|
||||
vertexMapper.Unmap();
|
||||
|
||||
Renderer::DrawIndexedPrimitives(PrimitiveMode_TriangleList, 0, spriteCount*6);
|
||||
}
|
||||
while (spriteChain < spriteChainCount);
|
||||
|
||||
spriteChainVector.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// On remet à zéro
|
||||
matEntry.enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DepthRenderTechnique::DrawBillboards(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const
|
||||
{
|
||||
const Shader* lastShader = nullptr;
|
||||
const ShaderUniforms* shaderUniforms = nullptr;
|
||||
|
||||
if (Renderer::HasCapability(RendererCap_Instancing))
|
||||
{
|
||||
VertexBuffer* instanceBuffer = Renderer::GetInstanceBuffer();
|
||||
instanceBuffer->SetVertexDeclaration(&s_billboardInstanceDeclaration);
|
||||
|
||||
Renderer::SetVertexBuffer(&s_quadVertexBuffer);
|
||||
|
||||
for (auto& matIt : layer.billboards)
|
||||
{
|
||||
const Material* material = matIt.first;
|
||||
auto& entry = matIt.second;
|
||||
auto& billboardVector = entry.billboards;
|
||||
|
||||
unsigned int billboardCount = billboardVector.size();
|
||||
if (billboardCount > 0)
|
||||
{
|
||||
// On commence par appliquer du matériau (et récupérer le shader ainsi activé)
|
||||
const Shader* shader = material->Apply(ShaderFlags_Billboard | ShaderFlags_Instancing | ShaderFlags_VertexColor);
|
||||
|
||||
// Les uniformes sont conservées au sein d'un programme, inutile de les renvoyer tant qu'il ne change pas
|
||||
if (shader != lastShader)
|
||||
{
|
||||
// Index des uniformes dans le shader
|
||||
shaderUniforms = GetShaderUniforms(shader);
|
||||
|
||||
// Position de la caméra
|
||||
shader->SendVector(shaderUniforms->eyePosition, Renderer::GetMatrix(MatrixType_ViewProj).GetTranslation());
|
||||
|
||||
lastShader = shader;
|
||||
}
|
||||
|
||||
const ForwardRenderQueue::BillboardData* data = &billboardVector[0];
|
||||
unsigned int maxBillboardPerDraw = instanceBuffer->GetVertexCount();
|
||||
do
|
||||
{
|
||||
unsigned int renderedBillboardCount = std::min(billboardCount, maxBillboardPerDraw);
|
||||
billboardCount -= renderedBillboardCount;
|
||||
|
||||
instanceBuffer->Fill(data, 0, renderedBillboardCount, true);
|
||||
data += renderedBillboardCount;
|
||||
|
||||
Renderer::DrawPrimitivesInstanced(renderedBillboardCount, PrimitiveMode_TriangleStrip, 0, 4);
|
||||
}
|
||||
while (billboardCount > 0);
|
||||
|
||||
billboardVector.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Renderer::SetIndexBuffer(&s_quadIndexBuffer);
|
||||
Renderer::SetVertexBuffer(&m_billboardPointBuffer);
|
||||
|
||||
for (auto& matIt : layer.billboards)
|
||||
{
|
||||
const Material* material = matIt.first;
|
||||
auto& entry = matIt.second;
|
||||
auto& billboardVector = entry.billboards;
|
||||
|
||||
unsigned int billboardCount = billboardVector.size();
|
||||
if (billboardCount > 0)
|
||||
{
|
||||
// On commence par appliquer du matériau (et récupérer le shader ainsi activé)
|
||||
const Shader* shader = material->Apply(ShaderFlags_Billboard | ShaderFlags_VertexColor);
|
||||
|
||||
// Les uniformes sont conservées au sein d'un programme, inutile de les renvoyer tant qu'il ne change pas
|
||||
if (shader != lastShader)
|
||||
{
|
||||
// Index des uniformes dans le shader
|
||||
shaderUniforms = GetShaderUniforms(shader);
|
||||
|
||||
// Position de la caméra
|
||||
shader->SendVector(shaderUniforms->eyePosition, Renderer::GetMatrix(MatrixType_ViewProj).GetTranslation());
|
||||
|
||||
lastShader = shader;
|
||||
}
|
||||
|
||||
const ForwardRenderQueue::BillboardData* data = &billboardVector[0];
|
||||
unsigned int maxBillboardPerDraw = std::min(s_maxQuads, m_billboardPointBuffer.GetVertexCount()/4);
|
||||
|
||||
do
|
||||
{
|
||||
unsigned int renderedBillboardCount = std::min(billboardCount, maxBillboardPerDraw);
|
||||
billboardCount -= renderedBillboardCount;
|
||||
|
||||
BufferMapper<VertexBuffer> vertexMapper(m_billboardPointBuffer, BufferAccess_DiscardAndWrite, 0, renderedBillboardCount*4);
|
||||
BillboardPoint* vertices = reinterpret_cast<BillboardPoint*>(vertexMapper.GetPointer());
|
||||
|
||||
for (unsigned int i = 0; i < renderedBillboardCount; ++i)
|
||||
{
|
||||
const ForwardRenderQueue::BillboardData& billboard = *data++;
|
||||
|
||||
vertices->color = billboard.color;
|
||||
vertices->position = billboard.center;
|
||||
vertices->sinCos = billboard.sinCos;
|
||||
vertices->size = billboard.size;
|
||||
vertices->uv.Set(0.f, 1.f);
|
||||
vertices++;
|
||||
|
||||
vertices->color = billboard.color;
|
||||
vertices->position = billboard.center;
|
||||
vertices->sinCos = billboard.sinCos;
|
||||
vertices->size = billboard.size;
|
||||
vertices->uv.Set(1.f, 1.f);
|
||||
vertices++;
|
||||
|
||||
vertices->color = billboard.color;
|
||||
vertices->position = billboard.center;
|
||||
vertices->sinCos = billboard.sinCos;
|
||||
vertices->size = billboard.size;
|
||||
vertices->uv.Set(0.f, 0.f);
|
||||
vertices++;
|
||||
|
||||
vertices->color = billboard.color;
|
||||
vertices->position = billboard.center;
|
||||
vertices->sinCos = billboard.sinCos;
|
||||
vertices->size = billboard.size;
|
||||
vertices->uv.Set(1.f, 0.f);
|
||||
vertices++;
|
||||
}
|
||||
|
||||
vertexMapper.Unmap();
|
||||
|
||||
Renderer::DrawIndexedPrimitives(PrimitiveMode_TriangleList, 0, renderedBillboardCount*6);
|
||||
}
|
||||
while (billboardCount > 0);
|
||||
|
||||
billboardVector.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DepthRenderTechnique::DrawOpaqueModels(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const
|
||||
{
|
||||
const Shader* lastShader = nullptr;
|
||||
const ShaderUniforms* shaderUniforms = nullptr;
|
||||
|
||||
for (auto& matIt : layer.opaqueModels)
|
||||
{
|
||||
auto& matEntry = matIt.second;
|
||||
|
||||
if (matEntry.enabled)
|
||||
{
|
||||
ForwardRenderQueue::MeshInstanceContainer& meshInstances = matEntry.meshMap;
|
||||
|
||||
if (!meshInstances.empty())
|
||||
{
|
||||
const Material* material = matIt.first;
|
||||
|
||||
bool instancing = m_instancingEnabled && matEntry.instancingEnabled;
|
||||
|
||||
// On commence par appliquer du matériau (et récupérer le shader ainsi activé)
|
||||
UInt8 freeTextureUnit;
|
||||
const Shader* shader = material->Apply((instancing) ? ShaderFlags_Instancing : 0, 0, &freeTextureUnit);
|
||||
|
||||
// Les uniformes sont conservées au sein d'un programme, inutile de les renvoyer tant qu'il ne change pas
|
||||
if (shader != lastShader)
|
||||
{
|
||||
// Index des uniformes dans le shader
|
||||
shaderUniforms = GetShaderUniforms(shader);
|
||||
lastShader = shader;
|
||||
}
|
||||
|
||||
// Meshes
|
||||
for (auto& meshIt : meshInstances)
|
||||
{
|
||||
const MeshData& meshData = meshIt.first;
|
||||
auto& meshEntry = meshIt.second;
|
||||
|
||||
const Spheref& squaredBoundingSphere = meshEntry.squaredBoundingSphere;
|
||||
std::vector<Matrix4f>& instances = meshEntry.instances;
|
||||
|
||||
if (!instances.empty())
|
||||
{
|
||||
const IndexBuffer* indexBuffer = meshData.indexBuffer;
|
||||
const VertexBuffer* vertexBuffer = meshData.vertexBuffer;
|
||||
|
||||
// Gestion du draw call avant la boucle de rendu
|
||||
Renderer::DrawCall drawFunc;
|
||||
Renderer::DrawCallInstanced instancedDrawFunc;
|
||||
unsigned int indexCount;
|
||||
|
||||
if (indexBuffer)
|
||||
{
|
||||
drawFunc = Renderer::DrawIndexedPrimitives;
|
||||
instancedDrawFunc = Renderer::DrawIndexedPrimitivesInstanced;
|
||||
indexCount = indexBuffer->GetIndexCount();
|
||||
}
|
||||
else
|
||||
{
|
||||
drawFunc = Renderer::DrawPrimitives;
|
||||
instancedDrawFunc = Renderer::DrawPrimitivesInstanced;
|
||||
indexCount = vertexBuffer->GetVertexCount();
|
||||
}
|
||||
|
||||
Renderer::SetIndexBuffer(indexBuffer);
|
||||
Renderer::SetVertexBuffer(vertexBuffer);
|
||||
|
||||
if (instancing)
|
||||
{
|
||||
// On calcule le nombre d'instances que l'on pourra afficher cette fois-ci (Selon la taille du buffer d'instancing)
|
||||
VertexBuffer* instanceBuffer = Renderer::GetInstanceBuffer();
|
||||
instanceBuffer->SetVertexDeclaration(VertexDeclaration::Get(VertexLayout_Matrix4));
|
||||
|
||||
const Matrix4f* instanceMatrices = &instances[0];
|
||||
unsigned int instanceCount = instances.size();
|
||||
unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); // Le nombre maximum d'instances en une fois
|
||||
|
||||
while (instanceCount > 0)
|
||||
{
|
||||
// On calcule le nombre d'instances que l'on pourra afficher cette fois-ci (Selon la taille du buffer d'instancing)
|
||||
unsigned int renderedInstanceCount = std::min(instanceCount, maxInstanceCount);
|
||||
instanceCount -= renderedInstanceCount;
|
||||
|
||||
// On remplit l'instancing buffer avec nos matrices world
|
||||
instanceBuffer->Fill(instanceMatrices, 0, renderedInstanceCount, true);
|
||||
instanceMatrices += renderedInstanceCount;
|
||||
|
||||
// Et on affiche
|
||||
instancedDrawFunc(renderedInstanceCount, meshData.primitiveMode, 0, indexCount);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Sans instancing, on doit effectuer un draw call pour chaque instance
|
||||
// Cela reste néanmoins plus rapide que l'instancing en dessous d'un certain nombre d'instances
|
||||
// À cause du temps de modification du buffer d'instancing
|
||||
for (const Matrix4f& matrix : instances)
|
||||
{
|
||||
Renderer::SetMatrix(MatrixType_World, matrix);
|
||||
drawFunc(meshData.primitiveMode, 0, indexCount);
|
||||
}
|
||||
}
|
||||
instances.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Et on remet à zéro les données
|
||||
matEntry.enabled = false;
|
||||
matEntry.instancingEnabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const DepthRenderTechnique::ShaderUniforms* DepthRenderTechnique::GetShaderUniforms(const Shader* shader) const
|
||||
{
|
||||
auto it = m_shaderUniforms.find(shader);
|
||||
if (it == m_shaderUniforms.end())
|
||||
{
|
||||
ShaderUniforms uniforms;
|
||||
uniforms.shaderReleaseSlot.Connect(shader->OnShaderRelease, this, &DepthRenderTechnique::OnShaderInvalidated);
|
||||
uniforms.shaderUniformInvalidatedSlot.Connect(shader->OnShaderUniformInvalidated, this, &DepthRenderTechnique::OnShaderInvalidated);
|
||||
|
||||
uniforms.eyePosition = shader->GetUniformLocation("EyePosition");
|
||||
uniforms.textureOverlay = shader->GetUniformLocation("TextureOverlay");
|
||||
|
||||
it = m_shaderUniforms.emplace(shader, std::move(uniforms)).first;
|
||||
}
|
||||
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
void DepthRenderTechnique::OnShaderInvalidated(const Shader* shader) const
|
||||
{
|
||||
m_shaderUniforms.erase(shader);
|
||||
}
|
||||
|
||||
IndexBuffer DepthRenderTechnique::s_quadIndexBuffer;
|
||||
VertexBuffer DepthRenderTechnique::s_quadVertexBuffer;
|
||||
VertexDeclaration DepthRenderTechnique::s_billboardInstanceDeclaration;
|
||||
VertexDeclaration DepthRenderTechnique::s_billboardVertexDeclaration;
|
||||
}
|
||||
@@ -77,7 +77,7 @@ namespace Nz
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
|
||||
///DOC: sinCosPtr et alphaPtr peuvent être nuls, ils seont remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
///DOC: sinCosPtr et alphaPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
Vector2f defaultSinCos(0.f, 1.f); // sin(0) = 0, cos(0) = 1
|
||||
|
||||
if (!sinCosPtr)
|
||||
@@ -120,7 +120,7 @@ namespace Nz
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
|
||||
///DOC: sinCosPtr et colorPtr peuvent être nuls, ils seont remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
///DOC: sinCosPtr et colorPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
float defaultRotation = 0.f;
|
||||
|
||||
if (!anglePtr)
|
||||
@@ -165,7 +165,7 @@ namespace Nz
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
|
||||
///DOC: sinCosPtr et alphaPtr peuvent être nuls, ils seont remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
///DOC: sinCosPtr et alphaPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
float defaultRotation = 0.f;
|
||||
|
||||
if (!anglePtr)
|
||||
@@ -212,7 +212,7 @@ namespace Nz
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
|
||||
///DOC: sinCosPtr et colorPtr peuvent être nuls, ils seont remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
///DOC: sinCosPtr et colorPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
Vector2f defaultSinCos(0.f, 1.f); // sin(0) = 0, cos(0) = 1
|
||||
|
||||
if (!sinCosPtr)
|
||||
@@ -253,7 +253,7 @@ namespace Nz
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
|
||||
///DOC: sinCosPtr et alphaPtr peuvent être nuls, ils seont remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
///DOC: sinCosPtr et alphaPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
Vector2f defaultSinCos(0.f, 1.f); // sin(0) = 0, cos(0) = 1
|
||||
|
||||
if (!sinCosPtr)
|
||||
@@ -296,7 +296,7 @@ namespace Nz
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
|
||||
///DOC: sinCosPtr et colorPtr peuvent être nuls, ils seont remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
///DOC: sinCosPtr et colorPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
float defaultRotation = 0.f;
|
||||
|
||||
if (!anglePtr)
|
||||
@@ -341,7 +341,7 @@ namespace Nz
|
||||
{
|
||||
NazaraAssert(material, "Invalid material");
|
||||
|
||||
///DOC: sinCosPtr et alphaPtr peuvent être nuls, ils seont remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
///DOC: sinCosPtr et alphaPtr peuvent être nuls, ils seront remplacés respectivement par Vector2f(0.f, 1.f) et Color::White
|
||||
float defaultRotation = 0.f;
|
||||
|
||||
if (!anglePtr)
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace Nz
|
||||
Color color;
|
||||
Vector3f position;
|
||||
Vector2f size;
|
||||
Vector2f sinCos; // doit suivre size
|
||||
Vector2f sinCos; // must follow `size` (both will be sent as a Vector4f)
|
||||
Vector2f uv;
|
||||
};
|
||||
|
||||
@@ -156,6 +156,9 @@ namespace Nz
|
||||
s_billboardInstanceDeclaration.EnableComponent(VertexComponent_InstanceData0, ComponentType_Float3, NazaraOffsetOf(ForwardRenderQueue::BillboardData, center));
|
||||
s_billboardInstanceDeclaration.EnableComponent(VertexComponent_InstanceData1, ComponentType_Float4, NazaraOffsetOf(ForwardRenderQueue::BillboardData, size)); // Englobe sincos
|
||||
s_billboardInstanceDeclaration.EnableComponent(VertexComponent_InstanceData2, ComponentType_Color, NazaraOffsetOf(ForwardRenderQueue::BillboardData, color));
|
||||
|
||||
s_shadowSampler.SetFilterMode(SamplerFilter_Bilinear);
|
||||
s_shadowSampler.SetWrapMode(SamplerWrap_Clamp);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
@@ -487,7 +490,8 @@ namespace Nz
|
||||
bool instancing = m_instancingEnabled && (!material->IsLightingEnabled() || noPointSpotLight) && matEntry.instancingEnabled;
|
||||
|
||||
// On commence par appliquer du matériau (et récupérer le shader ainsi activé)
|
||||
const Shader* shader = material->Apply((instancing) ? ShaderFlags_Instancing : 0);
|
||||
UInt8 freeTextureUnit;
|
||||
const Shader* shader = material->Apply((instancing) ? ShaderFlags_Instancing : 0, 0, &freeTextureUnit);
|
||||
|
||||
// Les uniformes sont conservées au sein d'un programme, inutile de les renvoyer tant qu'il ne change pas
|
||||
if (shader != lastShader)
|
||||
@@ -571,7 +575,10 @@ namespace Nz
|
||||
|
||||
// Sends the uniforms
|
||||
for (unsigned int i = 0; i < NAZARA_GRAPHICS_MAX_LIGHT_PER_PASS; ++i)
|
||||
SendLightUniforms(shader, shaderUniforms->lightUniforms, lightIndex++, i*shaderUniforms->lightOffset);
|
||||
SendLightUniforms(shader, shaderUniforms->lightUniforms, lightIndex++, shaderUniforms->lightOffset*i, freeTextureUnit + i);
|
||||
|
||||
// Et on passe à l'affichage
|
||||
drawFunc(meshData.primitiveMode, 0, indexCount);
|
||||
}
|
||||
|
||||
const Matrix4f* instanceMatrices = &instances[0];
|
||||
@@ -630,7 +637,7 @@ namespace Nz
|
||||
|
||||
// Sends the light uniforms to the shader
|
||||
for (unsigned int i = 0; i < NAZARA_GRAPHICS_MAX_LIGHT_PER_PASS; ++i)
|
||||
SendLightUniforms(shader, shaderUniforms->lightUniforms, lightIndex++, shaderUniforms->lightOffset*i);
|
||||
SendLightUniforms(shader, shaderUniforms->lightUniforms, lightIndex++, shaderUniforms->lightOffset*i, freeTextureUnit + i);
|
||||
|
||||
// Et on passe à l'affichage
|
||||
drawFunc(meshData.primitiveMode, 0, indexCount);
|
||||
@@ -680,7 +687,8 @@ namespace Nz
|
||||
const Material* material = modelData.material;
|
||||
|
||||
// On commence par appliquer du matériau (et récupérer le shader ainsi activé)
|
||||
const Shader* shader = material->Apply();
|
||||
UInt8 freeTextureUnit;
|
||||
const Shader* shader = material->Apply(0, 0, &freeTextureUnit);
|
||||
|
||||
// Les uniformes sont conservées au sein d'un programme, inutile de les renvoyer tant qu'il ne change pas
|
||||
if (shader != lastShader)
|
||||
@@ -699,7 +707,7 @@ namespace Nz
|
||||
lightCount = std::min(m_renderQueue.directionalLights.size(), static_cast<decltype(m_renderQueue.directionalLights.size())>(NAZARA_GRAPHICS_MAX_LIGHT_PER_PASS));
|
||||
|
||||
for (unsigned int i = 0; i < lightCount; ++i)
|
||||
SendLightUniforms(shader, shaderUniforms->lightUniforms, i, shaderUniforms->lightOffset * i);
|
||||
SendLightUniforms(shader, shaderUniforms->lightUniforms, i, shaderUniforms->lightOffset * i, freeTextureUnit++);
|
||||
}
|
||||
|
||||
lastShader = shader;
|
||||
@@ -738,7 +746,7 @@ namespace Nz
|
||||
ChooseLights(Spheref(position, radius), false);
|
||||
|
||||
for (unsigned int i = lightCount; i < NAZARA_GRAPHICS_MAX_LIGHT_PER_PASS; ++i)
|
||||
SendLightUniforms(shader, shaderUniforms->lightUniforms, i, shaderUniforms->lightOffset*i);
|
||||
SendLightUniforms(shader, shaderUniforms->lightUniforms, i, shaderUniforms->lightOffset*i, freeTextureUnit++);
|
||||
}
|
||||
|
||||
Renderer::SetMatrix(MatrixType_World, matrix);
|
||||
@@ -770,9 +778,13 @@ namespace Nz
|
||||
uniforms.lightUniforms.locations.type = type0Location;
|
||||
uniforms.lightUniforms.locations.color = shader->GetUniformLocation("Lights[0].color");
|
||||
uniforms.lightUniforms.locations.factors = shader->GetUniformLocation("Lights[0].factors");
|
||||
uniforms.lightUniforms.locations.lightViewProjMatrix = shader->GetUniformLocation("LightViewProjMatrix[0]");
|
||||
uniforms.lightUniforms.locations.parameters1 = shader->GetUniformLocation("Lights[0].parameters1");
|
||||
uniforms.lightUniforms.locations.parameters2 = shader->GetUniformLocation("Lights[0].parameters2");
|
||||
uniforms.lightUniforms.locations.parameters3 = shader->GetUniformLocation("Lights[0].parameters3");
|
||||
uniforms.lightUniforms.locations.pointLightShadowMap = shader->GetUniformLocation("PointLightShadowMap[0]");
|
||||
uniforms.lightUniforms.locations.shadowMapping = shader->GetUniformLocation("Lights[0].shadowMapping");
|
||||
uniforms.lightUniforms.locations.directionalSpotLightShadowMap = shader->GetUniformLocation("DirectionalSpotLightShadowMap[0]");
|
||||
}
|
||||
else
|
||||
uniforms.hasLightUniforms = false;
|
||||
@@ -789,6 +801,7 @@ namespace Nz
|
||||
}
|
||||
|
||||
IndexBuffer ForwardRenderTechnique::s_quadIndexBuffer;
|
||||
TextureSampler ForwardRenderTechnique::s_shadowSampler;
|
||||
VertexBuffer ForwardRenderTechnique::s_quadVertexBuffer;
|
||||
VertexDeclaration ForwardRenderTechnique::s_billboardInstanceDeclaration;
|
||||
VertexDeclaration ForwardRenderTechnique::s_billboardVertexDeclaration;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <Nazara/Core/Log.hpp>
|
||||
#include <Nazara/Graphics/Config.hpp>
|
||||
#include <Nazara/Graphics/DeferredRenderTechnique.hpp>
|
||||
#include <Nazara/Graphics/DepthRenderTechnique.hpp>
|
||||
#include <Nazara/Graphics/ForwardRenderTechnique.hpp>
|
||||
#include <Nazara/Graphics/GuillotineTextureAtlas.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
@@ -100,6 +101,12 @@ namespace Nz
|
||||
Loaders::RegisterTexture();
|
||||
|
||||
// RenderTechniques
|
||||
if (!DepthRenderTechnique::Initialize())
|
||||
{
|
||||
NazaraError("Failed to initialize Depth Rendering");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ForwardRenderTechnique::Initialize())
|
||||
{
|
||||
NazaraError("Failed to initialize Forward Rendering");
|
||||
@@ -175,6 +182,7 @@ namespace Nz
|
||||
Loaders::UnregisterTexture();
|
||||
|
||||
DeferredRenderTechnique::Uninitialize();
|
||||
DepthRenderTechnique::Uninitialize();
|
||||
ForwardRenderTechnique::Uninitialize();
|
||||
SkinningManager::Uninitialize();
|
||||
ParticleRenderer::Uninitialize();
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <Nazara/Graphics/Light.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Graphics/AbstractRenderQueue.hpp>
|
||||
#include <Nazara/Graphics/Enums.hpp>
|
||||
#include <Nazara/Math/Algorithm.hpp>
|
||||
#include <Nazara/Math/Sphere.hpp>
|
||||
#include <Nazara/Renderer/Renderer.hpp>
|
||||
@@ -18,7 +19,11 @@
|
||||
namespace Nz
|
||||
{
|
||||
Light::Light(LightType type) :
|
||||
m_type(type)
|
||||
m_type(type),
|
||||
m_shadowMapFormat(PixelFormatType_Depth16),
|
||||
m_shadowMapSize(512, 512),
|
||||
m_shadowCastingEnabled(false),
|
||||
m_shadowMapUpdated(false)
|
||||
{
|
||||
SetAmbientFactor((type == LightType_Directional) ? 0.2f : 0.f);
|
||||
SetAttenuation(0.9f);
|
||||
@@ -31,6 +36,11 @@ namespace Nz
|
||||
|
||||
void Light::AddToRenderQueue(AbstractRenderQueue* renderQueue, const Matrix4f& transformMatrix) const
|
||||
{
|
||||
static Matrix4f biasMatrix(0.5f, 0.f, 0.f, 0.f,
|
||||
0.f, 0.5f, 0.f, 0.f,
|
||||
0.f, 0.f, 0.5f, 0.f,
|
||||
0.5f, 0.5f, 0.5f, 1.f);
|
||||
|
||||
switch (m_type)
|
||||
{
|
||||
case LightType_Directional:
|
||||
@@ -40,6 +50,8 @@ namespace Nz
|
||||
light.color = m_color;
|
||||
light.diffuseFactor = m_diffuseFactor;
|
||||
light.direction = transformMatrix.Transform(Vector3f::Forward(), 0.f);
|
||||
light.shadowMap = m_shadowMap.Get();
|
||||
light.transformMatrix = Matrix4f::ViewMatrix(transformMatrix.GetRotation() * Vector3f::Forward() * 100.f, transformMatrix.GetRotation()) * Matrix4f::Ortho(0.f, 100.f, 100.f, 0.f, 1.f, 100.f) * biasMatrix;
|
||||
|
||||
renderQueue->AddDirectionalLight(light);
|
||||
break;
|
||||
@@ -55,6 +67,7 @@ namespace Nz
|
||||
light.invRadius = m_invRadius;
|
||||
light.position = transformMatrix.GetTranslation();
|
||||
light.radius = m_radius;
|
||||
light.shadowMap = m_shadowMap.Get();
|
||||
|
||||
renderQueue->AddPointLight(light);
|
||||
break;
|
||||
@@ -74,6 +87,8 @@ namespace Nz
|
||||
light.outerAngleTangent = m_outerAngleTangent;
|
||||
light.position = transformMatrix.GetTranslation();
|
||||
light.radius = m_radius;
|
||||
light.shadowMap = m_shadowMap.Get();
|
||||
light.transformMatrix = Matrix4f::ViewMatrix(transformMatrix.GetTranslation(), transformMatrix.GetRotation()) * Matrix4f::Perspective(m_outerAngle*2.f, 1.f, 0.1f, m_radius) * biasMatrix;
|
||||
|
||||
renderQueue->AddSpotLight(light);
|
||||
break;
|
||||
@@ -178,4 +193,19 @@ namespace Nz
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Light::UpdateShadowMap() const
|
||||
{
|
||||
if (m_shadowCastingEnabled)
|
||||
{
|
||||
if (!m_shadowMap)
|
||||
m_shadowMap = Texture::New();
|
||||
|
||||
m_shadowMap->Create((m_type == LightType_Point) ? ImageType_Cubemap : ImageType_2D, m_shadowMapFormat, m_shadowMapSize.x, m_shadowMapSize.y);
|
||||
}
|
||||
else
|
||||
m_shadowMap.Reset();
|
||||
|
||||
m_shadowMapUpdated = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,23 +44,6 @@ namespace Nz
|
||||
return true;
|
||||
}
|
||||
|
||||
Material::Material()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
Material::Material(const Material& material) :
|
||||
RefCounted(),
|
||||
Resource(material)
|
||||
{
|
||||
Copy(material);
|
||||
}
|
||||
|
||||
Material::~Material()
|
||||
{
|
||||
OnMaterialRelease(this);
|
||||
}
|
||||
|
||||
const Shader* Material::Apply(UInt32 shaderFlags, UInt8 textureUnit, UInt8* lastUsedUnit) const
|
||||
{
|
||||
const ShaderInstance& instance = m_shaders[shaderFlags];
|
||||
@@ -300,247 +283,12 @@ namespace Nz
|
||||
SetShader(matParams.shaderName);
|
||||
}
|
||||
|
||||
void Material::Enable(RendererParameter renderParameter, bool enable)
|
||||
{
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (renderParameter > RendererParameter_Max)
|
||||
{
|
||||
NazaraError("Renderer parameter out of enum");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
m_states.parameters[renderParameter] = enable;
|
||||
}
|
||||
|
||||
void Material::EnableAlphaTest(bool alphaTest)
|
||||
{
|
||||
m_alphaTestEnabled = alphaTest;
|
||||
|
||||
InvalidateShaders();
|
||||
}
|
||||
|
||||
void Material::EnableDepthSorting(bool depthSorting)
|
||||
{
|
||||
m_depthSortingEnabled = depthSorting;
|
||||
}
|
||||
|
||||
void Material::EnableLighting(bool lighting)
|
||||
{
|
||||
m_lightingEnabled = lighting;
|
||||
|
||||
InvalidateShaders();
|
||||
}
|
||||
|
||||
void Material::EnableTransform(bool transform)
|
||||
{
|
||||
m_transformEnabled = transform;
|
||||
|
||||
InvalidateShaders();
|
||||
}
|
||||
|
||||
Texture* Material::GetAlphaMap() const
|
||||
{
|
||||
return m_alphaMap;
|
||||
}
|
||||
|
||||
float Material::GetAlphaThreshold() const
|
||||
{
|
||||
return m_alphaThreshold;
|
||||
}
|
||||
|
||||
Color Material::GetAmbientColor() const
|
||||
{
|
||||
return m_ambientColor;
|
||||
}
|
||||
|
||||
RendererComparison Material::GetDepthFunc() const
|
||||
{
|
||||
return m_states.depthFunc;
|
||||
}
|
||||
|
||||
Color Material::GetDiffuseColor() const
|
||||
{
|
||||
return m_diffuseColor;
|
||||
}
|
||||
|
||||
TextureSampler& Material::GetDiffuseSampler()
|
||||
{
|
||||
return m_diffuseSampler;
|
||||
}
|
||||
|
||||
const TextureSampler& Material::GetDiffuseSampler() const
|
||||
{
|
||||
return m_diffuseSampler;
|
||||
}
|
||||
|
||||
Texture* Material::GetDiffuseMap() const
|
||||
{
|
||||
return m_diffuseMap;
|
||||
}
|
||||
|
||||
BlendFunc Material::GetDstBlend() const
|
||||
{
|
||||
return m_states.dstBlend;
|
||||
}
|
||||
|
||||
Texture* Material::GetEmissiveMap() const
|
||||
{
|
||||
return m_emissiveMap;
|
||||
}
|
||||
|
||||
FaceSide Material::GetFaceCulling() const
|
||||
{
|
||||
return m_states.faceCulling;
|
||||
}
|
||||
|
||||
FaceFilling Material::GetFaceFilling() const
|
||||
{
|
||||
return m_states.faceFilling;
|
||||
}
|
||||
|
||||
Texture* Material::GetHeightMap() const
|
||||
{
|
||||
return m_heightMap;
|
||||
}
|
||||
|
||||
Texture* Material::GetNormalMap() const
|
||||
{
|
||||
return m_normalMap;
|
||||
}
|
||||
|
||||
const RenderStates& Material::GetRenderStates() const
|
||||
{
|
||||
return m_states;
|
||||
}
|
||||
|
||||
const UberShader* Material::GetShader() const
|
||||
{
|
||||
return m_uberShader;
|
||||
}
|
||||
|
||||
const UberShaderInstance* Material::GetShaderInstance(UInt32 flags) const
|
||||
{
|
||||
const ShaderInstance& instance = m_shaders[flags];
|
||||
if (!instance.uberInstance)
|
||||
GenerateShader(flags);
|
||||
|
||||
return instance.uberInstance;
|
||||
}
|
||||
|
||||
float Material::GetShininess() const
|
||||
{
|
||||
return m_shininess;
|
||||
}
|
||||
|
||||
Color Material::GetSpecularColor() const
|
||||
{
|
||||
return m_specularColor;
|
||||
}
|
||||
|
||||
Texture* Material::GetSpecularMap() const
|
||||
{
|
||||
return m_specularMap;
|
||||
}
|
||||
|
||||
TextureSampler& Material::GetSpecularSampler()
|
||||
{
|
||||
return m_specularSampler;
|
||||
}
|
||||
|
||||
const TextureSampler& Material::GetSpecularSampler() const
|
||||
{
|
||||
return m_specularSampler;
|
||||
}
|
||||
|
||||
BlendFunc Material::GetSrcBlend() const
|
||||
{
|
||||
return m_states.srcBlend;
|
||||
}
|
||||
|
||||
bool Material::HasAlphaMap() const
|
||||
{
|
||||
return m_alphaMap.IsValid();
|
||||
}
|
||||
|
||||
bool Material::HasDiffuseMap() const
|
||||
{
|
||||
return m_diffuseMap.IsValid();
|
||||
}
|
||||
|
||||
bool Material::HasEmissiveMap() const
|
||||
{
|
||||
return m_emissiveMap.IsValid();
|
||||
}
|
||||
|
||||
bool Material::HasHeightMap() const
|
||||
{
|
||||
return m_heightMap.IsValid();
|
||||
}
|
||||
|
||||
bool Material::HasNormalMap() const
|
||||
{
|
||||
return m_normalMap.IsValid();
|
||||
}
|
||||
|
||||
bool Material::HasSpecularMap() const
|
||||
{
|
||||
return m_specularMap.IsValid();
|
||||
}
|
||||
|
||||
bool Material::IsAlphaTestEnabled() const
|
||||
{
|
||||
return m_alphaTestEnabled;
|
||||
}
|
||||
|
||||
bool Material::IsDepthSortingEnabled() const
|
||||
{
|
||||
return m_depthSortingEnabled;
|
||||
}
|
||||
|
||||
bool Material::IsEnabled(RendererParameter parameter) const
|
||||
{
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (parameter > RendererParameter_Max)
|
||||
{
|
||||
NazaraError("Renderer parameter out of enum");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_states.parameters[parameter];
|
||||
}
|
||||
|
||||
bool Material::IsLightingEnabled() const
|
||||
{
|
||||
return m_lightingEnabled;
|
||||
}
|
||||
|
||||
bool Material::IsTransformEnabled() const
|
||||
{
|
||||
return m_transformEnabled;
|
||||
}
|
||||
|
||||
bool Material::LoadFromFile(const String& filePath, const MaterialParams& params)
|
||||
{
|
||||
return MaterialLoader::LoadFromFile(this, filePath, params);
|
||||
}
|
||||
|
||||
bool Material::LoadFromMemory(const void* data, std::size_t size, const MaterialParams& params)
|
||||
{
|
||||
return MaterialLoader::LoadFromMemory(this, data, size, params);
|
||||
}
|
||||
|
||||
bool Material::LoadFromStream(Stream& stream, const MaterialParams& params)
|
||||
{
|
||||
return MaterialLoader::LoadFromStream(this, stream, params);
|
||||
}
|
||||
|
||||
void Material::Reset()
|
||||
{
|
||||
OnMaterialReset(this);
|
||||
|
||||
m_alphaMap.Reset();
|
||||
m_depthMaterial.Reset();
|
||||
m_diffuseMap.Reset();
|
||||
m_emissiveMap.Reset();
|
||||
m_heightMap.Reset();
|
||||
@@ -558,6 +306,8 @@ namespace Nz
|
||||
m_diffuseColor = Color::White;
|
||||
m_diffuseSampler = TextureSampler();
|
||||
m_lightingEnabled = true;
|
||||
m_shadowCastingEnabled = true;
|
||||
m_shadowReceiveEnabled = true;
|
||||
m_shininess = 50.f;
|
||||
m_specularColor = Color::White;
|
||||
m_specularSampler = TextureSampler();
|
||||
@@ -569,252 +319,32 @@ namespace Nz
|
||||
SetShader("Basic");
|
||||
}
|
||||
|
||||
bool Material::SetAlphaMap(const String& textureName)
|
||||
{
|
||||
TextureRef texture = TextureLibrary::Query(textureName);
|
||||
if (!texture)
|
||||
{
|
||||
texture = TextureManager::Get(textureName);
|
||||
if (!texture)
|
||||
return false;
|
||||
}
|
||||
|
||||
SetAlphaMap(std::move(texture));
|
||||
return true;
|
||||
}
|
||||
|
||||
void Material::SetAlphaMap(TextureRef alphaMap)
|
||||
{
|
||||
m_alphaMap = std::move(alphaMap);
|
||||
|
||||
InvalidateShaders();
|
||||
}
|
||||
|
||||
void Material::SetAlphaThreshold(float alphaThreshold)
|
||||
{
|
||||
m_alphaThreshold = alphaThreshold;
|
||||
}
|
||||
|
||||
void Material::SetAmbientColor(const Color& ambient)
|
||||
{
|
||||
m_ambientColor = ambient;
|
||||
}
|
||||
|
||||
void Material::SetDepthFunc(RendererComparison depthFunc)
|
||||
{
|
||||
m_states.depthFunc = depthFunc;
|
||||
}
|
||||
|
||||
void Material::SetDiffuseColor(const Color& diffuse)
|
||||
{
|
||||
m_diffuseColor = diffuse;
|
||||
}
|
||||
|
||||
bool Material::SetDiffuseMap(const String& textureName)
|
||||
{
|
||||
TextureRef texture = TextureLibrary::Query(textureName);
|
||||
if (!texture)
|
||||
{
|
||||
texture = TextureManager::Get(textureName);
|
||||
if (!texture)
|
||||
return false;
|
||||
}
|
||||
|
||||
SetDiffuseMap(std::move(texture));
|
||||
return true;
|
||||
}
|
||||
|
||||
void Material::SetDiffuseMap(TextureRef diffuseMap)
|
||||
{
|
||||
m_diffuseMap = std::move(diffuseMap);
|
||||
|
||||
InvalidateShaders();
|
||||
}
|
||||
|
||||
void Material::SetDiffuseSampler(const TextureSampler& sampler)
|
||||
{
|
||||
m_diffuseSampler = sampler;
|
||||
}
|
||||
|
||||
void Material::SetDstBlend(BlendFunc func)
|
||||
{
|
||||
m_states.dstBlend = func;
|
||||
}
|
||||
|
||||
bool Material::SetEmissiveMap(const String& textureName)
|
||||
{
|
||||
TextureRef texture = TextureLibrary::Query(textureName);
|
||||
if (!texture)
|
||||
{
|
||||
texture = TextureManager::Get(textureName);
|
||||
if (!texture)
|
||||
return false;
|
||||
}
|
||||
|
||||
SetEmissiveMap(std::move(texture));
|
||||
return true;
|
||||
}
|
||||
|
||||
void Material::SetEmissiveMap(TextureRef emissiveMap)
|
||||
{
|
||||
m_emissiveMap = std::move(emissiveMap);
|
||||
|
||||
InvalidateShaders();
|
||||
}
|
||||
|
||||
void Material::SetFaceCulling(FaceSide faceSide)
|
||||
{
|
||||
m_states.faceCulling = faceSide;
|
||||
}
|
||||
|
||||
void Material::SetFaceFilling(FaceFilling filling)
|
||||
{
|
||||
m_states.faceFilling = filling;
|
||||
}
|
||||
|
||||
bool Material::SetHeightMap(const String& textureName)
|
||||
{
|
||||
TextureRef texture = TextureLibrary::Query(textureName);
|
||||
if (!texture)
|
||||
{
|
||||
texture = TextureManager::Get(textureName);
|
||||
if (!texture)
|
||||
return false;
|
||||
}
|
||||
|
||||
SetHeightMap(std::move(texture));
|
||||
return true;
|
||||
}
|
||||
|
||||
void Material::SetHeightMap(TextureRef heightMap)
|
||||
{
|
||||
m_heightMap = std::move(heightMap);
|
||||
|
||||
InvalidateShaders();
|
||||
}
|
||||
|
||||
bool Material::SetNormalMap(const String& textureName)
|
||||
{
|
||||
TextureRef texture = TextureLibrary::Query(textureName);
|
||||
if (!texture)
|
||||
{
|
||||
texture = TextureManager::Get(textureName);
|
||||
if (!texture)
|
||||
return false;
|
||||
}
|
||||
|
||||
SetNormalMap(std::move(texture));
|
||||
return true;
|
||||
}
|
||||
|
||||
void Material::SetNormalMap(TextureRef normalMap)
|
||||
{
|
||||
m_normalMap = std::move(normalMap);
|
||||
|
||||
InvalidateShaders();
|
||||
}
|
||||
|
||||
void Material::SetRenderStates(const RenderStates& states)
|
||||
{
|
||||
m_states = states;
|
||||
}
|
||||
|
||||
void Material::SetShader(UberShaderConstRef uberShader)
|
||||
{
|
||||
m_uberShader = std::move(uberShader);
|
||||
|
||||
InvalidateShaders();
|
||||
}
|
||||
|
||||
bool Material::SetShader(const String& uberShaderName)
|
||||
{
|
||||
UberShaderConstRef uberShader = UberShaderLibrary::Get(uberShaderName);
|
||||
if (!uberShader)
|
||||
return false;
|
||||
|
||||
SetShader(std::move(uberShader));
|
||||
return true;
|
||||
}
|
||||
|
||||
void Material::SetShininess(float shininess)
|
||||
{
|
||||
m_shininess = shininess;
|
||||
}
|
||||
|
||||
void Material::SetSpecularColor(const Color& specular)
|
||||
{
|
||||
m_specularColor = specular;
|
||||
}
|
||||
|
||||
bool Material::SetSpecularMap(const String& textureName)
|
||||
{
|
||||
TextureRef texture = TextureLibrary::Query(textureName);
|
||||
if (!texture)
|
||||
{
|
||||
texture = TextureManager::Get(textureName);
|
||||
if (!texture)
|
||||
return false;
|
||||
}
|
||||
|
||||
SetSpecularMap(std::move(texture));
|
||||
return true;
|
||||
}
|
||||
|
||||
void Material::SetSpecularMap(TextureRef specularMap)
|
||||
{
|
||||
m_specularMap = std::move(specularMap);
|
||||
|
||||
InvalidateShaders();
|
||||
}
|
||||
|
||||
void Material::SetSpecularSampler(const TextureSampler& sampler)
|
||||
{
|
||||
m_specularSampler = sampler;
|
||||
}
|
||||
|
||||
void Material::SetSrcBlend(BlendFunc func)
|
||||
{
|
||||
m_states.srcBlend = func;
|
||||
}
|
||||
|
||||
Material& Material::operator=(const Material& material)
|
||||
{
|
||||
Resource::operator=(material);
|
||||
|
||||
Copy(material);
|
||||
return *this;
|
||||
}
|
||||
|
||||
MaterialRef Material::GetDefault()
|
||||
{
|
||||
return s_defaultMaterial;
|
||||
}
|
||||
|
||||
void Material::Copy(const Material& material)
|
||||
{
|
||||
// Copie des états de base
|
||||
m_alphaTestEnabled = material.m_alphaTestEnabled;
|
||||
m_alphaThreshold = material.m_alphaThreshold;
|
||||
m_ambientColor = material.m_ambientColor;
|
||||
m_depthSortingEnabled = material.m_depthSortingEnabled;
|
||||
m_diffuseColor = material.m_diffuseColor;
|
||||
m_diffuseSampler = material.m_diffuseSampler;
|
||||
m_lightingEnabled = material.m_lightingEnabled;
|
||||
m_shininess = material.m_shininess;
|
||||
m_specularColor = material.m_specularColor;
|
||||
m_specularSampler = material.m_specularSampler;
|
||||
m_states = material.m_states;
|
||||
m_transformEnabled = material.m_transformEnabled;
|
||||
m_alphaTestEnabled = material.m_alphaTestEnabled;
|
||||
m_alphaThreshold = material.m_alphaThreshold;
|
||||
m_ambientColor = material.m_ambientColor;
|
||||
m_depthSortingEnabled = material.m_depthSortingEnabled;
|
||||
m_diffuseColor = material.m_diffuseColor;
|
||||
m_diffuseSampler = material.m_diffuseSampler;
|
||||
m_lightingEnabled = material.m_lightingEnabled;
|
||||
m_shininess = material.m_shininess;
|
||||
m_shadowCastingEnabled = material.m_shadowCastingEnabled;
|
||||
m_shadowReceiveEnabled = material.m_shadowReceiveEnabled;
|
||||
m_specularColor = material.m_specularColor;
|
||||
m_specularSampler = material.m_specularSampler;
|
||||
m_states = material.m_states;
|
||||
m_transformEnabled = material.m_transformEnabled;
|
||||
|
||||
// Copie des références de texture
|
||||
m_alphaMap = material.m_alphaMap;
|
||||
m_diffuseMap = material.m_diffuseMap;
|
||||
m_emissiveMap = material.m_emissiveMap;
|
||||
m_heightMap = material.m_heightMap;
|
||||
m_normalMap = material.m_normalMap;
|
||||
m_specularMap = material.m_specularMap;
|
||||
|
||||
// Copie de la référence vers l'Über-Shader
|
||||
m_alphaMap = material.m_alphaMap;
|
||||
m_depthMaterial = material.m_depthMaterial;
|
||||
m_diffuseMap = material.m_diffuseMap;
|
||||
m_emissiveMap = material.m_emissiveMap;
|
||||
m_heightMap = material.m_heightMap;
|
||||
m_normalMap = material.m_normalMap;
|
||||
m_specularMap = material.m_specularMap;
|
||||
m_uberShader = material.m_uberShader;
|
||||
|
||||
// On copie les instances de shader par la même occasion
|
||||
@@ -832,6 +362,7 @@ namespace Nz
|
||||
list.SetParameter("LIGHTING", m_lightingEnabled);
|
||||
list.SetParameter("NORMAL_MAPPING", m_normalMap.IsValid());
|
||||
list.SetParameter("PARALLAX_MAPPING", m_heightMap.IsValid());
|
||||
list.SetParameter("SHADOW_MAPPING", m_shadowReceiveEnabled);
|
||||
list.SetParameter("SPECULAR_MAPPING", m_specularMap.IsValid());
|
||||
list.SetParameter("TEXTURE_MAPPING", m_alphaMap.IsValid() || m_diffuseMap.IsValid() || m_emissiveMap.IsValid() ||
|
||||
m_normalMap.IsValid() || m_heightMap.IsValid() || m_specularMap.IsValid() ||
|
||||
@@ -865,12 +396,6 @@ namespace Nz
|
||||
#undef CacheUniform
|
||||
}
|
||||
|
||||
void Material::InvalidateShaders()
|
||||
{
|
||||
for (ShaderInstance& instance : m_shaders)
|
||||
instance.uberInstance = nullptr;
|
||||
}
|
||||
|
||||
bool Material::Initialize()
|
||||
{
|
||||
if (!MaterialLibrary::Initialize())
|
||||
@@ -905,8 +430,8 @@ namespace Nz
|
||||
String fragmentShader(reinterpret_cast<const char*>(r_phongLightingFragmentShader), sizeof(r_phongLightingFragmentShader));
|
||||
String vertexShader(reinterpret_cast<const char*>(r_phongLightingVertexShader), sizeof(r_phongLightingVertexShader));
|
||||
|
||||
uberShader->SetShader(ShaderStageType_Fragment, fragmentShader, "FLAG_DEFERRED FLAG_TEXTUREOVERLAY ALPHA_MAPPING ALPHA_TEST AUTO_TEXCOORDS DIFFUSE_MAPPING EMISSIVE_MAPPING LIGHTING NORMAL_MAPPING PARALLAX_MAPPING SPECULAR_MAPPING");
|
||||
uberShader->SetShader(ShaderStageType_Vertex, vertexShader, "FLAG_BILLBOARD FLAG_DEFERRED FLAG_INSTANCING FLAG_VERTEXCOLOR COMPUTE_TBNMATRIX LIGHTING PARALLAX_MAPPING TEXTURE_MAPPING TRANSFORM UNIFORM_VERTEX_DEPTH");
|
||||
uberShader->SetShader(ShaderStageType_Fragment, fragmentShader, "FLAG_DEFERRED FLAG_TEXTUREOVERLAY ALPHA_MAPPING ALPHA_TEST AUTO_TEXCOORDS DIFFUSE_MAPPING EMISSIVE_MAPPING LIGHTING NORMAL_MAPPING PARALLAX_MAPPING SHADOW_MAPPING SPECULAR_MAPPING");
|
||||
uberShader->SetShader(ShaderStageType_Vertex, vertexShader, "FLAG_BILLBOARD FLAG_DEFERRED FLAG_INSTANCING FLAG_VERTEXCOLOR COMPUTE_TBNMATRIX LIGHTING PARALLAX_MAPPING SHADOW_MAPPING TEXTURE_MAPPING TRANSFORM UNIFORM_VERTEX_DEPTH");
|
||||
|
||||
UberShaderLibrary::Register("PhongLighting", uberShader);
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ namespace Nz
|
||||
"Advanced Forward",
|
||||
"Basic Forward",
|
||||
"Deferred Shading",
|
||||
"Depth Pass",
|
||||
"Light Pre-Pass",
|
||||
"User"
|
||||
};
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -8,6 +8,7 @@ layout(early_fragment_tests) in;
|
||||
|
||||
/********************Entrant********************/
|
||||
in vec4 vColor;
|
||||
in vec4 vLightSpacePos[3];
|
||||
in mat3 vLightToWorld;
|
||||
in vec3 vNormal;
|
||||
in vec2 vTexCoord;
|
||||
@@ -29,10 +30,13 @@ struct Light
|
||||
vec4 parameters1;
|
||||
vec4 parameters2;
|
||||
vec2 parameters3;
|
||||
bool shadowMapping;
|
||||
};
|
||||
|
||||
// Lumières
|
||||
uniform Light Lights[3];
|
||||
uniform samplerCube PointLightShadowMap[3];
|
||||
uniform sampler2D DirectionalSpotLightShadowMap[3];
|
||||
|
||||
// Matériau
|
||||
uniform sampler2D MaterialAlphaMap;
|
||||
@@ -81,6 +85,41 @@ vec4 EncodeNormal(in vec3 normal)
|
||||
return vec4(vec2(atan(normal.y, normal.x)/kPI, normal.z), 0.0, 0.0);
|
||||
}
|
||||
|
||||
float VectorToDepthValue(vec3 vec, float zNear, float zFar)
|
||||
{
|
||||
vec3 absVec = abs(vec);
|
||||
float localZ = max(absVec.x, max(absVec.y, absVec.z));
|
||||
|
||||
float normZ = ((zFar + zNear) * localZ - (2.0*zFar*zNear)) / ((zFar - zNear)*localZ);
|
||||
return (normZ + 1.0) * 0.5;
|
||||
}
|
||||
|
||||
float CalculateDirectionalShadowFactor(int lightIndex)
|
||||
{
|
||||
vec4 lightSpacePos = vLightSpacePos[lightIndex];
|
||||
return (texture(DirectionalSpotLightShadowMap[lightIndex], lightSpacePos.xy).x >= (lightSpacePos.z - 0.0005)) ? 1.0 : 0.0;
|
||||
}
|
||||
|
||||
float CalculatePointShadowFactor(int lightIndex, vec3 lightToWorld, float zNear, float zFar)
|
||||
{
|
||||
return (texture(PointLightShadowMap[lightIndex], vec3(lightToWorld.x, -lightToWorld.y, -lightToWorld.z)).x >= VectorToDepthValue(lightToWorld, zNear, zFar)) ? 1.0 : 0.0;
|
||||
}
|
||||
|
||||
float CalculateSpotShadowFactor(int lightIndex)
|
||||
{
|
||||
vec4 lightSpacePos = vLightSpacePos[lightIndex];
|
||||
|
||||
float visibility = 1.0;
|
||||
float x,y;
|
||||
for (y = -3.5; y <= 3.5; y+= 1.0)
|
||||
for (x = -3.5; x <= 3.5; x+= 1.0)
|
||||
visibility += (textureProj(DirectionalSpotLightShadowMap[lightIndex], lightSpacePos.xyw + vec3(x/1024.0 * lightSpacePos.w, y/1024.0 * lightSpacePos.w, 0.0)).x >= (lightSpacePos.z - 0.0005)/lightSpacePos.w) ? 1.0 : 0.0;
|
||||
|
||||
visibility /= 64.0;
|
||||
|
||||
return visibility;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 diffuseColor = MaterialDiffuse * vColor;
|
||||
@@ -168,6 +207,10 @@ void main()
|
||||
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
vec4 lightColor = Lights[i].color;
|
||||
float lightAmbientFactor = Lights[i].factors.x;
|
||||
float lightDiffuseFactor = Lights[i].factors.y;
|
||||
|
||||
switch (Lights[i].type)
|
||||
{
|
||||
case LIGHT_DIRECTIONAL:
|
||||
@@ -175,75 +218,120 @@ void main()
|
||||
vec3 lightDir = -Lights[i].parameters1.xyz;
|
||||
|
||||
// Ambient
|
||||
lightAmbient += Lights[i].color.rgb * Lights[i].factors.x * (MaterialAmbient.rgb + SceneAmbient.rgb);
|
||||
lightAmbient += lightColor.rgb * lightAmbientFactor * (MaterialAmbient.rgb + SceneAmbient.rgb);
|
||||
|
||||
float att = 1.0;
|
||||
|
||||
#if SHADOW_MAPPING
|
||||
if (Lights[i].shadowMapping)
|
||||
{
|
||||
float shadowFactor = CalculateDirectionalShadowFactor(i);
|
||||
if (shadowFactor == 0.0)
|
||||
break;
|
||||
|
||||
att *= shadowFactor;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Diffuse
|
||||
float lambert = max(dot(normal, lightDir), 0.0);
|
||||
|
||||
lightDiffuse += lambert * Lights[i].color.rgb * Lights[i].factors.y;
|
||||
lightDiffuse += att * lambert * lightColor.rgb * lightDiffuseFactor;
|
||||
|
||||
// Specular
|
||||
vec3 reflection = reflect(-lightDir, normal);
|
||||
float specularFactor = max(dot(reflection, eyeVec), 0.0);
|
||||
specularFactor = pow(specularFactor, MaterialShininess);
|
||||
|
||||
lightSpecular += specularFactor * Lights[i].color.rgb;
|
||||
lightSpecular += att * specularFactor * lightColor.rgb;
|
||||
break;
|
||||
}
|
||||
|
||||
case LIGHT_POINT:
|
||||
{
|
||||
vec3 lightDir = Lights[i].parameters1.xyz - vWorldPos;
|
||||
float lightDirLength = length(lightDir);
|
||||
lightDir /= lightDirLength; // Normalisation
|
||||
vec3 lightPos = Lights[i].parameters1.xyz;
|
||||
float lightAttenuation = Lights[i].parameters1.w;
|
||||
float lightInvRadius = Lights[i].parameters2.w;
|
||||
|
||||
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.w*lightDirLength, 0.0);
|
||||
vec3 worldToLight = lightPos - vWorldPos;
|
||||
float lightDirLength = length(worldToLight);
|
||||
vec3 lightDir = worldToLight / lightDirLength; // Normalisation
|
||||
|
||||
float att = max(lightAttenuation - lightInvRadius * lightDirLength, 0.0);
|
||||
|
||||
// Ambient
|
||||
lightAmbient += att * Lights[i].color.rgb * Lights[i].factors.x * (MaterialAmbient.rgb + SceneAmbient.rgb);
|
||||
lightAmbient += att * lightColor.rgb * lightAmbientFactor * (MaterialAmbient.rgb + SceneAmbient.rgb);
|
||||
|
||||
#if SHADOW_MAPPING
|
||||
if (Lights[i].shadowMapping)
|
||||
{
|
||||
float shadowFactor = CalculatePointShadowFactor(i, vWorldPos - lightPos, 0.1, 50.0);
|
||||
if (shadowFactor == 0.0)
|
||||
break;
|
||||
|
||||
att *= shadowFactor;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Diffuse
|
||||
float lambert = max(dot(normal, lightDir), 0.0);
|
||||
|
||||
lightDiffuse += att * lambert * Lights[i].color.rgb * Lights[i].factors.y;
|
||||
lightDiffuse += att * lambert * lightColor.rgb * lightDiffuseFactor;
|
||||
|
||||
// Specular
|
||||
vec3 reflection = reflect(-lightDir, normal);
|
||||
float specularFactor = max(dot(reflection, eyeVec), 0.0);
|
||||
specularFactor = pow(specularFactor, MaterialShininess);
|
||||
|
||||
lightSpecular += att * specularFactor * Lights[i].color.rgb;
|
||||
lightSpecular += att * specularFactor * lightColor.rgb;
|
||||
break;
|
||||
}
|
||||
|
||||
case LIGHT_SPOT:
|
||||
{
|
||||
vec3 lightDir = Lights[i].parameters1.xyz - vWorldPos;
|
||||
float lightDirLength = length(lightDir);
|
||||
lightDir /= lightDirLength; // Normalisation
|
||||
vec3 lightPos = Lights[i].parameters1.xyz;
|
||||
vec3 lightDir = Lights[i].parameters2.xyz;
|
||||
float lightAttenuation = Lights[i].parameters1.w;
|
||||
float lightInvRadius = Lights[i].parameters2.w;
|
||||
float lightInnerAngle = Lights[i].parameters3.x;
|
||||
float lightOuterAngle = Lights[i].parameters3.y;
|
||||
|
||||
vec3 worldToLight = lightPos - vWorldPos;
|
||||
float lightDistance = length(worldToLight);
|
||||
worldToLight /= lightDistance; // Normalisation
|
||||
|
||||
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.w*lightDirLength, 0.0);
|
||||
float att = max(lightAttenuation - lightInvRadius * lightDistance, 0.0);
|
||||
|
||||
// Ambient
|
||||
lightAmbient += att * Lights[i].color.rgb * Lights[i].factors.x * (MaterialAmbient.rgb + SceneAmbient.rgb);
|
||||
lightAmbient += att * lightColor.rgb * lightAmbientFactor * (MaterialAmbient.rgb + SceneAmbient.rgb);
|
||||
|
||||
#if SHADOW_MAPPING
|
||||
if (Lights[i].shadowMapping)
|
||||
{
|
||||
float shadowFactor = CalculateSpotShadowFactor(i);
|
||||
if (shadowFactor == 0.0)
|
||||
break;
|
||||
|
||||
att *= shadowFactor;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Modification de l'atténuation pour gérer le spot
|
||||
float curAngle = dot(Lights[i].parameters2.xyz, -lightDir);
|
||||
float outerAngle = Lights[i].parameters3.y;
|
||||
float innerMinusOuterAngle = Lights[i].parameters3.x - outerAngle;
|
||||
att *= max((curAngle - outerAngle) / innerMinusOuterAngle, 0.0);
|
||||
float curAngle = dot(lightDir, -worldToLight);
|
||||
float innerMinusOuterAngle = lightInnerAngle - lightOuterAngle;
|
||||
att *= max((curAngle - lightOuterAngle) / innerMinusOuterAngle, 0.0);
|
||||
|
||||
// Diffuse
|
||||
float lambert = max(dot(normal, lightDir), 0.0);
|
||||
float lambert = max(dot(normal, worldToLight), 0.0);
|
||||
|
||||
lightDiffuse += att * lambert * Lights[i].color.rgb * Lights[i].factors.y;
|
||||
lightDiffuse += att * lambert * lightColor.rgb * lightDiffuseFactor;
|
||||
|
||||
// Specular
|
||||
vec3 reflection = reflect(-lightDir, normal);
|
||||
vec3 reflection = reflect(-worldToLight, normal);
|
||||
float specularFactor = max(dot(reflection, eyeVec), 0.0);
|
||||
specularFactor = pow(specularFactor, MaterialShininess);
|
||||
|
||||
lightSpecular += att * specularFactor * Lights[i].color.rgb;
|
||||
lightSpecular += att * specularFactor * lightColor.rgb;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -256,61 +344,110 @@ void main()
|
||||
{
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
vec4 lightColor = Lights[i].color;
|
||||
float lightAmbientFactor = Lights[i].factors.x;
|
||||
float lightDiffuseFactor = Lights[i].factors.y;
|
||||
|
||||
switch (Lights[i].type)
|
||||
{
|
||||
case LIGHT_DIRECTIONAL:
|
||||
{
|
||||
vec3 lightDir = normalize(-Lights[i].parameters1.xyz);
|
||||
vec3 lightDir = -Lights[i].parameters1.xyz;
|
||||
|
||||
// Ambient
|
||||
lightAmbient += Lights[i].color.rgb * Lights[i].factors.x * (MaterialAmbient.rgb + SceneAmbient.rgb);
|
||||
lightAmbient += lightColor.rgb * lightAmbientFactor * (MaterialAmbient.rgb + SceneAmbient.rgb);
|
||||
|
||||
float att = 1.0;
|
||||
|
||||
#if SHADOW_MAPPING
|
||||
if (Lights[i].shadowMapping)
|
||||
{
|
||||
float shadowFactor = CalculateDirectionalShadowFactor(i);
|
||||
if (shadowFactor == 0.0)
|
||||
break;
|
||||
|
||||
att *= shadowFactor;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Diffuse
|
||||
float lambert = max(dot(normal, lightDir), 0.0);
|
||||
|
||||
lightDiffuse += lambert * Lights[i].color.rgb * Lights[i].factors.y;
|
||||
lightDiffuse += att * lambert * lightColor.rgb * lightDiffuseFactor;
|
||||
break;
|
||||
}
|
||||
|
||||
case LIGHT_POINT:
|
||||
{
|
||||
vec3 lightDir = Lights[i].parameters1.xyz - vWorldPos;
|
||||
float lightDirLength = length(lightDir);
|
||||
lightDir /= lightDirLength; // Normalisation
|
||||
vec3 lightPos = Lights[i].parameters1.xyz;
|
||||
float lightAttenuation = Lights[i].parameters1.w;
|
||||
float lightInvRadius = Lights[i].parameters2.w;
|
||||
|
||||
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.w*lightDirLength, 0.0);
|
||||
vec3 worldToLight = lightPos - vWorldPos;
|
||||
float lightDirLength = length(worldToLight);
|
||||
vec3 lightDir = worldToLight / lightDirLength; // Normalisation
|
||||
|
||||
float att = max(lightAttenuation - lightInvRadius * lightDirLength, 0.0);
|
||||
|
||||
// Ambient
|
||||
lightAmbient += att * Lights[i].color.rgb * Lights[i].factors.x * (MaterialAmbient.rgb + SceneAmbient.rgb);
|
||||
lightAmbient += att * lightColor.rgb * lightAmbientFactor * (MaterialAmbient.rgb + SceneAmbient.rgb);
|
||||
|
||||
#if SHADOW_MAPPING
|
||||
if (Lights[i].shadowMapping)
|
||||
{
|
||||
float shadowFactor = CalculatePointShadowFactor(i, vWorldPos - lightPos, 0.1, 50.0);
|
||||
if (shadowFactor == 0.0)
|
||||
break;
|
||||
|
||||
att *= shadowFactor;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Diffuse
|
||||
float lambert = max(dot(normal, lightDir), 0.0);
|
||||
|
||||
lightDiffuse += att * lambert * Lights[i].color.rgb * Lights[i].factors.y;
|
||||
lightDiffuse += att * lambert * lightColor.rgb * lightDiffuseFactor;
|
||||
break;
|
||||
}
|
||||
|
||||
case LIGHT_SPOT:
|
||||
{
|
||||
vec3 lightDir = Lights[i].parameters1.xyz - vWorldPos;
|
||||
float lightDirLength = length(lightDir);
|
||||
lightDir /= lightDirLength; // Normalisation
|
||||
vec3 lightPos = Lights[i].parameters1.xyz;
|
||||
vec3 lightDir = Lights[i].parameters2.xyz;
|
||||
float lightAttenuation = Lights[i].parameters1.w;
|
||||
float lightInvRadius = Lights[i].parameters2.w;
|
||||
float lightInnerAngle = Lights[i].parameters3.x;
|
||||
float lightOuterAngle = Lights[i].parameters3.y;
|
||||
|
||||
vec3 worldToLight = lightPos - vWorldPos;
|
||||
float lightDistance = length(worldToLight);
|
||||
worldToLight /= lightDistance; // Normalisation
|
||||
|
||||
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.w*lightDirLength, 0.0);
|
||||
float att = max(lightAttenuation - lightInvRadius * lightDistance, 0.0);
|
||||
|
||||
// Ambient
|
||||
lightAmbient += att * Lights[i].color.rgb * Lights[i].factors.x * (MaterialAmbient.rgb + SceneAmbient.rgb);
|
||||
lightAmbient += att * lightColor.rgb * lightAmbientFactor * (MaterialAmbient.rgb + SceneAmbient.rgb);
|
||||
|
||||
#if SHADOW_MAPPING
|
||||
if (Lights[i].shadowMapping)
|
||||
{
|
||||
float shadowFactor = CalculateSpotShadowFactor(i);
|
||||
if (shadowFactor == 0.0)
|
||||
break;
|
||||
|
||||
att *= shadowFactor;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Modification de l'atténuation pour gérer le spot
|
||||
float curAngle = dot(Lights[i].parameters2.xyz, -lightDir);
|
||||
float outerAngle = Lights[i].parameters3.y;
|
||||
float innerMinusOuterAngle = Lights[i].parameters3.x - outerAngle;
|
||||
att *= max((curAngle - outerAngle) / innerMinusOuterAngle, 0.0);
|
||||
float curAngle = dot(lightDir, -worldToLight);
|
||||
float innerMinusOuterAngle = lightInnerAngle - lightOuterAngle;
|
||||
att *= max((curAngle - lightOuterAngle) / innerMinusOuterAngle, 0.0);
|
||||
|
||||
// Diffuse
|
||||
float lambert = max(dot(normal, lightDir), 0.0);
|
||||
float lambert = max(dot(normal, worldToLight), 0.0);
|
||||
|
||||
lightDiffuse += att * lambert * Lights[i].color.rgb * Lights[i].factors.y;
|
||||
lightDiffuse += att * lambert * lightColor.rgb * lightDiffuseFactor;
|
||||
}
|
||||
|
||||
default:
|
||||
@@ -318,7 +455,7 @@ void main()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
lightSpecular *= MaterialSpecular.rgb;
|
||||
#if SPECULAR_MAPPING
|
||||
lightSpecular *= texture(MaterialSpecularMap, texCoord).rgb; // Utiliser l'alpha de MaterialSpecular n'aurait aucun sens
|
||||
@@ -340,3 +477,4 @@ void main()
|
||||
#endif // LIGHTING
|
||||
#endif // FLAG_DEFERRED
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -15,6 +15,7 @@ in vec2 VertexTexCoord;
|
||||
|
||||
/********************Sortant********************/
|
||||
out vec4 vColor;
|
||||
out vec4 vLightSpacePos[3];
|
||||
out mat3 vLightToWorld;
|
||||
out vec3 vNormal;
|
||||
out vec2 vTexCoord;
|
||||
@@ -23,6 +24,8 @@ out vec3 vWorldPos;
|
||||
|
||||
/********************Uniformes********************/
|
||||
uniform vec3 EyePosition;
|
||||
uniform mat4 InvViewMatrix;
|
||||
uniform mat4 LightViewProjMatrix[3];
|
||||
uniform float VertexDepth;
|
||||
uniform mat4 ViewProjMatrix;
|
||||
uniform mat4 WorldMatrix;
|
||||
@@ -121,6 +124,11 @@ void main()
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if SHADOW_MAPPING
|
||||
for (int i = 0; i < 3; ++i)
|
||||
vLightSpacePos[i] = LightViewProjMatrix[i] * WorldMatrix * vec4(VertexPosition, 1.0);
|
||||
#endif
|
||||
|
||||
#if TEXTURE_MAPPING
|
||||
vTexCoord = VertexTexCoord;
|
||||
#endif
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -995,6 +995,7 @@ namespace Nz
|
||||
glUniformMatrix4fv = reinterpret_cast<PFNGLUNIFORMMATRIX4FVPROC>(LoadEntry("glUniformMatrix4fv"));
|
||||
glUnmapBuffer = reinterpret_cast<PFNGLUNMAPBUFFERPROC>(LoadEntry("glUnmapBuffer"));
|
||||
glUseProgram = reinterpret_cast<PFNGLUSEPROGRAMPROC>(LoadEntry("glUseProgram"));
|
||||
glValidateProgram = reinterpret_cast<PFNGLVALIDATEPROGRAMPROC>(LoadEntry("glValidateProgram"));
|
||||
glVertexAttrib4f = reinterpret_cast<PFNGLVERTEXATTRIB4FPROC>(LoadEntry("glVertexAttrib4f"));
|
||||
glVertexAttribDivisor = reinterpret_cast<PFNGLVERTEXATTRIBDIVISORPROC>(LoadEntry("glVertexAttribDivisor"));
|
||||
glVertexAttribPointer = reinterpret_cast<PFNGLVERTEXATTRIBPOINTERPROC>(LoadEntry("glVertexAttribPointer"));
|
||||
@@ -1684,24 +1685,44 @@ namespace Nz
|
||||
format->dataFormat = GL_DEPTH_COMPONENT;
|
||||
format->dataType = GL_UNSIGNED_SHORT;
|
||||
format->internalFormat = GL_DEPTH_COMPONENT16;
|
||||
|
||||
format->swizzle[0] = GL_RED;
|
||||
format->swizzle[1] = GL_RED;
|
||||
format->swizzle[2] = GL_RED;
|
||||
format->swizzle[3] = GL_ONE;
|
||||
return true;
|
||||
|
||||
case PixelFormatType_Depth24:
|
||||
format->dataFormat = GL_DEPTH_COMPONENT;
|
||||
format->dataType = GL_UNSIGNED_INT;
|
||||
format->internalFormat = GL_DEPTH_COMPONENT24;
|
||||
|
||||
format->swizzle[0] = GL_RED;
|
||||
format->swizzle[1] = GL_RED;
|
||||
format->swizzle[2] = GL_RED;
|
||||
format->swizzle[3] = GL_ONE;
|
||||
return true;
|
||||
|
||||
case PixelFormatType_Depth24Stencil8:
|
||||
format->dataFormat = GL_DEPTH_STENCIL;
|
||||
format->dataType = GL_UNSIGNED_INT_24_8;
|
||||
format->internalFormat = GL_DEPTH24_STENCIL8;
|
||||
|
||||
format->swizzle[0] = GL_RED;
|
||||
format->swizzle[1] = GL_RED;
|
||||
format->swizzle[2] = GL_RED;
|
||||
format->swizzle[3] = GL_GREEN;
|
||||
return true;
|
||||
|
||||
case PixelFormatType_Depth32:
|
||||
format->dataFormat = GL_DEPTH_COMPONENT;
|
||||
format->dataType = GL_UNSIGNED_BYTE;
|
||||
format->internalFormat = GL_DEPTH_COMPONENT32;
|
||||
|
||||
format->swizzle[0] = GL_RED;
|
||||
format->swizzle[1] = GL_RED;
|
||||
format->swizzle[2] = GL_RED;
|
||||
format->swizzle[3] = GL_ONE;
|
||||
return true;
|
||||
|
||||
case PixelFormatType_Stencil1:
|
||||
@@ -2248,12 +2269,14 @@ PFNGLUNIFORMMATRIX4DVPROC glUniformMatrix4dv = nullptr;
|
||||
PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv = nullptr;
|
||||
PFNGLUNMAPBUFFERPROC glUnmapBuffer = nullptr;
|
||||
PFNGLUSEPROGRAMPROC glUseProgram = nullptr;
|
||||
PFNGLVALIDATEPROGRAMPROC glValidateProgram = nullptr;
|
||||
PFNGLVERTEXATTRIB4FPROC glVertexAttrib4f = nullptr;
|
||||
PFNGLVERTEXATTRIBDIVISORPROC glVertexAttribDivisor = nullptr;
|
||||
PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer = nullptr;
|
||||
PFNGLVERTEXATTRIBIPOINTERPROC glVertexAttribIPointer = nullptr;
|
||||
PFNGLVERTEXATTRIBLPOINTERPROC glVertexAttribLPointer = nullptr;
|
||||
PFNGLVIEWPORTPROC glViewport = nullptr;
|
||||
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormat = nullptr;
|
||||
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs = nullptr;
|
||||
|
||||
@@ -156,9 +156,6 @@ namespace Nz
|
||||
InvalidateSize();
|
||||
InvalidateTargets();
|
||||
|
||||
if (attachmentPoint == AttachmentPoint_Color && !m_impl->userDefinedTargets)
|
||||
m_impl->colorTargets.push_back(index);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -293,9 +290,6 @@ namespace Nz
|
||||
InvalidateSize();
|
||||
InvalidateTargets();
|
||||
|
||||
if (attachmentPoint == AttachmentPoint_Color && !m_impl->userDefinedTargets)
|
||||
m_impl->colorTargets.push_back(index);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -819,6 +813,15 @@ namespace Nz
|
||||
|
||||
void RenderTexture::UpdateTargets() const
|
||||
{
|
||||
if (!m_impl->userDefinedTargets)
|
||||
{
|
||||
m_impl->colorTargets.clear();
|
||||
|
||||
unsigned int colorIndex = 0;
|
||||
for (unsigned int index = attachmentIndex[AttachmentPoint_Color]; index < m_impl->attachments.size(); ++index)
|
||||
m_impl->colorTargets.push_back(colorIndex++);
|
||||
}
|
||||
|
||||
if (m_impl->colorTargets.empty())
|
||||
{
|
||||
m_impl->drawBuffers.resize(1);
|
||||
|
||||
@@ -1713,6 +1713,14 @@ namespace Nz
|
||||
// Et on termine par envoyer nos états au driver
|
||||
OpenGL::ApplyStates(s_states);
|
||||
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (!s_shader->Validate())
|
||||
{
|
||||
NazaraError(Error::GetLastError());
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -747,6 +747,31 @@ namespace Nz
|
||||
}
|
||||
}
|
||||
|
||||
bool Shader::Validate() const
|
||||
{
|
||||
#if NAZARA_RENDERER_SAFE
|
||||
if (!m_program)
|
||||
{
|
||||
NazaraError("Shader is not initialized");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
glValidateProgram(m_program);
|
||||
|
||||
GLint success;
|
||||
glGetProgramiv(m_program, GL_VALIDATE_STATUS, &success);
|
||||
|
||||
if (success == GL_TRUE)
|
||||
return true;
|
||||
else
|
||||
{
|
||||
NazaraError("Failed to validate shader: " + GetLog());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
unsigned int Shader::GetOpenGLID() const
|
||||
{
|
||||
return m_program;
|
||||
|
||||
@@ -1306,6 +1306,12 @@ namespace Nz
|
||||
glTexParameteri(target, GL_TEXTURE_SWIZZLE_A, openGLFormat.swizzle[3]);
|
||||
}
|
||||
|
||||
if (!proxy && PixelFormat::GetType(m_impl->format) == PixelFormatTypeType_Depth)
|
||||
{
|
||||
glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
|
||||
glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user