From d58505012b8f2823aa80729c7aa01215a4535803 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 24 Jan 2015 00:05:39 -0400 Subject: [PATCH] Added OpenGL ES support for the 'none' line join mode. --HG-- branch : minor --- src/modules/graphics/opengl/Polyline.cpp | 54 ++++++++++++++++++++++-- src/modules/graphics/opengl/Polyline.h | 6 ++- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/modules/graphics/opengl/Polyline.cpp b/src/modules/graphics/opengl/Polyline.cpp index 11c052351..86f70fbaa 100644 --- a/src/modules/graphics/opengl/Polyline.cpp +++ b/src/modules/graphics/opengl/Polyline.cpp @@ -18,14 +18,15 @@ * 3. This notice may not be removed or altered from any source distribution. **/ -#include - // LOVE #include "Polyline.h" // OpenGL #include "OpenGL.h" +// C++ +#include + // 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) diff --git a/src/modules/graphics/opengl/Polyline.h b/src/modules/graphics/opengl/Polyline.h index 3f139cc9c..c20dd1a18 100644 --- a/src/modules/graphics/opengl/Polyline.h +++ b/src/modules/graphics/opengl/Polyline.h @@ -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)