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

#include <C4FoWDrawStrategy.h>

Inheritance diagram for C4FoWDrawLightTextureStrategy:
[legend]
Collaboration diagram for C4FoWDrawLightTextureStrategy:
[legend]

Public Types

enum  DrawPhase {
  P_None, P_Fan, P_FanMaxed, P_Fade,
  P_Intermediate
}
 

Public Member Functions

 C4FoWDrawLightTextureStrategy (const C4FoWLight *light)
 
 ~C4FoWDrawLightTextureStrategy () override
 
void DrawLightVertex (float x, float y) override
 
void DrawDarkVertex (float x, float y) override
 
void Begin (const C4FoWRegion *region) override
 
void End (C4ShaderCall &call) override
 
virtual void BeginFan ()
 
virtual void EndFan ()
 
virtual void BeginFanMaxed ()
 
virtual void EndFanMaxed ()
 
virtual void BeginFade ()
 
virtual void EndFade ()
 
virtual void BeginIntermediateFade ()
 
virtual void EndIntermediateFade ()
 

Public Attributes

enum C4FoWDrawStrategy::DrawPhase P_None
 

Protected Attributes

C4FoWDrawTriangulator triangulator
 

Detailed Description

This draw strategy is the default draw strategy that draws the light onto the given region.

Definition at line 117 of file C4FoWDrawStrategy.h.

Member Enumeration Documentation

Enumerator
P_None 
P_Fan 
P_FanMaxed 
P_Fade 
P_Intermediate 

Definition at line 75 of file C4FoWDrawStrategy.h.

Constructor & Destructor Documentation

C4FoWDrawLightTextureStrategy::C4FoWDrawLightTextureStrategy ( const C4FoWLight light)

Definition at line 109 of file C4FoWDrawStrategy.cpp.

110  : light(light), region(nullptr), vbo_size(0), ibo_size(0)
111 {
112  bo[0] = bo[1] = 0u;
113 }
C4FoWDrawLightTextureStrategy::~C4FoWDrawLightTextureStrategy ( )
override

Definition at line 115 of file C4FoWDrawStrategy.cpp.

References CStdGL::FreeVAOID(), and pGL.

116 {
117  if (bo[0])
118  {
119  glDeleteBuffers(2, bo);
120  pGL->FreeVAOID(vaoids[2]);
121  pGL->FreeVAOID(vaoids[1]);
122  pGL->FreeVAOID(vaoids[0]);
123  }
124 }
void FreeVAOID(unsigned int vaoid)
Definition: C4DrawGL.cpp:967
CStdGL * pGL
Definition: C4DrawGL.cpp:905

Here is the call graph for this function:

Member Function Documentation

void C4FoWDrawLightTextureStrategy::Begin ( const C4FoWRegion region)
overridevirtual

Called before each rendering pass

Implements C4FoWDrawStrategy.

Definition at line 126 of file C4FoWDrawStrategy.cpp.

References CStdGL::GenVAOID(), and pGL.

127 {
128  region = regionPar;
129  if (!bo[0])
130  {
131  // lazy-init buffer objects
132  glGenBuffers(2, bo);
133  vaoids[0] = pGL->GenVAOID();
134  vaoids[1] = pGL->GenVAOID();
135  vaoids[2] = pGL->GenVAOID();
136  }
137 }
unsigned int GenVAOID()
Definition: C4DrawGL.cpp:925
CStdGL * pGL
Definition: C4DrawGL.cpp:905

Here is the call graph for this function:

virtual void C4FoWDrawStrategy::BeginFade ( )
inlinevirtualinherited

Called before rendering the quads in which the light fades out

Definition at line 102 of file C4FoWDrawStrategy.h.

References C4FoWDrawStrategy::P_Fade, C4FoWDrawTriangulator::Quads(), and C4FoWDrawStrategy::triangulator.

Here is the call graph for this function:

virtual void C4FoWDrawStrategy::BeginFan ( )
inlinevirtualinherited

Called before rendering the inner triangle fan (the area with 100% light)

Definition at line 92 of file C4FoWDrawStrategy.h.

References C4FoWDrawTriangulator::Fan(), C4FoWDrawStrategy::P_Fan, and C4FoWDrawStrategy::triangulator.

Here is the call graph for this function:

virtual void C4FoWDrawStrategy::BeginFanMaxed ( )
inlinevirtualinherited

Called before rendering the triangle fan existnsion (100% light, maxed out normals)

Definition at line 97 of file C4FoWDrawStrategy.h.

References C4FoWDrawStrategy::P_FanMaxed, C4FoWDrawTriangulator::Quads(), and C4FoWDrawStrategy::triangulator.

Here is the call graph for this function:

virtual void C4FoWDrawStrategy::BeginIntermediateFade ( )
inlinevirtualinherited

Called before rendering the triangles that fill the space between the fadeout quads

Definition at line 107 of file C4FoWDrawStrategy.h.

References C4FoWDrawTriangulator::Fan(), C4FoWDrawStrategy::P_Intermediate, and C4FoWDrawStrategy::triangulator.

Here is the call graph for this function:

void C4FoWDrawLightTextureStrategy::DrawDarkVertex ( float  x,
float  y 
)
overridevirtual

Reimplemented from C4FoWDrawStrategy.

Definition at line 330 of file C4FoWDrawStrategy.cpp.

References C4FoWDrawStrategy::DrawDarkVertex().

331 {
332  DrawVertex(x,y, false);
334 }
virtual void DrawDarkVertex(float x, float y)

Here is the call graph for this function:

void C4FoWDrawLightTextureStrategy::DrawLightVertex ( float  x,
float  y 
)
overridevirtual

Reimplemented from C4FoWDrawStrategy.

Definition at line 336 of file C4FoWDrawStrategy.cpp.

References C4FoWDrawStrategy::DrawLightVertex().

337 {
338  DrawVertex(x,y, true);
340 }
virtual void DrawLightVertex(float x, float y)

Here is the call graph for this function:

void C4FoWDrawLightTextureStrategy::End ( C4ShaderCall call)
overridevirtual

Called after each rendering pass

Reimplemented from C4FoWDrawStrategy.

Definition at line 139 of file C4FoWDrawStrategy.cpp.

References C4FoWRSA_Color, C4FoWRSA_Position, C4FoWRSU_VertexOffset, C4FoWDrawStrategy::End(), C4ShaderCall::GetAttribute(), C4FoWDrawTriangulator::GetIndices(), C4FoWDrawTriangulator::GetNIndices(), C4FoWRegion::getSurfaceHeight(), C4FoWRegion::getSurfaceWidth(), CStdGL::GetVAO(), pGL, C4ShaderCall::SetUniform2fv(), and C4FoWDrawStrategy::triangulator.

140 {
141  // If we have nothing to draw (e.g. directly after initialization), abort early.
142  if (vertices.empty()) return;
143 
144  const GLuint vbo = bo[0];
145  const GLuint ibo = bo[1];
146 
147  // Upload vertices
148  glBindBuffer(GL_ARRAY_BUFFER, vbo);
149  if (vbo_size < vertices.size())
150  {
151  glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_DYNAMIC_DRAW);
152  vbo_size = vertices.size();
153  }
154  else
155  {
156  glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vertex), &vertices[0]);
157  }
158  glBindBuffer(GL_ARRAY_BUFFER, 0);
159 
160  // Upload indices
161  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
162  if (ibo_size < triangulator.GetNIndices())
163  {
164  glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangulator.GetNIndices() * sizeof(GLuint), triangulator.GetIndices(), GL_DYNAMIC_DRAW);
165  ibo_size = triangulator.GetNIndices();
166  }
167  else
168  {
169  glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, triangulator.GetNIndices() * sizeof(GLuint), triangulator.GetIndices());
170  }
171  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
172 
173  // Region dimensions
174  const float width = region->getSurfaceWidth();
175  const float height = region->getSurfaceHeight() / 2.0;
176 
177  // Set Y offset for vertex
178  float y_offset[] = { 0.0f, 0.0f };
179  call.SetUniform2fv(C4FoWRSU_VertexOffset, 1, y_offset);
180 
181  // Enable scissor test to only draw in upper or lower half of texture
182  glEnable(GL_SCISSOR_TEST);
183  glScissor(0, height, width, height);
184 
185  // Setup state for 1st pass
186  GLuint vao1, vao2, vao3;
187  const bool has_vao1 = pGL->GetVAO(vaoids[0], vao1);
188  glBindVertexArray(vao1);
189  if (!has_vao1)
190  {
191  glBindBuffer(GL_ARRAY_BUFFER, vbo);
192  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
193  glEnableVertexAttribArray(call.GetAttribute(C4FoWRSA_Position));
194  glEnableVertexAttribArray(call.GetAttribute(C4FoWRSA_Color));
195  glVertexAttribPointer(call.GetAttribute(C4FoWRSA_Position), 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const uint8_t*>(offsetof(Vertex, x)));
196  glVertexAttribPointer(call.GetAttribute(C4FoWRSA_Color), 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const uint8_t*>(offsetof(Vertex, r1)));
197  }
198 
199  // Set up blend equation, see C4FoWDrawLightTextureStrategy::DrawVertex
200  // for details.
201  glBlendFunc(GL_ONE, GL_ONE);
202  glBlendEquationSeparate(GL_FUNC_ADD, GL_MAX);
203 
204  // Render 1st pass
205  glDrawElements(GL_TRIANGLES, triangulator.GetNIndices(), GL_UNSIGNED_INT, nullptr);
206 
207  // Prepare state for 2nd pass
208  //glBlendFunc(GL_ONE, GL_ONE);
209  glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
210 
211  const bool has_vao2 = pGL->GetVAO(vaoids[1], vao2);
212  glBindVertexArray(vao2);
213  if (!has_vao2)
214  {
215  glBindBuffer(GL_ARRAY_BUFFER, vbo);
216  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
217  glEnableVertexAttribArray(call.GetAttribute(C4FoWRSA_Position));
218  glEnableVertexAttribArray(call.GetAttribute(C4FoWRSA_Color));
219  glVertexAttribPointer(call.GetAttribute(C4FoWRSA_Position), 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const uint8_t*>(offsetof(Vertex, x)));
220  glVertexAttribPointer(call.GetAttribute(C4FoWRSA_Color), 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const uint8_t*>(offsetof(Vertex, r2)));
221  }
222 
223  // Render 2nd pass
224  glDrawElements(GL_TRIANGLES, triangulator.GetNIndices(), GL_UNSIGNED_INT, nullptr);
225 
226  // Prepare state for 3rd pass (color pass)
227  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
228  glBlendEquation(GL_FUNC_ADD);
229  glScissor(0, 0, width, height);
230  y_offset[1] = height;
231  call.SetUniform2fv(C4FoWRSU_VertexOffset, 1, y_offset);
232 
233  const bool has_vao3 = pGL->GetVAO(vaoids[2], vao3);
234  glBindVertexArray(vao3);
235  if (!has_vao3)
236  {
237  glBindBuffer(GL_ARRAY_BUFFER, vbo);
238  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
239  glEnableVertexAttribArray(call.GetAttribute(C4FoWRSA_Position));
240  glEnableVertexAttribArray(call.GetAttribute(C4FoWRSA_Color));
241  glVertexAttribPointer(call.GetAttribute(C4FoWRSA_Position), 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const uint8_t*>(offsetof(Vertex, x)));
242  glVertexAttribPointer(call.GetAttribute(C4FoWRSA_Color), 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const uint8_t*>(offsetof(Vertex, r3)));
243  }
244 
245  // Render 3rd pass
246  glDrawElements(GL_TRIANGLES, triangulator.GetNIndices(), GL_UNSIGNED_INT, nullptr);
247 
248  // Reset GL state
249  glBindVertexArray(0);
250  glDisable(GL_SCISSOR_TEST);
251 
252  // Assume the capacity stays the same:
253  vertices.resize(0);
255 }
GLint GetAttribute(int iAttribute) const
Definition: C4Shader.h:194
unsigned int GetNIndices() const
int32_t getSurfaceHeight() const
C4FoWDrawTriangulator triangulator
bool GetVAO(unsigned int vaoid, GLuint &vao)
Definition: C4DrawGL.cpp:1013
int32_t getSurfaceWidth() const
const unsigned int * GetIndices() const
virtual void End(C4ShaderCall &call)
CStdGL * pGL
Definition: C4DrawGL.cpp:905
void SetUniform2fv(int iUniform, int iLength, const float *pVals) const
Definition: C4Shader.h:293

Here is the call graph for this function:

virtual void C4FoWDrawStrategy::EndFade ( )
inlinevirtualinherited

Called after rendering the quads in which the light fades out

Definition at line 104 of file C4FoWDrawStrategy.h.

104 { };
virtual void C4FoWDrawStrategy::EndFan ( )
inlinevirtualinherited

Called after rendering the inner triangle fan

Definition at line 94 of file C4FoWDrawStrategy.h.

94 { };
virtual void C4FoWDrawStrategy::EndFanMaxed ( )
inlinevirtualinherited

Called after rendering the inner triangle fan

Definition at line 99 of file C4FoWDrawStrategy.h.

99 { };
virtual void C4FoWDrawStrategy::EndIntermediateFade ( )
inlinevirtualinherited

Called after rendering the triangles that fill the space between the fadeout quads

Definition at line 109 of file C4FoWDrawStrategy.h.

109 { };

Member Data Documentation


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