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(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(550, 550);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello");
init();
//glutDisplayFunc(display1);
glutDisplayFunc(display2);
glutMainLoop();
return 0;
}