Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question
100%

The other half is in the photos 

 

Read the paint program below carefully and complete the source code paint.cpp by:

1. write function “myInit” of your own where you must a) setup viewport, b)set clear color to black and clear window, and c) setup matrix mode and viewing
2. Create your right menu containing the following submenus and main entries:
a) color menu with eight entries “Red”, “Green”, “Blue”, “Cyan”, “Magenta”, “Yellow”, “White”, and “Black”
b) pixel menu with two entries “Increase pixel size” and “Decrease pixel size”
c) fill menu with two entries “Fill on” and “Fill off”
d) two main entries “clear” and “quit”
3. Compile and run the program. Your complete program must be able to draw line segments, rectangles, triangles, pixels with specified color. It must also be able to accept keyboard input and display the text on the screen when work in text mode.

 

/* paint.cpp   */

 

#define LINE 1

#define RECTANGLE 2

#define TRIANGLE  3

#define POINTS 4

#define TEXT 5

 

#include <stdlib.h>

#include <GL/glut.h>

 

// global variables

GLsizei wh = 500, ww = 500;        ​ // initial window size

GLfloat size = 3.0;                 ​// half side length of square

int draw_mode = 0;                  ​// drawing mode

int rx, ry;                         ​// raster position

 

GLfloat r = 1.0, g = 1.0, b = 1.0;  ​// drawing color

int fill = 0;                       ​// fill flag

 

// pick function

int pick(int x, int y)

{

   y = wh - y;

   if(y < wh-ww/10) return 0;

   else if(x < ww/10) return LINE;

   else if(x < ww/5) return RECTANGLE;

   else if(x < 3*ww/10) return TRIANGLE;

   else if(x < 2*ww/5) return POINTS;

   else if(x < ww/2) return TEXT;

   else return 0;

}

 

// draw square function

void drawSquare(int x, int y)

{

       y=wh-y;

       glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256);

       glBegin(GL_POLYGON);

           glVertex2f(x+size, y+size);

           glVertex2f(x-size, y+size);

           glVertex2f(x-size, y-size);

           glVertex2f(x+size, y-size);

       glEnd();

}

 

// mouse event handlers

void mouse(int btn, int state, int x, int y)

{

   static int count;

   int where;

   static int xp[2],yp[2];

   if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)

   {

      glPushAttrib(GL_ALL_ATTRIB_BITS);

     

      where = pick(x,y);

      glColor3f(r, g, b);

      if(where != 0)

      {

         count = 0;

         draw_mode = where;

      }

      else

          switch(draw_mode)

          {

            case(LINE):

             if(count==0)

             {

                 count++;

                 xp[0] = x;

                 yp[0] = y;

             }

             else

             {

                 glBegin(GL_LINES);

                    glVertex2i(x,wh-y);

                    glVertex2i(xp[0],wh-yp[0]);

                 glEnd();

                 count=0;

             }

             break;

 

           case(RECTANGLE):

             if(count == 0)

             {

                 count++;

                 xp[0] = x;

                 yp[0] = y;

             }

             else

             {

                 if(fill) glBegin(GL_POLYGON);

                 else glBegin(GL_LINE_LOOP);

                    glVertex2i(x,wh-y);

                    glVertex2i(x,wh-yp[0]);

                    glVertex2i(xp[0],wh-yp[0]);

                    glVertex2i(xp[0],wh-y);

                 glEnd();

                 count=0;

             }

             break;

 

           case (TRIANGLE):

             switch(count)

             {

               case(0):

                 count++;

                 xp[0] = x;

                 yp[0] = y;

                 break;

               case(1):

                 count++;

                 xp[1] = x;

                 yp[1] = y;

                 break;

               case(2):

                 if(fill) glBegin(GL_POLYGON);

                 else glBegin(GL_LINE_LOOP);

                    glVertex2i(xp[0],wh-yp[0]);

                    glVertex2i(xp[1],wh-yp[1]);

                    glVertex2i(x,wh-y);

                 glEnd();

                 count=0;

             }

             break;

 

             case(POINTS):

             {

                drawSquare(x,y);

                count++;

             }

             break;

 

            case(TEXT):

            {

​rx=x;

​ry=wh-y;

​glRasterPos2i(rx,ry);

​count=0;

            }

        }

 

      glPopAttrib();

      glFlush();

   }

}

 

// keyboard input function

void key(unsigned char k, int xx, int yy)

{

   if(draw_mode!=TEXT) return;

   glColor3f(0.0,0.0,0.0);

   glRasterPos2i(rx,ry);

   glutBitmapCharacter(GLUT_BITMAP_9_BY_15, k);

   rx+=glutBitmapWidth(GLUT_BITMAP_9_BY_15,k);

 

}

 

// draw screen box

void screen_box(int x, int y, int s )

{

   glBegin(GL_QUADS);

         glVertex2i(x, y);

         glVertex2i(x+s, y);

         glVertex2i(x+s, y+s);

         glVertex2i(x, y+s);

   glEnd();

}

 

}
// display callback function
void display(void)
{
int shift=0;
glPushAttrib(GL_ALL_ATTRIB_BITS);
glClearColor (0.8, 0.8, 0.8, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
screen_box(0,wh-ww/10,ww/10);
glColor3f(1.0, 0.0, 0.0);
screen_box(ww/10,wh-ww/10,ww/10);
glColor3f(0.0, 1.0, 0.0);
screen_box(ww/5,wh-ww/10,ww/10);
glColor3f(0.0, 0.0, 1.0);
screen_box(3*ww/10,wh-ww/10,ww/10);
glColor3f(1.0, 1.0, 0.0);
screen_box(2*ww/5,wh-ww/10,ww/10);
glColor3f(0.0, 0.0, 0.0);
screen_box(ww/10+ww/40,wh-ww/10+ww/40,ww/20);
glBegin(GL_LINES);
glVertex2i(wh/40,wh-ww/20);
glVertex2i(wh/40+ww/20,wh-ww/20);
glEnd();
glBegin(GL_TRIANGLES);
glVertex2i(ww/5+ww/40,wh-ww/10+ww/40);
glVertex2i(ww/5+ww/20,wh-ww/40);
glVertex2i(ww/5+3*ww/40,wh-ww/10+ww/40);
glEnd();
glPointSize(3.0);
glBegin(GL_POINTS);
glVertex2i(3*ww/10+ww/20, wh-ww/20);
glEnd();
glRasterPos2i(2*ww/5,wh-ww/20);
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'A');
shift=glutBitmapWidth(GLUT_BITMAP_9_BY_15, 'A');
glRasterPos2i(2*ww/5+shift,wh-ww/20);
glutBitmapCharacter(GLUT_BITMAP_9 BY 15, 'B');
shift+=glutBitmapWidth(GLUT_BITMAP_9_BY_15,
'B');
glRasterPos2i(2*ww/5+shift,wh-ww/20);
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'C');
glFlush();
glPopAttrib();
}
// rehaping routine called whenever window is resized or
moved
void myReshape(GLsizei w, GLsizei h)
{
// adjust clipping box
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// adjust viewport and clear
glViewport(0,0,w,h);
glClearColor (0.8, 0.8, 0.8, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
display();
expand button
Transcribed Image Text:} // display callback function void display(void) { int shift=0; glPushAttrib(GL_ALL_ATTRIB_BITS); glClearColor (0.8, 0.8, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); screen_box(0,wh-ww/10,ww/10); glColor3f(1.0, 0.0, 0.0); screen_box(ww/10,wh-ww/10,ww/10); glColor3f(0.0, 1.0, 0.0); screen_box(ww/5,wh-ww/10,ww/10); glColor3f(0.0, 0.0, 1.0); screen_box(3*ww/10,wh-ww/10,ww/10); glColor3f(1.0, 1.0, 0.0); screen_box(2*ww/5,wh-ww/10,ww/10); glColor3f(0.0, 0.0, 0.0); screen_box(ww/10+ww/40,wh-ww/10+ww/40,ww/20); glBegin(GL_LINES); glVertex2i(wh/40,wh-ww/20); glVertex2i(wh/40+ww/20,wh-ww/20); glEnd(); glBegin(GL_TRIANGLES); glVertex2i(ww/5+ww/40,wh-ww/10+ww/40); glVertex2i(ww/5+ww/20,wh-ww/40); glVertex2i(ww/5+3*ww/40,wh-ww/10+ww/40); glEnd(); glPointSize(3.0); glBegin(GL_POINTS); glVertex2i(3*ww/10+ww/20, wh-ww/20); glEnd(); glRasterPos2i(2*ww/5,wh-ww/20); glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'A'); shift=glutBitmapWidth(GLUT_BITMAP_9_BY_15, 'A'); glRasterPos2i(2*ww/5+shift,wh-ww/20); glutBitmapCharacter(GLUT_BITMAP_9 BY 15, 'B'); shift+=glutBitmapWidth(GLUT_BITMAP_9_BY_15, 'B'); glRasterPos2i(2*ww/5+shift,wh-ww/20); glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'C'); glFlush(); glPopAttrib(); } // rehaping routine called whenever window is resized or moved void myReshape(GLsizei w, GLsizei h) { // adjust clipping box glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // adjust viewport and clear glViewport(0,0,w,h); glClearColor (0.8, 0.8, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); display();
glFlush();
// set global size for use by drawing routine
ww = w;
wh = h;
}
// init attributes and viewing
void myInit(void) {}
// menu associated with right button
void right_menu(int id)
{
if(id
else display();
}
1) exit(0);
==
// color submenu
void color_menu(int id)
= 1) {r= 1.0; g = 0.0; b = 0.0;}
if(id
else if(id == 2) {r= 0.0; g = 1.0; b = 0.0;}
else if(id ==
else if(id
else if(id
else if(id
else if(id == 7) {r=1.0; g = 1.0; b = 1.0;}
else if(id
}
3) {r = 0.0; g = 0.0; b = 1.0;}
4) {r= 0.0; g = 1.0; b = 1.0;}
5) {r= 1.0; g = 0.0; b = 1.0;}
6) {r= 1.0; g = 1.0; b = 0.0;}
8) {r= 0.0; g = 0.0; b = 0.0;}
==
// pixel size submenu
void pixel_menu(int id)
{
if (id
else if (size > 1) size = size/2;
}
1) size = 2 * size;
// fill submenu
void fill_menu(int id)
{
if (id
else fill = 0;
}
1) fill = 1;
==
// entrance of program
int main(int argc, char**
{
int c menu, p menu, f menu;
argv)
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("square");
glutDisplayFunc(display);
// create your submenus and right menu here
myInit ();
glutReshapeFunc(myReshape);
glutKeyboardFunc(key);
glutMouseFunc (mouse);
glutMainLoop();
expand button
Transcribed Image Text:glFlush(); // set global size for use by drawing routine ww = w; wh = h; } // init attributes and viewing void myInit(void) {} // menu associated with right button void right_menu(int id) { if(id else display(); } 1) exit(0); == // color submenu void color_menu(int id) = 1) {r= 1.0; g = 0.0; b = 0.0;} if(id else if(id == 2) {r= 0.0; g = 1.0; b = 0.0;} else if(id == else if(id else if(id else if(id else if(id == 7) {r=1.0; g = 1.0; b = 1.0;} else if(id } 3) {r = 0.0; g = 0.0; b = 1.0;} 4) {r= 0.0; g = 1.0; b = 1.0;} 5) {r= 1.0; g = 0.0; b = 1.0;} 6) {r= 1.0; g = 1.0; b = 0.0;} 8) {r= 0.0; g = 0.0; b = 0.0;} == // pixel size submenu void pixel_menu(int id) { if (id else if (size > 1) size = size/2; } 1) size = 2 * size; // fill submenu void fill_menu(int id) { if (id else fill = 0; } 1) fill = 1; == // entrance of program int main(int argc, char** { int c menu, p menu, f menu; argv) glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow("square"); glutDisplayFunc(display); // create your submenus and right menu here myInit (); glutReshapeFunc(myReshape); glutKeyboardFunc(key); glutMouseFunc (mouse); glutMainLoop();
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education