Added Material class

Former-commit-id: db16249b1ecbfdc59ecdca709cb75f4bf1b6d10b
This commit is contained in:
Lynix 2012-11-25 14:32:19 +01:00
parent d3c027e29c
commit b662bca33d
2 changed files with 295 additions and 0 deletions

View File

@ -0,0 +1,89 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_MATERIAL_HPP
#define NAZARA_MATERIAL_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Color.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Renderer/Texture.hpp>
struct NAZARA_API NzMaterialParams
{
bool loadDiffuseMap = true;
bool loadSpecularMap = true;
bool IsValid() const;
};
class NzMaterial;
using NzMaterialLoader = NzResourceLoader<NzMaterial, NzMaterialParams>;
class NAZARA_API NzMaterial : public NzResource
{
friend NzMaterialLoader;
public:
NzMaterial();
~NzMaterial();
void EnableAlphaBlending(bool alphaBlending);
void EnableZTest(bool zTest);
void EnableZWrite(bool zWrite);
NzColor GetAmbientColor() const;
NzColor GetDiffuseColor() const;
const NzTexture* GetDiffuseMap() const;
nzBlendFunc GetDstAlpha() const;
nzFaceCulling GetFaceCulling() const;
nzFaceFilling GetFaceFilling() const;
float GetShininess() const;
NzColor GetSpecularColor() const;
const NzTexture* GetSpecularMap() const;
nzBlendFunc GetSrcAlpha() const;
nzRendererComparison GetZTestCompare() const;
bool IsAlphaBlendingEnabled() const;
bool IsZTestEnabled() const;
bool IsZWriteEnabled() const;
void Reset();
void SetAmbientColor(const NzColor& ambient);
void SetDiffuseColor(const NzColor& diffuse);
void SetDiffuseMap(const NzTexture* map);
void SetDstAlpha(nzBlendFunc func);
void SetFaceCulling(nzFaceCulling culling);
void SetFaceFilling(nzFaceFilling filling);
void SetShininess(float shininess);
void SetSpecularColor(const NzColor& specular);
void SetSpecularMap(const NzTexture* map);
void SetSrcAlpha(nzBlendFunc func);
void SetZTestCompare(nzRendererComparison compareFunc);
private:
nzBlendFunc m_dstAlpha;
nzBlendFunc m_srcAlpha;
nzFaceCulling m_faceCulling;
nzFaceFilling m_faceFilling;
nzRendererComparison m_zTestCompareFunc;
NzColor m_ambientColor;
NzColor m_diffuseColor;
NzColor m_specularColor;
const NzTexture* m_diffuseMap;
const NzTexture* m_specularMap;
bool m_alphaBlendingEnabled;
bool m_zTestEnabled;
bool m_zWriteEnabled;
float m_shininess;
static NzMaterialLoader::LoaderList s_loaders;
};
#endif // NAZARA_MATERIAL_HPP

View File

@ -0,0 +1,206 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/Material.hpp>
bool NzMaterialParams::IsValid() const
{
return true;
}
NzMaterial::NzMaterial() :
m_diffuseMap(nullptr),
m_specularMap(nullptr)
{
Reset();
}
NzMaterial::~NzMaterial()
{
if (m_diffuseMap)
m_diffuseMap->RemoveResourceReference();
if (m_specularMap)
m_specularMap->RemoveResourceReference();
}
void NzMaterial::EnableAlphaBlending(bool alphaBlending)
{
m_alphaBlendingEnabled = alphaBlending;
}
void NzMaterial::EnableZTest(bool zTest)
{
m_zTestEnabled = zTest;
}
void NzMaterial::EnableZWrite(bool zWrite)
{
m_zWriteEnabled = zWrite;
}
NzColor NzMaterial::GetAmbientColor() const
{
return m_ambientColor;
}
NzColor NzMaterial::GetDiffuseColor() const
{
return m_diffuseColor;
}
const NzTexture* NzMaterial::GetDiffuseMap() const
{
return m_diffuseMap;
}
nzBlendFunc NzMaterial::GetDstAlpha() const
{
return m_dstAlpha;
}
nzFaceCulling NzMaterial::GetFaceCulling() const
{
return m_faceCulling;
}
nzFaceFilling NzMaterial::GetFaceFilling() const
{
return m_faceFilling;
}
float NzMaterial::GetShininess() const
{
return m_shininess;
}
NzColor NzMaterial::GetSpecularColor() const
{
return m_specularColor;
}
const NzTexture* NzMaterial::GetSpecularMap() const
{
return m_specularMap;
}
nzBlendFunc NzMaterial::GetSrcAlpha() const
{
return m_srcAlpha;
}
nzRendererComparison NzMaterial::GetZTestCompare() const
{
return m_zTestCompareFunc;
}
bool NzMaterial::IsAlphaBlendingEnabled() const
{
return m_alphaBlendingEnabled;
}
bool NzMaterial::IsZTestEnabled() const
{
return m_zTestEnabled;
}
bool NzMaterial::IsZWriteEnabled() const
{
return m_zWriteEnabled;
}
void NzMaterial::Reset()
{
if (m_diffuseMap)
{
m_diffuseMap->RemoveResourceReference();
m_diffuseMap = nullptr;
}
if (m_specularMap)
{
m_specularMap->RemoveResourceReference();
m_specularMap = nullptr;
}
m_alphaBlendingEnabled = false;
m_ambientColor = NzColor::Black;
m_diffuseColor = NzColor::White;
m_dstAlpha = nzBlendFunc_Zero;
m_faceCulling = nzFaceCulling_Back;
m_faceFilling = nzFaceFilling_Fill;
m_shininess = 0;
m_specularColor = NzColor::White;
m_srcAlpha = nzBlendFunc_One;
m_zTestCompareFunc = nzRendererComparison_LessOrEqual;
m_zTestEnabled = true;
m_zWriteEnabled = true;
}
void NzMaterial::SetAmbientColor(const NzColor& ambient)
{
m_ambientColor = ambient;
}
void NzMaterial::SetDiffuseColor(const NzColor& diffuse)
{
m_diffuseColor = diffuse;
}
void NzMaterial::SetDiffuseMap(const NzTexture* map)
{
if (m_diffuseMap)
m_diffuseMap->RemoveResourceReference();
m_diffuseMap = map;
if (m_diffuseMap)
m_diffuseMap->AddResourceReference();
}
void NzMaterial::SetDstAlpha(nzBlendFunc func)
{
m_dstAlpha = func;
}
void NzMaterial::SetFaceCulling(nzFaceCulling culling)
{
m_faceCulling = culling;
}
void NzMaterial::SetFaceFilling(nzFaceFilling filling)
{
m_faceFilling = filling;
}
void NzMaterial::SetShininess(float shininess)
{
m_shininess = shininess;
}
void NzMaterial::SetSpecularColor(const NzColor& specular)
{
m_specularColor = specular;
}
void NzMaterial::SetSpecularMap(const NzTexture* map)
{
if (m_specularMap)
m_specularMap->RemoveResourceReference();
m_specularMap = map;
if (m_specularMap)
m_specularMap->AddResourceReference();
}
void NzMaterial::SetSrcAlpha(nzBlendFunc func)
{
m_srcAlpha = func;
}
void NzMaterial::SetZTestCompare(nzRendererComparison compareFunc)
{
m_zTestEnabled = compareFunc;
}
NzMaterialLoader::LoaderList NzMaterial::s_loaders;