Assignment 3 - Due March 6

A simple drawing program

Listed below is a primitive drawing program which lets the user draw curves, but loses the display upon refresh (as when we visit another program and then return to this window).

Modify the program to include these features:

Email your source code to me before class on 3/6/01, and turn in a listing of your program at the start of class as well.

You'll need some appropriate data structures. I'd recommend an array of colors and a global variable for the current color index (set by the color menu) rather than setting the color directly through the menu. You'll have to associate a color with each curve drawn, and an index is more economical than a color vector. Also, you'll need a large array of points and/or curves; you can assume some upper bound on the number of points/curves, but you should check against overflow.

Since your image will be a collection of curves, each with a fixed color and width, it would be wasteful to associate a color and width with every single point -- try to think of some way to store the instances where colors and widths change. Also, check that if you draw two curves and the screen is redrawn, the curves are not connected to each other by mistake.

Let's agree that the user will not connect more than 1000 points -- this allows you to use static arrays. Just have the program quit drawing if the limit is reached.

Here's a screen shot of typical menus:

And here's the current program:

/* A simple curve drawing program, 1/31/01 by Mark Harris
Illustrates mouse, mouse motion callback functions
*/

#include <GL/glut.h>

int squarecolor=1;
int anchorx,anchory;
int WindowWidth=500;
int WindowHeight=500;

void myinit(void)
{

        /* attributes */

        glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */

        /* set up viewing */
        /* 500 x 500 window with origin lower left */

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0.0, 500.0, 0.0, 500.0);
        glMatrixMode(GL_MODELVIEW);
}

void display( void )
{
        glClear(GL_COLOR_BUFFER_BIT);  /*clear the window */
        glFlush(); /* clear buffers */
}

void mouse(int button,int state,int x,int y)
{
        y=500-y; // convert system orientation
        anchorx=x;
        anchory=y;
        return;
}

void motion(int x, int y)
{
        y=500-y; // convert system orientation
        glColor3f(1,0,0); /* draw in red */
        glBegin(GL_LINES);
                glVertex2f(anchorx,anchory); 
                glVertex2f(x,y); 
        glEnd();
        anchorx=x; // update anchor point
        anchory=y;
}

void main(int argc, char** argv)
{

        glutInit(&argc,argv);
        glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
        glutInitWindowSize(WindowWidth,WindowHeight);
        glutInitWindowPosition(0,0); /* place window top left on display */
        glutCreateWindow("Draw"); /* window title */
        glutDisplayFunc(display); /* display callback invoked when window opened */
        glutMouseFunc(mouse);
        glutMotionFunc(motion);
        myinit(); /* set attributes */

        glutMainLoop(); /* enter event loop */
}
You'll be modifying this code to implement color selection and a display function which resurrects everything you've drawn, including curves, colors, and widths.