Browse Source

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

--HG--
branch : minor
Alex Szpakowski 10 năm trước cách đây
mục cha
commit
d58505012b

+ 50 - 4
src/modules/graphics/opengl/Polyline.cpp

@@ -18,14 +18,15 @@
  * 3. This notice may not be removed or altered from any source distribution.
  * 3. This notice may not be removed or altered from any source distribution.
  **/
  **/
 
 
-#include <algorithm>
-
 // LOVE
 // LOVE
 #include "Polyline.h"
 #include "Polyline.h"
 
 
 // OpenGL
 // OpenGL
 #include "OpenGL.h"
 #include "OpenGL.h"
 
 
+// C++
+#include <algorithm>
+
 // treat adjacent segments with angles between their directions <5 degree as straight
 // treat adjacent segments with angles between their directions <5 degree as straight
 static const float LINES_PARALLEL_EPS = 0.05f;
 static const float LINES_PARALLEL_EPS = 0.05f;
 
 
@@ -327,13 +328,50 @@ Polyline::~Polyline()
 
 
 void Polyline::draw()
 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();
 	gl.prepareDraw();
 
 
 	// draw the core line
 	// draw the core line
 	gl.bindTexture(gl.getDefaultTexture());
 	gl.bindTexture(gl.getDefaultTexture());
 	glEnableVertexAttribArray(ATTRIB_POS);
 	glEnableVertexAttribArray(ATTRIB_POS);
 	glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, 0, vertices);
 	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)
 	if (overdraw)
 	{
 	{
@@ -345,7 +383,12 @@ void Polyline::draw()
 		glEnableVertexAttribArray(ATTRIB_COLOR);
 		glEnableVertexAttribArray(ATTRIB_COLOR);
 		glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, colors);
 		glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, colors);
 		glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, 0, overdraw);
 		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);
 		glDisableVertexAttribArray(ATTRIB_COLOR);
 
 
 		delete[] colors;
 		delete[] colors;
@@ -354,6 +397,9 @@ void Polyline::draw()
 	}
 	}
 
 
 	glDisableVertexAttribArray(ATTRIB_POS);
 	glDisableVertexAttribArray(ATTRIB_POS);
+
+	if (indices)
+		delete[] indices;
 }
 }
 
 
 void Polyline::fill_color_array(Color *colors, const Color &c)
 void Polyline::fill_color_array(Color *colors, const Color &c)

+ 4 - 2
src/modules/graphics/opengl/Polyline.h

@@ -43,12 +43,13 @@ namespace opengl
 class Polyline
 class Polyline
 {
 {
 public:
 public:
-	Polyline(GLenum mode = GL_TRIANGLE_STRIP)
+	Polyline(GLenum mode = GL_TRIANGLE_STRIP, bool quadindices = false)
 		: vertices(NULL)
 		: vertices(NULL)
 		, overdraw(NULL)
 		, overdraw(NULL)
 		, vertex_count(0)
 		, vertex_count(0)
 		, overdraw_vertex_count(0)
 		, overdraw_vertex_count(0)
 		, draw_mode(mode)
 		, draw_mode(mode)
+		, use_quad_indices(quadindices)
 	{}
 	{}
 	virtual ~Polyline();
 	virtual ~Polyline();
 
 
@@ -90,6 +91,7 @@ protected:
 	size_t vertex_count;
 	size_t vertex_count;
 	size_t overdraw_vertex_count;
 	size_t overdraw_vertex_count;
 	GLenum draw_mode;
 	GLenum draw_mode;
+	bool use_quad_indices;
 
 
 }; // Polyline
 }; // Polyline
 
 
@@ -102,7 +104,7 @@ class NoneJoinPolyline : public Polyline
 {
 {
 public:
 public:
 	NoneJoinPolyline()
 	NoneJoinPolyline()
-		: Polyline(GL_QUADS)
+		: Polyline(GL_TRIANGLES, true)
 	{}
 	{}
 
 
 	void render(const float *vertices, size_t count, float halfwidth, float pixel_size, bool draw_overdraw)
 	void render(const float *vertices, size_t count, float halfwidth, float pixel_size, bool draw_overdraw)