## Saturday, December 17, 2011

### 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 pressedvoid handleKeypress(unsigned char key, int x, int y) {	switch (key) {		case 27: //Escape key			exit(0);	}}//Initializes 3D renderingvoid initRendering() {	//Makes 3D drawing work when something is in front of something else	glEnable(GL_DEPTH_TEST);}//Called when the window is resizedvoid 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	glLoadIdentity(); //Reset the camera	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 scenevoid drawScene() {	//Clear information from last draw	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective	glLoadIdentity(); //Reset the drawing perspective	glBegin(GL_QUADS); //Begin quadrilateral coordinates	//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);	glEnd(); //End quadrilateral coordinates	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 for the above program is