Jump to content
xisto Community
Sign in to follow this  
t3jem

Programming In Glut (lesson 3) Animating your objects

Recommended Posts

Lesson 3 of 6.

In this tutorial I am going to talk about how to get animation in your program. I will be working directly off of Lesson 2 and will now take out the notes left behind from Lesson 1, but I will leave the notes from Lesson 2.

#include<glut.h>//include our glut library

First we are going to initialize a variable called "time" which will record how much time has passed so we can use it to determine how to animate the objects.

Then we have our init function that initializes some stuff in GLUT
float time = 0;//(NEW) variable to record the how much time has passedvoid init(){	glClearColor(0,0,0,0);	gluOrtho2D(-5,5,-5,5);//Setup our viewing area}

Next is our display function, this time we will have three objects with their colors, but there will be five new functions introduced, all having to do with transformations.

glPushMatrix(); creates a new matrix layer where you can do transformations, you use this function combined with glPopMatrix(); to keep from setting transformations that will effect your entire application.
glPopMatrix(); ends transformations that you have made to the matrix you were using.
glRotatef(); rotates objects around the origin. The first argument is the amount of degrees to rotate, the other three tell the program what axis to rotate it on. If you use (90,1,0,0); it will rotate 90 degrees on the x axis.
glScalef(); scales objects around the origin. The values you enter into the arguments multiply each vertex, so (2,2,2) will multiply each vertex argument by 2 so the vertex (3,2) will be moved to (6,4)
glTranslatef(); "slides" the verteces around on the axii. With the arguments (3,2,0); will move all verteces 3 units to the right, 2 up, and 0 back.

The rest of the code is the same.
void display(){	glClear(GL_COLOR_BUFFER_BIT);	glColor3f(1,0,0);//Set color to red	glPushMatrix();//(NEW) Create a new matrix	glRotatef(time,0,0,1);//(NEW) Rotate the triangle on z axis based on how much time has passed	glBegin(GL_TRIANGLES);//Start drawing triangles	glVertex2f(3,-4);//1st vertex	glVertex2f(3.5,-3);//2nd vertex	glVertex2f(4,-4);//last vertex	glEnd();//stop drawing	glPopMatrix();//(NEW) stops current transformations to the matrix	glColor3f(0,1,0);//set the color to green	glPushMatrix();//(NEW) create new matrix	glTranslatef(time/50,0,0);//(NEW) slide the object on the x axis based on time	glBegin(GL_QUADS);//Start drawing quadrilaterals	glVertex2f(-4,-4);//1st vertex	glVertex2f(-4,-2);//2nd vertex	glColor3f(0,0,1);//Change color to blue halfway through	glVertex2f(-2,-2);//3rd vertex	glVertex2f(-2,-4);//last vertex	glEnd();//stop drawing	glPopMatrix();//(NEW) stop all current transformations	glPushMatrix();//(NEW) create a new matrix	glScalef(time/200,time/200,time/200);//(NEW) scale the object on all axiis based on time	glColor3f(1,0,0);//First vertex is red	glBegin(GL_POLYGON);//start drawing a polygon	glVertex2f(-2,2);//first vertex	glColor3f(0,1,0);//Second vertex is green	glVertex2f(-1,3);//second vertex	glColor3f(0,0,1);//Third vertex is blue	glVertex2f(0,2);//third vertex	glColor3f(1,0,1);//Third vertex is purple	glVertex2f(-0.5,0);//fourth vertex	glColor3f(1,1,0);//last vertex is yello	glVertex2f(-1.5,0);//last vertex	glEnd();//tell the program we are done drawing our polygon	glPopMatrix();//(NEW) End transformations currently used	glFlush();	glutPostRedisplay();//refresh the screen}

Next we have a new function called idle. This function will be called to update our time variable. With the idle function set in GLUT you can keep your display code clean and have all the background work in an easy to find function. We will reset the time variable to 0 every now and then so the objects dont get too big and they come back after they slide away.
void idle(){	time += 0.1;//(NEW) increase the time variable	if(time > 360)		time = 0;//(NEW) reset the time variable}

there is only one new call in the main function and that is glutIdleFunc(); this is used to call our idle function that we created just above.
void main(int argc, char ** argv){	glutInit(&argc, argv);	glutInitWindowSize(800,600);	glutInitWindowPosition(10,50);	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);	glutCreateWindow("Lesson 3");	init();	glutDisplayFunc(display);	glutIdleFunc(idle);//(NEW) calls our idle function	glutMainLoop();}


The function "glRotatef()" is used to rotate objects around the origin. The reason the triangle spins around the screen is because it was not placed at the origin of the screen, but at the edge; therefore, making the triangle run around the screen.

The function "glTranslatef()" moves objects to a certain spot. It just adds the translation arguments to all the verteces that apply. It does not matter where the object was originally placed

The function "glScalef()" scales objects according to the arguments provided. all verteces are multiplied by the arguments. If you put 2 in the x argument of glScalef() then all applicable vertices will be multiplied by 2 on the x axis. This will have an odd effect on objects not placed at the origin as seen in this tutorial.

I hope you now know how to run animations, The next tutorial is on it's way



Editted on December 20th 2006 to make it easier to read

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.