PDA

View Full Version : A glVertex3f question



Spin3D
02-11-2007, 01:06 PM
Hi,

I am trying to make a 3D pyramid for the practice purpose. In the following code, display2() is used to create a 3D pyramid. However the code end up with a 2D triangle. No 3D effect at all.

Then in order to do a test, I created display1() to see how 3D triangle works. I commented the line
glutDisplayFunc(display2);
and uncommented the line
glutDisplayFunc(display1);

Surprisingly, no matter how do I change the third parameter of the glVertex3f in display1(), the output had no change. A 2D triangle is the only I could see. For example, in display1(), I used
glVertex3f( 1.0f,-1.0f, 3.0f) for the right angle of the triangle. But it created an exact triangle as what I got when I used glVertex3f( 1.0f,-1.0f, 1.0f);

I guess I am missing something very basic. But just couldn't figure out what it is. Could anyone give me some help?

Thanks in advance!




#include <GL/glui.h>

void display2(void) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glBegin(GL_TRIANGLES); // Start Drawing A Triangle
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front)

glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f( 1.0f,-1.0f, -1.0f); // Right Of Triangle (Right)

glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f( 1.0f,-1.0f, -1.0f); // Left Of Triangle (Back)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(-1.0f,-1.0f, -1.0f); // Right Of Triangle (Back)

glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left)
glEnd(); // Done Drawing The Pyramid

glFlush();
}


void display1( void )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glColor3f( .9, .9, .9 );

glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f( 1.0f,-1.0f, 3.0f); // Right Of Triangle (Front)
glEnd();

glFlush();

}

void init(void)
{
glOrtho(-5.0, 5.0, -5.0, 5.0, -15.0, 15.0);
glEnable(GL_DEPTH_TEST); // We enable the depth test (also called z buffer)
}
int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(550, 550);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello");
init();
//glutDisplayFunc(display1);
glutDisplayFunc(display2);

glutMainLoop();
return 0;
}

Komat
02-11-2007, 02:11 PM
You are using the ortographic projection so the position on the screen does not change with distance. You need to use glFrustum instead of the glOrtho.

Spin3D
02-11-2007, 05:03 PM
Thanks, Komat.

I replaced glOrtho with
glFrustum(-5.0, 5.0, -5.0, 5.0, -20.0, 200.0);

However, the outcome still doesn't look right to me. The triangle does changed. But it looks like the triangle was chopped because the color of the right of the triangle is a color between red and blue, while I assume the color should be blue. Also, the size is not right. I assume the size of the triangle should be about 1/10 of the screen width but look like it occupied the whole screen. What is wrong?

I pasted the code as below. I appreciate any advice. Thanks!


#include <GL/glui.h>

void display1( void )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f( 1.0f,-1.0f, 3.0f); // Right Of Triangle (Front)
glEnd();

glFlush();

}


void init(void)
{

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glFrustum(-5.0, 5.0, -5.0, 5.0, -20.0, 200.0);
// glOrtho(-5.0, 5.0, -5.0, 5.0, -15.0, 15.0);
glEnable(GL_DEPTH_TEST); // We enable the depth test (also called z buffer)
glMatrixMode( GL_MODELVIEW );

}
int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(550, 550);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello");
init();
glutDisplayFunc(display1);
//glutDisplayFunc(display2);

glutMainLoop();
return 0;
}

dorbie
02-11-2007, 08:09 PM
Don't specify -20 as your near. Just use 20.

Also the window you specify for frustum is on the near plane so -5 +5 at 20 is 10 wide 20 away, which is somewhat narrow fov of 2*atan(5/20).

Depending on your glShadeModel() call the color on the vertices may be interpolated between vertices. Look up that API call and experiment with it, you'll get the hang of it. Remember that the invoking vertex in a triangle is the last one issued and the GL_FLAT shademodel uses the color at that time. With GL_SMOOTH each vertex uses the current color.