Nov 20, 2009

Programming In Glut (lesson 1) - The first of a series of tutorials on how to use the OpenGL Utility To

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 1) - The first of a series of tutorials on how to use the OpenGL Utility To

t3jem
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, and I may be able to help you.

Note: It is recommended that you have at least some basic knowledge of C++ before learning OpenGL; though, it is not required, it will help with understanding the tutorials.

Note: I have tested this code myself in Microsoft Visual C++ 6.0; however, it is possible that it may not work on other compilers. If you do find a mistake or you have questions I would be happy to try and answer them.



First we will have to include our header file that contains all of our OpenGL commands
CODE

#include<glut.h>//This header file contains all the commands for the OpenGL Utility Toolkit



The init() function will be called after the window is created to initialize the settings for the OpenGL window.
Right now all we will be doing is set the background color to black.
The glClearColor() function defines what the background color will be, the values for each one range from 0 to 1 in Red, Green, Blue order. If you put (1,0,0,0) in it will be solid red, and (1,1,1,0) will be white.
CODE

//function to initialize GLUT settings
void init()
{
    glClearColor(0,0,0,0);//(NEW) we define the background color here
}


The function is the main function in GLUT. We will use this function do draw all of our object; however, in this tutorial there are no objects to draw, but you still need to have some functions in there for the window to refresh.
The glClear() function clears the screen to the default background color (in this case black). Without it your window will be clear and it will not refresh.
The glFlush() function draws all of the objects we defined on to the screen. In this tutorial we have no objects so it doesn't do anything.
glutPostRedisplay() tells the program to start drawing the screen again. This is a very important command for animation, without it your objects will not move.
CODE

//The function where all the drawing occurs
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);//Clear the screen


    glFlush();//Draw everything to the screen
    glutPostRedisplay();//Tell the program to refresh
}


The main function is the backbone of your program. This is where it begins and where you define your window, initialize GLUT, and define what your vital function are.

glutInit() initializes glut and tells the program that we are using it
glutInitWindowSize() defines the width and height of the window in pixels
glutInitWindowPosition() defines where the window will be placed, in pixels, from the upper right corner of the screen
glutInitDisplayMode() initializes what modes we are using, in this program we are only using a single buffer and red,green, blue coloring.
glutCreateWindow() obviously creates a window. The one parameter for it is the name that will appear in the title bar.

After we create our window we call our init function to set some GLUT variables (in this case the background color), then we tell GLUT where to find our display function
glutDisplayFunc() tells glut which function does all the drawing
glutMainLoop() tells GLUT that we are not done with our program and to keep going through the program
CODE

void main(int argc, char ** argv)
{
    glutInit(&argc, argv);//initialize GLUT.
    glutInitWindowSize(800,600);//define the window size as 800 pixels wide and 600 pixels high    glutInitWindowPosition(10,50);//Set the window position at (10,50)
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);//Set the initial display mode
    glutCreateWindow("Lesson 1");//Create our window
    init();//call init()
    glutDisplayFunc(display);//tell GLUT what our display function is
    glutMainLoop();//Tell the program we are not done yet
}




I hope you learned something from the tutorial. Go ahead and mess around with settings and figure out how to work them. I will be adding new tutorials soon, but right now im just too tired, possibly tomarrow though.


Edited Dec 11 2006 by T3jem to make it more readable (thanks twitch)

 

 

 


Comment/Reply (w/o sign-up)

twitch
Interesting tutorial, however I would remove the comments from the actual code and use them as part of your post. You'll get more credits wink.gif and you'll declutter your actual code. It will also be more readable.

Comments should only be brief in your code scripts. Make them too long and they'll clutter your file and confuse the developer.

Comment/Reply (w/o sign-up)

t3jem
QUOTE(twitch @ Dec 10 2006, 03:39 AM) *

Interesting tutorial, however I would remove the comments from the actual code and use them as part of your post. You'll get more credits wink.gif and you'll declutter your actual code. It will also be more readable.

Comments should only be brief in your code scripts. Make them too long and they'll clutter your file and confuse the developer.


ok, I wanted to put it all in one so if people didn't want to read they could just copy and paste into their compiler and learn later, but yea thats a good idea, I can get that done, I'll probably do that some time this week though its pretty busy here, thanks for the tip though

Comment/Reply (w/o sign-up)

iGuest-fairzy
thanks
Programming In Glut (lesson 1)

I am a leaner and I am from china, thanks for your lesson!

-reply by fairzy

Comment/Reply (w/o sign-up)

(G)Monty

Nice tutorial...It helped me a lot in understanding glut programming...Thank you!

-Monty


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, 1

  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 3)
    How to animate objects in GLUT (1)
    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 //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....
  6. 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....
  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, 1

See Also,

*SIMILAR VIDEOS*
Searching Video's for programming, glut, lesson, 1
advertisement



Programming In Glut (lesson 1) - The first of a series of tutorials on how to use the OpenGL Utility To

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