// File: ztest.cxx // This is a simple test program for the zbuffer class #include #include #include "zbuffer.h" using namespace std; const int WIDTH=300; const int HEIGHT=400; const int EYE_DISTANCE=400; void draw_design(ZBuffer& zb, double angle); // This function draws a little design on the currently active page // using the ZBuffer zb to control which pixels are seen. The // Design consists of a yellow rectangle with a red line going // through its center. The angle parameter determines the angle // of the line in the x-z plane. The ends of the line have // cyan and green spheres. void swap_screens( ); // This function swaps the active and visual pages, which must be // pages number 1 and 2. int main( ) { double angle=0; ZBuffer zb(WIDTH, HEIGHT, EYE_DISTANCE); initwindow(WIDTH, HEIGHT); setactivepage(1); setvisualpage(2); while (!kbhit( )) { clearviewport( ); // Clear the screen zb.clear( ); // And clear the zbuffer draw_design(zb, angle); swap_screens( ); angle += 0.0314159; } getch( ); return 0; } void draw_design(ZBuffer& zb, double angle) { int i, j; double x1, z1, x2, y2, z2; // Compute x and z coordinates of a line: x2 = WIDTH/2*cos(angle); z1 = WIDTH/2*sin(2*angle); x1 = WIDTH/2*cos(angle+3.14159); y2 = HEIGHT/2*cos(angle); z2 = WIDTH/2*sin(2*angle+3.14159); // Draw a YELLOW rectangle, a RED line and a GREEN sphere: for (i = -30; i < 30; i++) for (j = 0; j < 30; j++) zb.point(i, j, 0, YELLOW); zb.line(x1, 15, z1, x2, y2, z2, RED); zb.sphere(x1, 15, z1, 20, CYAN); zb.sphere(x2, y2, z2, 20, GREEN); zb.line(0, -100, 0, 0, 100, 0, WHITE); } void swap_screens( ) { if (getactivepage() == 1) { setactivepage(2); setvisualpage(1); } else { setactivepage(1); setvisualpage(2); } }