Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Programming In Glut (lesson 1), The first of a series of tutorials on how to use the OpenGL Utility To
t3jem
post Nov 23 2006, 04:51 AM
Post #1


Member [ Level 1 ]
Group Icon

Group: Members
Posts: 48
Joined: 23-November 06
Member No.: 17,478



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)

This post has been edited by t3jem: Dec 11 2006, 10:19 PM
Go to the top of the page
 
+Quote Post
twitch
post Dec 10 2006, 10:39 AM
Post #2


Veteran Nut
Group Icon

Group: Members
Posts: 527
Joined: 4-October 05
From: UK
Member No.: 8,895



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.
Go to the top of the page
 
+Quote Post
t3jem
post Dec 10 2006, 04:36 PM
Post #3


Member [ Level 1 ]
Group Icon

Group: Members
Posts: 48
Joined: 23-November 06
Member No.: 17,478



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
Go to the top of the page
 
+Quote Post
iGuest
post Aug 12 2008, 01:53 AM
Post #4


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



thanks
Programming In Glut (lesson 1)

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

-reply by fairzy
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Programming In Glut (lesson 2)(8)
  2. Programming In Glut (lesson 3)(1)
  3. Programming In Glut (lesson 4)(7)
  4. Installing Glut To Dev C++(3)
  5. Programming In Glut (lesson 6)(1)


 



- Lo-Fi Version Time is now: 30th August 2008 - 09:42 AM