|
Drawing with triangles
Greetings all,
I been redoing some of my code where I use quads and redone it using triangles. Although, my sides are not always made in the counter-clockwise winding. For instance, when I set glPolygonMode(GL_FRONT, GL_LINE), some sides of the cube have clockwise winding and others do not. Here is some code of a partial cube made out of triangle_strips. Maybe it is just bad implementation. Constructive criticism is encouraged. Thanks in advance.
-----------------------------
"There are ones that say they can and there are those who actually do."
"...u can not learn programming in a class, you have to learn it on your own."
Edited by - cMADsc on March 5, 2002 6:23:36 PM
-----------------------------"There are ones that say they can and there are those who actually do.""...u can not learn programming in a class, you have to learn it on your own."
Ok, I have seemed to of figured it out. Not a complete cube yet but I am getting there. Trial and error....., don't you just love it. Not certain though, any suggestion still appreciated.
source]
glBegin(GL_TRIANGLE_STRIP);
//front
glColor3f(1,0,0);
glVertex3f(25, 25, 25);//tr
glVertex3f(-25, 25, 25);//tl
glVertex3f(25,-25, 25);//br
glColor3f(1,0,1);
glVertex3f(-25,-25,25);//b/
glColor3f(0,0,1);
glVertex3f(-25,-25,-25);
glVertex3f(-25,25,-25);
glColor3f(0.5,1,0);
glVertex3f(25, -25, -25);
glVertex3f(25, 25, -25);
glVertex3f(25, 25, 25);
glColor3f(1,1,0);
glVertex3f(-25, 25, 25);
glVertex3f(25, -25, 25);
glVertex3f(-25, -25, 25);
//glVertex3f(25, -25, 25);
glEnd();
glPopMatrix();
return 0;
}
[/source]
-----------------------------
"There are ones that say they can and there are those who actually do."
"...u can not learn programming in a class, you have to learn it on your own."
Edited by - cMADsc on March 5, 2002 7:41:47 PM
source]
glBegin(GL_TRIANGLE_STRIP);
//front
glColor3f(1,0,0);
glVertex3f(25, 25, 25);//tr
glVertex3f(-25, 25, 25);//tl
glVertex3f(25,-25, 25);//br
glColor3f(1,0,1);
glVertex3f(-25,-25,25);//b/
glColor3f(0,0,1);
glVertex3f(-25,-25,-25);
glVertex3f(-25,25,-25);
glColor3f(0.5,1,0);
glVertex3f(25, -25, -25);
glVertex3f(25, 25, -25);
glVertex3f(25, 25, 25);
glColor3f(1,1,0);
glVertex3f(-25, 25, 25);
glVertex3f(25, -25, 25);
glVertex3f(-25, -25, 25);
//glVertex3f(25, -25, 25);
glEnd();
glPopMatrix();
return 0;
}
[/source]
-----------------------------
"There are ones that say they can and there are those who actually do."
"...u can not learn programming in a class, you have to learn it on your own."
Edited by - cMADsc on March 5, 2002 7:41:47 PM
-----------------------------"There are ones that say they can and there are those who actually do.""...u can not learn programming in a class, you have to learn it on your own."
For one thing, you might try putting all the vertices in an array and using OpenGL's vertex array capability (or at least glVertex3fv(boxcorner[1]); rather than glVertex3f(float, float, float)) The code is shorter that way and a lot easier to read, so you don't need to worry that maybe your code is 99.9% right but you missed the sign on one of the floats or something.
[edited] here I had written a paragraph on why it isn't possible to make a cube out of a single triangle strip and as soon as I saw it posted I realized what I had done wrong
I'll post the complete code shortly.
Edited by - Chromebender on March 5, 2002 9:50:53 PM
[edited] here I had written a paragraph on why it isn't possible to make a cube out of a single triangle strip and as soon as I saw it posted I realized what I had done wrong

Edited by - Chromebender on March 5, 2002 9:50:53 PM
OK, I finally managed to tesselate the cube into a triangle strip. Flattened, the cube would look like this:
(each X represents a face of the cube, the slashes are half-faces)
X\-----
-X-----
-X-----
-X-----
-\X----
Here are the relevant code snippets (I used Nehe Lesson 7 as the base code):
Basically, looking at the cube from above, the vertices of the top face in clockwise order, starting at the upper-left are 0,1,2,3. If the top face were removed, the corners of the bottom face in clockwise order from the upper-let would be 4,5,6,7.
GLfloat boxcoords[8][3] = { -1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, -1.0, 1.0, -1.0,-1.0, 1.0,
-1.0, 1.0,-1.0, 1.0, 1.0,-1.0,
1.0,-1.0,-1.0, -1.0,-1.0,-1.0 };
The actual triangle strip drawing code (with texture support)
glBegin(GL_TRIANGLE_STRIP);
// Top Face
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(boxcoords[0]);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(boxcoords[1]);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(boxcoords[3]);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(boxcoords[2]);
// Front upper-right half face
glNormal3f( 0.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(boxcoords[6]);
// Right Face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(boxcoords[1]);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(boxcoords[5]);
// Rear Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(boxcoords[0]);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(boxcoords[4]);
// Left Face
glNormal3f( -1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(boxcoords[3]);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(boxcoords[7]);
// Front lower-left half face
glNormal3f( 0.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(boxcoords[6]);
//Bottom face
glNormal3f( 0.0f, 0.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(boxcoords[4]);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(boxcoords[5]);
glEnd();
(each X represents a face of the cube, the slashes are half-faces)
X\-----
-X-----
-X-----
-X-----
-\X----
Here are the relevant code snippets (I used Nehe Lesson 7 as the base code):
Basically, looking at the cube from above, the vertices of the top face in clockwise order, starting at the upper-left are 0,1,2,3. If the top face were removed, the corners of the bottom face in clockwise order from the upper-let would be 4,5,6,7.
GLfloat boxcoords[8][3] = { -1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, -1.0, 1.0, -1.0,-1.0, 1.0,
-1.0, 1.0,-1.0, 1.0, 1.0,-1.0,
1.0,-1.0,-1.0, -1.0,-1.0,-1.0 };
The actual triangle strip drawing code (with texture support)
glBegin(GL_TRIANGLE_STRIP);
// Top Face
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(boxcoords[0]);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(boxcoords[1]);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(boxcoords[3]);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(boxcoords[2]);
// Front upper-right half face
glNormal3f( 0.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(boxcoords[6]);
// Right Face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(boxcoords[1]);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(boxcoords[5]);
// Rear Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(boxcoords[0]);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(boxcoords[4]);
// Left Face
glNormal3f( -1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(boxcoords[3]);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(boxcoords[7]);
// Front lower-left half face
glNormal3f( 0.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(boxcoords[6]);
//Bottom face
glNormal3f( 0.0f, 0.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(boxcoords[4]);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(boxcoords[5]);
glEnd();
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement