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

◆ ~C4FoW()

C4FoW::~C4FoW ( )

Definition at line 26 of file C4FoW.cpp.

27 {
28  if (deleted_lights)
29  {
31  }
32 }
void ClearDeletedLights()
Definition: C4FoW.cpp:34

References ClearDeletedLights().

Here is the call graph for this function:

Member Function Documentation

◆ Add()

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

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

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

Referenced by C4Object::UpdateLight().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ClearDeletedLights()

void C4FoW::ClearDeletedLights ( )

Definition at line 34 of file C4FoW.cpp.

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

References C4FoWLight::getNext().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFramebufShader()

C4Shader * C4FoW::GetFramebufShader ( )

Definition at line 47 of file C4FoW.cpp.

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

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetRenderShader()

C4Shader * C4FoW::GetRenderShader ( )

Definition at line 106 of file C4FoW.cpp.

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

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Invalidate()

void C4FoW::Invalidate ( C4Rect  r)

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

Definition at line 221 of file C4FoW.cpp.

222 {
223 #ifndef USE_CONSOLE
224  for (C4FoWLight *pLight = pLights; pLight; pLight = pLight->getNext())
225  pLight->Invalidate(r);
226 #endif
227 }

References C4FoWLight::getNext().

Here is the call graph for this function:

◆ Remove()

void C4FoW::Remove ( C4Object pObj)

Removes the light associated with the given object, if any

Definition at line 201 of file C4FoW.cpp.

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

References C4FoWLight::getNext().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Render()

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

Definition at line 238 of file C4FoW.cpp.

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

References C4FoWRSU_ProjectionMatrix, C4ScriptGuiWindowActionID::Call, ClearDeletedLights(), C4FoWLight::getNext(), and GetRenderShader().

Referenced by C4FoWRegion::Render().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Update()

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

Update all light beams within the given rectangle

Definition at line 229 of file C4FoW.cpp.

230 {
231 #ifndef USE_CONSOLE
232  for (C4FoWLight *pLight = pLights; pLight; pLight = pLight->getNext())
233  if (pLight->IsVisibleForPlayer(pPlr))
234  pLight->Update(r);
235 #endif
236 }

References C4FoWLight::getNext().

Referenced by C4FoWRegion::Render().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ Ambient

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: