CpSc 465, Principles of Computer Graphics
due February 17
Begin with an empty window. Each mouse click in an empty spot in
the window should cause a small marker (e.g., a dot, a square, or an "X")
to be drawn. The first click should produce only a marker. Subsequent
clicks should mark the spot and add a new polyline segment from the
previous point to the new position. The picture below shows an example
after the mouse has been clicked six times:
Add a menu item that will allow you to clear the window.
Once the functionality above is working, add the ability to drag each
marker to a new position. Rubber-band the two line segments connected
to the marker being dragged. Clicking other spots in the window should
continue to add polyline segments.
- Keep an array of points as your "database":
- When the user clicks in a blank spot, add
the mouse coordinates to the array.
- In your draw callback, clear the screen,
then iterate over the array to draw the polyline.
- To handle interaction:
- To determine if the user has clicked in
a blank spot or on a marker, take the mouse
coordinates and compare them to each point in
the database. Points that are close to a marker
are a hit and should switch you to
- When you are in "drag" mode, the mouse
motion callback should handle rubber-banding.
Erase the old lines and draw the new lines.
- When the user releases the mouse button,
switch back out of "drag" mode and trigger the
- If you are new to event-driven programming, you may want to
consider using SPiGoT. You will, however, need to use OpenGL
for subsequent assignments.
- Remember too that you may choose to work with another student.
If you are comfortable with event-driver programming, please
consider pairing with a student with less experience. Let the
other student "drive," sitting in front of the computer and
typing, while you help figure out what to do next. Resist the
urge to take the keyboard and write all the code yourself. This
is a software engineering technique known as "pair programming."
Pair programming works well to bring new programmers up to speed,
and two experienced programmers can often be more productive in
a pair than they could be individually.
- An easy way to avoid damaging the drawing while rubber-banding
is to use XOR mode for line drawing.
- Ordinary drawing is done with copy
mode, copying the current color into the
- With XOR mode, the current color is XOR'ed
with the color of each affected pixel.
- Remember that XOR'ing a value a second time
with the same value will yield the original value.
- For example, 0x12345678 ^ 0xDEADBEEF =
0xCC99E897. But 0xCC99E897 ^ 0xDEADBEEF =
- So to erase a line, simply draw it again.
- To do this in OpenGL:
- Enable logical operations with
- Switch to XOR mode with
- Switch back with glLogicOp(GL_COPY)
- SPiGoT does not support XOR mode (although it does use it
to implement GetMouseClick()). If you are using SPiGoT
for this project, go ahead and erase lines by drawing them again
in the background color. This will damage the picture, so make
sure to call RequestRedraw() when the user finishes
dragging the point.