views:

846

answers:

2

Hi guys,

I'm quite new to opengl es so I'm wondering if it is possible to use the glDrawElements function to draw two separate squares(I actually want to draw more than two, but for learning I will create only two) ?

the vertex array looks like this:

CGFloat array[] =

{ 0.2, 0.4, 0.2, 0.2, 0.4, 0.2, 0.4, 0.4,

0.8, 1.0, 0.8, 0.8, 1.0, 0.8, 1.0, 1.0, };

Thanks a lot for your attention !

A: 

because you're using triangle strips, you can't just do this in the obvious way, because you'll draw some extra triangles in between your two squares.

Here are a couple of articles about how to get around this by using extra invisible triangles:

http://www.gamedev.net/reference/articles/article1871.asp http://en.wikipedia.org/wiki/Triangle%5Fstrip

if you're not drawing a huge number of squares, you might just want to stick with separate short glDrawElements calls and avoid having to think about all this.

David Maymudes
From what I'm seeing I can only use a common verticle for two squares, which is not really what I need, grr, the problem is that I need these two square to not have a common point. Like calling the draw function for the first 4 verticles once, and another call for the last 4 verticles, so I'm going to get two squares, but I'm going to make two calls, which I'm trying to avoid, grrr, I'm not sure if this is even possible...Tho, thanks a lot for your answer !!!
Andy
+3  A: 

You can do this by using an array of indices with your vertices, or by using degenerate triangles between your squares. An index array will let you specify what vertex is connected to what, and makes it easy to create many disconnected objects. For an example, you can look at the source code to my application Molecules, where I render many disconnected atoms and bonds all within the same indexed array (indexed vertex buffer object, really). The iPhone graphics hardware is optimized for strip-ordered indexed triangles.

As David points out, if you still wish to use triangle strips for your squares, you can connect them simply by creating an extra degenerate triangle between your two squares that has a side composed of the same vertex repeated twice, with the third vertex being the starting point of your second square. If you were to draw it out, it would look like a line between your two squares. Modern GPUs are pretty efficient at stripping out these degenerate triangles.

Brad Larson