Here are some C functions which implement a software framebuffer, consisting
of a 50x50 array of pixels. To make the pixels easy to see, they are
drawn as little squares (or "big pixels").
To color a pixel, you should call the function
colorPixel.
Makefile
The basic project (worth 85 points) is to use the
colorPixel function to draw a rasterized version
of any trapezoid with horizontal top and bottom sides.
Your trapezoid drawings
should consist entirely of big pixels, drawn by calling
colorPixel.
Your function should take the vertices of
the trapezoid as floating-point inputs, and also four colors,
one for each vertex of the trapezoid. The colors should be linearly
interpolated up the sides of the trapezoid, and then across each row.
You should interpolate the colors of the endpoints of the left and
right edges of the trapezoid to get the endpoint colors at each row,
and then interpolate the colors along the row.
To show that your function works, draw several
trapezoids with different shapes and interpolated colors.
You might want to try to make a picture or a pleasing pattern.
You may NOT use OpenGL calls which draw polygons to draw the trapezoid.
You may NOT use the sqrt function, or any other function from the
math library; they all take a long time and are avoided in time-critical
graphics functions.
To get started, download the four files above into a directory on
one of the machines in
Kemper 67, and type "make". You should get an executable called
bigPix. Typing "bigPix" will run it, bringing up a window with
a gray square (the framebuffer) with one pixel colored red in the
middle. Find the function drawContents. Right now it just
draws one red pixel. Write your trapezoid drawing function, and then
call it from drawContents.
You can convert the code to C++ if you want; the Makefile should
work for C++ as well.
Note: Sometimes people have problems with the Makefile when they
download it to Windows and then transfer it to the Linux
machines. This can replace the TAB characters with spaces, which
unfortunately ruins the Makefile (very silly Makefile syntax, where
TAB has meaning! ugh!). Try downloading the Makefile straight from the
browser.
Additional Features
Here are some ideas for ways to extend the basic project. Other ideas are
welcome; check with me or the TA to make sure they are appropriate.
Only very exceptionaly projects will get more than 100 points total.
- (up to 10 points) Rasterize arbitrary triangles rather than trapezoids. Notice you will need some logic to figure out which pair of triangle edges bounds a given row. Watch out for corner cases like horizontal edges.
- (up to 15 points) Use Bresenham's algorithm to rasterize
lines. This should be for rasterizing lines in addition to trapezoids, not as part of your trapezoid
rasterization code. This classic algorithm, optimized to avoid not only floating
point operations but even multiplication, is described on pages
399-401 in the book, in this document
from a game developer's club in Colorado, and lots of other places.
Make the color interpolation work with interger arithmetic and without
multiplication or division in the inner loop, the same way the x-positon
interpolation works.
- (up to 15 points) Read in an image, and average 10x10 blocks of input
pixels to make a big-pixelated version of the image. This should be pretty
cool looking. One good way to read in an image is to steal the code
that reads a .ppm image from the sample code
ppmdisplay.c
from the textbook. The .ppm format is not the most efficient, but it is very
simple. To make a .ppm image from .jpg or .gif images on the machines in 67 Kemper, you can type:
jpegtopnm filename.jpg > pnmtoplainpnm > filename.ppm
giftopmn filename.gif > pnmtoplainpnm > filename.ppm
Here is more info on .ppm and its relations.
- Up to 10 points: Implement rasterized Bezier curves.
- Up to 15 points: Animate the trapezoid somehow (this was suggested by someone in the class; we will get
to animation later in the quarter, but if you want to figure it out on your own now, go ahead).
The score here will really depend on the quality of your animation, ie. good ideas, technichal difficulty, etc.
Turning in the project
Turn in the project using the handin script (this is explained on
the TA's Web page). Hand in your source code, a Makefile for the Linux machines
in 67 Kemper, an executable that runs on the machines in 67 Kemper, an input
image file if you used one, and a brief text file explaining how to run your program and what you implemented. Make sure your name appears on all of the source files and prominently in the text file.