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.

#include<windows.h>
#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
}
}
//Initializes 3D rendering
void initRendering() {
//Makes 3D drawing work when something is in front of something else
glEnable(GL_DEPTH_TEST);
}
//Called when the window is resized
void handleResize(int w, int h) {
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
gluPerspective(45.0,                  //The camera angle
(double)w / (double)h, //The width-to-height ratio
1.0,                   //The near z clipping coordinate
200.0);                //The far z clipping coordinate
}
//Draws the 3D scene
void drawScene() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
//Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -0.5f, -5.0f);
glVertex3f(-0.7f, -0.5f, -5.0f);
glBegin(GL_TRIANGLES); //Begin triangle coordinates
//Triangle
glVertex3f(-0.5f, 0.5f, -5.0f);
glVertex3f(-1.0f, 1.5f, -5.0f);
glVertex3f(-1.5f, 0.5f, -5.0f);
glEnd(); //End triangle coordinates
glutSwapBuffers(); //Send the 3D scene to the screen
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400); //Set the window size
//Create the window
glutCreateWindow("Basic Shapes - programming-technique.blogspot.com");
initRendering(); //Initialize rendering
//Set handler functions for drawing, keypresses, and window resizes
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop(); //Start the main loop
return 0;
}

The output of the above program is :

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

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
#include
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,,,,