Jump to content
xisto Community
Sign in to follow this  
t3jem1405241533

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

Recommended Posts

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.

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

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.

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.
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
Edited by t3jem (see edit history)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 :P :: sorry for my english :P

Share this post


Link to post
Share on other sites

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 :P

:: sorry for my english :P



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.

Share this post


Link to post
Share on other sites
Not well on linuxProgramming In Glut (lesson 2)

cpu runs like crazy when executing on linux (ubuntu 9.04 with freeglut3).I am trying to figure out what s going on T__T

-reply by bubuzzz

Share this post


Link to post
Share on other sites
Animation of triangleProgramming 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

Share this post


Link to post
Share on other sites
Glut PaintProgramming In Glut (lesson 2)

Hello,

I made some online program to help us on glut draw, you can see it at:

glut.Paint.At/  or

rodrigosoares.Awardspace.Com/glpaint

Thanks

-reply by Rodrigo

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.