Jump to content
xisto Community
Sign in to follow this  
t3jem

Programming In Glut (lesson 4) Creating 3D objects

Recommended Posts

Lesson 4 of 6. I hope you are enjoying them :unsure: .

Hello, in this tutorial we will be creating a 3D pyramid. We are building this tutorial from Lesson 3, but I took out the 2D objects and placed a 3D pyramid in there instead. The 3rd axis for drawing can be a litle confusing, but after you get the hand of it you'll do fine. Now when you are setting a 3D vertex just remember that the camera is on the positive end of the z axis. So things that have a more positive z axis value are closer than verteces with a more negative value. Well let's get started

We always start by including the glut library

#include<glut.h>

the time variable will be used to keep track of how much time has passed.
float time = 0;// set time variable to 0

Our first new function is glOrtho();, this function is much like gluOrtho2D(), but glOrtho() defines how far along the z axis you can view objects as well, this is very important when drawing 3D objects.

Next we want to enable depth testing to let our program test which polygons are where and draw them in the correct order for us, to do this we use glEnable() with the argument GL_DEPTH_TEST.
void init(){	glClearColor(0,0,0,0);	glOrtho(-5,5,-5,5,-5,5);//(NEW) set up our viewing area	glEnable(GL_DEPTH_TEST);//(NEW) Enable depth testing}

In our display function we will create a 3D pyramid. To set the verteces we will use glVertex3f() instead of glVertex2f() so we can define where the point is on the z axis to add depth.

in our glClear() function we add a new argument that we seperate with a "|". The new argument is GL_DEPTH_BUFFER_BIT to enable the depth buffer so our program can draw 3D objects correctly.
void display(){	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//(NEW) setup our buffers	glPushMatrix();	glRotatef(time,0.8,5,0);//rotate our objects	glBegin(GL_TRIANGLES);	//This is going to be the face facing the camera	//We want this face to be red	glColor3f(1,0,0);	glVertex3f(-1,-1,1);//(NEW) We are now defining the vertex position on all three axis	glVertex3f(0,1,0);//the top vertex	glVertex3f(1,-1,1);//last vertex of this face	//We do not need to enter glEnd() here because we are still drawing triangles	//The back left face	//This face will be green	glColor3f(0,1,0);	glVertex3f(-1,-1,1);//Vertex closest to us	glVertex3f(0,1,0);//The top vertex	glVertex3f(0,-1,-1);//The vertex farthest away	//The back right face	//This face will be blue	glColor3f(0,0,1);	glVertex3f(0,-1,-1);//The farthest vertex	glVertex3f(0,1,0);//The heighest vertex	glVertex3f(1,-1,1);//The closest vertex	//The bottom face	//This is face will be white	glColor3f(1,1,1);	glVertex3f(-1,-1,1);//Each corner of the base of the pyramid	glVertex3f(0,-1,-1);	glVertex3f(1,-1,1);	glEnd();//done drawing our triangle	glPopMatrix();	glFlush();	glutPostRedisplay();//This function is crucial in animation, it refreshes the screen}

next we create our idle function which will update our time variable
void idle(){	time += 0.1;// increase our time variable	if(time > 360)		time = 0;// reset time variable}

next is our main function. The only difference is in the gluInitDisplayMode() function, we added GLUT_DEPTH as an argument to tell the program to use the depth buffer.
void main(int argc, char ** argv){	glutInit(&argc, argv);	glutInitWindowSize(800,600);	glutInitWindowPosition(10,50);	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);//tell the program we are running the depth buffer	glutCreateWindow("Lesson 4");	init();	glutDisplayFunc(display);	glutIdleFunc(idle);// This function calls our idle function to update our variables	glutMainLoop();}

depth testing is very important when creating 3D objects, you must enable, tell the program to clear it, and you must tell it that you are using the depth buffer. If you don't do all of these than your program will draw everything in the wrong order and it will look very confusing.

I hope you enjoyed yet another GLUT tutorial. The next tutorial will include texturing and keyboard interaction.

Edited on December 28 2006 to make more readable.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.