StrayProg
StrayProg

Reputation: 11

Conversion from a shape drawn with quads, to a shape drawn with triangles

I am a student, for a legacy openGL assignment a few weeks ago we were asked to program 3D shapes using openGL commands such as GL_DRAW_QUADS, I had been using quads for essentially everything up until that point (since it was admittedly easier)

I've ran into a hurdle. I have multiple shapes that can be rendered with quads, such as a sphere and torus, and these currently work as intended when rendered directly with GL_QUADS

However, when storing a shape to be rendered later via the vertex buffers, which is the much better way of doing things, My model renderer will only work with shapes which are composed of triangles. and although a quad is technically two triangles, simply changing my methods to work with GL_TRIANGLES, or GL_TRIANGLE_FAN or GL_TRIANGLE_STRIP doesn't solve my issues.

I'm wondering what is the best way to convert shapes that have been drawn in quads to triangles without having to completely rewrite methods for rendering these shapes.

Upvotes: 0

Views: 463

Answers (1)

Jeffrey
Jeffrey

Reputation: 11400

Assuming you have a set of vertices {v1,v2,v3,v4,v5,...} and assuming you are passing a list of indices {i1,i2,i3,i4,i5,...} to the draw command, you simply need to pass a different index list.

Where you would have passed {i1,i2,i3,i4} before, you can now pass {i1,i2,i4,i4,i2,i3} to get two triangles covering the same quad. It gets more complicated if you want fans. You do the same process every four indices.

1 -- 2           1 -- 2 
|    |  becomes  |  / |
|    |           | /  |
4 -- 3           4 -- 3

Upvotes: 2

Related Questions