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)
 
virtual ~C4FoWDrawLightTextureStrategy ()
 
virtual void DrawLightVertex (float x, float y)
 
virtual void DrawDarkVertex (float x, float y)
 
virtual void Begin (const C4FoWRegion *region)
 
virtual void End (C4ShaderCall &call)
 
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 phase
 

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 118 of file C4FoWDrawStrategy.h.

Member Enumeration Documentation

Enumerator
P_None 
P_Fan 
P_FanMaxed 
P_Fade 
P_Intermediate 

Definition at line 76 of file C4FoWDrawStrategy.h.

Constructor & Destructor Documentation

C4FoWDrawLightTextureStrategy::C4FoWDrawLightTextureStrategy ( const C4FoWLight light)

Definition at line 112 of file C4FoWDrawStrategy.cpp.

113  : light(light), region(nullptr), vbo_size(0), ibo_size(0)
114 {
115  bo[0] = bo[1] = 0u;
116 }
C4FoWDrawLightTextureStrategy::~C4FoWDrawLightTextureStrategy ( )
virtual

Definition at line 118 of file C4FoWDrawStrategy.cpp.

References CStdGL::FreeVAOID(), and pGL.

119 {
120  if (bo[0])
121  {
122  glDeleteBuffers(2, bo);
123  pGL->FreeVAOID(vaoids[2]);
124  pGL->FreeVAOID(vaoids[1]);
125  pGL->FreeVAOID(vaoids[0]);
126  }
127 }
void FreeVAOID(unsigned int vaoid)
Definition: C4DrawGL.cpp:976
CStdGL * pGL
Definition: C4DrawGL.cpp:914

Here is the call graph for this function:

Member Function Documentation

void C4FoWDrawLightTextureStrategy::Begin ( const C4FoWRegion region)
virtual

Called before each rendering pass

Implements C4FoWDrawStrategy.

Definition at line 129 of file C4FoWDrawStrategy.cpp.

References CStdGL::GenVAOID(), and pGL.

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

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 103 of file C4FoWDrawStrategy.h.

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

103 { triangulator.Quads(); phase = P_Fade; };
enum C4FoWDrawStrategy::DrawPhase phase
C4FoWDrawTriangulator 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 93 of file C4FoWDrawStrategy.h.

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

93 { triangulator.Fan(); phase = P_Fan; };
enum C4FoWDrawStrategy::DrawPhase phase
C4FoWDrawTriangulator 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 98 of file C4FoWDrawStrategy.h.

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

enum C4FoWDrawStrategy::DrawPhase phase
C4FoWDrawTriangulator 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 108 of file C4FoWDrawStrategy.h.

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

Here is the call graph for this function:

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

Reimplemented from C4FoWDrawStrategy.

Definition at line 333 of file C4FoWDrawStrategy.cpp.

References C4FoWDrawStrategy::DrawDarkVertex().

334 {
335  DrawVertex(x,y, false);
337 }
virtual void DrawDarkVertex(float x, float y)

Here is the call graph for this function:

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

Reimplemented from C4FoWDrawStrategy.

Definition at line 339 of file C4FoWDrawStrategy.cpp.

References C4FoWDrawStrategy::DrawLightVertex().

340 {
341  DrawVertex(x,y, true);
343 }
virtual void DrawLightVertex(float x, float y)

Here is the call graph for this function:

void C4FoWDrawLightTextureStrategy::End ( C4ShaderCall call)
virtual

Called after each rendering pass

Reimplemented from C4FoWDrawStrategy.

Definition at line 142 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.

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

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 105 of file C4FoWDrawStrategy.h.

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

Called after rendering the inner triangle fan

Definition at line 95 of file C4FoWDrawStrategy.h.

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

Called after rendering the inner triangle fan

Definition at line 100 of file C4FoWDrawStrategy.h.

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

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

Definition at line 110 of file C4FoWDrawStrategy.h.

110 { };

Member Data Documentation


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