Merge 'remotes/trunk'
[0ad.git] / source / renderer / RenderModifiers.cpp
blob49f224694ff0024f906212381417c7474a5225f6
1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #include "precompiled.h"
20 #include "renderer/RenderModifiers.h"
22 #include "graphics/GameView.h"
23 #include "graphics/LightEnv.h"
24 #include "graphics/LOSTexture.h"
25 #include "graphics/Model.h"
26 #include "graphics/TextureManager.h"
27 #include "maths/Vector3D.h"
28 #include "maths/Vector4D.h"
29 #include "maths/Matrix3D.h"
30 #include "ps/CStrInternStatic.h"
31 #include "ps/Game.h"
32 #include "renderer/Renderer.h"
33 #include "renderer/SceneRenderer.h"
34 #include "renderer/ShadowMap.h"
36 #include <boost/algorithm/string.hpp>
38 ///////////////////////////////////////////////////////////////////////////////////////////////
39 // LitRenderModifier implementation
41 LitRenderModifier::LitRenderModifier()
42 : m_Shadow(0), m_LightEnv(0)
46 LitRenderModifier::~LitRenderModifier()
50 // Set the shadow map for subsequent rendering
51 void LitRenderModifier::SetShadowMap(const ShadowMap* shadow)
53 m_Shadow = shadow;
56 // Set the light environment for subsequent rendering
57 void LitRenderModifier::SetLightEnv(const CLightEnv* lightenv)
59 m_LightEnv = lightenv;
62 ///////////////////////////////////////////////////////////////////////////////////////////////
63 // ShaderRenderModifier implementation
65 ShaderRenderModifier::ShaderRenderModifier()
66 : m_ShadingColor(1.0f, 1.0f, 1.0f, 1.0f), m_PlayerColor(1.0f, 1.0f, 1.0f, 1.0f)
70 void ShaderRenderModifier::BeginPass(
71 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
72 Renderer::Backend::IShaderProgram* shader)
74 const CMatrix3D transform =
75 g_Renderer.GetSceneRenderer().GetViewCamera().GetViewProjection();
76 deviceCommandContext->SetUniform(
77 shader->GetBindingSlot(str_transform), transform.AsFloatArray());
78 deviceCommandContext->SetUniform(
79 shader->GetBindingSlot(str_cameraPos),
80 g_Renderer.GetSceneRenderer().GetViewCamera().GetOrientation().GetTranslation().AsFloatArray());
82 if (GetShadowMap())
83 GetShadowMap()->BindTo(deviceCommandContext, shader);
85 if (GetLightEnv())
87 deviceCommandContext->SetUniform(
88 shader->GetBindingSlot(str_ambient),
89 GetLightEnv()->m_AmbientColor.AsFloatArray());
90 deviceCommandContext->SetUniform(
91 shader->GetBindingSlot(str_sunDir),
92 GetLightEnv()->GetSunDir().AsFloatArray());
93 deviceCommandContext->SetUniform(
94 shader->GetBindingSlot(str_sunColor),
95 GetLightEnv()->m_SunColor.AsFloatArray());
97 deviceCommandContext->SetUniform(
98 shader->GetBindingSlot(str_fogColor),
99 GetLightEnv()->m_FogColor.AsFloatArray());
100 deviceCommandContext->SetUniform(
101 shader->GetBindingSlot(str_fogParams),
102 GetLightEnv()->m_FogFactor, GetLightEnv()->m_FogMax);
105 if (shader->GetBindingSlot(str_losTex) >= 0)
107 CLOSTexture& los = g_Renderer.GetSceneRenderer().GetScene().GetLOSTexture();
108 deviceCommandContext->SetTexture(
109 shader->GetBindingSlot(str_losTex), los.GetTextureSmooth());
110 // Don't bother sending the whole matrix, we just need two floats (scale and translation)
111 deviceCommandContext->SetUniform(
112 shader->GetBindingSlot(str_losTransform),
113 los.GetTextureMatrix()[0], los.GetTextureMatrix()[12]);
116 m_BindingInstancingTransform = shader->GetBindingSlot(str_instancingTransform);
117 m_BindingShadingColor = shader->GetBindingSlot(str_shadingColor);
118 m_BindingPlayerColor = shader->GetBindingSlot(str_playerColor);
120 if (m_BindingShadingColor >= 0)
122 m_ShadingColor = CColor(1.0f, 1.0f, 1.0f, 1.0f);
123 deviceCommandContext->SetUniform(
124 m_BindingShadingColor, m_ShadingColor.AsFloatArray());
127 if (m_BindingPlayerColor >= 0)
129 m_PlayerColor = g_Game->GetPlayerColor(0);
130 deviceCommandContext->SetUniform(
131 m_BindingPlayerColor, m_PlayerColor.AsFloatArray());
135 void ShaderRenderModifier::PrepareModel(
136 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
137 CModel* model)
139 if (m_BindingInstancingTransform >= 0)
141 deviceCommandContext->SetUniform(
142 m_BindingInstancingTransform, model->GetTransform().AsFloatArray());
145 if (m_BindingShadingColor >= 0 && m_ShadingColor != model->GetShadingColor())
147 m_ShadingColor = model->GetShadingColor();
148 deviceCommandContext->SetUniform(
149 m_BindingShadingColor, m_ShadingColor.AsFloatArray());
152 if (m_BindingPlayerColor >= 0)
154 const CColor& playerColor = g_Game->GetPlayerColor(model->GetPlayerID());
155 if (m_PlayerColor != playerColor)
157 m_PlayerColor = playerColor;
158 deviceCommandContext->SetUniform(
159 m_BindingPlayerColor, m_PlayerColor.AsFloatArray());