Question: / * Filename: SimplePaintProgram.cpp Author: TMT 2 0 3 3 Introduction to Computer Graphics Date: 6 January 2 0 1 0 Description: This is a

/* Filename: SimplePaintProgram.cpp
Author: TMT2033 Introduction to Computer Graphics
Date: 6 January 2010
Description: This is a simple paint program that can draw lines and points. Upon
pressing the 'c' or 'r' key on the keyboard, the drawing area will clear. The program
will close when the 'e' key is pressed.
Modification Log: -
*/
#include
#include
#include
#define LINE 1
#define POINT 2
#define CLEAR 3
//initialize global variables
int point_size=1;
int line_size=1;
int X1=-1, X2=-1, Y1=-1, Y2=-1;
bool P1= false, P2=false, P3=false, Cleared=false, Cleared2=false, Line=false, Point=false;
char *shape="No Shape Selected";
//this function draws the program's panels
void drawPanel()
{
//panel backgound
glBegin(GL_QUADS);
glColor3f(1,0,1);
glVertex2i(0,0);
glVertex2i(0,60);
glVertex2i(600,60);
glVertex2i(600,0);
glEnd();
//panel lines
glBegin(GL_LINES);
glColor3f(0,0,0);
glVertex2i(0,60);
glVertex2i(600,60);
glVertex2i(0,30);
glVertex2i(600,30);
glVertex2i(120,60);
glVertex2i(120,0);
glEnd();
//this draws image of a point on its button
glBegin(GL_POINTS);
glColor3f(0,0,0);
glVertex2i(61,45);
glVertex2i(61,46);
glVertex2i(60,45);
glVertex2i(60,46);
glEnd();
//this draws image of a line on its button
glBegin(GL_LINES);
glColor3f(0,0,0);
glVertex2i(30,15);
glVertex2i(90,15);
glEnd();
//this displays the shape status on the panel
glColor3f(0,0,0);
int tlength = strlen(shape);
glRasterPos3f(178,37,0);
for (int counter=0; counter =tlength; counter++){
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, shape[counter]);
}
}
//this function draws points on the drawing area when the point button is selected
void drawPoint()
{
if(P1==true)
{
glPointSize(point_size); //set point size
glBegin(GL_POINTS);
glColor3f(0,0,0);
glVertex2i(X1,Y1);
glEnd();
P1=false;
}
}
//this draws lines on the drawing area when the line button is selected
//a line is drawn by clicking twice on the drawing area
void drawLine()
{
if(P1==true && P2==true)
{
glLineWidth(line_size);
glBegin(GL_LINES);
glColor3f(0,0,0);
glVertex2i(X1,Y1);
glVertex2i(X2,Y2);
glEnd();
P1=false;
P2=false;
}
}
//this is where all the calls to the functions are made
void draw()
{
//the GL_COLOR_BUFFER_BIT is cleared only once to keep the content of the drawing page
if(Cleared == false)
{
glClear(GL_COLOR_BUFFER_BIT);
Cleared = true;
}
//the drawPanel() function is called everytime the draw function is called
drawPanel();
//checks if the object is selected using the boolean, and calls the appropriate function
if (Line==true)
drawLine();
if (Point==true)
drawPoint();
glFlush();
}
//this function is responsible for the action that happens when we select from the menu panel
void processMenuEvents(int option)
{
if(Cleared2== false)
{
glClear(GL_COLOR_BUFFER_BIT);
Cleared2= true;
}
switch (option)
{
case LINE :
Line= true;
Point= false;
P1=false;P2=false;P3=false;
shape="Drawing a Line";
break;
case POINT :
Line= false;
Point= true;
P1=false;P2=false;P3=false;
shape="Drawing a Point";
break;
case CLEAR :
glClear(GL_COLOR_BUFFER_BIT);
P1= false,P2=false, P3= false;
break;
}
glutPostRedisplay();
}
//this function is responsible for mouse interaction
void mouse (int btn, int state, int x, int y)
{
//these are listeners for left mouse click events in the drawing area
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN && 600-y >60)
{
if (Line==true)
{
if(P1==true && P2==false)
{
X2=x;Y2=600-y;
P2=true;
}
if(P1==false)
{
X1=x;Y1=600-y;
P1=true;
}
}
if (Point==true)
{
if(P1==false)
{
X1=x;Y1=600-y;
P1=true;
}
}
}
//these are listeners for buttons in the panel
if( x >0 && x 120 && 600-y >0 && 600-y 30)
processMenuEvents(LINE); // line button
if( x >0 && x 120 && 600-y >30 && 600-y 60)
processMenuEvents(POINT); // point button
glutPostRedisplay();
}
//this function is responsible for creating a pop-up menu when the right mouse button is clicked
void createGLUTMenus()
{
int menu;
menu = glutCreateMenu(processMenuEvents);
glutAddMenuEntry("Clear",CLEAR);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
//this function is responsible for keyboard interaction
void keyboard (unsigned char key, int x, int y)
{
switch(key)
{
case 99:
glClear(GL_COLOR_BUFFER_BIT);
break;
case 114:
glClear(GL_COLOR_BUFFER_BIT);
break;
case 101:
exit(0);
break;
}
}
/* this is the main function where the toolkit is initialized, the display mode is set,
the window is created and the callback functions are registered. */
void main()
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(20,20);
glutCreateWindow("Simple Paint Program for TMT2033");
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glClearColor(1,1,1,1);
glutDisplayFunc(draw);
glutIdleFunc(draw);
glOrtho(0,600,0,600,0,1);
createGLUTMenus();
glutMainLoop();
}
/ * Filename: SimplePaintProgram.cpp Author: TMT

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!