Drawing a cube, using C in the DJGPP environment - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Drawing a cube, using C in the DJGPP environment

Here is the template can anyone help me draw the cube?

/* Your next graphics assignment due July 1 is to modify the following 3D cube

display program to carry out the required transformations from a 3D world

coordinate space to the screen display coordinates. The goal is to introduce

you to 3D representations and floating point transformations. As demoed in

class the program should display a wireframe "superrubix" cube as viewed from

a slightly restricted range of eye viewpoints. The wireframe dynamically

changes as the eye position moves under mouse movement control. (If you have

a very slow machine you may substitute mouse clicks for continuous motion.)

All of your changes will be in the body of the xyz_line() function which has

as input the XYZ end coordinates of a line in "world" space along with a draw

color. The function will perform the 4 steps outlined by the comments to

prepare for the call to GrLine() to produce a 2D line on the screen. As we

will see later there are more general methods for doing this using homogeneous

coordinate matricies but this is better for an introductory understanding.

The dynamic motion is already taken care of by 1st undrawing and the view

and then drawing a new view. Although there is no rotation involved in your

code, you will notice the partial rotation like behavior.

This file is available as "asn4.c" from the web page.




MouseEvent my_mouse_event;

#define M my_mouse_event

#define GADDR ((unsigned char *)0xD0000000)

#define W 640 /* physical screen width in pixels */

#define H 480 /* physical screen height in pixels */

#define LSW 12.0 /* logical screen width in world coordinates */

#define LSH 9.0 /* logical screen height in world coordinates */

#define BLUE 255

#define WHITE 254

#define RED 253

int hide = 1; /* default to hidden line removal */

int xview = W/2, yview = H/2, zview = -W; /* initial view point front&center */

main(argc, argv) char *argv[]; {

if(argc > 1) /* suppress surface hiding if there is an extra arg */

hide = 0;

GrSetMode(GR_width_height_graphics, W, H);

GrSetColor(RED, 255, 0, 0);

GrSetColor(BLUE, 0, 0, 255);

GrSetColor(WHITE, 255, 255, 255);

memset(GADDR, BLUE, W*H);

draw_view(xview, yview, zview, WHITE);

for( ; ; ) {


if(M.flags & M_KEYPRESS) {

break; /* exit on any keypress */

} else if(M.flags & M_MOTION) {

draw_view(xview, yview, zview, BLUE); /* erase old */

xview = M.x; /* remember view position */


zview = -W + (H - M.y);


yview = M.y;

draw_view(xview, yview, zview, WHITE); /* draw new */



GrSetMode(GR_80_25_text); /* restore text mode before exiting */



float x_eye, y_eye, z_eye; /* global coords of eye for xyz_line */

draw_view(ixv, iyv, izv, color) {

unsigned char *p;

float x, y;

char txt[128];

x_eye = ((float)ixv - W/2) / (W/LSW); /* world float view point */

y_eye = (H/2 - (float)iyv) / (H/LSH);

z_eye = ((float)izv) / (W/LSW);

sprintf(txt, " %6.2f %6.2f %6.2f ", x_eye, y_eye, z_eye);

GrTextXY(10, 20, txt, WHITE, BLUE);

p = GADDR + W * iyv + ixv;

*p = color; /* leave a dot at the xy view point */

if(!hide || z_eye < -2.0)

face(-2.0, color);

if(!hide || z_eye > 2.0)

face(2.0, color);

if(!hide || y_eye < -2.0)

topbot(-2.0, color);

if(!hide || y_eye > 2.0)

topbot(2.0, color);

if(!hide || x_eye < -2.0)

side(-2.0, color);

if(!hide || x_eye > 2.0)

side(2.0, color);


face(z, color) float z; {

float x, y;

for(x = -2.0; x <= 2.0; x += 1.0)<br>
xyz_line(x, -2.0, z, x, 2.0, z, color);

for(y = -2.0; y <= 2.0; y += 1.0)<br>
xyz_line(-2.0, y, z, 2.0, y, z, color);


topbot(y, color) float y; {

float x, z;

for(x = -2.0; x <= 2.0; x += 1.0)<br>
xyz_line(x, y, -2.0, x, y, 2.0, color);

for(z = -2.0; z <= 2.0; z += 1.0)<br>
xyz_line(-2.0, y, z, 2.0, y, z, color);


side(x, color) float x; {

float y, z;

for(y = -2.0; y <= 2.0; y += 1.0)<br>
xyz_line(x, y, -2.0, x, y, 2.0, color);

for(z = -2.0; z <= 2.0; z += 1.0)<br>
xyz_line(x, 2.0, z, x, -2.0, z, color);


/* draw a line in 3D world coordinate space - also uses x_eye, y_eye, z_eye */

xyz_line(x1, y1, z1, x2, y2, z2, c) float x1, y1, z1, x2, y2, z2; {

int sx1, sx2, sy1, sy2; /* physical screen coordinates */

/* convert to eye relative coords */

/* perspective scale each point */

/* reposition to logical screen xy */

/* convert to phys screen coords */

GrLine(sx1, sy1, sx2, sy2, c); /* 2D line with phys screen coords */



  • To do 3D -> 2D conversion you have to divide X and Y coordinates with Z coordinate - if Z goes away of eye. Same in other words: You divide horisontal and vertical coords with depth coord, like this:

    SreenX= Xcoor3D/Zcoor3D;

    SreenY= Ycoor3D/Zcoor3D;

    There you will have one problen, what if "Zcoor3D" is zero... Then you could do like this:

    if (Zcoor3D==0)


    SreenX= Xcoor3D/Zcoor3D;

    SreenY= Ycoor3D/Zcoor3D;

    Which isn't bad idea if you aren't often close at eye. (Zcoor3D is distance from eye, imagine what object could look if it is zero pixels away of your own eye...)

    If you have many objects on zero Z coordinate, then another way is to add some constant value on Z coor, like this:

    Zcoor3D+=256; // This isn't bad

    if (Zcoor3D==0)


    SreenX= Xcoor3D/Zcoor3D;

    SreenY= Ycoor3D/Zcoor3D;

    I hope that this helps you.


Sign In or Register to comment.