Sign in to follow this  
Blips

OpenGL Quick confirmation on Triangle Strips / Degenerate Triangles

Recommended Posts

Blips    129
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
Blips    129
[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
mhagain    13430
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
Blips    129
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
L. Spiro    25638
[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
Blips    129
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
L. Spiro    25638
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
Blips    129
[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  

  • Similar Content

    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
    • By Picpenguin
      Hi
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
  • Popular Now