Line data Source code
1 : /* Copyright (C) 2022 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
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.
8 : *
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.
13 : *
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/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "renderer/RenderModifiers.h"
21 :
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 "lib/ogl.h"
28 : #include "maths/Vector3D.h"
29 : #include "maths/Vector4D.h"
30 : #include "maths/Matrix3D.h"
31 : #include "ps/CStrInternStatic.h"
32 : #include "ps/Game.h"
33 : #include "renderer/Renderer.h"
34 : #include "renderer/SceneRenderer.h"
35 : #include "renderer/ShadowMap.h"
36 :
37 : #include <boost/algorithm/string.hpp>
38 :
39 : ///////////////////////////////////////////////////////////////////////////////////////////////
40 : // LitRenderModifier implementation
41 :
42 0 : LitRenderModifier::LitRenderModifier()
43 0 : : m_Shadow(0), m_LightEnv(0)
44 : {
45 0 : }
46 :
47 0 : LitRenderModifier::~LitRenderModifier()
48 : {
49 0 : }
50 0 :
51 : // Set the shadow map for subsequent rendering
52 0 : void LitRenderModifier::SetShadowMap(const ShadowMap* shadow)
53 0 : {
54 : m_Shadow = shadow;
55 0 : }
56 :
57 : // Set the light environment for subsequent rendering
58 0 : void LitRenderModifier::SetLightEnv(const CLightEnv* lightenv)
59 : {
60 0 : m_LightEnv = lightenv;
61 0 : }
62 :
63 : ///////////////////////////////////////////////////////////////////////////////////////////////
64 0 : // ShaderRenderModifier implementation
65 :
66 0 : ShaderRenderModifier::ShaderRenderModifier()
67 0 : {
68 : }
69 :
70 : void ShaderRenderModifier::BeginPass(const CShaderProgramPtr& shader)
71 : {
72 0 : shader->Uniform(str_transform, g_Renderer.GetSceneRenderer().GetViewCamera().GetViewProjection());
73 : shader->Uniform(str_cameraPos, g_Renderer.GetSceneRenderer().GetViewCamera().GetOrientation().GetTranslation());
74 0 :
75 : if (GetShadowMap())
76 0 : GetShadowMap()->BindTo(shader);
77 :
78 0 : if (GetLightEnv())
79 0 : {
80 : shader->Uniform(str_ambient, GetLightEnv()->m_AmbientColor);
81 0 : shader->Uniform(str_sunDir, GetLightEnv()->GetSunDir());
82 0 : shader->Uniform(str_sunColor, GetLightEnv()->m_SunColor);
83 :
84 0 : shader->Uniform(str_fogColor, GetLightEnv()->m_FogColor);
85 : shader->Uniform(str_fogParams, GetLightEnv()->m_FogFactor, GetLightEnv()->m_FogMax, 0.f, 0.f);
86 0 : }
87 0 :
88 0 : if (shader->GetTextureBinding(str_losTex).Active())
89 : {
90 0 : CLOSTexture& los = g_Renderer.GetSceneRenderer().GetScene().GetLOSTexture();
91 0 : shader->BindTexture(str_losTex, los.GetTextureSmooth());
92 : // Don't bother sending the whole matrix, we just need two floats (scale and translation)
93 : shader->Uniform(str_losTransform, los.GetTextureMatrix()[0], los.GetTextureMatrix()[12], 0.f, 0.f);
94 0 : }
95 :
96 0 : m_BindingInstancingTransform = shader->GetUniformBinding(str_instancingTransform);
97 0 : m_BindingShadingColor = shader->GetUniformBinding(str_shadingColor);
98 : m_BindingPlayerColor = shader->GetUniformBinding(str_playerColor);
99 0 : }
100 :
101 : void ShaderRenderModifier::PrepareModel(const CShaderProgramPtr& shader, CModel* model)
102 0 : {
103 0 : if (m_BindingInstancingTransform.Active())
104 0 : shader->Uniform(m_BindingInstancingTransform, model->GetTransform());
105 0 :
106 : if (m_BindingShadingColor.Active())
107 0 : shader->Uniform(m_BindingShadingColor, model->GetShadingColor());
108 :
109 0 : if (m_BindingPlayerColor.Active())
110 0 : shader->Uniform(m_BindingPlayerColor, g_Game->GetPlayerColor(model->GetPlayerID()));
111 : }
|