Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Quick confirmation on Triangle Strips / Degenerate Triangles


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • This topic is locked This topic is locked
11 replies to this topic

#1 Blips   Members   -  Reputation: 122

Like
0Likes
Like

Posted 11 August 2011 - 03:22 PM

In order to squeeze some increased performance out of iOS devices, I'm working on converting my geometry into indexed triangle strips. I found a program (htgen) that works with wavefront obj files and produces a list of indices to produce triangle strips. Brilliant I thought! But then when I tried rendering the newly generated data, the end result looked worse than this:

Posted Image

Now, initially I knew that I had to connect strips with degenerate triangles, however I had no idea that the number of degenerate triangles between strips were not constant.

After spending hours reading up on triangle strips, I finally discovered the winding of a proceeding strip can be altered if the incorrect number of degenerate triangles were inserted in between two strips. I spent a few more hours writing out some basic triangle strips over pages and pages of paper and figured out (or so I thought?) exactly how many degenerate triangles were needed depending upon the previous triangle's number.

So please correct me if I'm wrong, but:
1) If I have 4 triangles, defined as the indices 0 1 2 3 4 5, OpenGL will draw 4 triangles in the following order: 0 1 2, 2 1 3, 2 3 4, 4 3 5 (which let's assume is CCW for this example).

2)
When joining two strips, normally only 2 extra indices need to be inserted, generating 4 degenerate triangles. So building on the previous strip, if I were to want to join a new strip consisting of 3 new triangles defined as a strip of 6 7 8 9 10 (triangles 6 7 8, 8 7 9, 8 9 10), the final joined list of indices would look like 0 1 2 3 4 5 5 6 6 7 8 9 10 where the bolded numbers are the extra inserted vertices creating 4 new degenerate triangles.

3)
If joining two strips, and the first triangle of the new strip is odd (as in, the first strip had 3 triangles, numbered 0, 1 and 2, and now the starting triangle of the 2nd strip is numbered 3) three additional indices must be inserted in between the two strips generating a total 5 degenerate triangles in order for the winding of the 2nd strip to remain CCW.

Now based on what I just outline above, I fixed up my code to insert the correct number of degenerate triangles between strips and got the above image which while is an improvement over the original, it's still in no way correct (the object should look like Canada and the US). So now I'm stuck and I have to assume that if my above understandings are correct, that the htgen program is not generating strips with consistent winding orders (the objects render perfectly fine if I turn off culling).

If that's the case, it looks like I'll have try generating strips myself because I literally cannot find another suitable application that will generate triangle strips. So in the interest of saving myself even more headache and time spent on writing my own application to generate triangle strips, I wanted to confirm that my understanding of the subject is indeed correct.

Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 8281

Like
0Likes
Like

Posted 11 August 2011 - 04:39 PM

If you're using indexes you don't need degenerate triangles. Just draw with GL_TRIANGLES instead and arrange your indexes so that your strips are concatenated.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#3 Blips   Members   -  Reputation: 122

Like
0Likes
Like

Posted 11 August 2011 - 05:22 PM

If you're using indexes you don't need degenerate triangles. Just draw with GL_TRIANGLES instead and arrange your indexes so that your strips are concatenated.


Do you have something I can reference that describes what you're talking about? I've only ever read of concatenating strips using degenerate triangles. I'm not sure how I'd go about doing so in such a way that rendering the data as GL_TRIANGLES would work.

#4 mhagain   Crossbones+   -  Reputation: 8281

Like
0Likes
Like

Posted 11 August 2011 - 06:25 PM

Say you've got 2 strips, strip 1 has vertexes 0, 1, 2, 3 and strip 2 has vertexes 4, 5, 6, 7. To render them with GL_TRIANGLES you use indexes 0, 1, 2, 1, 3, 2, 4, 5, 6, 5, 7, 6 for your glDrawElements call. No need to make any changes to the vertexes. That'll do it.

The number of indexes you need for each strip is always (numverts - 2) * 3 and just remember to follow the winding order rules, reversing the order for alternate tris that make up the strip. Code for writing out indexes might look something like this:

for (int i = 2; i < stripverts; i++)
		{
			indexes[totalindexes++] = totalverts + i - 2;
			indexes[totalindexes++] = (i & 1) ? (totalverts + i) : (totalverts + i - 1);
			indexes[totalindexes++] = (i & 1) ? (totalverts + i - 1) : (totalverts + i);
		}

		totalverts += stripverts;

Then just ensure that all of your vertexes for all strips are in a single big array (or single VBO, as appropriate) and your draw call is:

glDrawElements (GL_TRIANGLES, totalindexes, GL_UNSIGNED_WHATEVER, indexes); // _WHATEVER is _SHORT or _INT, replace indexes with 0 for a VBO

OK, it's more indexes, but indexes are much smaller than full vertexes, the duplicate vertexes will be in your cache and so won't need to be retransformed, and you'll find that the overall submission is lighter than with degenerate triangles.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#5 Blips   Members   -  Reputation: 122

Like
0Likes
Like

Posted 11 August 2011 - 08:34 PM

Thanks a lot mhagain for your detailed explanation. In the end it's essentially functions like indexed triangles correct? If so I'm not sure it'll provide any performance improvements over what I'm doing already. It's recommended that on the iPhone that actual triangle strips are to be used for optimal performance.

#6 L. Spiro   Crossbones+   -  Reputation: 14314

Like
0Likes
Like

Posted 11 August 2011 - 08:58 PM

This site has source code that works and an explanation of all the little gotchas related to creating triangle strips.
For example, yes you need to add a vertex when joining strips, but only one vertex if the first vertex of the second strip has already been duplicated.
Some strips need to be reversed under certain circumstances.

Following his article and code I implemented my own triangle stripper that does work 100% of the time.
However as you can see by my results here, adding triangle strips actually decreased my performance. The reason was that they thrashed my cache.
For me, ordering the vertices for best use of the cache has proved the most helpful. My site is specifically for these kinds of discoveries, so that you can see what types of things have what impact on performance.

However there is no difference between graphics RAM and CPU RAM on iPhone devices, and caching will work a bit differently. You could continue your quest and see what happens, but don’t be surprised if nothing is gained.
I am waiting for my Macintosh computer so I can test this myself too, and I will post my results when I have done so.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#7 Blips   Members   -  Reputation: 122

Like
0Likes
Like

Posted 12 August 2011 - 02:07 PM

I love it when a plan comes together! After writing my Triangle Stripper program over the course of a few hours, I tested it today and it worked completely in the first try! It runs amazingly quick (about 1 second to strip a model composed of 2400+ vertices) and produces some very large strips. Unfortunately it produces quite a few short strips as well. But most importantly is, the strips that it does generate render 100% perfectly.

Compared to the htgen program I was using before, it produces roughly 17% more strips :angry:.

Now that it works, I can tinker with it and try and eliminating those rogue strips of 1 or 2 faces.

edit:
And just an additional small update. My game's performance has now jumped over 26% from using indexed triangles in VBOs. Now I just need to perform some basic frustum culling and I should be set!

#8 L. Spiro   Crossbones+   -  Reputation: 14314

Like
0Likes
Like

Posted 13 August 2011 - 12:36 AM

You made one based off the link I gave?

Be warned: the code he provides, while very helpful for getting a result and understanding all the gotchas, is not particularly fast (and there is a leak of memory when it fails in some cases).
My rewrite of his code is over 4 times faster, so if you did use his as a reference, be aware that there is tons of room for improvement. Your 1 second would go below to 0.25 seconds.

Suggestions for improving upon his code: Use a faster sort (I use a bottom-up merge sort) and make fewer allocations. Pass buffers down to be reused when you are searching the 3 directions of the triangle strips.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#9 Blips   Members   -  Reputation: 122

Like
0Likes
Like

Posted 13 August 2011 - 12:49 AM

You made one based off the link I gave?

Be warned: the code he provides, while very helpful for getting a result and understanding all the gotchas, is not particularly fast (and there is a leak of memory when it fails in some cases).
My rewrite of his code is over 4 times faster, so if you did use his as a reference, be aware that there is tons of room for improvement. Your 1 second would go below to 0.25 seconds.

Suggestions for improving upon his code: Use a faster sort (I use a bottom-up merge sort) and make fewer allocations. Pass buffers down to be reused when you are searching the 3 directions of the triangle strips.

L. Spiro


I read over the article before in the past, and re-read it again but wrote my stripper on my own. I'm not doing much sorting and the results are written as binary data to files which are loaded at the appropriate time when the game is running. So the actual performance of the code is not a huge importance since the binary files are loaded extremely quickly.

The actual logic for the stripper is rather simple. It essentially just generates all possible strips for a given mesh and selects the longest strip and keeps repeating until no more free faces are left.

#10 glxy   Members   -  Reputation: 101

Like
0Likes
Like

Posted 28 September 2012 - 12:53 AM

any one have sample code for triangle strip

#11 glxy   Members   -  Reputation: 101

Like
0Likes
Like

Posted 28 September 2012 - 01:01 AM

Say you've got 2 strips, strip 1 has vertexes 0, 1, 2, 3 and strip 2 has vertexes 4, 5, 6, 7. To render them with GL_TRIANGLES you use indexes 0, 1, 2, 1, 3, 2, 4, 5, 6, 5, 7, 6 for your glDrawElements call. No need to make any changes to the vertexes. That'll do it.

The number of indexes you need for each strip is always (numverts - 2) * 3 and just remember to follow the winding order rules, reversing the order for alternate tris that make up the strip. Code for writing out indexes might look something like this:

for (int i = 2; i < stripverts; i++)
		{
			indexes[totalindexes++] = totalverts + i - 2;
			indexes[totalindexes++] = (i & 1) ? (totalverts + i) : (totalverts + i - 1);
			indexes[totalindexes++] = (i & 1) ? (totalverts + i - 1) : (totalverts + i);
		}

		totalverts += stripverts;

Then just ensure that all of your vertexes for all strips are in a single big array (or single VBO, as appropriate) and your draw call is:

glDrawElements (GL_TRIANGLES, totalindexes, GL_UNSIGNED_WHATEVER, indexes); // _WHATEVER is _SHORT or _INT, replace indexes with 0 for a VBO

OK, it's more indexes, but indexes are much smaller than full vertexes, the duplicate vertexes will be in your cache and so won't need to be retransformed, and you'll find that the overall submission is lighter than with degenerate triangles.

do u have any sample code i m begiiner in open gl

#12 rip-off   Moderators   -  Reputation: 8762

Like
0Likes
Like

Posted 28 September 2012 - 01:39 AM

Please do not re-open old threads.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS