Nominate our 2022 Qt Champions!

How to interpret Index matrix in the OpenGl Cube example

  • I am new to OpenGl. I was trying to understand the Qt OpenGL cude example code. I am confused with the indices matrix in the geometryengine code.

    The index buffer values that i found over the internet where 36 size matrixes. But in Qt example it's 34 size matrix. I found some thing related to degenerated triangle strips in one article. Still I am unable to connect the dots.

    I think this is not related to Qt, but can any one help me out in understanding how to arrive at those values.
    Is there any article in the internet related to the generation of index matrix that we are having in this example?

  • Moderators

    Hi, welcome to devnet.

    First of all it's a index list, not a matrix. The text is just formatted like that for readability.
    The example uses triangle strips. The way they work is for N indices N-2 triangles are created like this:
    indices: 0 1 2 3 4 5 -> triangles 012, 123, 234, 345.

    So in the example there are 34 indices, which creates 32 triangles. That's still too much, as there are only 12 triangles in a cube. Well the example uses a little trick that allows to connect a couple of strips that don't share vertices.

    Consider a list like this: 1 2 3 3 3 4 5:
    This will create 5 triangles: 123, 233, 333, 334, 345.
    The triangles 233, 333 and 334 are a zero-area triangles so they are early discarded by the graphics pipeline.

    Back to the cube example: 0 1 2 3 3 4 4 5 6 7 7...
    This creates triangles 012 123 -233- -334- -344- -445- 456 567 -677- ...
    So a lot of these are actually empty. Count triplets that don't contain repeated number and from the 34 indices provided you will arrive at the actual 12 triangles that form a cube. Text is formatted so that there are 2 visible triangles (a single face) in each line.

    I admit this is not very beginner friendly example and in real life geometry like that is not that common. The models usually come from some 3d modeling software that calculates these strips indices for you so you don't have to think about them too much. But a lot of times it's just easier to use triangle lists (unless you draw tons and tons of geometry and each index counts).

  • Hi Chris,
    Thanks for the reply. I get it now. One more question. The 3D modelling softwares that you were referring to, how can I create these lists in them? Do I have to draw my model in those softwares to generate these lists?

  • Moderators

    Well you don't have to do anything. You can of course create some simple geometry like the cube or a cone by hand, but I can't imagine modeling something like a character face with 1000 vertices using notepad ;)

    Because of how today's models actually look like and with the advancements in GPU throughput there's very little software that uses triangle strips.
    You can see even in this short example that there's little gain in using strips. We get 34 indices where a triangle list would require 36(3 for each of 12 triangle) and would definitely be easier to understand.

    Programs like Blender or Maya usually export geometry using plain triangle lists, not triangle strips. I would suggest to switch too.
    The way it works is you model in such software and then export it to a file in one of supported formats. For beginners the ".obj": is a very simple and human readable text based format. It contains this list of vertex coordinates and indices, which you can then read in your program and feed to OpenGL.

Log in to reply