C++ tutorial - draw basic shapes


Draw basic shapes

In the previous page, you leant how to install freeglut library with Dev-Cpp and write a simple program to display a teapot, and understand commands frequently used in drawing objects, on the window. In this tutorial, you will learn how to draw basic shapes such as lines, and polygons.

Drawing a line

Line is referred to a line segment. To draw a line segment, you need to have two points or vertices. A point or vertex simply is a set of floating-pointer numbers. The user specifies two floating-point numbers: x and y for a point or vertex. A point can be made from three floating-point numbers: x, y, and z. OpenGL automatically  assignes z to zero if only x and y are specified. You will use glVertex2f or glVertex3f to specify a vertex. The glVertex2f function accepts two floating-point numbers while glVertex3f accepts three floating-point numbers. Typically, commands to draw a shape are placed between glBegin and glEnd commands. You need to use GL_LINES constant to draw a line from one vertex to another vertex. The displayLine below can be called to draw a red line. The glColor3f function specifies the color of drawing shape. It has three arguements that represent red, green, , and blue values respectively.

#include <cstdlib>
#include <iostream>
#include <gl/freeglut.h>
using namespace std;

void setup() {

//specifies color to clear the window

glClearColor(0.0f, 1.0f, 1.0f, 1.0f);

//specify coordinate system in drawing shapes
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);


}

void displayLine() {

//clear the window to the color specified in setup function

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//specify color in drawing line
glColor3f(1.0f, 0.0f, 0.0f);

//start drawing a line
glBegin(GL_LINES);
glVertex2f(-0.5f,0.0f);
glVertex2f(0.5f,0.0f);
glEnd();

//wait until buffers completely displayed
glutSwapBuffers();

}

int main(int argc, char *argv[])
{

glutInit(&argc, argv); //initialize window
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); //specify a color mode or buffer model to use
glutInitWindowSize(500,400); //specify window size
glutCreateWindow("Drawing a line"); //create a window
setup(); //call the setup function
glutDisplayFunc(displayLine); //draw a line
glutMainLoop(); //display the window

system("PAUSE");
return EXIT_SUCCESS;

}

line

Drawing a polygon

A polygon is a set of ordered vertices. Generally, it is an area enclosed by a single closed loops of line segments. You will use the GL_POLYGON constant in the glBegin command to specify that you want to draw a filled polygon. The displayTriangle below can be called from main function to display a filled triangle shape. If you don't want a filled polygon, you will use GL_LINE_LOOP instead of GL_POLYGON.

void displayTriangle(){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.25,0.25);
glColor3f(1.0,0.0, 0.0);
glVertex2f(0.75,0.25);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(0.50,0.75);
glEnd();
glutSwapBuffers();

}

triangle



Comments

sdlkskd comment

 sdlkskd

#include<iostream.h>
#include<conio.h>
#include<graphic.h>
#include<math.h>
void main()
{clrscr();
int g=0,d;
initgraph(&g,&d,"c:\turboc3\bgi");
setbkcolor(15);
setcolor(1);
settextstyle(3,0,5);
outtextxy(190,140,"G");
setcolor(4);
settextstyle(3,0,5);
outtextxy(220,140,"O");
setcolor(3);
settextstyle(3,0,5);
outtextxy(250,140,"O");
setcolor(1);
settextstyle(3,0,5);
outtextxy(280,140,"G");
setcolor(2);
settextstyle(3,0,5);
outtextxy(310,140,"L");
setcolor(4);
settextstyle(3,0,5);
outtextxy(340,140,"E");
setcolor(1);
settextstyle(3,0,5);
setcolor(4);
settextstyle(1,0,2);
outtextxy(100,350,"SEARCH");
settextstyle(1,0,2);
outtextxy(380,350,"I'M FEELING LUCKY");
setcolor(8);
rectangle(140,200,480,230);
rectangle(93,350,183,380);
rectangle(370,350,580,380);
getch();
}


2015-10-11



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.