Hello Guest

Cannot get TRIANGLE STRIP to render a Quad in 3D

  • 8 Replies
  • 14820 Views
Cannot get TRIANGLE STRIP to render a Quad in 3D
« on: June 06, 2013, 16:10:38 »
I have been trying for hours to render a square in 3D space with TIRANGLE_STRIP..

Can anyone please just show me how to render one?
Just show me in immediate mode and ill adapt it to my rendering..

example:
glBegin(GL_TRIANGLE_STRIP);
    glVertex3f(.....);
    glVertex3f(.....);
    .....
glEnd();

i cant get mine to work it renders an odd shape for some reason.

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Cannot get TRIANGLE STRIP to render a Quad in 3D
« Reply #1 on: June 06, 2013, 16:27:06 »
For a square of length 2, centre 0, 0 pointing along the y-axis (as in normal 0, 1, 0)

Code: [Select]
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(-1, 0, -1);
glVertex3f(1, 0, -1);
glVertex3f(1, 0, 1);
glVertex3f(-1, 0, 1);
glEnd();

As far as I can see, this is exactly the same as in 2D but with added 0s, so your problem is more likely to be in your viewing or projection matrix. Also sometimes people forget to enable GL_DEPTH_TEST but if you're drawing just the one shape then this shouldn't be a problem.

Re: Cannot get TRIANGLE STRIP to render a Quad in 3D
« Reply #2 on: June 06, 2013, 16:51:29 »
Yeah but thats one side.. I can get one side to show up but I start getting confused when I try to draw 6 sides of the 3D square

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Cannot get TRIANGLE STRIP to render a Quad in 3D
« Reply #3 on: June 06, 2013, 20:44:28 »
OK, you mean a cube.

You can't do that with only one triangle strip, you would need 3 individual strips. If you were using GL_TRIANGLE_FAN, then you could do it with just two, but, provided that you are using index buffers, I would simply use GL_TRIANGLES it just makes everything simpler since you can do it all with only one glDrawElements and you can draw several cubes at the same time with only this command.

For a cube length 2, centre (0, 0, 0): I give both strip and fan.

I have labelled each vertex bellow and both sections use the same numbering to help convert to vbos. Essentially 0 - 3 is the vertices of the bottom face wrapped counter-clockwise and 4 - 7 are the vertices of the top face also counter-clockwise. But that doesn't really matter.

As 3 strips
Code: [Select]
glBegin(GL_TRIANGLE_STRIP); //Draws 4 sides
glVertex3f(-1, -1, -1); //0
glVertex3f(-1, 1, -1); //4
glVertex3f(1, 1, -1); //5
glVertex3f(1, -1, -1); //1
glVertex3f(1, -1, 1); //2
glVertex3f(1, 1, 1); //6
glVertex3f(-1, -1, 1); //3
glVertex3f(-1, 1, 1); //7
glVertex3f(-1, -1, -1); //0
glVertex3f(-1, 1, -1); //4
glEnd();
glBegin(GL_TRIANGLE_STRIP); //Draws bottom
glVertex3f(-1, -1, -1); //0
glVertex3f(1, -1, -1); //1
glVertex3f(1, -1, 1); //2
glVertex3f(-1, -1, 1); //3
glEnd();
glBegin(GL_TRIANGLE_STRIP); //Draws top
glVertex3f(-1, 1, -1); //4
glVertex3f(1, 1, -1); //5
glVertex3f(1, 1, 1); //6
glVertex3f(-1, 1, 1); //7
glEnd();

As 2 fans
Code: [Select]
glBegin(GL_TRIANGLE_FAN); //Draws bottom, left and front
glVertex(-1, -1, -1); //0
glVertex(1, -1, -1); //1
glVertex(1, 1, -1); //5
glVertex(-1, 1, -1); //4
glVertex(-1, 1, 1); //7
glVertex(-1, -1, 1); //3
glVertex(1, -1, 1); //2
glVertex(1, -1, -1); //1
glEnd();
glBegin(GL_TRIANGLE_FAN); //Draws top, right and back
glVertex(1, 1, 1); //6
glVertex(1, 1, -1); //5
glVertex(-1, 1, -1); //4
glVertex(-1, 1, 1); //7
glVertex(-1, -1, 1); //3
glVertex(1, -1, 1); //2
glVertex(1, -1, -1); //1
glVertex(1, 1, -1); //5
glEnd();

You can figure out triangles yourself I'm sure, sorry but it would take to long to write in immediate mode.

Re: Cannot get TRIANGLE STRIP to render a Quad in 3D
« Reply #4 on: June 07, 2013, 19:37:43 »
Thanks for the help bro but can you label in the first triangle strip begin call where u render the 4 sides which vertex calls are for which sides or just separate them into different begin calls like you did for the top and bottom? im a gl noob so its hard for me to figure out which is what and i need them separated...

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Cannot get TRIANGLE STRIP to render a Quad in 3D
« Reply #5 on: June 07, 2013, 20:20:05 »
Well it's not all that simple since they sort of overlap, but I'll try and hopefully it will be clear. The reason I put top and bottom in seperate glBegin - glEnds is because they have to be. As I said before you cannot make a cube with a single triangle strip, it has to be 3.

You get the "overlap" of indices because in a cube, each edge is shared by two faces
Code: [Select]
glBegin(GL_TRIANGLE_STRIP); //Draws 4 sides
glVertex3f(-1, -1, -1); //0 LEFT
glVertex3f(-1, 1, -1); //4 LEFT
glVertex3f(1, 1, -1); //5 LEFT + BACK
glVertex3f(1, -1, -1); //1 LEFT + BACK
glVertex3f(1, -1, 1); //2 BACK + RIGHT
glVertex3f(1, 1, 1); //6 BACK + RIGHT
glVertex3f(-1, -1, 1); //3 RIGHT + FRONT
glVertex3f(-1, 1, 1); //7 RIGHT + FRONT
glVertex3f(-1, -1, -1); //0 FRONT
glVertex3f(-1, 1, -1); //4 FRONT
glEnd();

And if you wanted to do them in individual glBegin - glEnd pairs, just take out all of the vertices of that face in order. So BACK would be 5, 1, 2, 6.

Re: Cannot get TRIANGLE STRIP to render a Quad in 3D
« Reply #6 on: June 07, 2013, 21:30:48 »
Ahh ok, and if I would use VBOs so there is not glBegin/glEnd calls it will just be continues buff.put(...).put(...).put(...) So still there has to be overlap for it to use a triangle strip?
if i were to use drawarrays
« Last Edit: June 07, 2013, 21:35:37 by gopgop »

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Cannot get TRIANGLE STRIP to render a Quad in 3D
« Reply #7 on: June 08, 2013, 09:09:04 »
Yes, that's right. However I still recommend using index buffers and GL_TRiANGLES, but if you're sure about strips...

Re: Cannot get TRIANGLE STRIP to render a Quad in 3D
« Reply #8 on: June 08, 2013, 09:49:43 »
But wouldn't triangles be slower?