Astahost.com   Mar 21, 2010
Open Discussion & Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > MISC (not applicable to above Programming Categories)
Pages: 1, 2

Programming In Glut (lesson 2) - Learn how to draw 2D polygons in this tutorial

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 2) - Learn how to draw 2D polygons in this tutorial

t3jem
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<glut.h>//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 left, right, bottom, and top borders of the viewing area, in this case we are viewing 5 units to the left, right, down, and up.

CODE

void init()
{
    glClearColor(0,0,0,0);//Define our background color
    gluOrtho2D(-5,5,-5,5);//(NEW)  Define our viewing area
}


We are now going to draw our polygons using the function glBegin and glEnd. All the verteces defined between these two functions will be drawn using the rules given in the glBegin function. The rules that can be used can be found at the end of this tutorial.
glColor3f(); changes the colors of objects, any verteces following this function will be changed to the color defined. The colors are defined just as they are in glClearColor();
glVertex2f(); defines a 2D vertex as an (x, y) vertex. We will use this function to define our polygons.

In the next function we will create three polygons, a triangle, square, and pentagon. Each polygon will have it's own color.

CODE

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);//Clear the screen

    glColor3f(1,0,0);//Change the object color to red

    glBegin(GL_TRIANGLES);//Start drawing a triangle
    glVertex2f(3,-4);//draw our first coordinate
    glVertex2f(3.5,-3);//Our second coordinate
    glVertex2f(4,-4);//Our last coordinate
    glEnd();//Stop drawing triangles


    glColor3f(0,1,0);//Change the object colors to green

    glBegin(GL_QUADS);//Start drawing quads
    glVertex2f(-4,-4);//first coordinate
    glVertex2f(-4,-2);//second coordinate

    glColor3f(0,0,1);//Change the color to blue halfway through to create a neat color effect

    glVertex2f(-2,-2);//third coordinate (now blue)
    glVertex2f(-2,-4);//last coordinate
    glEnd();//Stop drawing quads

    glColor3f(1,0,0);//Change color to red
    glBegin(GL_POLYGON);//Start drawing a polygon
    glVertex2f(-2,2);//first vertex
    glColor3f(0,1,0);//Change color to green
    glVertex2f(-1,3);//second vertex
    glColor3f(0,0,1);//Change color to blue
    glVertex2f(0,2);//third vertex
    glColor3f(1,0,1);//Change color to purple
    glVertex2f(-0.5,0);//fourth vertex
    glColor3f(1,1,0);//Change color to yellow
    glVertex2f(-1.5,0);//last vertex
    glEnd();//Stop drawing our polygon


    glFlush();//Draw everything to the screen
    glutPostRedisplay();//Start drawing again
}



Now we finish the code off with our main function again.
CODE

void main(int argc, char ** argv)
{
    glutInit(&argc, argv);//Initialize GLUT
    glutInitWindowSize(800,600);//define the window size
    glutInitWindowPosition(10,50);//Position the window
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);//Define the drawing mode
    glutCreateWindow("Lesson 2");//Create our window
    init();//initialize our variables
    glutDisplayFunc(display);//tell Glut what our display function is
    glutMainLoop();//Keep the program running
}



As you can see there are many different modes for the function "glBegin()". The modes that can be used and what they are used for are listed below.

GL_POINTS This is used to just draw points, instead of polygons or lines. You only need to define one vertex to see a point
GL_LINES -- This mode lets you draw lines. You need to define 2 verteces at a time.
GL_LINE_LOOP -- This mode creates a loop of lines. You define the verteces much like GL_POLYGON and when you finish it will link the last and first two verteces together with a line.
GL_LINE_STRIP -- This mode does the same as GL_LINE_LOOP except it does not connect the first and second lines
GL_TRIANGLES -- This mode creates triangles and takes 3 verteces at a time
GL_TRIANGLE_STRIP -- This mode creates a strip of triangles. The second triangle uses 2 verteces of the first, and you just define the last vertex of the next triangle
GL_TRIANGLE_FAN -- This function creates a fan of triangles. All the triangles use one same point and 1 point from the triangle before them, you define the last vertex
GL_QUADS -- This creates quadrilaterals and take 4 verteces at a time
GL_QUAD_STRIP -- This mode creates a strip of quadrilaterals. After the first quad, it uses 2 verteces from the preveious quad then you define the other 2
GL_POLYGON -- This mode lets you create a polygon will n verteces


I hope you enjoyed the tutorial and found it helpful. Many more are on their way. Again go ahead and fiddle with the settings and modes, you can learn alot more from playing with the code then just reading it.


Editted on December 18, 2006 to make it more readable

 

 

 


Comment/Reply (w/o sign-up)

kgd2006
Interesting topic, I remember pieces of these codes from a class that I have taken in college. It certainly bring back memories. But those that are going to try to program this must need to setup they compiler to have certain key libraries to have it actually run and execute. Poping in the code will not give you the results you want automatically I believe. You will need to make certain modifications and addtions to your compiler before you can see your program run.

Comment/Reply (w/o sign-up)

t3jem
QUOTE(kgd2006 @ Nov 26 2006, 03:29 AM) *

Interesting topic, I remember pieces of these codes from a class that I have taken in college. It certainly bring back memories. But those that are going to try to program this must need to setup they compiler to have certain key libraries to have it actually run and execute. Poping in the code will not give you the results you want automatically I believe. You will need to make certain modifications and addtions to your compiler before you can see your program run.


Well I have a tutorial someone sent me a while back on how to get a free compiler and set up GLUT on it, but I don't think I would be allowed to post it since it isn't mine.

Comment/Reply (w/o sign-up)

kgd2006
Ohhh, so you are using a free compiler? When I learned GLUT my college professor had us download and copy certain file libraries into VISUAL STUDIO C++ .NET compiler. So those that already have .net should look into trying to find those files that are needed so they can run the experience of GLUT programming.

And if it helps your tutorial I think I remember the pieces that the professor made me download was from "microsofts" website itself, not sure what the libiary files are though, its been quite a while since I last programmed in GLUT.

Comment/Reply (w/o sign-up)

t3jem
QUOTE(kgd2006 @ Nov 27 2006, 05:48 AM) *

Ohhh, so you are using a free compiler?


No, im actually using Microsoft Visual C++ 6.0, that's why I can't make a tutorial telling people how to get a free compiler and install GLUT on it, i'v never done it. I tried to post a tutorial I recieved from somebody when I had a website with these tutorials, I quoted the whole thing, but I don't know if they will let me post it.

Comment/Reply (w/o sign-up)

qwijibow
Great..

I've been looking for some documentation on Rendering Diretly to the Frame Buffer( Bypasing the overhead of glx ) using glFBDev().

But found almost nothing...

Does anyone have any experiance / example code utilisng glFBDev ???


Comment/Reply (w/o sign-up)

t3jem
QUOTE(qwijibow @ Nov 28 2006, 06:12 AM) *

Great..

I've been looking for some documentation on Rendering Diretly to the Frame Buffer( Bypasing the overhead of glx ) using glFBDev().

But found almost nothing...

Does anyone have any experiance / example code utilisng glFBDev ???



I'm afraid that I have neither learned how to use that function, nor even heard of it. I am still more of an amateur programmer and am still learning how to use OpenGL. Maybe one day I will learn how to use that, but for now, im just teaching the stuff I know.

Comment/Reply (w/o sign-up)

masterio
That tutorial will be very great to me if I have some skill in C/C++. But for now I haven't skill for those language. Do you know some good books to start learning C/C++ language?. What step should i pick up first? start learning C++ without C?.

Thanks biggrin.gif

:: sorry for my english smile.gif

Comment/Reply (w/o sign-up)

t3jem
QUOTE(masterio @ Nov 29 2006, 12:22 PM) *

That tutorial will be very great to me if I have some skill in C/C++. But for now I haven't skill for those language. Do you know some good books to start learning C/C++ language?. What step should i pick up first? start learning C++ without C?.

Thanks biggrin.gif

:: sorry for my english smile.gif



I suggest learning C++ without C because C++ is pretty much a different version of C. I learned C++ then went on to OpenGL, the book I used though was about 10 years old so I don't know of any recent books to use, but I use very little C++, just some basic C++ knowledge should suffice to be able to understand how my OpenGL tutorials work. You could probably find a few short tutorials on the internet that would help.

 

 

 


Comment/Reply (w/o sign-up)

(G)Abdul Hameed
Animation of triangle
Programming In Glut (lesson 2)

Develop a program in C language and code for making window of size 600,600 and

having window name “Animation of triangle” (develop this window using OpenGL

and glut library in C) and perform following:

• A triangle of red color (In the way shown in figure)

• Animation of the triangle starts rotation on left button click of mouse and on right

mouse button click stops its rotation.

•Window should terminate on Escape key or Ctrl+T key

-question by Abdul Hameed

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)

Pages: 1, 2
Similar Topics

Keywords : programming, glut, lesson, 2, learn, draw, 2d, polygons, tutorial

  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++ (10)
    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 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, 2, learn, draw, 2d, polygons, tutorial



See Also,

*SIMILAR VIDEOS*
Searching Video's for programming, glut, lesson, 2, learn, draw, 2d, polygons, tutorial
advertisement




Programming In Glut (lesson 2) - Learn how to draw 2D polygons in this tutorial

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



Creative Commons License