Graphics: Add basic reflection mapping

This commit is contained in:
Lynix
2016-12-18 01:18:48 +01:00
parent 459c5e7a09
commit 37d36a89a6
10 changed files with 104 additions and 26 deletions

View File

@@ -214,8 +214,15 @@ namespace Nz
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_reflectionSampler.SetFilterMode(SamplerFilter_Bilinear);
s_reflectionSampler.SetWrapMode(SamplerWrap_Clamp);
s_shadowSampler.SetFilterMode(SamplerFilter_Bilinear);
s_shadowSampler.SetWrapMode(SamplerWrap_Clamp);
std::array<UInt8, 6> whitePixels = { { 255, 255, 255, 255, 255, 255 } };
s_dummyReflection.Create(ImageType_Cubemap, PixelFormatType_L8, 1, 1);
s_dummyReflection.Update(whitePixels.data());
}
catch (const std::exception& e)
{
@@ -232,6 +239,7 @@ namespace Nz
void ForwardRenderTechnique::Uninitialize()
{
s_dummyReflection.Destroy();
s_quadIndexBuffer.Reset();
s_quadVertexBuffer.Reset();
}
@@ -583,6 +591,10 @@ namespace Nz
const Shader* lastShader = nullptr;
const ShaderUniforms* shaderUniforms = nullptr;
Texture* reflectionMap = sceneData.globalReflectionTexture;
if (!reflectionMap)
reflectionMap = &s_dummyReflection;
for (auto& pipelinePair : layer.opaqueModels)
{
const MaterialPipeline* pipeline = pipelinePair.first;
@@ -618,6 +630,15 @@ namespace Nz
{
material->Apply(pipelineInstance);
if (shaderUniforms->reflectionMap != -1)
{
unsigned int textureUnit = Material::GetTextureUnit(TextureMap_ReflectionCube);
shader->SendInteger(shaderUniforms->reflectionMap, textureUnit);
Renderer::SetTexture(textureUnit, reflectionMap);
Renderer::SetTextureSampler(textureUnit, s_reflectionSampler);
}
ForwardRenderQueue::MeshInstanceContainer& meshInstances = matEntry.meshMap;
// Meshes
@@ -895,6 +916,7 @@ namespace Nz
uniforms.shaderUniformInvalidatedSlot.Connect(shader->OnShaderUniformInvalidated, this, &ForwardRenderTechnique::OnShaderInvalidated);
uniforms.eyePosition = shader->GetUniformLocation("EyePosition");
uniforms.reflectionMap = shader->GetUniformLocation("ReflectionMap");
uniforms.sceneAmbient = shader->GetUniformLocation("SceneAmbient");
uniforms.textureOverlay = shader->GetUniformLocation("TextureOverlay");
@@ -1037,6 +1059,8 @@ namespace Nz
}
IndexBuffer ForwardRenderTechnique::s_quadIndexBuffer;
Texture ForwardRenderTechnique::s_dummyReflection;
TextureSampler ForwardRenderTechnique::s_reflectionSampler;
TextureSampler ForwardRenderTechnique::s_shadowSampler;
VertexBuffer ForwardRenderTechnique::s_quadVertexBuffer;
VertexDeclaration ForwardRenderTechnique::s_billboardInstanceDeclaration;

View File

@@ -77,19 +77,20 @@ namespace Nz
NazaraAssert(m_pipelineInfo.uberShader, "Material pipeline has no uber shader");
ParameterList list;
list.SetParameter("ALPHA_MAPPING", m_pipelineInfo.hasAlphaMap);
list.SetParameter("ALPHA_TEST", m_pipelineInfo.alphaTest);
list.SetParameter("COMPUTE_TBNMATRIX", m_pipelineInfo.hasNormalMap || m_pipelineInfo.hasHeightMap);
list.SetParameter("DIFFUSE_MAPPING", m_pipelineInfo.hasDiffuseMap);
list.SetParameter("EMISSIVE_MAPPING", m_pipelineInfo.hasEmissiveMap);
list.SetParameter("NORMAL_MAPPING", m_pipelineInfo.hasNormalMap);
list.SetParameter("PARALLAX_MAPPING", m_pipelineInfo.hasHeightMap);
list.SetParameter("SHADOW_MAPPING", m_pipelineInfo.shadowReceive);
list.SetParameter("SPECULAR_MAPPING", m_pipelineInfo.hasSpecularMap);
list.SetParameter("TEXTURE_MAPPING", m_pipelineInfo.hasAlphaMap || m_pipelineInfo.hasDiffuseMap || m_pipelineInfo.hasEmissiveMap ||
m_pipelineInfo.hasNormalMap || m_pipelineInfo.hasHeightMap || m_pipelineInfo.hasSpecularMap ||
flags & ShaderFlags_TextureOverlay);
list.SetParameter("TRANSFORM", true);
list.SetParameter("ALPHA_MAPPING", m_pipelineInfo.hasAlphaMap);
list.SetParameter("ALPHA_TEST", m_pipelineInfo.alphaTest);
list.SetParameter("COMPUTE_TBNMATRIX", m_pipelineInfo.hasNormalMap || m_pipelineInfo.hasHeightMap);
list.SetParameter("DIFFUSE_MAPPING", m_pipelineInfo.hasDiffuseMap);
list.SetParameter("EMISSIVE_MAPPING", m_pipelineInfo.hasEmissiveMap);
list.SetParameter("NORMAL_MAPPING", m_pipelineInfo.hasNormalMap);
list.SetParameter("PARALLAX_MAPPING", m_pipelineInfo.hasHeightMap);
list.SetParameter("REFLECTION_MAPPING", m_pipelineInfo.reflectionMapping);
list.SetParameter("SHADOW_MAPPING", m_pipelineInfo.shadowReceive);
list.SetParameter("SPECULAR_MAPPING", m_pipelineInfo.hasSpecularMap);
list.SetParameter("TEXTURE_MAPPING", m_pipelineInfo.hasAlphaMap || m_pipelineInfo.hasDiffuseMap || m_pipelineInfo.hasEmissiveMap ||
m_pipelineInfo.hasNormalMap || m_pipelineInfo.hasHeightMap || m_pipelineInfo.hasSpecularMap ||
m_pipelineInfo.reflectionMapping || flags & ShaderFlags_TextureOverlay);
list.SetParameter("TRANSFORM", true);
list.SetParameter("FLAG_BILLBOARD", static_cast<bool>((flags & ShaderFlags_Billboard) != 0));
list.SetParameter("FLAG_DEFERRED", static_cast<bool>((flags & ShaderFlags_Deferred) != 0));
@@ -175,7 +176,7 @@ namespace Nz
OverrideShader("Shaders/PhongLighting/core.vert", &vertexShader);
#endif
uberShader->SetShader(ShaderStageType_Fragment, fragmentShader, "FLAG_DEFERRED FLAG_TEXTUREOVERLAY ALPHA_MAPPING ALPHA_TEST AUTO_TEXCOORDS DIFFUSE_MAPPING EMISSIVE_MAPPING NORMAL_MAPPING PARALLAX_MAPPING SHADOW_MAPPING SPECULAR_MAPPING");
uberShader->SetShader(ShaderStageType_Fragment, fragmentShader, "FLAG_DEFERRED FLAG_TEXTUREOVERLAY ALPHA_MAPPING ALPHA_TEST AUTO_TEXCOORDS DIFFUSE_MAPPING EMISSIVE_MAPPING NORMAL_MAPPING PARALLAX_MAPPING REFLECTION_MAPPING SHADOW_MAPPING SPECULAR_MAPPING");
uberShader->SetShader(ShaderStageType_Vertex, vertexShader, "FLAG_BILLBOARD FLAG_DEFERRED FLAG_INSTANCING FLAG_VERTEXCOLOR COMPUTE_TBNMATRIX PARALLAX_MAPPING SHADOW_MAPPING TEXTURE_MAPPING TRANSFORM UNIFORM_VERTEX_DEPTH");
UberShaderLibrary::Register("PhongLighting", uberShader);