Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Programming In Glut (lesson 2), Learn how to draw 2D polygons in this tutorial
t3jem
post Nov 23 2006, 03:37 PM
Post #1


Member [ Level 1 ]
Group Icon

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



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

This post has been edited by t3jem: Dec 18 2006, 10:54 PM
Go to the top of the page
 
+Quote Post
kgd2006
post Nov 26 2006, 10:29 AM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 318
Joined: 1-March 06
Member No.: 11,638



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.
Go to the top of the page
 
+Quote Post
t3jem
post Nov 26 2006, 05:38 PM
Post #3


Member [ Level 1 ]
Group Icon

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



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.
Go to the top of the page
 
+Quote Post
kgd2006
post Nov 27 2006, 12:48 PM
Post #4


Premium Member
Group Icon

Group: [HOSTED]
Posts: 318
Joined: 1-March 06
Member No.: 11,638



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.
Go to the top of the page
 
+Quote Post
t3jem
post Nov 27 2006, 10:06 PM
Post #5


Member [ Level 1 ]
Group Icon

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



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.
Go to the top of the page
 
+Quote Post
qwijibow
post Nov 28 2006, 01:12 PM
Post #6


Way Out Of Control - You need a life :)
Group Icon

Group: Members
Posts: 1,366
Joined: 14-September 04
From: Nottingham England
Member No.: 570



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 ???

Go to the top of the page
 
+Quote Post
t3jem
post Nov 29 2006, 01:01 AM
Post #7


Member [ Level 1 ]
Group Icon

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



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.
Go to the top of the page
 
+Quote Post
masterio
post Nov 29 2006, 07:22 PM
Post #8


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 21
Joined: 24-September 06
Member No.: 16,119



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
Go to the top of the page
 
+Quote Post
t3jem
post Nov 29 2006, 10:26 PM
Post #9


Member [ Level 1 ]
Group Icon

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



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.
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 1)(3)
  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: 13th October 2008 - 04:01 PM