Merge branch 'NDK-ShadowMapping'

Former-commit-id: e77949168073f06d52d10785afb41bad2f7f86c0
This commit is contained in:
Lynix
2016-05-13 13:06:23 +02:00
39 changed files with 2338 additions and 690 deletions

View File

@@ -62,7 +62,9 @@ namespace Nz
struct DirectionalLight
{
Color color;
Matrix4f transformMatrix;
Vector3f direction;
Texture* shadowMap;
float ambientFactor;
float diffuseFactor;
};
@@ -71,6 +73,7 @@ namespace Nz
{
Color color;
Vector3f position;
Texture* shadowMap;
float ambientFactor;
float attenuation;
float diffuseFactor;
@@ -81,8 +84,10 @@ namespace Nz
struct SpotLight
{
Color color;
Matrix4f transformMatrix;
Vector3f direction;
Vector3f position;
Texture* shadowMap;
float ambientFactor;
float attenuation;
float diffuseFactor;

View File

@@ -0,0 +1,53 @@
// 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
#pragma once
#ifndef NAZARA_DEPTHRENDERQUEUE_HPP
#define NAZARA_DEPTHRENDERQUEUE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Color.hpp>
#include <Nazara/Graphics/AbstractRenderQueue.hpp>
#include <Nazara/Graphics/ForwardRenderQueue.hpp>
#include <Nazara/Math/Box.hpp>
#include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Utility/IndexBuffer.hpp>
#include <Nazara/Utility/VertexBuffer.hpp>
#include <map>
#include <tuple>
namespace Nz
{
class NAZARA_GRAPHICS_API DepthRenderQueue : public ForwardRenderQueue
{
public:
DepthRenderQueue();
~DepthRenderQueue() = default;
void AddBillboard(int renderOrder, const Material* material, const Vector3f& position, const Vector2f& size, const Vector2f& sinCos = Vector2f(0.f, 1.f), const Color& color = Color::White) override;
void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const Vector2f> sizePtr, SparsePtr<const Vector2f> sinCosPtr = nullptr, SparsePtr<const Color> colorPtr = nullptr) override;
void 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) override;
void 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 = nullptr) override;
void 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) override;
void AddBillboards(int renderOrder, const Material* material, unsigned int count, SparsePtr<const Vector3f> positionPtr, SparsePtr<const float> sizePtr, SparsePtr<const Vector2f> sinCosPtr = nullptr, SparsePtr<const Color> colorPtr = nullptr) override;
void 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) override;
void 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 = nullptr) override;
void 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) override;
void AddDirectionalLight(const DirectionalLight& light) override;
void AddMesh(int renderOrder, const Material* material, const MeshData& meshData, const Boxf& meshAABB, const Matrix4f& transformMatrix) override;
void AddPointLight(const PointLight& light) override;
void AddSpotLight(const SpotLight& light) override;
void AddSprites(int renderOrder, const Material* material, const VertexStruct_XYZ_Color_UV* vertices, unsigned int spriteCount, const Texture* overlay = nullptr) override;
private:
inline bool IsMaterialSuitable(const Material* material) const;
MaterialRef m_baseMaterial;
};
}
#include <Nazara/Graphics/DepthRenderQueue.inl>
#endif // NAZARA_DEPTHRENDERQUEUE_HPP

View File

@@ -0,0 +1,17 @@
// 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/Debug.hpp>
namespace Nz
{
bool DepthRenderQueue::IsMaterialSuitable(const Material* material) const
{
NazaraAssert(material, "Invalid material");
return material->HasDepthMaterial() || (material->IsEnabled(RendererParameter_DepthBuffer) && material->IsEnabled(RendererParameter_DepthWrite) && material->IsShadowCastingEnabled());
}
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@@ -0,0 +1,79 @@
// 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
#pragma once
#ifndef NAZARA_DEPTHRENDERTECHNIQUE_HPP
#define NAZARA_DEPTHRENDERTECHNIQUE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Graphics/AbstractRenderTechnique.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/DepthRenderQueue.hpp>
#include <Nazara/Graphics/Light.hpp>
#include <Nazara/Renderer/Shader.hpp>
#include <Nazara/Utility/IndexBuffer.hpp>
#include <Nazara/Utility/VertexBuffer.hpp>
namespace Nz
{
class NAZARA_GRAPHICS_API DepthRenderTechnique : public AbstractRenderTechnique
{
public:
DepthRenderTechnique();
~DepthRenderTechnique() = default;
void Clear(const SceneData& sceneData) const override;
bool Draw(const SceneData& sceneData) const override;
AbstractRenderQueue* GetRenderQueue() override;
RenderTechniqueType GetType() const override;
static bool Initialize();
static void Uninitialize();
private:
struct ShaderUniforms;
void DrawBasicSprites(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const;
void DrawBillboards(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const;
void DrawOpaqueModels(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const;
const ShaderUniforms* GetShaderUniforms(const Shader* shader) const;
void OnShaderInvalidated(const Shader* shader) const;
struct LightIndex
{
LightType type;
float score;
unsigned int index;
};
struct ShaderUniforms
{
NazaraSlot(Shader, OnShaderUniformInvalidated, shaderUniformInvalidatedSlot);
NazaraSlot(Shader, OnShaderRelease, shaderReleaseSlot);
// Autre uniformes
int eyePosition;
int sceneAmbient;
int textureOverlay;
};
mutable std::unordered_map<const Shader*, ShaderUniforms> m_shaderUniforms;
Buffer m_vertexBuffer;
mutable DepthRenderQueue m_renderQueue;
VertexBuffer m_billboardPointBuffer;
VertexBuffer m_spriteBuffer;
static IndexBuffer s_quadIndexBuffer;
static VertexBuffer s_quadVertexBuffer;
static VertexDeclaration s_billboardInstanceDeclaration;
static VertexDeclaration s_billboardVertexDeclaration;
};
}
#include <Nazara/Graphics/dEPTHRenderTechnique.inl>
#endif // NAZARA_DEPTHRENDERTECHNIQUE_HPP

View File

@@ -0,0 +1,3 @@
// 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

View File

@@ -108,6 +108,7 @@ namespace Nz
RenderTechniqueType_AdvancedForward, // AdvancedForwardRenderTechnique
RenderTechniqueType_BasicForward, // BasicForwardRenderTechnique
RenderTechniqueType_DeferredShading, // DeferredRenderTechnique
RenderTechniqueType_Depth, // DepthRenderTechnique
RenderTechniqueType_LightPrePass, // LightPrePassRenderTechnique
RenderTechniqueType_User,

View File

@@ -36,7 +36,7 @@ namespace Nz
static bool Initialize();
static void Uninitialize();
private:
protected:
struct ShaderUniforms;
void ChooseLights(const Spheref& object, bool includeDirectionalLights = true) const;
@@ -46,7 +46,7 @@ namespace Nz
void DrawTransparentModels(const SceneData& sceneData, ForwardRenderQueue::Layer& layer) const;
const ShaderUniforms* GetShaderUniforms(const Shader* shader) const;
void OnShaderInvalidated(const Shader* shader) const;
void SendLightUniforms(const Shader* shader, const LightUniforms& uniforms, unsigned int index, unsigned int uniformOffset) const;
void SendLightUniforms(const Shader* shader, const LightUniforms& uniforms, unsigned int index, unsigned int uniformOffset, UInt8 availableTextureUnit) const;
static float ComputeDirectionalLightScore(const Spheref& object, const AbstractRenderQueue::DirectionalLight& light);
static float ComputePointLightScore(const Spheref& object, const AbstractRenderQueue::PointLight& light);
@@ -89,6 +89,7 @@ namespace Nz
unsigned int m_maxLightPassPerObject;
static IndexBuffer s_quadIndexBuffer;
static TextureSampler s_shadowSampler;
static VertexBuffer s_quadVertexBuffer;
static VertexDeclaration s_billboardInstanceDeclaration;
static VertexDeclaration s_billboardVertexDeclaration;

View File

@@ -2,10 +2,16 @@
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/Renderer.hpp>
namespace Nz
{
inline void ForwardRenderTechnique::SendLightUniforms(const Shader* shader, const LightUniforms& uniforms, unsigned int index, unsigned int uniformOffset) const
inline void ForwardRenderTechnique::SendLightUniforms(const Shader* shader, const LightUniforms& uniforms, unsigned int index, unsigned int uniformOffset, UInt8 availableTextureUnit) const
{
// If anyone got a better idea..
int dummyCubemap = Renderer::GetMaxTextureUnits() - 1;
int dummyTexture = Renderer::GetMaxTextureUnits() - 2;
if (index < m_lights.size())
{
const LightIndex& lightIndex = m_lights[index];
@@ -21,6 +27,20 @@ namespace Nz
shader->SendColor(uniforms.locations.color + uniformOffset, light.color);
shader->SendVector(uniforms.locations.factors + uniformOffset, Vector2f(light.ambientFactor, light.diffuseFactor));
shader->SendVector(uniforms.locations.parameters1 + uniformOffset, Vector4f(light.direction));
shader->SendBoolean(uniforms.locations.shadowMapping + uniformOffset, light.shadowMap != nullptr);
if (light.shadowMap)
{
Renderer::SetTexture(availableTextureUnit, light.shadowMap);
Renderer::SetTextureSampler(availableTextureUnit, s_shadowSampler);
shader->SendMatrix(uniforms.locations.lightViewProjMatrix + index, light.transformMatrix);
shader->SendInteger(uniforms.locations.directionalSpotLightShadowMap + index, availableTextureUnit);
}
else
shader->SendInteger(uniforms.locations.directionalSpotLightShadowMap + index, dummyTexture);
shader->SendInteger(uniforms.locations.pointLightShadowMap + index, dummyCubemap);
break;
}
@@ -32,6 +52,19 @@ namespace Nz
shader->SendVector(uniforms.locations.factors + uniformOffset, Vector2f(light.ambientFactor, light.diffuseFactor));
shader->SendVector(uniforms.locations.parameters1 + uniformOffset, Vector4f(light.position, light.attenuation));
shader->SendVector(uniforms.locations.parameters2 + uniformOffset, Vector4f(0.f, 0.f, 0.f, light.invRadius));
shader->SendBoolean(uniforms.locations.shadowMapping + uniformOffset, light.shadowMap != nullptr);
if (light.shadowMap)
{
Renderer::SetTexture(availableTextureUnit, light.shadowMap);
Renderer::SetTextureSampler(availableTextureUnit, s_shadowSampler);
shader->SendInteger(uniforms.locations.pointLightShadowMap + index, availableTextureUnit);
}
else
shader->SendInteger(uniforms.locations.pointLightShadowMap + index, dummyCubemap);
shader->SendInteger(uniforms.locations.directionalSpotLightShadowMap + index, dummyTexture);
break;
}
@@ -44,12 +77,31 @@ namespace Nz
shader->SendVector(uniforms.locations.parameters1 + uniformOffset, Vector4f(light.position, light.attenuation));
shader->SendVector(uniforms.locations.parameters2 + uniformOffset, Vector4f(light.direction, light.invRadius));
shader->SendVector(uniforms.locations.parameters3 + uniformOffset, Vector2f(light.innerAngleCosine, light.outerAngleCosine));
shader->SendBoolean(uniforms.locations.shadowMapping + uniformOffset, light.shadowMap != nullptr);
if (light.shadowMap)
{
Renderer::SetTexture(availableTextureUnit, light.shadowMap);
Renderer::SetTextureSampler(availableTextureUnit, s_shadowSampler);
shader->SendMatrix(uniforms.locations.lightViewProjMatrix + index, light.transformMatrix);
shader->SendInteger(uniforms.locations.directionalSpotLightShadowMap + index, availableTextureUnit);
}
else
shader->SendInteger(uniforms.locations.directionalSpotLightShadowMap + index, dummyTexture);
shader->SendInteger(uniforms.locations.pointLightShadowMap + index, dummyCubemap);
break;
}
}
}
else
{
shader->SendInteger(uniforms.locations.type + uniformOffset, -1); //< Disable the light in the shader
shader->SendInteger(uniforms.locations.directionalSpotLightShadowMap + index, dummyTexture);
shader->SendInteger(uniforms.locations.pointLightShadowMap + index, dummyCubemap);
}
}
inline float ForwardRenderTechnique::ComputeDirectionalLightScore(const Spheref& object, const AbstractRenderQueue::DirectionalLight& light)
@@ -78,7 +130,7 @@ namespace Nz
NazaraUnused(object);
NazaraUnused(light);
// Directional light are always suitables
// Directional light are always suitable
return true;
}

View File

@@ -11,6 +11,8 @@
#include <Nazara/Core/Color.hpp>
#include <Nazara/Graphics/Enums.hpp>
#include <Nazara/Graphics/Renderable.hpp>
#include <Nazara/Renderer/RenderTexture.hpp>
#include <Nazara/Renderer/Texture.hpp>
namespace Nz
{
@@ -21,7 +23,8 @@ namespace Nz
{
public:
Light(LightType type = LightType_Point);
Light(const Light& light) = default;
inline Light(const Light& light);
Light(Light&& light) = default;
~Light() = default;
void AddToRenderQueue(AbstractRenderQueue* renderQueue, const Matrix4f& transformMatrix) const override;
@@ -31,37 +34,56 @@ namespace Nz
bool Cull(const Frustumf& frustum, const Matrix4f& transformMatrix) const override;
float GetAmbientFactor() const;
float GetAttenuation() const;
Color GetColor() const;
float GetDiffuseFactor() const;
float GetInnerAngle() const;
float GetInnerAngleCosine() const;
float GetInvRadius() const;
LightType GetLightType() const;
float GetOuterAngle() const;
float GetOuterAngleCosine() const;
float GetOuterAngleTangent() const;
float GetRadius() const;
inline void EnableShadowCasting(bool castShadows);
void SetAmbientFactor(float factor);
void SetAttenuation(float attenuation);
void SetColor(const Color& color);
void SetDiffuseFactor(float factor);
void SetInnerAngle(float innerAngle);
void SetLightType(LightType type);
void SetOuterAngle(float outerAngle);
void SetRadius(float radius);
inline void EnsureShadowMapUpdate() const;
inline float GetAmbientFactor() const;
inline float GetAttenuation() const;
inline Color GetColor() const;
inline float GetDiffuseFactor() const;
inline float GetInnerAngle() const;
inline float GetInnerAngleCosine() const;
inline float GetInvRadius() const;
inline LightType GetLightType() const;
inline float GetOuterAngle() const;
inline float GetOuterAngleCosine() const;
inline float GetOuterAngleTangent() const;
inline float GetRadius() const;
inline TextureRef GetShadowMap() const;
inline PixelFormatType GetShadowMapFormat() const;
inline const Vector2ui& GetShadowMapSize() const;
inline bool IsShadowCastingEnabled() const;
inline void SetAmbientFactor(float factor);
inline void SetAttenuation(float attenuation);
inline void SetColor(const Color& color);
inline void SetDiffuseFactor(float factor);
inline void SetInnerAngle(float innerAngle);
inline void SetLightType(LightType type);
inline void SetOuterAngle(float outerAngle);
inline void SetRadius(float radius);
inline void SetShadowMapFormat(PixelFormatType shadowFormat);
inline void SetShadowMapSize(const Vector2ui& size);
void UpdateBoundingVolume(const Matrix4f& transformMatrix) override;
Light& operator=(const Light& light) = default;
Light& operator=(const Light& light);
Light& operator=(Light&& light) = default;
private:
void MakeBoundingVolume() const override;
inline void InvalidateShadowMap();
void UpdateShadowMap() const;
LightType m_type;
Color m_color;
LightType m_type;
PixelFormatType m_shadowMapFormat;
Vector2ui m_shadowMapSize;
mutable TextureRef m_shadowMap;
bool m_shadowCastingEnabled;
mutable bool m_shadowMapUpdated;
float m_ambientFactor;
float m_attenuation;
float m_diffuseFactor;
@@ -80,10 +102,14 @@ namespace Nz
{
int type;
int color;
int directionalSpotLightShadowMap;
int factors;
int lightViewProjMatrix;
int parameters1;
int parameters2;
int parameters3;
int pointLightShadowMap;
int shadowMapping;
};
bool ubo;

View File

@@ -7,6 +7,42 @@
namespace Nz
{
inline Light::Light(const Light& light) :
Renderable(light),
m_type(light.m_type),
m_shadowMapFormat(light.m_shadowMapFormat),
m_color(light.m_color),
m_shadowMapSize(light.m_shadowMapSize),
m_shadowCastingEnabled(light.m_shadowCastingEnabled),
m_shadowMapUpdated(false),
m_ambientFactor(light.m_ambientFactor),
m_attenuation(light.m_attenuation),
m_diffuseFactor(light.m_diffuseFactor),
m_innerAngle(light.m_innerAngle),
m_innerAngleCosine(light.m_innerAngleCosine),
m_invRadius(light.m_invRadius),
m_outerAngle(light.m_outerAngle),
m_outerAngleCosine(light.m_outerAngleCosine),
m_outerAngleTangent(light.m_outerAngleTangent),
m_radius(light.m_radius)
{
}
inline void Light::EnableShadowCasting(bool castShadows)
{
if (m_shadowCastingEnabled != castShadows)
{
m_shadowCastingEnabled = castShadows;
m_shadowMapUpdated = false;
}
}
inline void Light::EnsureShadowMapUpdate() const
{
if (!m_shadowMapUpdated)
UpdateShadowMap();
}
inline float Light::GetAmbientFactor() const
{
return m_ambientFactor;
@@ -67,6 +103,28 @@ namespace Nz
return m_radius;
}
inline TextureRef Light::GetShadowMap() const
{
EnsureShadowMapUpdate();
return m_shadowMap;
}
inline PixelFormatType Light::GetShadowMapFormat() const
{
return m_shadowMapFormat;
}
inline const Vector2ui& Light::GetShadowMapSize() const
{
return m_shadowMapSize;
}
inline bool Light::IsShadowCastingEnabled() const
{
return m_shadowCastingEnabled;
}
inline void Light::SetAmbientFactor(float factor)
{
m_ambientFactor = factor;
@@ -96,6 +154,8 @@ namespace Nz
inline void Light::SetLightType(LightType type)
{
m_type = type;
InvalidateShadowMap();
}
inline void Light::SetOuterAngle(float outerAngle)
@@ -115,6 +175,53 @@ namespace Nz
InvalidateBoundingVolume();
}
inline void Light::SetShadowMapFormat(PixelFormatType shadowFormat)
{
NazaraAssert(PixelFormat::GetType(shadowFormat) == PixelFormatTypeType_Depth, "Shadow format type is not a depth format");
m_shadowMapFormat = shadowFormat;
InvalidateShadowMap();
}
inline void Light::SetShadowMapSize(const Vector2ui& size)
{
NazaraAssert(size.x > 0 && size.y > 0, "Shadow map size must have a positive size");
m_shadowMapSize = size;
InvalidateShadowMap();
}
inline Light& Light::operator=(const Light& light)
{
Renderable::operator=(light);
m_ambientFactor = light.m_ambientFactor;
m_attenuation = light.m_attenuation;
m_color = light.m_color;
m_diffuseFactor = light.m_diffuseFactor;
m_innerAngle = light.m_innerAngle;
m_innerAngleCosine = light.m_innerAngleCosine;
m_invRadius = light.m_invRadius;
m_outerAngle = light.m_outerAngle;
m_outerAngleCosine = light.m_outerAngleCosine;
m_outerAngleTangent = light.m_outerAngleTangent;
m_radius = light.m_radius;
m_shadowCastingEnabled = light.m_shadowCastingEnabled;
m_shadowMapFormat = light.m_shadowMapFormat;
m_shadowMapSize = light.m_shadowMapSize;
m_type = light.m_type;
InvalidateShadowMap();
return *this;
}
inline void Light::InvalidateShadowMap()
{
m_shadowMapUpdated = false;
}
}
#include <Nazara/Renderer/DebugOff.hpp>

View File

@@ -57,92 +57,99 @@ namespace Nz
friend class Graphics;
public:
Material();
Material(const Material& material);
~Material();
inline Material();
inline Material(const Material& material);
inline ~Material();
const Shader* Apply(UInt32 shaderFlags = 0, UInt8 textureUnit = 0, UInt8* lastUsedUnit = nullptr) const;
void BuildFromParameters(const ParameterList& matData, const MaterialParams& matParams = MaterialParams());
void Enable(RendererParameter renderParameter, bool enable);
void EnableAlphaTest(bool alphaTest);
void EnableDepthSorting(bool depthSorting);
void EnableLighting(bool lighting);
void EnableTransform(bool transform);
inline void Enable(RendererParameter renderParameter, bool enable);
inline void EnableAlphaTest(bool alphaTest);
inline void EnableDepthSorting(bool depthSorting);
inline void EnableLighting(bool lighting);
inline void EnableShadowCasting(bool castShadows);
inline void EnableShadowReceive(bool receiveShadows);
inline void EnableTransform(bool transform);
Texture* GetAlphaMap() const;
float GetAlphaThreshold() const;
Color GetAmbientColor() const;
RendererComparison GetDepthFunc() const;
Color GetDiffuseColor() const;
Texture* GetDiffuseMap() const;
TextureSampler& GetDiffuseSampler();
const TextureSampler& GetDiffuseSampler() const;
BlendFunc GetDstBlend() const;
Texture* GetEmissiveMap() const;
FaceSide GetFaceCulling() const;
FaceFilling GetFaceFilling() const;
Texture* GetHeightMap() const;
Texture* GetNormalMap() const;
const RenderStates& GetRenderStates() const;
const UberShader* GetShader() const;
const UberShaderInstance* GetShaderInstance(UInt32 flags = ShaderFlags_None) const;
float GetShininess() const;
Color GetSpecularColor() const;
Texture* GetSpecularMap() const;
TextureSampler& GetSpecularSampler();
const TextureSampler& GetSpecularSampler() const;
BlendFunc GetSrcBlend() const;
inline const TextureRef& GetAlphaMap() const;
inline float GetAlphaThreshold() const;
inline Color GetAmbientColor() const;
inline RendererComparison GetDepthFunc() const;
inline const MaterialRef& GetDepthMaterial() const;
inline Color GetDiffuseColor() const;
inline const TextureRef& GetDiffuseMap() const;
inline TextureSampler& GetDiffuseSampler();
inline const TextureSampler& GetDiffuseSampler() const;
inline BlendFunc GetDstBlend() const;
inline const TextureRef& GetEmissiveMap() const;
inline FaceSide GetFaceCulling() const;
inline FaceFilling GetFaceFilling() const;
inline const TextureRef& GetHeightMap() const;
inline const TextureRef& GetNormalMap() const;
inline const RenderStates& GetRenderStates() const;
inline const UberShader* GetShader() const;
inline const UberShaderInstance* GetShaderInstance(UInt32 flags = ShaderFlags_None) const;
inline float GetShininess() const;
inline Color GetSpecularColor() const;
inline const TextureRef& GetSpecularMap() const;
inline TextureSampler& GetSpecularSampler();
inline const TextureSampler& GetSpecularSampler() const;
inline BlendFunc GetSrcBlend() const;
bool HasAlphaMap() const;
bool HasDiffuseMap() const;
bool HasEmissiveMap() const;
bool HasHeightMap() const;
bool HasNormalMap() const;
bool HasSpecularMap() const;
inline bool HasAlphaMap() const;
inline bool HasDepthMaterial() const;
inline bool HasDiffuseMap() const;
inline bool HasEmissiveMap() const;
inline bool HasHeightMap() const;
inline bool HasNormalMap() const;
inline bool HasSpecularMap() const;
bool IsAlphaTestEnabled() const;
bool IsDepthSortingEnabled() const;
bool IsEnabled(RendererParameter renderParameter) const;
bool IsLightingEnabled() const;
bool IsTransformEnabled() const;
inline bool IsAlphaTestEnabled() const;
inline bool IsDepthSortingEnabled() const;
inline bool IsEnabled(RendererParameter renderParameter) const;
inline bool IsLightingEnabled() const;
inline bool IsShadowCastingEnabled() const;
inline bool IsShadowReceiveEnabled() const;
inline bool IsTransformEnabled() const;
bool LoadFromFile(const String& filePath, const MaterialParams& params = MaterialParams());
bool LoadFromMemory(const void* data, std::size_t size, const MaterialParams& params = MaterialParams());
bool LoadFromStream(Stream& stream, const MaterialParams& params = MaterialParams());
inline bool LoadFromFile(const String& filePath, const MaterialParams& params = MaterialParams());
inline bool LoadFromMemory(const void* data, std::size_t size, const MaterialParams& params = MaterialParams());
inline bool LoadFromStream(Stream& stream, const MaterialParams& params = MaterialParams());
void Reset();
bool SetAlphaMap(const String& textureName);
void SetAlphaMap(TextureRef alphaMap);
void SetAlphaThreshold(float alphaThreshold);
void SetAmbientColor(const Color& ambient);
void SetDepthFunc(RendererComparison depthFunc);
void SetDiffuseColor(const Color& diffuse);
bool SetDiffuseMap(const String& textureName);
void SetDiffuseMap(TextureRef diffuseMap);
void SetDiffuseSampler(const TextureSampler& sampler);
void SetDstBlend(BlendFunc func);
bool SetEmissiveMap(const String& textureName);
void SetEmissiveMap(TextureRef textureName);
void SetFaceCulling(FaceSide faceSide);
void SetFaceFilling(FaceFilling filling);
bool SetHeightMap(const String& textureName);
void SetHeightMap(TextureRef textureName);
bool SetNormalMap(const String& textureName);
void SetNormalMap(TextureRef textureName);
void SetRenderStates(const RenderStates& states);
void SetShader(UberShaderConstRef uberShader);
bool SetShader(const String& uberShaderName);
void SetShininess(float shininess);
void SetSpecularColor(const Color& specular);
bool SetSpecularMap(const String& textureName);
void SetSpecularMap(TextureRef specularMap);
void SetSpecularSampler(const TextureSampler& sampler);
void SetSrcBlend(BlendFunc func);
inline bool SetAlphaMap(const String& textureName);
inline void SetAlphaMap(TextureRef alphaMap);
inline void SetAlphaThreshold(float alphaThreshold);
inline void SetAmbientColor(const Color& ambient);
inline void SetDepthFunc(RendererComparison depthFunc);
inline void SetDepthMaterial(MaterialRef depthMaterial);
inline void SetDiffuseColor(const Color& diffuse);
inline bool SetDiffuseMap(const String& textureName);
inline void SetDiffuseMap(TextureRef diffuseMap);
inline void SetDiffuseSampler(const TextureSampler& sampler);
inline void SetDstBlend(BlendFunc func);
inline bool SetEmissiveMap(const String& textureName);
inline void SetEmissiveMap(TextureRef textureName);
inline void SetFaceCulling(FaceSide faceSide);
inline void SetFaceFilling(FaceFilling filling);
inline bool SetHeightMap(const String& textureName);
inline void SetHeightMap(TextureRef textureName);
inline bool SetNormalMap(const String& textureName);
inline void SetNormalMap(TextureRef textureName);
inline void SetRenderStates(const RenderStates& states);
inline void SetShader(UberShaderConstRef uberShader);
inline bool SetShader(const String& uberShaderName);
inline void SetShininess(float shininess);
inline void SetSpecularColor(const Color& specular);
inline bool SetSpecularMap(const String& textureName);
inline void SetSpecularMap(TextureRef specularMap);
inline void SetSpecularSampler(const TextureSampler& sampler);
inline void SetSrcBlend(BlendFunc func);
Material& operator=(const Material& material);
inline Material& operator=(const Material& material);
static MaterialRef GetDefault();
template<typename... Args> static MaterialRef New(Args&&... args);
@@ -161,7 +168,7 @@ namespace Nz
void Copy(const Material& material);
void GenerateShader(UInt32 flags) const;
void InvalidateShaders();
inline void InvalidateShaders();
static bool Initialize();
static void Uninitialize();
@@ -169,6 +176,7 @@ namespace Nz
Color m_ambientColor;
Color m_diffuseColor;
Color m_specularColor;
MaterialRef m_depthMaterial; //< Materialception
RenderStates m_states;
TextureSampler m_diffuseSampler;
TextureSampler m_specularSampler;
@@ -183,6 +191,8 @@ namespace Nz
bool m_alphaTestEnabled;
bool m_depthSortingEnabled;
bool m_lightingEnabled;
bool m_shadowCastingEnabled;
bool m_shadowReceiveEnabled;
bool m_transformEnabled;
float m_alphaThreshold;
float m_shininess;

View File

@@ -7,6 +7,513 @@
namespace Nz
{
inline Material::Material()
{
Reset();
}
inline Material::Material(const Material& material) :
RefCounted(),
Resource(material)
{
Copy(material);
}
inline Material::~Material()
{
OnMaterialRelease(this);
}
inline void Material::Enable(RendererParameter renderParameter, bool enable)
{
NazaraAssert(renderParameter <= RendererParameter_Max, "Renderer parameter out of enum");
m_states.parameters[renderParameter] = enable;
}
inline void Material::EnableAlphaTest(bool alphaTest)
{
m_alphaTestEnabled = alphaTest;
InvalidateShaders();
}
inline void Material::EnableDepthSorting(bool depthSorting)
{
// Has no influence on shaders
m_depthSortingEnabled = depthSorting;
}
inline void Material::EnableLighting(bool lighting)
{
m_lightingEnabled = lighting;
InvalidateShaders();
}
inline void Material::EnableShadowCasting(bool castShadows)
{
// Has no influence on shaders
m_shadowCastingEnabled = castShadows;
}
inline void Material::EnableShadowReceive(bool receiveShadows)
{
m_shadowReceiveEnabled = receiveShadows;
InvalidateShaders();
}
inline void Material::EnableTransform(bool transform)
{
m_transformEnabled = transform;
InvalidateShaders();
}
inline const TextureRef& Material::GetAlphaMap() const
{
return m_alphaMap;
}
inline float Material::GetAlphaThreshold() const
{
return m_alphaThreshold;
}
inline Color Material::GetAmbientColor() const
{
return m_ambientColor;
}
inline RendererComparison Material::GetDepthFunc() const
{
return m_states.depthFunc;
}
inline const MaterialRef& Material::GetDepthMaterial() const
{
return m_depthMaterial;
}
inline Color Material::GetDiffuseColor() const
{
return m_diffuseColor;
}
inline TextureSampler& Material::GetDiffuseSampler()
{
return m_diffuseSampler;
}
inline const TextureSampler& Material::GetDiffuseSampler() const
{
return m_diffuseSampler;
}
const TextureRef& Material::GetDiffuseMap() const
{
return m_diffuseMap;
}
inline BlendFunc Material::GetDstBlend() const
{
return m_states.dstBlend;
}
inline const TextureRef& Material::GetEmissiveMap() const
{
return m_emissiveMap;
}
inline FaceSide Material::GetFaceCulling() const
{
return m_states.faceCulling;
}
inline FaceFilling Material::GetFaceFilling() const
{
return m_states.faceFilling;
}
inline const TextureRef& Material::GetHeightMap() const
{
return m_heightMap;
}
inline const TextureRef& Material::GetNormalMap() const
{
return m_normalMap;
}
inline const RenderStates& Material::GetRenderStates() const
{
return m_states;
}
inline const UberShader* Material::GetShader() const
{
return m_uberShader;
}
inline const UberShaderInstance* Material::GetShaderInstance(UInt32 flags) const
{
const ShaderInstance& instance = m_shaders[flags];
if (!instance.uberInstance)
GenerateShader(flags);
return instance.uberInstance;
}
inline float Material::GetShininess() const
{
return m_shininess;
}
inline Color Material::GetSpecularColor() const
{
return m_specularColor;
}
inline const TextureRef& Material::GetSpecularMap() const
{
return m_specularMap;
}
inline TextureSampler& Material::GetSpecularSampler()
{
return m_specularSampler;
}
inline const TextureSampler& Material::GetSpecularSampler() const
{
return m_specularSampler;
}
inline BlendFunc Material::GetSrcBlend() const
{
return m_states.srcBlend;
}
inline bool Material::HasAlphaMap() const
{
return m_alphaMap.IsValid();
}
inline bool Material::HasDepthMaterial() const
{
return m_depthMaterial.IsValid();
}
inline bool Material::HasDiffuseMap() const
{
return m_diffuseMap.IsValid();
}
inline bool Material::HasEmissiveMap() const
{
return m_emissiveMap.IsValid();
}
inline bool Material::HasHeightMap() const
{
return m_heightMap.IsValid();
}
inline bool Material::HasNormalMap() const
{
return m_normalMap.IsValid();
}
inline bool Material::HasSpecularMap() const
{
return m_specularMap.IsValid();
}
inline bool Material::IsAlphaTestEnabled() const
{
return m_alphaTestEnabled;
}
inline bool Material::IsDepthSortingEnabled() const
{
return m_depthSortingEnabled;
}
inline bool Material::IsEnabled(RendererParameter parameter) const
{
NazaraAssert(parameter <= RendererParameter_Max, "Renderer parameter out of enum");
return m_states.parameters[parameter];
}
inline bool Material::IsLightingEnabled() const
{
return m_lightingEnabled;
}
inline bool Material::IsShadowCastingEnabled() const
{
return m_shadowCastingEnabled;
}
inline bool Material::IsShadowReceiveEnabled() const
{
return m_shadowReceiveEnabled;
}
inline bool Material::IsTransformEnabled() const
{
return m_transformEnabled;
}
inline bool Material::LoadFromFile(const String& filePath, const MaterialParams& params)
{
return MaterialLoader::LoadFromFile(this, filePath, params);
}
inline bool Material::LoadFromMemory(const void* data, std::size_t size, const MaterialParams& params)
{
return MaterialLoader::LoadFromMemory(this, data, size, params);
}
inline bool Material::LoadFromStream(Stream& stream, const MaterialParams& params)
{
return MaterialLoader::LoadFromStream(this, stream, params);
}
inline 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;
}
inline void Material::SetAlphaMap(TextureRef alphaMap)
{
m_alphaMap = std::move(alphaMap);
InvalidateShaders();
}
inline void Material::SetAlphaThreshold(float alphaThreshold)
{
m_alphaThreshold = alphaThreshold;
}
inline void Material::SetAmbientColor(const Color& ambient)
{
m_ambientColor = ambient;
}
inline void Material::SetDepthFunc(RendererComparison depthFunc)
{
m_states.depthFunc = depthFunc;
}
inline void Material::SetDepthMaterial(MaterialRef depthMaterial)
{
m_depthMaterial = std::move(depthMaterial);
}
inline void Material::SetDiffuseColor(const Color& diffuse)
{
m_diffuseColor = diffuse;
}
inline 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;
}
inline void Material::SetDiffuseMap(TextureRef diffuseMap)
{
m_diffuseMap = std::move(diffuseMap);
InvalidateShaders();
}
inline void Material::SetDiffuseSampler(const TextureSampler& sampler)
{
m_diffuseSampler = sampler;
}
inline void Material::SetDstBlend(BlendFunc func)
{
m_states.dstBlend = func;
}
inline 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;
}
inline void Material::SetEmissiveMap(TextureRef emissiveMap)
{
m_emissiveMap = std::move(emissiveMap);
InvalidateShaders();
}
inline void Material::SetFaceCulling(FaceSide faceSide)
{
m_states.faceCulling = faceSide;
}
inline void Material::SetFaceFilling(FaceFilling filling)
{
m_states.faceFilling = filling;
}
inline 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;
}
inline void Material::SetHeightMap(TextureRef heightMap)
{
m_heightMap = std::move(heightMap);
InvalidateShaders();
}
inline 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;
}
inline void Material::SetNormalMap(TextureRef normalMap)
{
m_normalMap = std::move(normalMap);
InvalidateShaders();
}
inline void Material::SetRenderStates(const RenderStates& states)
{
m_states = states;
}
inline void Material::SetShader(UberShaderConstRef uberShader)
{
m_uberShader = std::move(uberShader);
InvalidateShaders();
}
inline bool Material::SetShader(const String& uberShaderName)
{
UberShaderConstRef uberShader = UberShaderLibrary::Get(uberShaderName);
if (!uberShader)
return false;
SetShader(std::move(uberShader));
return true;
}
inline void Material::SetShininess(float shininess)
{
m_shininess = shininess;
}
inline void Material::SetSpecularColor(const Color& specular)
{
m_specularColor = specular;
}
inline 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;
}
inline void Material::SetSpecularMap(TextureRef specularMap)
{
m_specularMap = std::move(specularMap);
InvalidateShaders();
}
inline void Material::SetSpecularSampler(const TextureSampler& sampler)
{
m_specularSampler = sampler;
}
inline void Material::SetSrcBlend(BlendFunc func)
{
m_states.srcBlend = func;
}
inline Material& Material::operator=(const Material& material)
{
Resource::operator=(material);
Copy(material);
return *this;
}
inline MaterialRef Material::GetDefault()
{
return s_defaultMaterial;
}
inline void Material::InvalidateShaders()
{
for (ShaderInstance& instance : m_shaders)
instance.uberInstance = nullptr;
}
template<typename... Args>
MaterialRef Material::New(Args&&... args)
{