Refactored materials

(Renderer) Removed Apply function
(Renderer) Renamed SetTextureSampling to SetTextureSampler
Updated demo


Former-commit-id: d40a9ce05df35b078e645927451093da44eec314
This commit is contained in:
Lynix
2012-12-18 16:17:30 +01:00
parent 8b67d17e38
commit 10730de74e
5 changed files with 119 additions and 71 deletions

View File

@@ -3,6 +3,9 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/Material.hpp>
#include <Nazara/Renderer/Renderer.hpp>
#include <Nazara/Renderer/Shader.hpp>
#include <Nazara/Renderer/Debug.hpp>
bool NzMaterialParams::IsValid() const
{
@@ -25,6 +28,78 @@ NzMaterial::~NzMaterial()
m_specularMap->RemoveResourceReference();
}
void NzMaterial::Apply() const
{
NzShader* shader = NzRenderer::GetShader();
#if NAZARA_RENDERER_SAFE
if (!shader)
{
NazaraError("No shader bound");
return;
}
#endif
int ambientColorLocation = shader->GetUniformLocation("ambientColor");
int diffuseColorLocation = shader->GetUniformLocation("diffuseColor");
int shininessLocation = shader->GetUniformLocation("shininess");
int specularColorLocation = shader->GetUniformLocation("specularColor");
if (ambientColorLocation != -1)
shader->SendColor(ambientColorLocation, m_ambientColor);
if (diffuseColorLocation != -1)
shader->SendColor(diffuseColorLocation, m_diffuseColor);
if (m_diffuseMap)
{
int diffuseMapLocation = shader->GetUniformLocation("diffuseMap");
if (diffuseMapLocation != -1)
{
nzUInt8 textureUnit;
if (shader->SendTexture(diffuseMapLocation, m_diffuseMap, &textureUnit))
NzRenderer::SetTextureSampler(textureUnit, m_diffuseSampler);
else
NazaraWarning("Failed to send diffuse map");
}
}
if (shininessLocation != -1)
shader->SendFloat(shininessLocation, m_shininess);
if (specularColorLocation != -1)
shader->SendColor(ambientColorLocation, m_specularColor);
if (m_specularMap)
{
int specularMapLocation = shader->GetUniformLocation("specularMap");
if (specularMapLocation != -1)
{
nzUInt8 textureUnit;
if (shader->SendTexture(specularMapLocation, m_specularMap, &textureUnit))
NzRenderer::SetTextureSampler(textureUnit, m_specularSampler);
else
NazaraWarning("Failed to send diffuse map");
}
}
if (m_alphaBlendingEnabled)
{
NzRenderer::Enable(nzRendererParameter_Blend, true);
NzRenderer::SetBlendFunc(m_srcBlend, m_dstBlend);
}
else
NzRenderer::Enable(nzRendererParameter_Blend, false);
if (m_zTestEnabled)
{
NzRenderer::Enable(nzRendererParameter_DepthTest, true);
NzRenderer::Enable(nzRendererParameter_DepthWrite, m_zWriteEnabled);
NzRenderer::SetDepthFunc(m_zTestCompareFunc);
}
else
NzRenderer::Enable(nzRendererParameter_DepthTest, false);
}
void NzMaterial::EnableAlphaBlending(bool alphaBlending)
{
m_alphaBlendingEnabled = alphaBlending;
@@ -50,6 +125,11 @@ NzColor NzMaterial::GetDiffuseColor() const
return m_diffuseColor;
}
const NzTextureSampler& NzMaterial::GetDiffuseSampler() const
{
return m_diffuseSampler;
}
const NzTexture* NzMaterial::GetDiffuseMap() const
{
return m_diffuseMap;
@@ -85,6 +165,11 @@ const NzTexture* NzMaterial::GetSpecularMap() const
return m_specularMap;
}
const NzTextureSampler& NzMaterial::GetSpecularSampler() const
{
return m_specularSampler;
}
nzBlendFunc NzMaterial::GetSrcBlend() const
{
return m_srcBlend;
@@ -142,13 +227,13 @@ void NzMaterial::Reset()
m_alphaBlendingEnabled = false;
m_ambientColor = NzColor::Black;
m_diffuseColor = NzColor::White;
m_diffuseSampler = NzTextureSampler();
m_dstBlend = nzBlendFunc_Zero;
m_faceCulling = nzFaceCulling_Back;
m_faceFilling = nzFaceFilling_Fill;
m_samplerFilter = nzSamplerFilter_Default;
m_samplerWrap = nzSamplerWrap_Repeat;
m_shininess = 0;
m_specularColor = NzColor::White;
m_specularSampler = NzTextureSampler();
m_srcBlend = nzBlendFunc_One;
m_zTestCompareFunc = nzRendererComparison_LessOrEqual;
m_zTestEnabled = true;
@@ -175,6 +260,11 @@ void NzMaterial::SetDiffuseMap(const NzTexture* map)
m_diffuseMap->AddResourceReference();
}
void NzMaterial::SetDiffuseSampler(const NzTextureSampler& sampler)
{
m_diffuseSampler = sampler;
}
void NzMaterial::SetDstBlend(nzBlendFunc func)
{
m_dstBlend = func;
@@ -210,6 +300,11 @@ void NzMaterial::SetSpecularMap(const NzTexture* map)
m_specularMap->AddResourceReference();
}
void NzMaterial::SetSpecularSampler(const NzTextureSampler& sampler)
{
m_specularSampler = sampler;
}
void NzMaterial::SetSrcBlend(nzBlendFunc func)
{
m_srcBlend = func;

View File

@@ -87,57 +87,6 @@ namespace
unsigned int s_stencilReference;
}
void NzRenderer::ApplyMaterial(const NzMaterial* material)
{
///FIXME: Bouger vers Material::Apply ?
#if NAZARA_RENDERER_SAFE
if (!material)
{
NazaraError("Invalid material");
return;
}
#endif
NzShader* shader = s_shader;
int ambientColorLocation = shader->GetUniformLocation("ambientColor");
int diffuseColorLocation = shader->GetUniformLocation("diffuseColor");
int diffuseMapLocation = shader->GetUniformLocation("diffuseMap");
int shininessLocation = shader->GetUniformLocation("shininess");
int specularColorLocation = shader->GetUniformLocation("specularColor");
int specularMapLocation = shader->GetUniformLocation("specularMap");
if (ambientColorLocation != -1)
shader->SendColor(ambientColorLocation, material->GetAmbientColor());
if (diffuseColorLocation != -1)
shader->SendColor(diffuseColorLocation, material->GetDiffuseColor());
if (diffuseMapLocation != -1)
shader->SendTexture(diffuseMapLocation, material->GetDiffuseMap());
if (shininessLocation != -1)
shader->SendFloat(shininessLocation, material->GetShininess());
if (specularColorLocation != -1)
shader->SendColor(ambientColorLocation, material->GetSpecularColor());
if (specularMapLocation != -1)
shader->SendTexture(specularMapLocation, material->GetSpecularMap());
if (material->IsAlphaBlendingEnabled())
{
Enable(nzRendererParameter_Blend, true);
SetBlendFunc(material->GetSrcBlend(), material->GetDstBlend());
}
else
Enable(nzRendererParameter_Blend, false);
Enable(nzRendererParameter_DepthTest, material->IsZTestEnabled());
Enable(nzRendererParameter_DepthWrite, material->IsZWriteEnabled());
SetDepthFunc(material->GetZTestCompare());
}
void NzRenderer::Clear(unsigned long flags)
{
#ifdef NAZARA_DEBUG
@@ -942,7 +891,7 @@ bool NzRenderer::SetTarget(NzRenderTarget* target)
return true;
}
void NzRenderer::SetTexture(unsigned int unit, const NzTexture* texture)
void NzRenderer::SetTexture(nzUInt8 unit, const NzTexture* texture)
{
#if NAZARA_RENDERER_SAFE
if (unit >= s_textureUnits.size())
@@ -967,7 +916,7 @@ void NzRenderer::SetTexture(unsigned int unit, const NzTexture* texture)
}
}
void NzRenderer::SetTextureSampling(unsigned int unit, const NzTextureSampler& sampler)
void NzRenderer::SetTextureSampler(nzUInt8 unit, const NzTextureSampler& sampler)
{
#if NAZARA_RENDERER_SAFE
if (unit >= s_textureUnits.size())
@@ -978,9 +927,11 @@ void NzRenderer::SetTextureSampling(unsigned int unit, const NzTextureSampler& s
#endif
s_textureUnits[unit].sampler = sampler;
s_textureUnits[unit].sampler.UseMipmaps(s_textureUnits[unit].texture->HasMipmaps());
s_textureUnits[unit].samplerUpdated = false;
s_textureUnits[unit].updated = false;
if (s_textureUnits[unit].texture)
s_textureUnits[unit].sampler.UseMipmaps(s_textureUnits[unit].texture->HasMipmaps());
}
bool NzRenderer::SetVertexBuffer(const NzVertexBuffer* vertexBuffer)