Nov 25, 2009

Programming In Glut (lesson 3) - How to animate objects in GLUT

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > MISC (not applicable to above Programming Categories)

Programming In Glut (lesson 3) - How to animate objects in GLUT

t3jem
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.

CODE

#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
CODE

float time = 0;//(NEW) variable to record the how much time has passed

void 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.
CODE

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.
CODE


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.
CODE

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

 

 

 


Comment/Reply (w/o sign-up)

FeedBacker
how do I make objects move by mouse click
Programming In Glut (lesson 3)

Hi I was able to make objects in glut (thanks to you. ^^ *thanks you*)

But I'm trying to move objects to a position by mouse click, I learned the mouse function for glut, but don't know the formulas (how) to make it move to a position. I'm not sure how to convert the mouse clicks coordinates to regular numbers / smaller numbers.

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : programming, glut, lesson, 3, animate, objects, glut

  1. Programming In Glut (lesson 6)
    Texture filters and lighting (1)
  2. Programming In Glut (lesson 5)
    How to texture map and use keyboard controls. (1)
    In this tutorial I am going to teach you how to texture map your polygons and implement basic
    keyboard control. There is alot of new material in this tutorial and I hope I have explained it
    enough. Before you get started you will want to name a bmp file "image.bmp" or "image" depending on
    your computer settings (if one doesn't work then use the other) and place it in the same folder
    as your project. Now to start we will be making a seperate header file where we will write our
    function to load a bitmap file. You should name it "bmpload.h". The code to write in it ....
  3. Installing Glut To Dev C++
    A tutorial to install GLUT on Dev C++ (9)
    This is a tutorial that someone submitted to my programming site when it was up because I didn't
    know how to install glut on any other compiler than my own. I though it would be helpful to post
    this up for everyone here so they can use GLUT as well without having to google for hours. I'm
    not sure if I'm allowed, but I have quoted it and have given credit where credit is due, so if
    this post is allowed I hope this helps those people who couldn't program in GLUT yet. QUOTE
    Installing G.L.U.T. to Dev C++ By James Duran (email: vrok137@yahoo.com) ....
  4. Programming In Glut (lesson 4)
    Making 3D objects (7)
    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 CODE #i....
  5. Programming In Glut (lesson 2)
    Learn how to draw 2D polygons in this tutorial (10)
    This is the second lesson in my series of tutorials on how to use GLUT to create graphics. In this
    tutorial I am going to be teaching you how to create different types of polygons. I am going to be
    adding on to last tutorial's code and will leave the notes in to help you remember what all the
    function are. I will also be noting the new functions that we will be using and how to use them.
    CODE #include //Include the GLUT functions This time we are going to not only set the
    background color, but set the area that we are viewing as well. gluOrtho2D(); sets....
  6. Programming In Glut (lesson 1)
    The first of a series of tutorials on how to use the OpenGL Utility To (4)
    Hello, I'm starting a series on how to program in OpenGL using the OpenGL Utility Toolkit,
    a.k.a. GLUT. I chose GLUT because it is quick and easy to write, and very easy to learn. In this
    tutorial I am going to teach you how to create a basic window which we will build off of in later
    tutorials. Throughout the tutorial I will leave notes to let you know what each command does, and
    how you can modify it to fit your needs. Everything in the code section can be copied and pasted
    into your compiler and it should compile proporly, if it does not, please let me know, a....
  7. Programming With Coffee
    Begining Programming with Java (2)
    Since Java is a complete programming language, one tutorial is not going to cover everything.
    However, I will try to cover the basics and possibly extend on them later, and will assume you know
    very little about programming. If something seems unclear, it is because I am not very good at
    explaining. Read on and if you reach the end of the tutorial and still don’t get it, tell me. Tell
    me even if you figure it out, but something is unclear, I will try to fix it. Any style
    guides/conventions are not mandatory at all, it’s just how many programmers write their code and ma....

    1. Looking for programming, glut, lesson, 3, animate, objects, glut

See Also,

*SIMILAR VIDEOS*
Searching Video's for programming, glut, lesson, 3, animate, objects, glut
advertisement



Programming In Glut (lesson 3) - How to animate objects in GLUT

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com