Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RenderModifiers.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 /*
19  * Implementation of common RenderModifiers
20  */
21 
22 #include "precompiled.h"
23 
24 #include "lib/ogl.h"
25 #include "maths/Vector3D.h"
26 #include "maths/Vector4D.h"
27 #include "maths/Matrix3D.h"
28 
29 #include "ps/Game.h"
30 
31 #include "graphics/GameView.h"
32 #include "graphics/LightEnv.h"
33 #include "graphics/LOSTexture.h"
34 #include "graphics/Model.h"
36 
38 #include "renderer/Renderer.h"
39 #include "renderer/ShadowMap.h"
40 
41 #include <boost/algorithm/string.hpp>
42 
43 ///////////////////////////////////////////////////////////////////////////////////////////////
44 // LitRenderModifier implementation
45 
47  : m_Shadow(0), m_LightEnv(0)
48 {
49 }
50 
52 {
53 }
54 
55 // Set the shadow map for subsequent rendering
57 {
58  m_Shadow = shadow;
59 }
60 
61 // Set the light environment for subsequent rendering
63 {
64  m_LightEnv = lightenv;
65 }
66 
67 ///////////////////////////////////////////////////////////////////////////////////////////////
68 // ShaderRenderModifier implementation
69 
71 {
72 }
73 
75 {
76  shader->Uniform(str_transform, g_Renderer.GetViewCamera().GetViewProjection());
77  shader->Uniform(str_cameraPos, g_Renderer.GetViewCamera().GetOrientation().GetTranslation());
78 
79  if (GetShadowMap() && shader->GetTextureBinding(str_shadowTex).Active())
80  {
81  shader->BindTexture(str_shadowTex, GetShadowMap()->GetTexture());
82  shader->Uniform(str_shadowTransform, GetShadowMap()->GetTextureMatrix());
83  int width = GetShadowMap()->GetWidth();
84  int height = GetShadowMap()->GetHeight();
85  shader->Uniform(str_shadowScale, width, height, 1.0f / width, 1.0f / height);
86  }
87 
88  if (GetLightEnv())
89  {
90  shader->Uniform(str_ambient, GetLightEnv()->m_UnitsAmbientColor);
91  shader->Uniform(str_sunDir, GetLightEnv()->GetSunDir());
92  shader->Uniform(str_sunColor, GetLightEnv()->m_SunColor);
93 
94  shader->Uniform(str_fogColor, GetLightEnv()->m_FogColor);
95  shader->Uniform(str_fogParams, GetLightEnv()->m_FogFactor, GetLightEnv()->m_FogMax, 0.f, 0.f);
96  }
97 
98  if (shader->GetTextureBinding(str_losTex).Active())
99  {
100  CLOSTexture& los = g_Renderer.GetScene().GetLOSTexture();
101  shader->BindTexture(str_losTex, los.GetTextureSmooth());
102  // Don't bother sending the whole matrix, we just need two floats (scale and translation)
103  shader->Uniform(str_losTransform, los.GetTextureMatrix()[0], los.GetTextureMatrix()[12], 0.f, 0.f);
104  }
105 
106  m_BindingInstancingTransform = shader->GetUniformBinding(str_instancingTransform);
107  m_BindingShadingColor = shader->GetUniformBinding(str_shadingColor);
108  m_BindingPlayerColor = shader->GetUniformBinding(str_playerColor);
109 }
110 
112 {
114  shader->Uniform(m_BindingInstancingTransform, model->GetTransform());
115 
117  shader->Uniform(m_BindingShadingColor, model->GetShadingColor());
118 
120  shader->Uniform(m_BindingPlayerColor, g_Game->GetPlayerColour(model->GetPlayerID()));
121 }
CShaderProgram::Binding m_BindingShadingColor
void BeginPass(const CShaderProgramPtr &shader)
BeginPass: Setup OpenGL for the given rendering pass.
GLuint GetTextureSmooth()
Definition: LOSTexture.cpp:106
Class ShadowMap: Maintain the shadow map texture and perform necessary OpenGL setup, including matrix calculations.
Definition: ShadowMap.h:39
const CLightEnv * GetLightEnv() const
#define g_Renderer
Definition: Renderer.h:61
const CLightEnv * m_LightEnv
int GetWidth() const
GetWidth: Return the width of the depth texture.
Definition: ShadowMap.cpp:559
void PrepareModel(const CShaderProgramPtr &shader, CModel *model)
PrepareModel: Called before rendering the given model.
void SetShadowMap(const ShadowMap *shadow)
SetShadowMap: Set the shadow map that will be used for rendering.
CGame * g_Game
Globally accessible pointer to the CGame object.
Definition: Game.cpp:56
CShaderProgram::Binding m_BindingInstancingTransform
const ShadowMap * m_Shadow
virtual player_id_t GetPlayerID() const
void SetLightEnv(const CLightEnv *lightenv)
SetLightEnv: Set the light environment that will be used for rendering.
const ShadowMap * GetShadowMap() const
static size_t model
Definition: x86_x64.cpp:211
CColor GetPlayerColour(int player) const
Definition: Game.cpp:354
Maintains the LOS (fog-of-war / shroud-of-darkness) texture, used for rendering and for the minimap...
Definition: LOSTexture.h:32
const CMatrix3D & GetTransform() const
Definition: Model.h:50
virtual CColor GetShadingColor() const
const CMatrix3D & GetTextureMatrix()
Returns a matrix to map (x,y,z) world coordinates onto (u,v) LOS texture coordinates, in the form expected by glLoadMatrixf.
Definition: LOSTexture.cpp:184
Class CLightEnv: description of a lighting environment - contains all the necessary parameters for re...
Definition: LightEnv.h:36
shared_ptr< CShaderProgram > CShaderProgramPtr
CShaderProgram::Binding m_BindingPlayerColor
int GetHeight() const
GetHeight: Return the height of the depth texture.
Definition: ShadowMap.cpp:564
bool Active()
Returns whether this uniform attribute is active in the shader.