Added OpenGL ES support for the 'none' line join mode.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2015-01-24 00:05:39 -04:00
parent 970704c2b7
commit d58505012b
2 changed files with 54 additions and 6 deletions
+50 -4
View File
@@ -18,14 +18,15 @@
* 3. This notice may not be removed or altered from any source distribution.
**/
#include <algorithm>
// LOVE
#include "Polyline.h"
// OpenGL
#include "OpenGL.h"
// C++
#include <algorithm>
// treat adjacent segments with angles between their directions <5 degree as straight
static const float LINES_PARALLEL_EPS = 0.05f;
@@ -327,13 +328,50 @@ Polyline::~Polyline()
void Polyline::draw()
{
GLushort *indices = nullptr;
// TODO: We should probably be using a reusable index buffer.
if (use_quad_indices)
{
size_t numindices = (vertex_count / 4) * 6;
if (overdraw)
numindices = std::max(numindices, (overdraw_vertex_count / 4) * 6);
try
{
indices = new GLushort[numindices];
}
catch (std::bad_alloc &)
{
throw love::Exception("Out of memory.");
}
// Fill the index array to make 2 triangles from each quad.
for (size_t i = 0; i < numindices / 6; i++)
{
// First triangle.
indices[i * 6 + 0] = GLushort(i * 4 + 0);
indices[i * 6 + 1] = GLushort(i * 4 + 1);
indices[i * 6 + 2] = GLushort(i * 4 + 2);
// Second triangle.
indices[i * 6 + 3] = GLushort(i * 4 + 0);
indices[i * 6 + 4] = GLushort(i * 4 + 2);
indices[i * 6 + 5] = GLushort(i * 4 + 3);
}
}
gl.prepareDraw();
// draw the core line
gl.bindTexture(gl.getDefaultTexture());
glEnableVertexAttribArray(ATTRIB_POS);
glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, 0, vertices);
gl.drawArrays(draw_mode, 0, vertex_count);
if (use_quad_indices)
gl.drawElements(draw_mode, (vertex_count / 4) * 6, GL_UNSIGNED_SHORT, indices);
else
gl.drawArrays(draw_mode, 0, vertex_count);
if (overdraw)
{
@@ -345,7 +383,12 @@ void Polyline::draw()
glEnableVertexAttribArray(ATTRIB_COLOR);
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, colors);
glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, 0, overdraw);
gl.drawArrays(draw_mode, 0, overdraw_vertex_count);
if (use_quad_indices)
gl.drawElements(draw_mode, (overdraw_vertex_count / 4) * 6, GL_UNSIGNED_SHORT, indices);
else
gl.drawArrays(draw_mode, 0, overdraw_vertex_count);
glDisableVertexAttribArray(ATTRIB_COLOR);
delete[] colors;
@@ -354,6 +397,9 @@ void Polyline::draw()
}
glDisableVertexAttribArray(ATTRIB_POS);
if (indices)
delete[] indices;
}
void Polyline::fill_color_array(Color *colors, const Color &c)
+4 -2
View File
@@ -43,12 +43,13 @@ namespace opengl
class Polyline
{
public:
Polyline(GLenum mode = GL_TRIANGLE_STRIP)
Polyline(GLenum mode = GL_TRIANGLE_STRIP, bool quadindices = false)
: vertices(NULL)
, overdraw(NULL)
, vertex_count(0)
, overdraw_vertex_count(0)
, draw_mode(mode)
, use_quad_indices(quadindices)
{}
virtual ~Polyline();
@@ -90,6 +91,7 @@ protected:
size_t vertex_count;
size_t overdraw_vertex_count;
GLenum draw_mode;
bool use_quad_indices;
}; // Polyline
@@ -102,7 +104,7 @@ class NoneJoinPolyline : public Polyline
{
public:
NoneJoinPolyline()
: Polyline(GL_QUADS)
: Polyline(GL_TRIANGLES, true)
{}
void render(const float *vertices, size_t count, float halfwidth, float pixel_size, bool draw_overdraw)