From faa4a33fe965c4bb461a1eedf50a32d935fb7567 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 28 Feb 2015 02:32:34 -0400 Subject: [PATCH] Use triangle strips rather than triangle fans when possible. --HG-- branch : minor --- src/modules/graphics/Quad.cpp | 19 ++++++++++++------- src/modules/graphics/opengl/Canvas.cpp | 15 ++++++++++----- src/modules/graphics/opengl/Font.cpp | 8 ++++++-- src/modules/graphics/opengl/Image.cpp | 19 ++++++++++++------- src/modules/graphics/opengl/Polyline.cpp | 2 +- src/modules/graphics/opengl/VertexBuffer.cpp | 6 +++++- 6 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/modules/graphics/Quad.cpp b/src/modules/graphics/Quad.cpp index 55619f40f..d4aa80b2b 100644 --- a/src/modules/graphics/Quad.cpp +++ b/src/modules/graphics/Quad.cpp @@ -45,23 +45,28 @@ void Quad::refresh(const Quad::Viewport &v, float sw, float sh) { viewport = v; - vertices[0].x = 0; - vertices[0].y = 0; - vertices[1].x = 0; + // Vertices are ordered for use with triangle strips: + // 0----2 + // | / | + // | / | + // 1----3 + vertices[0].x = 0.0f; + vertices[0].y = 0.0f; + vertices[1].x = 0.0f; vertices[1].y = v.h; vertices[2].x = v.w; - vertices[2].y = v.h; + vertices[2].y = 0.0f; vertices[3].x = v.w; - vertices[3].y = 0; + vertices[3].y = v.h; vertices[0].s = v.x/sw; vertices[0].t = v.y/sh; vertices[1].s = v.x/sw; vertices[1].t = (v.y+v.h)/sh; vertices[2].s = (v.x+v.w)/sw; - vertices[2].t = (v.y+v.h)/sh; + vertices[2].t = v.y/sh; vertices[3].s = (v.x+v.w)/sw; - vertices[3].t = v.y/sh; + vertices[3].t = (v.y+v.h)/sh; } void Quad::setViewport(const Quad::Viewport &v) diff --git a/src/modules/graphics/opengl/Canvas.cpp b/src/modules/graphics/opengl/Canvas.cpp index 9e6a8ec6b..2b8dc029b 100644 --- a/src/modules/graphics/opengl/Canvas.cpp +++ b/src/modules/graphics/opengl/Canvas.cpp @@ -110,15 +110,20 @@ Canvas::Canvas(int width, int height, Format format, int msaa) float w = static_cast(width); float h = static_cast(height); + // Vertices are ordered for use with triangle strips: + // 0----2 + // | / | + // | / | + // 1----3 // world coordinates vertices[0].x = 0; vertices[0].y = 0; vertices[1].x = 0; vertices[1].y = h; vertices[2].x = w; - vertices[2].y = h; + vertices[2].y = 0; vertices[3].x = w; - vertices[3].y = 0; + vertices[3].y = h; // texture coordinates vertices[0].s = 0; @@ -126,9 +131,9 @@ Canvas::Canvas(int width, int height, Format format, int msaa) vertices[1].s = 0; vertices[1].t = 1; vertices[2].s = 1; - vertices[2].t = 1; + vertices[2].t = 0; vertices[3].s = 1; - vertices[3].t = 0; + vertices[3].t = 1; loadVolatile(); @@ -305,7 +310,7 @@ void Canvas::drawv(const Matrix &t, const Vertex *v) glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), &v[0].s); gl.prepareDraw(); - gl.drawArrays(GL_TRIANGLE_FAN, 0, 4); + gl.drawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray(ATTRIB_TEXCOORD); glDisableVertexAttribArray(ATTRIB_POS); diff --git a/src/modules/graphics/opengl/Font.cpp b/src/modules/graphics/opengl/Font.cpp index 331747443..d5f00bd7b 100644 --- a/src/modules/graphics/opengl/Font.cpp +++ b/src/modules/graphics/opengl/Font.cpp @@ -265,11 +265,15 @@ const Font::Glyph &Font::addGlyph(uint32 glyph) float tX = (float) textureX, tY = (float) textureY; float tWidth = (float) textureWidth, tHeight = (float) textureHeight; + // 0----2 + // | / | + // | / | + // 1----3 const GlyphVertex verts[4] = { { 0.0f, 0.0f, tX/tWidth, tY/tHeight}, { 0.0f, float(h), tX/tWidth, (tY+h)/tHeight}, - {float(w), float(h), (tX+w)/tWidth, (tY+h)/tHeight}, - {float(w), 0.0f, (tX+w)/tWidth, tY/tHeight} + {float(w), 0.0f, (tX+w)/tWidth, tY/tHeight}, + {float(w), float(h), (tX+w)/tWidth, (tY+h)/tHeight} }; // Copy vertex data to the glyph and set proper bearing. diff --git a/src/modules/graphics/opengl/Image.cpp b/src/modules/graphics/opengl/Image.cpp index f49917731..90154f04d 100644 --- a/src/modules/graphics/opengl/Image.cpp +++ b/src/modules/graphics/opengl/Image.cpp @@ -93,26 +93,31 @@ Image::~Image() void Image::preload() { - // For colors. - memset(vertices, 255, sizeof(Vertex)*4); + for (int i = 0; i < 4; i++) + vertices[i].r = vertices[i].g = vertices[i].b = vertices[i].a = 255; + // Vertices are ordered for use with triangle strips: + // 0----2 + // | / | + // | / | + // 1----3 vertices[0].x = 0.0f; vertices[0].y = 0.0f; vertices[1].x = 0.0f; vertices[1].y = (float) height; vertices[2].x = (float) width; - vertices[2].y = (float) height; + vertices[2].y = 0.0f; vertices[3].x = (float) width; - vertices[3].y = 0.0f; + vertices[3].y = (float) height; vertices[0].s = 0.0f; vertices[0].t = 0.0f; vertices[1].s = 0.0f; vertices[1].t = 1.0f; vertices[2].s = 1.0f; - vertices[2].t = 1.0f; + vertices[2].t = 0.0f; vertices[3].s = 1.0f; - vertices[3].t = 0.0f; + vertices[3].t = 1.0f; if (flags.mipmaps) filter.mipmap = defaultMipmapFilter; @@ -362,7 +367,7 @@ void Image::drawv(const Matrix &t, const Vertex *v) glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), &v[0].s); gl.prepareDraw(); - gl.drawArrays(GL_TRIANGLE_FAN, 0, 4); + gl.drawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray(ATTRIB_TEXCOORD); glDisableVertexAttribArray(ATTRIB_POS); diff --git a/src/modules/graphics/opengl/Polyline.cpp b/src/modules/graphics/opengl/Polyline.cpp index 79dd312b6..940cdb28c 100644 --- a/src/modules/graphics/opengl/Polyline.cpp +++ b/src/modules/graphics/opengl/Polyline.cpp @@ -355,7 +355,7 @@ void Polyline::draw() indices[i * 6 + 2] = GLushort(i * 4 + 2); // Second triangle. - indices[i * 6 + 3] = GLushort(i * 4 + 0); + indices[i * 6 + 3] = GLushort(i * 4 + 1); indices[i * 6 + 4] = GLushort(i * 4 + 2); indices[i * 6 + 5] = GLushort(i * 4 + 3); } diff --git a/src/modules/graphics/opengl/VertexBuffer.cpp b/src/modules/graphics/opengl/VertexBuffer.cpp index 26a2d0bb7..435727416 100644 --- a/src/modules/graphics/opengl/VertexBuffer.cpp +++ b/src/modules/graphics/opengl/VertexBuffer.cpp @@ -363,13 +363,17 @@ void VertexIndex::fill() T *indices = (T *) mapper.get(); + // 0----2 + // | / | + // | / | + // 1----3 for (size_t i = 0; i < maxSize; ++i) { indices[i*6+0] = T(i * 4 + 0); indices[i*6+1] = T(i * 4 + 1); indices[i*6+2] = T(i * 4 + 2); - indices[i*6+3] = T(i * 4 + 0); + indices[i*6+3] = T(i * 4 + 1); indices[i*6+4] = T(i * 4 + 2); indices[i*6+5] = T(i * 4 + 3); }