PDA

View Full Version : Make a cube with a single GL_TRIANGLE_STRIP



mixedcase
03-29-2013, 06:53 AM
Is it possible to create a cube with a single GL_TRIANGLE_STRIP? I have searched around but most examples use multiple strips to make the cube. I found a few people saying it's possible with a single strip but their examples are incredibly poor with garbled ASCII art and no indication of the order the vertexes need to be in. In fact, many didn't seem to understand how triangle strips actually work as far as winding and the edges that will be used for the next triangle.

I have tried a million different ways but I can't seem to make it work. I even made a physical cube that I unfolded but it seems impossible. The problem is the edges that have to change direction as I render around the cube. I just can't make it work with the alternating winding rules.

Alfonse Reinheart
03-29-2013, 07:01 AM
Why do you need to? It's twelve triangles; the GPU is not going to choke on 12 triangles rendered as GL_TRIANGLES.

mixedcase
03-29-2013, 07:06 AM
Because I want to output the cube from a geometry shader and it can only output triangle strips.

mixedcase
03-29-2013, 07:17 AM
I mean if I can't do it with a single strip it's not the end of the world. I was just trying to optimize if possible. Maybe it will make no difference. I suppose two strips of 3 faces each will work just as well.

Alfonse Reinheart
03-29-2013, 09:37 AM
A GS can output triangle strips. It does so by using EndPrimitive() (http://www.opengl.org/wiki/Geometry_Shader#Outputs) to end the primitive and start a new one. So you could output individual triangles.

But you shouldn't be outputting a cube from a GS in any case. That's way too much data for a GS to dump and still get anything remotely like performance.

trinitrotoluene
03-29-2013, 12:04 PM
You can use primitive restart. See the documentation (http://www.opengl.org/sdk/docs/man/xhtml/glPrimitiveRestartIndex.xml).

If you choose the value of the primitive restart index:



GLuint pri = a_number;
GLfloat vertices[?]={...};
GLuint indices[i]={0,1,2,...,pri,12,13,14,...};
glPrimitiveRestartIndex(pri);
...
glEnable[(GL_PRIMITIVE_RESTART);//bind VAO and element array buffer
...
glDrawElements(GL_TRIANGLE_STRIP,0,i,nullptr);




Or you can also just enable GL_PRIMITIVE_RESTART_FIXED_INDEX then the value of pri must be equal of the following number 2^n-1. Where n is equal to 8*sizeof(GLuint or GLushort or GLubyte).

Dark Photon
03-29-2013, 02:34 PM
Or you can also just enable GL_PRIMITIVE_RESTART_FIXED_INDEX then the value of pri must be equal of the following number 2^n-1.
That's interesting. Had never heard of it. But I see now it's because it's one of the goodies coming back to us from GL-ES that I haven't read up on: ARB_ES3_compatibility (http://www.opengl.org/registry/specs/ARB/ES3_compatibility.txt). And supported on my desktop drivers too. Need to read up!

mixedcase
03-29-2013, 04:46 PM
But you shouldn't be outputting a cube from a GS in any case. That's way too much data for a GS to dump and still get anything remotely like performance.

In my tests outputting from the geometry shader in this way is considerably faster than using primitives from a static VBO. At least 25% faster and sometimes more.



You can use primitive restart. See the documentation (http://www.opengl.org/sdk/docs/man/xhtml/glPrimitiveRestartIndex.xml).

Is this possible from within a geometry shader? Something to keep in my toolbox anyway, thanks!

skynet
03-29-2013, 05:25 PM
http://www.cs.umd.edu/gvil/papers/av_ts.pdf (first page)

Alfonse Reinheart
03-29-2013, 06:07 PM
In my tests outputting from the geometry shader in this way is considerably faster than using primitives from a static VBO. At least 25% faster and sometimes more.

Then your static VBO code is rendering them really wrongly.

mixedcase
03-29-2013, 06:27 PM
http://www.cs.umd.edu/gvil/papers/av_ts.pdf (first page)

Thanks, great reading!


Then your static VBO code is rendering them really wrongly.

Uh, how hard and wrong is "glDrawArrays", lol.

Alfonse Reinheart
03-29-2013, 06:35 PM
Uh, how hard and wrong is "glDrawArrays", lol.

I don't know; are you calling it once per cube, with uniform changes between? And how many cubes are we talking about here? If you're trying to make Yet Another Minecraft Ripoff, then you should know that Minecraft doesn't render cubes.

Also, how did you measure this performance difference?

mixedcase
03-29-2013, 06:52 PM
I don't know; are you calling it once per cube, with uniform changes between? And how many cubes are we talking about here? If you're trying to make Yet Another Minecraft Ripoff, then you should know that Minecraft doesn't render cubes.

Also, how did you measure this performance difference?

I'm editing this response to try to end this derailment. We both know there are many ways to accomplish these things and no offense but you're in no position to understand my requirements. None of this geometry shader and performance talk has anything to do with my original question.