Computer Graphics

Moderators: Sephiroth
Number of threads: 1241
Number of posts: 2641

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Edit Report
Drawing a cube, using C in the DJGPP environment Posted by GraphicsRookie on 29 Jun 1999 at 4:16 PM
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&center */<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>
}


Edit Report
Re: Drawing a cube, using C in the DJGPP environment Posted by Sepi on 29 Jun 1999 at 10:19 PM
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:<p>
SreenX= Xcoor3D/Zcoor3D;<br>
SreenY= Ycoor3D/Zcoor3D;<p>
There you will have one problen, what if "Zcoor3D" is zero... Then you could do like this:<p>
if (Zcoor3D==0)<br>
Zcoor3D=1;<br>
SreenX= Xcoor3D/Zcoor3D;<br>
SreenY= Ycoor3D/Zcoor3D;<p>
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...)<p>
If you have many objects on zero Z coordinate, then another way is to add some constant value on Z coor, like this:<p>
Zcoor3D+=256; // This isn't bad<br>
if (Zcoor3D==0)<br>
Zcoor3D=1;<br>
SreenX= Xcoor3D/Zcoor3D;<br>
SreenY= Ycoor3D/Zcoor3D;<p>
<br>
I hope that this helps you.<p>
-Sepi





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.