Browse Source

Use triangle strips rather than triangle fans when possible.

--HG--
branch : minor
Alex Szpakowski 10 years ago
parent
commit
faa4a33fe9

+ 12 - 7
src/modules/graphics/Quad.cpp

@@ -45,23 +45,28 @@ void Quad::refresh(const Quad::Viewport &v, float sw, float sh)
 {
 {
 	viewport = v;
 	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[1].y = v.h;
 	vertices[2].x = v.w;
 	vertices[2].x = v.w;
-	vertices[2].y = v.h;
+	vertices[2].y = 0.0f;
 	vertices[3].x = v.w;
 	vertices[3].x = v.w;
-	vertices[3].y = 0;
+	vertices[3].y = v.h;
 
 
 	vertices[0].s = v.x/sw;
 	vertices[0].s = v.x/sw;
 	vertices[0].t = v.y/sh;
 	vertices[0].t = v.y/sh;
 	vertices[1].s = v.x/sw;
 	vertices[1].s = v.x/sw;
 	vertices[1].t = (v.y+v.h)/sh;
 	vertices[1].t = (v.y+v.h)/sh;
 	vertices[2].s = (v.x+v.w)/sw;
 	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].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)
 void Quad::setViewport(const Quad::Viewport &v)

+ 10 - 5
src/modules/graphics/opengl/Canvas.cpp

@@ -110,15 +110,20 @@ Canvas::Canvas(int width, int height, Format format, int msaa)
 	float w = static_cast<float>(width);
 	float w = static_cast<float>(width);
 	float h = static_cast<float>(height);
 	float h = static_cast<float>(height);
 
 
+	// Vertices are ordered for use with triangle strips:
+	// 0----2
+	// |  / |
+	// | /  |
+	// 1----3
 	// world coordinates
 	// world coordinates
 	vertices[0].x = 0;
 	vertices[0].x = 0;
 	vertices[0].y = 0;
 	vertices[0].y = 0;
 	vertices[1].x = 0;
 	vertices[1].x = 0;
 	vertices[1].y = h;
 	vertices[1].y = h;
 	vertices[2].x = w;
 	vertices[2].x = w;
-	vertices[2].y = h;
+	vertices[2].y = 0;
 	vertices[3].x = w;
 	vertices[3].x = w;
-	vertices[3].y = 0;
+	vertices[3].y = h;
 
 
 	// texture coordinates
 	// texture coordinates
 	vertices[0].s = 0;
 	vertices[0].s = 0;
@@ -126,9 +131,9 @@ Canvas::Canvas(int width, int height, Format format, int msaa)
 	vertices[1].s = 0;
 	vertices[1].s = 0;
 	vertices[1].t = 1;
 	vertices[1].t = 1;
 	vertices[2].s = 1;
 	vertices[2].s = 1;
-	vertices[2].t = 1;
+	vertices[2].t = 0;
 	vertices[3].s = 1;
 	vertices[3].s = 1;
-	vertices[3].t = 0;
+	vertices[3].t = 1;
 
 
 	loadVolatile();
 	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);
 	glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), &v[0].s);
 
 
 	gl.prepareDraw();
 	gl.prepareDraw();
-	gl.drawArrays(GL_TRIANGLE_FAN, 0, 4);
+	gl.drawArrays(GL_TRIANGLE_STRIP, 0, 4);
 
 
 	glDisableVertexAttribArray(ATTRIB_TEXCOORD);
 	glDisableVertexAttribArray(ATTRIB_TEXCOORD);
 	glDisableVertexAttribArray(ATTRIB_POS);
 	glDisableVertexAttribArray(ATTRIB_POS);

+ 6 - 2
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 tX     = (float) textureX,     tY      = (float) textureY;
 		float tWidth = (float) textureWidth, tHeight = (float) textureHeight;
 		float tWidth = (float) textureWidth, tHeight = (float) textureHeight;
 
 
+		// 0----2
+		// |  / |
+		// | /  |
+		// 1----3
 		const GlyphVertex verts[4] = {
 		const GlyphVertex verts[4] = {
 			{    0.0f,     0.0f,     tX/tWidth,     tY/tHeight},
 			{    0.0f,     0.0f,     tX/tWidth,     tY/tHeight},
 			{    0.0f, float(h),     tX/tWidth, (tY+h)/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.
 		// Copy vertex data to the glyph and set proper bearing.

+ 13 - 8
src/modules/graphics/opengl/Image.cpp

@@ -93,26 +93,31 @@ Image::~Image()
 
 
 void Image::preload()
 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].x = 0.0f;
 	vertices[0].y = 0.0f;
 	vertices[0].y = 0.0f;
 	vertices[1].x = 0.0f;
 	vertices[1].x = 0.0f;
 	vertices[1].y = (float) height;
 	vertices[1].y = (float) height;
 	vertices[2].x = (float) width;
 	vertices[2].x = (float) width;
-	vertices[2].y = (float) height;
+	vertices[2].y = 0.0f;
 	vertices[3].x = (float) width;
 	vertices[3].x = (float) width;
-	vertices[3].y = 0.0f;
+	vertices[3].y = (float) height;
 
 
 	vertices[0].s = 0.0f;
 	vertices[0].s = 0.0f;
 	vertices[0].t = 0.0f;
 	vertices[0].t = 0.0f;
 	vertices[1].s = 0.0f;
 	vertices[1].s = 0.0f;
 	vertices[1].t = 1.0f;
 	vertices[1].t = 1.0f;
 	vertices[2].s = 1.0f;
 	vertices[2].s = 1.0f;
-	vertices[2].t = 1.0f;
+	vertices[2].t = 0.0f;
 	vertices[3].s = 1.0f;
 	vertices[3].s = 1.0f;
-	vertices[3].t = 0.0f;
+	vertices[3].t = 1.0f;
 
 
 	if (flags.mipmaps)
 	if (flags.mipmaps)
 		filter.mipmap = defaultMipmapFilter;
 		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);
 	glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), &v[0].s);
 
 
 	gl.prepareDraw();
 	gl.prepareDraw();
-	gl.drawArrays(GL_TRIANGLE_FAN, 0, 4);
+	gl.drawArrays(GL_TRIANGLE_STRIP, 0, 4);
 
 
 	glDisableVertexAttribArray(ATTRIB_TEXCOORD);
 	glDisableVertexAttribArray(ATTRIB_TEXCOORD);
 	glDisableVertexAttribArray(ATTRIB_POS);
 	glDisableVertexAttribArray(ATTRIB_POS);

+ 1 - 1
src/modules/graphics/opengl/Polyline.cpp

@@ -355,7 +355,7 @@ void Polyline::draw()
 			indices[i * 6 + 2] = GLushort(i * 4 + 2);
 			indices[i * 6 + 2] = GLushort(i * 4 + 2);
 
 
 			// Second triangle.
 			// 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 + 4] = GLushort(i * 4 + 2);
 			indices[i * 6 + 5] = GLushort(i * 4 + 3);
 			indices[i * 6 + 5] = GLushort(i * 4 + 3);
 		}
 		}

+ 5 - 1
src/modules/graphics/opengl/VertexBuffer.cpp

@@ -363,13 +363,17 @@ void VertexIndex::fill()
 
 
 	T *indices = (T *) mapper.get();
 	T *indices = (T *) mapper.get();
 
 
+	// 0----2
+	// |  / |
+	// | /  |
+	// 1----3
 	for (size_t i = 0; i < maxSize; ++i)
 	for (size_t i = 0; i < maxSize; ++i)
 	{
 	{
 		indices[i*6+0] = T(i * 4 + 0);
 		indices[i*6+0] = T(i * 4 + 0);
 		indices[i*6+1] = T(i * 4 + 1);
 		indices[i*6+1] = T(i * 4 + 1);
 		indices[i*6+2] = T(i * 4 + 2);
 		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+4] = T(i * 4 + 2);
 		indices[i*6+5] = T(i * 4 + 3);
 		indices[i*6+5] = T(i * 4 + 3);
 	}
 	}