40 void C4FoWDrawTriangulator::FinishPrimitive()
46 assert(cur_vertices == 0 || cur_vertices >= 3);
49 assert(cur_vertices % 4 == 0);
57 begin_vertices += cur_vertices;
67 if (cur_vertices == 3)
69 indices.push_back(begin_vertices);
70 indices.push_back(begin_vertices + 1);
71 indices.push_back(begin_vertices + 2);
73 else if (cur_vertices > 3)
75 indices.push_back(begin_vertices);
76 indices.push_back(begin_vertices + cur_vertices - 2);
77 indices.push_back(begin_vertices + cur_vertices - 1);
82 if (cur_vertices % 4 == 0)
85 indices.push_back(begin_vertices + cur_vertices - 4);
86 indices.push_back(begin_vertices + cur_vertices - 3);
87 indices.push_back(begin_vertices + cur_vertices - 2);
90 indices.push_back(begin_vertices + cur_vertices - 4);
91 indices.push_back(begin_vertices + cur_vertices - 2);
92 indices.push_back(begin_vertices + cur_vertices - 1);
110 : light(light), region(nullptr), vbo_size(0), ibo_size(0)
119 glDeleteBuffers(2, bo);
142 if (vertices.empty())
return;
144 const GLuint vbo = bo[0];
145 const GLuint ibo = bo[1];
148 glBindBuffer(GL_ARRAY_BUFFER, vbo);
149 if (vbo_size < vertices.size())
151 glBufferData(GL_ARRAY_BUFFER, vertices.size() *
sizeof(Vertex), &vertices[0], GL_DYNAMIC_DRAW);
152 vbo_size = vertices.size();
156 glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() *
sizeof(Vertex), &vertices[0]);
158 glBindBuffer(GL_ARRAY_BUFFER, 0);
161 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
171 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
178 float y_offset[] = { 0.0f, 0.0f };
182 glEnable(GL_SCISSOR_TEST);
183 glScissor(0, height, width, height);
186 GLuint vao1, vao2, vao3;
187 const bool has_vao1 =
pGL->
GetVAO(vaoids[0], vao1);
188 glBindVertexArray(vao1);
191 glBindBuffer(GL_ARRAY_BUFFER, vbo);
192 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
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)));
201 glBlendFunc(GL_ONE, GL_ONE);
202 glBlendEquationSeparate(GL_FUNC_ADD, GL_MAX);
209 glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
211 const bool has_vao2 =
pGL->
GetVAO(vaoids[1], vao2);
212 glBindVertexArray(vao2);
215 glBindBuffer(GL_ARRAY_BUFFER, vbo);
216 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
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)));
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;
233 const bool has_vao3 =
pGL->
GetVAO(vaoids[2], vao3);
234 glBindVertexArray(vao3);
237 glBindBuffer(GL_ARRAY_BUFFER, vbo);
238 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
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)));
249 glBindVertexArray(0);
250 glDisable(GL_SCISSOR_TEST);
257 void C4FoWDrawLightTextureStrategy::DrawVertex(
float x,
float y,
bool shadow)
284 vertices.emplace_back();
285 Vertex& vtx = vertices.back();
286 vtx.x = x; vtx.y = y;
296 float dx = x - light->
getX();
297 float dy = y - light->
getY();
298 float dist = sqrt(dx*dx+dy*dy);
300 float mult = std::min(0.5f / light->
getNormalSize(), 0.5f / dist);
301 float normX =
Clamp(0.5f + dx * mult, 0.0f, 1.0f) / 1.5f;
302 float normY =
Clamp(0.5f + dy * mult, 0.0f, 1.0f) / 1.5f;
303 vtx.r1 = 0.0f; vtx.g1 = normX; vtx.b1 = normY; vtx.a1 = bright;
307 vtx.r1 = 0.0f; vtx.g1 = 0.5f / 1.5f; vtx.b1 = 0.5f / 1.5f; vtx.a1 = 0.0f;
311 vtx.r2 = 0.0f; vtx.g2 = 0.5f / 1.5f; vtx.b2 = 0.5f / 1.5f; vtx.a2 = 0.0f;
324 vtx.r3 = light->
getR();
325 vtx.g3 = light->
getG();
326 vtx.b3 = light->
getB();
332 DrawVertex(x,y,
false);
338 DrawVertex(x,y,
true);
343 screen(screen), vbo_size(0), ibo_size(0)
351 glDeleteBuffers(2, bo);
362 if (vertices.empty())
return;
364 const GLuint vbo = bo[0];
365 const GLuint ibo = bo[1];
367 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
370 glBindBuffer(GL_ARRAY_BUFFER, vbo);
371 if (vbo_size < vertices.size())
373 glBufferData(GL_ARRAY_BUFFER, vertices.size() *
sizeof(Vertex), &vertices[0], GL_STREAM_DRAW);
374 vbo_size = vertices.size();
378 glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() *
sizeof(Vertex), &vertices[0]);
380 glBindBuffer(GL_ARRAY_BUFFER, 0);
383 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
393 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
396 const bool has_vao =
pGL->
GetVAO(vaoid, vao);
397 glBindVertexArray(vao);
400 glBindBuffer(GL_ARRAY_BUFFER, vbo);
401 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
405 glVertexAttribPointer(call.
GetAttribute(
C4FoWRSA_Position), 2, GL_FLOAT, GL_FALSE,
sizeof(Vertex),
reinterpret_cast<const uint8_t*
>(offsetof(Vertex, x)));
406 glVertexAttribPointer(call.
GetAttribute(
C4FoWRSA_Color), 4, GL_FLOAT, GL_FALSE,
sizeof(Vertex),
reinterpret_cast<const uint8_t*
>(offsetof(Vertex, r)));
410 const float y_offset[] = { 0.0f, 0.0f };
416 glBindVertexArray(0);
417 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
424 void C4FoWDrawWireframeStrategy::DrawVertex(Vertex& vtx)
428 vtx.x += screen->
X - screen->
TargetX;
429 vtx.y += screen->
Y - screen->
TargetY;
435 vertices.emplace_back();
436 Vertex& vtx = vertices.back();
437 vtx.x = x; vtx.y = y;
442 case P_Fade: vtx.r = 0.0f; vtx.g = 0.5f; vtx.b = 0.0f; vtx.a = 1.0f;
break;
443 case P_Intermediate: vtx.r = 0.0f; vtx.g = 0.0f; vtx.b = 0.5f; vtx.a = 1.0f;
break;
444 default: assert(
false);
453 vertices.emplace_back();
454 Vertex& vtx = vertices.back();
455 vtx.x = x; vtx.y = y;
460 case P_Fan: vtx.r = 1.0f; vtx.g = 0.0f; vtx.b = 0.0f; vtx.a = 1.0f;
break;
461 case P_FanMaxed: vtx.r = 1.0f; vtx.g = 1.0f; vtx.b = 0.0f; vtx.a = 1.0f;
break;
462 case P_Fade: vtx.r = 0.0f; vtx.g = 1.0f; vtx.b = 0.0f; vtx.a = 1.0f;
break;
463 case P_Intermediate: vtx.r = 0.0f; vtx.g = 0.0f; vtx.b = 1.0f; vtx.a = 1.0f;
break;
464 default: assert(
false);
T Clamp(T bval, T lbound, T rbound)
void ApplyZoom(float &X, float &Y)
void Begin(const C4FoWRegion *region) override
void End(C4ShaderCall &call) override
~C4FoWDrawLightTextureStrategy() override
void DrawDarkVertex(float x, float y) override
C4FoWDrawLightTextureStrategy(const C4FoWLight *light)
void DrawLightVertex(float x, float y) override
enum C4FoWDrawStrategy::DrawPhase P_None
virtual void DrawLightVertex(float x, float y)
virtual void End(C4ShaderCall &call)
C4FoWDrawTriangulator triangulator
virtual void DrawDarkVertex(float x, float y)
unsigned int GetNIndices() const
const unsigned int * GetIndices() const
void DrawDarkVertex(float x, float y) override
C4FoWDrawWireframeStrategy(const C4FoWLight *light, const C4TargetFacet *screen)
void Begin(const C4FoWRegion *region) override
void DrawLightVertex(float x, float y) override
~C4FoWDrawWireframeStrategy() override
void End(C4ShaderCall &call) override
float getBrightness() const
int32_t getNormalSize() const
float getLightness() const
int32_t getSurfaceHeight() const
const C4Rect & getRegion() const
int32_t getSurfaceWidth() const
GLint GetAttribute(int iAttribute) const
void SetUniform2fv(int iUniform, int iLength, const float *pVals) const
void FreeVAOID(unsigned int vaoid)
bool GetVAO(unsigned int vaoid, GLuint &vao)