Jump to content
xisto Community
Sign in to follow this  
t3jem1405241533

Programming In Glut (lesson 4) Making 3D objects

Recommended Posts

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.
Edited by t3jem (see edit history)

Share this post


Link to post
Share on other sites

Really nice set of tutorials so far t3jem,

 

 

 

This is what Asthost is all about, helping others to help yourself.

 

I am too busy with learning php and mysql atm to learn another language but I am sure others on the board will learn a lot from these tutorials.

 

 

 

Keep up the good work :P

Share this post


Link to post
Share on other sites

Really nice set of tutorials so far t3jem,

 

 

 

This is what Asthost is all about, helping others to help yourself.

 

I am too busy with learning php and mysql atm to learn another language but I am sure others on the board will learn a lot from these tutorials.

 

 

 

Keep up the good work :P

 


Thank you, I hope to get another one up tomarrow, it will be texturing and basic keyboard interaction. I plan to take these tutorials quite a ways, not sure how far yet, but as far as I can.

Share this post


Link to post
Share on other sites

a question about the pyramid

Programming In Glut (lesson 4)

 

Hello :)

 

If you still see this message, can you please tell me how to transform this code, the vertex coordinates in order to draw a pyramid given a certain size of it.

For example if we want to draw a pyramid with its side of 2, or 3 how can we modify these coordinates?

I need to draw a pyramid wih a dimension given by the user.

Thank you in advance.

 

-reply by Nora

Share this post


Link to post
Share on other sites

a question about the opengl pyramid

Programming In Glut (lesson 4)

 

Replying to t3jem

 

Hello :)

 

If you still see this message, can you please tell me how to transform this code, the vertex coordinates in order to draw a pyramid given a certain size of it.

For example if we want to draw a pyramid with its side of 2, or 3 how can we modify these coordinates?

I need to draw a pyramid wih a dimension given by the user.

Thank you in advance.

 

-reply by Nora

Share this post


Link to post
Share on other sites

Hi,I appriciate what you have done, thank you for all but...I don't want to animate the object, I just want to see a cube(in 3d). When I wrote the program I only saw one face of the cube, no depth, no other faces!I need this because I will render the cube, which I will define, with gouraud shading algorithm. So I need to see a 3d representation.What should I do!?Please help me!Regards,Replying to t3jemReplying to t3jem-reply by baris

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.