diff --git a/examples/AnimatedMesh/main.cpp b/examples/AnimatedMesh/main.cpp index b129902e1..ce57e59c9 100644 --- a/examples/AnimatedMesh/main.cpp +++ b/examples/AnimatedMesh/main.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -24,8 +24,8 @@ int main() // Maintenant nous initialisons le Renderer (Qui initialisera le noyau ainsi que le module utilitaire) // Cette étape est obligatoire pour beaucoup de fonctionnalités (Notamment le chargement de ressources et le rendu) - NzInitializer renderer; - if (!renderer) + NzInitializer nazara; + if (!nazara) { // Ça n'a pas fonctionné, le pourquoi se trouve dans le fichier NazaraLog.log std::cout << "Failed to initialize Nazara, see NazaraLog.log for further informations" << std::endl; @@ -33,8 +33,6 @@ int main() return EXIT_FAILURE; } - NzDebugDrawer::Initialize(); - // Maintenant nous pouvons utiliser le moteur comme bon nous semble, tout d'abord nous allons charger les ressources // Charger une ressource se fait actuellement manuellement, mais un ResourceManager est à venir @@ -508,6 +506,8 @@ int main() if (drawHellknight) DrawModel(hellknight); + else + NzRenderer::SetMatrix(nzMatrixType_World, hellknight.GetTransformMatrix()); if (drawSkeleton) { @@ -713,7 +713,7 @@ void DrawModel(const NzModel& model) // On récupère le submesh const NzSubMesh* subMesh = model.GetMesh()->GetSubMesh(i); - NzRenderer::ApplyMaterial(model.GetMaterial(i)); + model.GetMaterial(i)->Apply(); NzRenderer::SetVertexBuffer(subMesh->GetVertexBuffer()); diff --git a/include/Nazara/Renderer/Material.hpp b/include/Nazara/Renderer/Material.hpp index 0b6484790..7a9cc6f12 100644 --- a/include/Nazara/Renderer/Material.hpp +++ b/include/Nazara/Renderer/Material.hpp @@ -12,6 +12,7 @@ #include #include #include +#include struct NAZARA_API NzMaterialParams { @@ -33,6 +34,8 @@ class NAZARA_API NzMaterial : public NzResource NzMaterial(); ~NzMaterial(); + void Apply() const; + void EnableAlphaBlending(bool alphaBlending); void EnableZTest(bool zTest); void EnableZWrite(bool zWrite); @@ -40,14 +43,14 @@ class NAZARA_API NzMaterial : public NzResource NzColor GetAmbientColor() const; NzColor GetDiffuseColor() const; const NzTexture* GetDiffuseMap() const; + const NzTextureSampler& GetDiffuseSampler() const; nzBlendFunc GetDstBlend() const; nzFaceCulling GetFaceCulling() const; nzFaceFilling GetFaceFilling() const; - nzSamplerFilter GetSamplerFilter() const; - nzSamplerWrap GetSamplerWrap() const; float GetShininess() const; NzColor GetSpecularColor() const; const NzTexture* GetSpecularMap() const; + const NzTextureSampler& GetSpecularSampler() const; nzBlendFunc GetSrcBlend() const; nzRendererComparison GetZTestCompare() const; @@ -64,14 +67,15 @@ class NAZARA_API NzMaterial : public NzResource void SetAmbientColor(const NzColor& ambient); void SetDiffuseColor(const NzColor& diffuse); void SetDiffuseMap(const NzTexture* map); + void SetDiffuseSampler(const NzTextureSampler& sampler); void SetDstBlend(nzBlendFunc func); void SetFaceCulling(nzFaceCulling culling); void SetFaceFilling(nzFaceFilling filling); - void SetSamplerFilter(nzSamplerFilter filter); void SetSamplerWrap(nzSamplerWrap wrapMode); void SetShininess(float shininess); void SetSpecularColor(const NzColor& specular); void SetSpecularMap(const NzTexture* map); + void SetSpecularSampler(const NzTextureSampler& sampler); void SetSrcBlend(nzBlendFunc func); void SetZTestCompare(nzRendererComparison compareFunc); @@ -83,11 +87,11 @@ class NAZARA_API NzMaterial : public NzResource nzFaceCulling m_faceCulling; nzFaceFilling m_faceFilling; nzRendererComparison m_zTestCompareFunc; - nzSamplerFilter m_samplerFilter; - nzSamplerWrap m_samplerWrap; NzColor m_ambientColor; NzColor m_diffuseColor; NzColor m_specularColor; + NzTextureSampler m_diffuseSampler; + NzTextureSampler m_specularSampler; const NzTexture* m_diffuseMap; const NzTexture* m_specularMap; bool m_alphaBlendingEnabled; diff --git a/include/Nazara/Renderer/Renderer.hpp b/include/Nazara/Renderer/Renderer.hpp index 6b525f05c..6679ee730 100644 --- a/include/Nazara/Renderer/Renderer.hpp +++ b/include/Nazara/Renderer/Renderer.hpp @@ -30,8 +30,6 @@ class NAZARA_API NzRenderer NzRenderer() = delete; ~NzRenderer() = delete; - static void ApplyMaterial(const NzMaterial* material); - static void Clear(unsigned long flags = nzRendererClear_Color | nzRendererClear_Depth); static void DrawIndexedPrimitives(nzPrimitiveType primitive, unsigned int firstIndex, unsigned int indexCount); @@ -77,8 +75,8 @@ class NAZARA_API NzRenderer static void SetStencilReferenceValue(unsigned int refValue); static void SetStencilZFailOperation(nzStencilOperation zfailOperation); static bool SetTarget(NzRenderTarget* target); - static void SetTexture(unsigned int unit, const NzTexture* texture); - static void SetTextureSampling(unsigned int unit, const NzTextureSampler& sampler); + static void SetTexture(nzUInt8 unit, const NzTexture* texture); + static void SetTextureSampler(nzUInt8 textureUnit, const NzTextureSampler& sampler); static bool SetVertexBuffer(const NzVertexBuffer* vertexBuffer); static void SetViewport(const NzRectui& viewport); diff --git a/src/Nazara/Renderer/Material.cpp b/src/Nazara/Renderer/Material.cpp index 8fef7efaf..febbf9139 100644 --- a/src/Nazara/Renderer/Material.cpp +++ b/src/Nazara/Renderer/Material.cpp @@ -3,6 +3,9 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include +#include +#include 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; diff --git a/src/Nazara/Renderer/Renderer.cpp b/src/Nazara/Renderer/Renderer.cpp index 8fda92817..e36235e25 100644 --- a/src/Nazara/Renderer/Renderer.cpp +++ b/src/Nazara/Renderer/Renderer.cpp @@ -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)