GLUT Tutorial: Drawing Basic Shapes (Triangle and Rectangle)

This is the first tutorial on GLUT. In this tutorial I am going to show you how to draw basic 2D shapes like triangle and rectangles using OpenGL. Although OpenGL is basically made for 3D programming, drawing 2D shapes gives the basic outline and introduction to OpenGL and gives the idea about how to start drawing objects in OpenGL. The following example draws a triangle and a rectangle to a GLUT window. Drawing rectangle and triangle is very easy on OpenGL because it provides a function for it. glBegin(GL_TRIANGLES) and glBegins(GL_QUADS) are functions for drawing triangle and rectangle respectively. We have to give a 3D coordinates of each vertex and OpenGL automatically draws the object as specified by the attribute passed to function glBegin. For example if we call glBegin(GL_QUADS) and provide coordinates of four vertices then OpenGL will draw a rectangle for us.


The output of the above program is :


SHARE GLUT Tutorial: Drawing Basic Shapes (Triangle and Rectangle)

You may also like...

9 Responses

  1. Beulah says:

    Are you looking for CAD conversion and other CAD Services? There are many Professional organizations that offer Cad Conversion Services, outsource cad drawings and cad conversion services at best prices that includes computer aided design 3d drafting services.

  2. psm says:

    i tried to compile this program in Ubuntu 14. Compiler gave error: fatal error: windows.h: No such file or directory
    which header file do i use as substitute for windows.h

  3. Anonymous says:

    in linux this works if you just exclude that include, meaning: just delete the line. also, know that this is c++. to make it work with c: just change iostream to stdio.h, delete the namespace thing and add a semicolon after each function declaration. (e.g. func(..){..}; )

  4. Anonymous says:

    Windows.h Does nothing in this code. You can Exclude it in windows also

  5. Yes, I am from Nepal 🙂

  6. Anup Roy says:

    Would you provide How to draw a bulb in open_GL Please,,,,

  7. Thanks for helpful article!

  8. ufo 3d says:

    Thanks for helpful article!

Leave a Reply

Your email address will not be published. Required fields are marked *