PDA

View Full Version : Triangle Strip



Cosmosis
03-12-2007, 09:39 AM
In an effort to learn OpenGL as well as some basic AI, I am trying to create a Rubik's Cube solver. This is strictly an educational exercise, so I chose to use C# and the TAO framework so that I could concentrate on the object oriented design without worrying about memory or the nitty gritty WIN32 details.

When I render the Rubik's Cube using GL_QUADS, it works great. But when I try to modify my code so that it uses GL_TRIANGLE_STRIP I run in to some problems.

The problem is visible in the following screen shots ... in these shots, I am only rendering the top/front/left corner of the cube.

http://members.shaw.ca/afoat/problem.jpg


The weird thing is that this problem only appears on blue squares that are next to orange squares

http://members.shaw.ca/afoat/problem1.jpg

1) All drawn with GL_QUADS - works great
2) Yellow = GL_QUADS, Orange = GL_TRIANGLE_STRIP - works great
3) Blue = GL_TRIANGLE_STRIP - works great
4) Both = GL_TRIANGLE_STRIP
5) Yellow = GL_QUADS, Blue = GL_TRIANGLE_STRIP

The only difference between 3 and 4 is that I removed the commented portion of code that draws the orange face.

Here is the code for the orange and blue triangle strips


//Orange List
s_FrontList = Gl.glGenLists(6);
Gl.glNewList(s_FrontList, Gl.GL_COMPILE);
Gl.glBegin(Gl.GL_TRIANGLE_STRIP);
Gl.glNormal3f(0.0f, 0.0f, 1.0f);
Gl.glTexCoord2f(0f, 1f); Gl.glVertex3f(-1f, 1f, 1f);
Gl.glTexCoord2f(0f, 0f); Gl.glVertex3f(-1f, -1f, 1f);
Gl.glTexCoord2f(1f, 0f); Gl.glVertex3f( 1f, -1f, 1f);
Gl.glTexCoord2f(1f, 1f); Gl.glVertex3f( 1f, 1f, 1f);
Gl.glEnd();
Gl.glEndList();

//Blue List
s_TopList = s_FrontList + 1;
Gl.glNewList(s_TopList, Gl.GL_COMPILE);
Gl.glBegin(Gl.GL_TRIANGLE_STRIP);
Gl.glNormal3f(0.0f, 1.0f, 0.0f);
Gl.glTexCoord2f(0f, 0f); Gl.glVertex3f(-1f, 1f, -1f);
Gl.glTexCoord2f(0f, 1f); Gl.glVertex3f(-1f, 1f, 1f);
Gl.glTexCoord2f(1f, 1f); Gl.glVertex3f( 1f, 1f, 1f);
Gl.glTexCoord2f(1f, 0f); Gl.glVertex3f( 1f, 1f, -1f);
Gl.glEnd();
Gl.glEndList();

...

Gl.glCallList(s_FrontList);
Gl.glCallList(s_TopList);Even though these two faces are drawn within their own glBegin/glEnd blocks, do neighboring triangle strips share verticies?

I'm not sure if it matters, but the texture is a mostly transparent PNG with a black border ... this is used to separate squares on a face to give it that Rubik's-y feel.

Can someone please explain what I might be missing?

thinks
03-12-2007, 11:52 AM
Even though these two faces are drawn within their own glBegin/glEnd blocks, do neighboring triangle strips share verticies?
No, they don't.

I can take a guess at what's wrong in your code. When rendering a triangle strip you must make sure that when you "add" a vertex, the two previous vertices define the "edge" that together with the "added" vertex make up the triangle.

For instance. Consider the "front face" (with constant z-value that is closest to viewer, i.e. if z-axis points into screen, the smallest constant z-value), this face has four vertices. We order them as follows:

0 = top-left
1 = bottom-left
2 = top-right
3 = bottom-right

You must then pass the vertices in the following order:

0 1 2 3

which corresponds to the following two triangles:

Triangle 1 = 0 1 2
Triangle 2 = 2 1 3

This maintains orientation off the triangles.

From quickly glancing at your code you seem to have the ordering wrong. It appears that you have:

0 = top-left
1 = bottom-left
2 = bottom-right
3 = top-right

Try changing this and see if it helps. I assume that you z-axis is pointing out from the screen, so that this really is your front face. If not, then check this too.

Cosmosis
03-12-2007, 01:30 PM
Thanks for your help.

I finally found the problem ... as it turns out I am a moron :)

When I was refactoring my code to implement the vertex lists I apparently left a stray call to glBegin(GL_QUADS) before I was drawing any of the lists.

So OpenGl must have been confused after I called glBegin(GL_TRIANGLE_STRIP)

I fixed things up, and things are now working as expected.

Thanks again for your time

thinks
03-12-2007, 01:42 PM
Ok, that would have been hard for me to spot! ;)

Out of curiosity, did you have to change anything in your TRIANLGE_STRIP ordering?

Cosmosis
03-12-2007, 01:55 PM
After I removed the stray call to GL_QUADS, the ordering that you provided me worked perfectly.