OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4FoW Class Reference

#include <C4FoW.h>

Collaboration diagram for C4FoW:
[legend]

Public Member Functions

 C4FoW ()
 
 ~C4FoW ()
 
C4ShaderGetFramebufShader ()
 
C4ShaderGetRenderShader ()
 
void ClearDeletedLights ()
 
void Add (C4Object *pObj)
 
void Remove (C4Object *pObj)
 
void Update (C4Rect r, C4Player *player)
 
void Invalidate (C4Rect r)
 
void Render (class C4FoWRegion *pRegion, const C4TargetFacet *pOnScreen, C4Player *pPlr, const StdProjectionMatrix &projectionMatrix)
 

Public Attributes

C4FoWAmbient Ambient
 

Detailed Description

This class holds all lights for the objects. It forwards the update, invalidation and render calls each to the lights.

Definition at line 101 of file C4FoW.h.

Constructor & Destructor Documentation

C4FoW::C4FoW ( )

Definition at line 24 of file C4FoW.cpp.

25  : pLights(nullptr), deleted_lights(nullptr)
26 {
27 }
C4FoW::~C4FoW ( )

Definition at line 29 of file C4FoW.cpp.

References ClearDeletedLights(), C4Draw::EnsureMainContextSelected(), and pDraw.

30 {
31  if (deleted_lights)
32  {
35  }
36 }
void ClearDeletedLights()
Definition: C4FoW.cpp:38
C4Draw * pDraw
Definition: C4Draw.cpp:45
virtual bool EnsureMainContextSelected()=0

Here is the call graph for this function:

Member Function Documentation

void C4FoW::Add ( C4Object pObj)

Updates the view range of the given object in its associated light or create a new light if none exists yet.

Definition at line 169 of file C4FoW.cpp.

References C4FoWLight::getNext(), C4FoWLight::getObj(), C4Object::lightColor, C4Object::lightFadeoutRange, C4Object::lightRange, Remove(), C4FoWLight::SetColor(), C4FoWLight::SetReach(), and C4PropList::Status.

Referenced by C4Object::UpdateLight().

170 {
171 #ifndef USE_CONSOLE
172  // No view range? Probably want to remove instead
173  if(!pObj->lightRange && !pObj->lightFadeoutRange)
174  {
175  Remove(pObj);
176  return;
177  }
178 
179  // Safety
180  if (!pObj->Status) return;
181 
182  // Look for matching light
183  C4FoWLight *pLight;
184  for (pLight = pLights; pLight; pLight = pLight->getNext())
185  if (pLight->getObj() == pObj)
186  break;
187 
188  if (pLight)
189  {
190 
191  // Update reach and light color
192  pLight->SetReach(pObj->lightRange, pObj->lightFadeoutRange);
193  pLight->SetColor(pObj->lightColor);
194  }
195  else
196  {
197  // Create new light otherwise
198  pLight = new C4FoWLight(pObj);
199  pLight->pNext = pLights;
200  pLights = pLight;
201  }
202 #endif
203 }
void SetColor(uint32_t iValue)
Definition: C4FoWLight.cpp:83
C4Object * getObj() const
Definition: C4FoWLight.h:74
C4FoWLight * getNext() const
Definition: C4FoWLight.h:73
int32_t lightFadeoutRange
Definition: C4Object.h:123
int32_t lightRange
Definition: C4Object.h:122
int32_t Status
Definition: C4PropList.h:170
void Remove(C4Object *pObj)
Definition: C4FoW.cpp:205
void SetReach(int32_t iReach, int32_t iFadeout)
Definition: C4FoWLight.cpp:60
uint32_t lightColor
Definition: C4Object.h:124

Here is the call graph for this function:

Here is the caller graph for this function:

void C4FoW::ClearDeletedLights ( )

Definition at line 38 of file C4FoW.cpp.

References C4FoWLight::getNext().

Referenced by Render(), and ~C4FoW().

39 {
40 #ifndef USE_CONSOLE
41  // Kill any dead lights
42  while (deleted_lights)
43  {
44  C4FoWLight *light = deleted_lights;
45  deleted_lights = deleted_lights->getNext();
46  delete light;
47  }
48 #endif
49 }
C4FoWLight * getNext() const
Definition: C4FoWLight.h:73

Here is the call graph for this function:

Here is the caller graph for this function:

C4Shader * C4FoW::GetFramebufShader ( )

Definition at line 51 of file C4FoW.cpp.

References C4Shader::AddFragmentSlices(), C4Shader::AddVertexSlices(), C4FoWFSA_Count, C4FoWFSA_Position, C4FoWFSA_TexCoord, C4FoWFSU_Count, C4FoWFSU_ProjectionMatrix, C4FoWFSU_Texture, C4Shader::ClearSlices(), C4Shader::Init(), and C4Shader::Initialised().

Referenced by C4FoWRegion::Render().

52 {
53 #ifndef USE_CONSOLE
54  // Not created yet?
55  if (!FramebufShader.Initialised())
56  {
57  // Create the frame buffer shader. The details are in C4FoWRegion, but
58  // this is about how to utilise old frame buffer data in the lights texture.
59  // Or put in other words: This shader is responsible for fading lights out.
60  const char* FramebufVertexShader =
61  "in vec2 oc_Position;\n"
62  "in vec2 oc_TexCoord;\n"
63  "out vec2 texcoord;\n"
64  "uniform mat4 projectionMatrix;\n"
65  "\n"
66  "slice(position)\n"
67  "{\n"
68  " gl_Position = projectionMatrix * vec4(oc_Position, 0.0, 1.0);\n"
69  "}\n"
70  "\n"
71  "slice(texcoord)\n"
72  "{\n"
73  " texcoord = oc_TexCoord;\n"
74  "}";
75 
76  const char* FramebufFragmentShader =
77  "in vec2 texcoord;\n"
78  "uniform sampler2D tex;\n"
79  "out vec4 fragColor;\n"
80  "\n"
81  "slice(color)\n"
82  "{\n"
83  " fragColor = texture(tex, texcoord);\n"
84  "}";
85 
86  FramebufShader.AddVertexSlices("built-in FoW framebuf shader", FramebufVertexShader);
87  FramebufShader.AddFragmentSlices("built-in FoW framebuf shader", FramebufFragmentShader);
88 
89  const char *szUniforms[C4FoWFSU_Count + 1];
90  szUniforms[C4FoWFSU_ProjectionMatrix] = "projectionMatrix";
91  szUniforms[C4FoWFSU_Texture] = "tex";
92  szUniforms[C4FoWFSU_Count] = nullptr;
93 
94  const char *szAttributes[C4FoWFSA_Count + 1];
95  szAttributes[C4FoWFSA_Position] = "oc_Position";
96  szAttributes[C4FoWFSA_TexCoord] = "oc_TexCoord";
97  szAttributes[C4FoWFSA_Count] = nullptr;
98 
99  if (!FramebufShader.Init("framebuf", szUniforms, szAttributes)) {
100  FramebufShader.ClearSlices();
101  return nullptr;
102  }
103  }
104  return &FramebufShader;
105 #else
106  return nullptr;
107 #endif
108 }
void AddFragmentSlices(const char *szWhat, const char *szText, const char *szSource="", int iFileTime=0)
Definition: C4Shader.cpp:89
void AddVertexSlices(const char *szWhat, const char *szText, const char *szSource="", int iFileTime=0)
Definition: C4Shader.cpp:84
void ClearSlices()
Definition: C4Shader.cpp:326
bool Initialised() const
Definition: C4Shader.h:107
bool Init(const char *szWhat, const char **szUniforms, const char **szAttributes)
Definition: C4Shader.cpp:350

Here is the call graph for this function:

Here is the caller graph for this function:

C4Shader * C4FoW::GetRenderShader ( )

Definition at line 110 of file C4FoW.cpp.

References C4Shader::AddFragmentSlices(), C4Shader::AddVertexSlices(), C4FoWRSA_Color, C4FoWRSA_Count, C4FoWRSA_Position, C4FoWRSU_Count, C4FoWRSU_ProjectionMatrix, C4FoWRSU_VertexOffset, C4Shader::ClearSlices(), C4Shader::Init(), and C4Shader::Initialised().

Referenced by Render().

111 {
112 #ifndef USE_CONSOLE
113  // Not created yet?
114  if (!RenderShader.Initialised())
115  {
116  // Create the render shader. Fairly simple pass-through.
117  const char* RenderVertexShader =
118  "in vec2 oc_Position;\n"
119  "in vec4 oc_Color;\n"
120  "out vec4 vtxColor;\n"
121  "uniform mat4 projectionMatrix;\n"
122  "uniform vec2 vertexOffset;\n"
123  "\n"
124  "slice(position)\n"
125  "{\n"
126  " gl_Position = projectionMatrix * vec4(oc_Position + vertexOffset, 0.0, 1.0);\n"
127  "}\n"
128  "\n"
129  "slice(color)\n"
130  "{\n"
131  " vtxColor = oc_Color;\n"
132  "}";
133 
134  const char* RenderFragmentShader =
135  "in vec4 vtxColor;\n"
136  "out vec4 fragColor;\n"
137  "\n"
138  "slice(color)\n"
139  "{\n"
140  " fragColor = vtxColor;\n"
141  "}";
142 
143  RenderShader.AddVertexSlices("built-in FoW render shader", RenderVertexShader);
144  RenderShader.AddFragmentSlices("built-in FoW render shader", RenderFragmentShader);
145 
146  const char* szUniforms[C4FoWRSU_Count + 1];
147  szUniforms[C4FoWRSU_ProjectionMatrix] = "projectionMatrix";
148  szUniforms[C4FoWRSU_VertexOffset] = "vertexOffset";
149  szUniforms[C4FoWRSU_Count] = nullptr;
150 
151  const char* szAttributes[C4FoWRSA_Count + 1];
152  szAttributes[C4FoWRSA_Position] = "oc_Position";
153  szAttributes[C4FoWRSA_Color] = "oc_Color";
154  szAttributes[C4FoWRSA_Count] = nullptr;
155 
156  if (!RenderShader.Init("fowRender", szUniforms, szAttributes)) {
157  RenderShader.ClearSlices();
158  return nullptr;
159  }
160 
161  }
162  return &RenderShader;
163 #else
164  return nullptr;
165 #endif
166 }
void AddFragmentSlices(const char *szWhat, const char *szText, const char *szSource="", int iFileTime=0)
Definition: C4Shader.cpp:89
void AddVertexSlices(const char *szWhat, const char *szText, const char *szSource="", int iFileTime=0)
Definition: C4Shader.cpp:84
void ClearSlices()
Definition: C4Shader.cpp:326
bool Initialised() const
Definition: C4Shader.h:107
bool Init(const char *szWhat, const char **szUniforms, const char **szAttributes)
Definition: C4Shader.cpp:350

Here is the call graph for this function:

Here is the caller graph for this function:

void C4FoW::Invalidate ( C4Rect  r)

Triggers the recalculation of all light beams within the given rectangle because the landscape changed.

Definition at line 225 of file C4FoW.cpp.

References C4FoWLight::getNext().

226 {
227 #ifndef USE_CONSOLE
228  for (C4FoWLight *pLight = pLights; pLight; pLight = pLight->getNext())
229  pLight->Invalidate(r);
230 #endif
231 }
C4FoWLight * getNext() const
Definition: C4FoWLight.h:73

Here is the call graph for this function:

void C4FoW::Remove ( C4Object pObj)

Removes the light associated with the given object, if any

Definition at line 205 of file C4FoW.cpp.

References C4FoWLight::getNext().

Referenced by Add(), and C4Object::StatusDeactivate().

206 {
207 #ifndef USE_CONSOLE
208  // Look for matching light
209  C4FoWLight *pPrev = nullptr, *pLight;
210  for (pLight = pLights; pLight; pPrev = pLight, pLight = pLight->getNext())
211  if (pLight->getObj() == pObj)
212  break;
213  if (!pLight)
214  return;
215 
216  // Remove from list
217  (pPrev ? pPrev->pNext : pLights) = pLight->getNext();
218 
219  // Delete on next render pass
220  pLight->pNext = deleted_lights;
221  deleted_lights = pLight;
222 #endif
223 }
C4FoWLight * getNext() const
Definition: C4FoWLight.h:73

Here is the call graph for this function:

Here is the caller graph for this function:

void C4FoW::Render ( class C4FoWRegion pRegion,
const C4TargetFacet pOnScreen,
C4Player pPlr,
const StdProjectionMatrix &  projectionMatrix 
)

Definition at line 242 of file C4FoW.cpp.

References C4FoWRSU_ProjectionMatrix, Call, ClearDeletedLights(), C4ShaderCall::Finish(), C4FoWLight::getNext(), GetRenderShader(), C4ShaderCall::SetUniformMatrix4x4(), and C4ShaderCall::Start().

Referenced by C4FoWRegion::Render().

243 {
244 #ifndef USE_CONSOLE
245  // Delete any dead lights
247  // Set up shader. If this one doesn't work, we're really in trouble.
248  C4Shader *pShader = GetRenderShader();
249  assert(pShader);
250  if (!pShader) return;
251 
252  C4ShaderCall Call(pShader);
253  Call.Start();
254  Call.SetUniformMatrix4x4(C4FoWRSU_ProjectionMatrix, projectionMatrix);
255 
256  for (C4FoWLight *pLight = pLights; pLight; pLight = pLight->getNext())
257  if (pLight->IsVisibleForPlayer(pPlr))
258  pLight->Render(pRegion, pOnScreen, Call);
259 
260  Call.Finish();
261 #endif
262 }
C4FoWLight * getNext() const
Definition: C4FoWLight.h:73
void ClearDeletedLights()
Definition: C4FoW.cpp:38
C4Shader * GetRenderShader()
Definition: C4FoW.cpp:110

Here is the call graph for this function:

Here is the caller graph for this function:

void C4FoW::Update ( C4Rect  r,
C4Player player 
)

Update all light beams within the given rectangle

Definition at line 233 of file C4FoW.cpp.

References C4FoWLight::getNext().

Referenced by C4FoWRegion::Render().

234 {
235 #ifndef USE_CONSOLE
236  for (C4FoWLight *pLight = pLights; pLight; pLight = pLight->getNext())
237  if (pLight->IsVisibleForPlayer(pPlr))
238  pLight->Update(r);
239 #endif
240 }
C4FoWLight * getNext() const
Definition: C4FoWLight.h:73

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

C4FoWAmbient C4FoW::Ambient

Definition at line 115 of file C4FoW.h.

Referenced by C4LandscapeRenderGL::Draw(), and CStdGL::SetupMultiBlt().


The documentation for this class was generated from the following files: