js9b+@andrew.cmu.edu (Jon C. Slenk) (11/17/88)
Ok - I have been working on this for some time (few days) and have a book which isn't helping much, nor are the others. So: How does one do 3D to 2D transformations ie (x,y,z) -> screen (x,y). Email, please, as I am sure most everyone knows all about all this... thanks. Jon Slenk / js9b CMU.
josef@ugun21.UUCP (11/21/88)
Subject: How does one do 3D to 2D transformations ie (x,y,z) -> screen (x,y).
I know that
> "most everyone knows all about all this..."
but I DON'T, and I would be interested as well ...
Josef Moellers
paper mail: e-mail:
c/o Nixdorf Computer AG USA: uunet!linus!nixbur!nixpbe!mollers.pad
Abt. EG-3 !USA: mcvax!unido!nixpbe!mollers.pad
Unterer Frankfurter Weg
D-4790 Paderborn
tel.: (+49) 5251 104691
Standard disclaimer: Blablabla opinion blablabla employer blablabla!cheng@galaxy.ee.rochester.edu (Bruce Cheng (396 Account)) (11/21/88)
In article <wXUS8hy00V4IE1Fl1r@andrew.cmu.edu> js9b+@andrew.cmu.edu (Jon C. Slenk) writes: >Ok - I have been working on this for some time (few days) and have a book which >isn't helping much, nor are the others. So: > >How does one do 3D to 2D transformations ie (x,y,z) -> screen (x,y). > >Email, please, as I am sure most everyone knows all about all this... > >thanks. > >Jon Slenk / js9b CMU. Can any one send me imformatino about this also? Thanks in advance. Bruce Cheng cheng@cs.rochester.edu
scott@applix.UUCP (Scott Evernden) (11/26/88)
In article <16400001@ugun21> josef@ugun21.UUCP writes: > > >Subject: How does one do 3D to 2D transformations ie (x,y,z) -> screen (x,y). > >I know that >> "most everyone knows all about all this..." >but I DON'T, and I would be interested as well ... You can get a good introduction to this topic as well as some functioning Pascal code with hidden-line handling in the March and April 1981 issues of BYTE mag. Look for the 2-part article "Three-Dimensional Computer Graphics" by Franklin Crow. All you need to know to get started... -scott
gates@nsc.nsc.com (Tim Gates) (11/26/88)
To convert from 3D (x,y,z) to 2D (h,v):
Address your screen via two variables h (horiz) and v (vert).
On this flat screen you have three lines:
1) a vertical line (represents z axis)
2) a diagonal line higher on left than right (represents y axis)
3) a diagonal line lower on left than right (represents x axis)
Phi is the angle of line 2 from horizontal.
Theta is the angle of line 3 from horizontal.
Phi and Theta determine the "tilt" of the projection. Small angles make the
x-y plane appear to be perpendicular to your screen. Larger angles tilt the
x-y plane. I like 30 degrees (0.52 radians) for both phi and theta.
Next Calculate 4 constants:
sin_y = -sin(phi);
cos_y = cos(phi);
sin_x = -sin(theta);
cos_x = cos(theta);
The conversion from 3D to 2D is:
v = z + (y * sin_y) + (x * sin_x); /* double flat_v(x,y,z); */
h = (y * cos_y) - (x * cos_x); /* double flat_h(x,y); */
I suggest doing the conversion as a macro:
#define flat_v(x,y,z) ((z) + ((y) * sin_y) + ((x) * sin_x))
#define flat_h(x,y) (((y) * cos_y) - ((x) * cos_x))
Notice that h and v are floating point values; they should be scaled
to convert them to screen pixel cooridinates:
Assume that max_x, max_y, max_z, min_x, min_y, min_z are the limits of 3-Space
you will be dealing with. Now to calculate the limits of 2-space:
double max_x, max_y, min_x, min_y;
max_v = flat_v(min_x,min_y,max_z);
min_v = flat_v(max_x,max_y,min_z);
max_h = flat_h(max_x,min_y);
min_h = flat_h(min_x,max_y);
Determine the scaling amounts:
Assume that max_c is the number of horizontal pixels (columns) on the screen.
Assume that max_r is the number of vertical pixels (rows) on the screen.
Lower left corner of screen is 0,0.
double max_c = 799.0; /* screen is 800x600 */
double max_r = 599.0;
double scl_c, scl_r, off_c, off_r; /* scaling parameters */
off_c = min_h;
scl_c = (max_c + 1.0) / (max_h - min_h);
off_r = min_v;
scl_r = (max_r + 1.0) / (max_v - min_v);
The Scaling functions are:
int scale_c(horz)
double horz;
{
int c;
double t;
t = (horz - off_c) * scl_c;
if(t < 0.0) t = 0.0;
if(t > max_c) t = max_c;
c = t + 0.5; /* round off */
return(c);
}
int scale_r(vert)
double vert;
{
int r;
double t;
t = (vert - off_r) * scl_cr
if(t < 0.0) t = 0.0;
if(t > max_r) t = max_r;
r = t + 0.5; /* round off */
return(r);
}
To plot a pixel:
Assume that the function set_pix(c,r,color) sets the pixel in row r, column c
to color.
The 3-space pixel plotting is:
void set_pix3(x,y,z,c)
double x,y,z;
int c; /* color */
{
set_pix(
scale_c( flat_h(x,y)),
scale_r( flat_v(x,y,z)),
c);
}
Speedups:
In the scaling routines:
Convert to int, then bounds check.
Convert set_pix3 to a macro.
I hope you have fun with this...
Standard discalmer: Use at your own risk. I entered this from memory.
--
Tim Gates
National Semiconductor
{amdahl|decwrl|hplabs|nscpdc|pyramid|sun|voder}!nsc!gates
"Where the men are men, and the computers run scared."chem4m@jetson.uh.edu (08/04/90)
I am looking for generic routines to convert 3d coordinates into 2d ( I am actually writing the prog. In TP pascal. Any help in this matter would be appreciated. Thanks in advance Avijit Ghosh