Sign in to follow this  
Blips

OpenGL Quick confirmation on Triangle Strips / Degenerate Triangles

Recommended Posts

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 ([url="http://www2.cs.cas.cz/%7Esima/htgen-en.html"]htgen[/url]) 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:

[img]http://img34.imageshack.us/img34/6213/img0020g.png[/img]

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:
[b]1)[/b] 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).
[b]
2)[/b] 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 [b]5 6[/b] 6 7 8 9 10 where the bolded numbers are the extra inserted vertices creating 4 new degenerate triangles.
[b]
3)[/b] 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) [b]three[/b] 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 [i]is[/i] 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 ([b]the objects render perfectly fine if I turn off culling[/b]).

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.

Share this post


Link to post
Share on other sites
[quote name='mhagain' timestamp='1313102347' post='4847975']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites
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:

[code] 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;[/code]

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:

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

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[url="http://www.codercorner.com/Strips.htm"]This site[/url] 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 [url="http://lspiroengine.com/?p=60"]here[/url], 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

Share this post


Link to post
Share on other sites
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.

[b]edit[/b]:
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[quote name='YogurtEmperor' timestamp='1313217383' post='4848531']
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
[/quote]

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.

Share this post


Link to post
Share on other sites
[quote name='mhagain' timestamp='1313108738' post='4848022']
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:

[code] 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;[/code]

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:

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

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.
[/quote]
do u have any sample code i m begiiner in open gl

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628396
    • Total Posts
      2982438
  • Similar Content

    • By test opty
      Hi all,
       
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
       
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
  • Popular Now