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 ( )
default
C4FoW::~C4FoW ( )

Definition at line 26 of file C4FoW.cpp.

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

27 {
28  if (deleted_lights)
29  {
32  }
33 }
void ClearDeletedLights()
Definition: C4FoW.cpp:35
C4Draw * pDraw
Definition: C4Draw.cpp:42
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 166 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().

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

References C4FoWLight::getNext().

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

C4Shader * C4FoW::GetFramebufShader ( )

Definition at line 48 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().

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

Here is the call graph for this function:

Here is the caller graph for this function:

C4Shader * C4FoW::GetRenderShader ( )

Definition at line 107 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().

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

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 222 of file C4FoW.cpp.

References C4FoWLight::getNext().

223 {
224 #ifndef USE_CONSOLE
225  for (C4FoWLight *pLight = pLights; pLight; pLight = pLight->getNext())
226  pLight->Invalidate(r);
227 #endif
228 }
C4FoWLight * getNext() const
Definition: C4FoWLight.h:71

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 202 of file C4FoW.cpp.

References C4FoWLight::getNext().

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

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

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 239 of file C4FoW.cpp.

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

Referenced by C4FoWRegion::Render().

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

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 230 of file C4FoW.cpp.

References C4FoWLight::getNext().

Referenced by C4FoWRegion::Render().

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

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: