Here is the template can anyone help me draw the cube?<p>
/* Your next graphics assignment due July 1 is to modify the following 3D cube<br>
display program to carry out the required transformations from a 3D world<br>
coordinate space to the screen display coordinates. The goal is to introduce<br>
you to 3D representations and floating point transformations. As demoed in<br>
class the program should display a wireframe "superrubix" cube as viewed from<br>
a slightly restricted range of eye viewpoints. The wireframe dynamically<br>
changes as the eye position moves under mouse movement control. (If you have<br>
a very slow machine you may substitute mouse clicks for continuous motion.)<br>
All of your changes will be in the body of the xyz_line() function which has<br>
as input the XYZ end coordinates of a line in "world" space along with a draw<br>
color. The function will perform the 4 steps outlined by the comments to<br>
prepare for the call to GrLine() to produce a 2D line on the screen. As we<br>
will see later there are more general methods for doing this using homogeneous<br>
coordinate matricies but this is better for an introductory understanding.<br>
The dynamic motion is already taken care of by 1st undrawing and the view<br>
and then drawing a new view. Although there is no rotation involved in your<br>
code, you will notice the partial rotation like behavior.<br>
This file is available as "asn4.c" from the web page.<br>
*/<br>
#include <graphics.h><br>
#include <mouse.h><br>
MouseEvent my_mouse_event;<br>
#define M my_mouse_event<br>
#define GADDR ((unsigned char *)0xD0000000)<br>
#define W 640 /* physical screen width in pixels */<br>
#define H 480 /* physical screen height in pixels */<br>
#define LSW 12.0 /* logical screen width in world coordinates */<br>
#define LSH 9.0 /* logical screen height in world coordinates */<br>
#define BLUE 255<br>
#define WHITE 254<br>
#define RED 253<br>
int hide = 1; /* default to hidden line removal */<br>
int xview = W/2, yview = H/2, zview = -W; /* initial view point front¢er */<p>
main(argc, argv) char *argv[]; {<br>
if(argc > 1) /* suppress surface hiding if there is an extra arg */<br>
hide = 0;<br>
GrSetMode(GR_width_height_graphics, W, H);<br>
GrSetColor(RED, 255, 0, 0);<br>
GrSetColor(BLUE, 0, 0, 255);<br>
GrSetColor(WHITE, 255, 255, 255);<br>
memset(GADDR, BLUE, W*H);<br>
draw_view(xview, yview, zview, WHITE);<br>
for( ; ; ) {<br>
MouseGetEvent(M_KEYPRESS|M_MOTION, &M);<br>
if(M.flags & M_KEYPRESS) {<br>
break; /* exit on any keypress */<br>
} else if(M.flags & M_MOTION) {<br>
draw_view(xview, yview, zview, BLUE); /* erase old */<br>
xview = M.x; /* remember view position */<br>
if(M.buttons)<br>
zview = -W + (H - M.y);<br>
else<br>
yview = M.y;<br>
draw_view(xview, yview, zview, WHITE); /* draw new */<br>
}<br>
}<br>
GrSetMode(GR_80_25_text); /* restore text mode before exiting */<br>
exit(0);<br>
}<p>
float x_eye, y_eye, z_eye; /* global coords of eye for xyz_line */<br>
draw_view(ixv, iyv, izv, color) {<br>
unsigned char *p;<br>
float x, y;<br>
char txt[128];<br>
x_eye = ((float)ixv - W/2) / (W/LSW); /* world float view point */<br>
y_eye = (H/2 - (float)iyv) / (H/LSH);<br>
z_eye = ((float)izv) / (W/LSW);<br>
sprintf(txt, " %6.2f %6.2f %6.2f ", x_eye, y_eye, z_eye);<br>
GrTextXY(10, 20, txt, WHITE, BLUE);<br>
p = GADDR + W * iyv + ixv;<br>
*p = color; /* leave a dot at the xy view point */<br>
if(!hide || z_eye < -2.0)<br>
face(-2.0, color);<br>
if(!hide || z_eye > 2.0)<br>
face(2.0, color);<br>
if(!hide || y_eye < -2.0)<br>
topbot(-2.0, color);<br>
if(!hide || y_eye > 2.0)<br>
topbot(2.0, color);<br>
if(!hide || x_eye < -2.0)<br>
side(-2.0, color);<br>
if(!hide || x_eye > 2.0)<br>
side(2.0, color);<br>
}<p>
face(z, color) float z; {<br>
float x, y;<br>
for(x = -2.0; x <= 2.0; x += 1.0)<br>
xyz_line(x, -2.0, z, x, 2.0, z, color);<br>
for(y = -2.0; y <= 2.0; y += 1.0)<br>
xyz_line(-2.0, y, z, 2.0, y, z, color);<br>
}<p>
topbot(y, color) float y; {<br>
float x, z;<br>
for(x = -2.0; x <= 2.0; x += 1.0)<br>
xyz_line(x, y, -2.0, x, y, 2.0, color);<br>
for(z = -2.0; z <= 2.0; z += 1.0)<br>
xyz_line(-2.0, y, z, 2.0, y, z, color);<br>
}<p>
side(x, color) float x; {<br>
float y, z;<br>
for(y = -2.0; y <= 2.0; y += 1.0)<br>
xyz_line(x, y, -2.0, x, y, 2.0, color);<br>
for(z = -2.0; z <= 2.0; z += 1.0)<br>
xyz_line(x, 2.0, z, x, -2.0, z, color);<br>
}<p>
/* draw a line in 3D world coordinate space - also uses x_eye, y_eye, z_eye */<br>
xyz_line(x1, y1, z1, x2, y2, z2, c) float x1, y1, z1, x2, y2, z2; {<br>
int sx1, sx2, sy1, sy2; /* physical screen coordinates */<br>
/* convert to eye relative coords */<br>
/* perspective scale each point */<br>
/* reposition to logical screen xy */<br>
/* convert to phys screen coords */<br>
GrLine(sx1, sy1, sx2, sy2, c); /* 2D line with phys screen coords */<br>
}