How do I get a 3d representation on a 2d window surface.

How do I get a 3d representation on a 2d window surface.

Post by James William » Sun, 06 Jul 2003 02:27:32



Hello,

I have been trying to take 3d lines,vectors and project them onto a screen
2d surface, but can't get it right.
I want to have a standard isometric view such that the angles of projection
on the 2d surface is 30 degrees to the left for the y axis, 30 degrees right
for the x axis and z is going upward only the 2d y axis.  The problem is
that z seems to project downward, y goes right and x goes left.  And on top
of them they are not scaled correct.  I.E. a  cube does not project the same
lengths along each of the transformed axis.

Why isn't it project like it should?  with \|/ projection?

Here is what I have been trying:
typedef struct
{
  double x,y,z;

Quote:}TCube;

//There are eight points that define a cube in 3d space
TCube Cube[8]={{0,0,0}{40,0,0},{40,40,0},{0,40,0},
                             {0,0,40},{40,0,40},{40,40,40},{0,40,40}}; // a
40 pixel cube.
//Create an initial matrix identity.
double Matrix[4][4]={{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};

//Transform the matrix. yaxis -30, x axis 30; Then project by removing the z
compents.
yrot(DegToRad(-30),&Matrix);
xrot(DegToRad(30),&Matrix);
project(3,&Matrix);  //Project the z axis.

  for(int i = 0;i<8;i++)
  {
    Cube[i].x = Matrix[0][0]*Cube[i].x + Matrix[1][0]*Cube[i].y +
Matrix[2][0]*Cube[i].z + Matrix[3][0]*1.0;
    Cube[i].y = Matrix[0][1]*Cube[i].x + Matrix[1][1]*Cube[i].y +
Matrix[2][1]*Cube[i].z + Matrix[3][1]*1.0;
    //Because we can't draw in 3D in windows, there is no need to transform
the z's of Cube.
  }

//Now draw onto canvas.
int i=1;
int h = PaintBox1->Height;//Because windows origin is in the upper corner.
PaintBox1->Canvas->MoveTo(Cube[0].x,h-Cube[0].y);
for(;i<3;i++)
{

  PaintBox1->Canvas->LineTo(Cube[i].x,h-Cube[i].y);

Quote:}

//Now close the lower part.
PaintBox1->Canvas->LineTo(Cube[0].x,h-Cube[0].y);
PaintBox1->Canvas->MoveTo(Cube[4].x,h-Cube[4].y);
i++;
for(;i<3;i++)
{
  PaintBox1->Canvas->LineTo(Cube[i].x,h-Cube[i].y);

Quote:}

//Draw the lines from the two boxs to form the 3d cube.
 PaintBox1->Canvas->MoveTo(Cube[0].x,h-Cube[0].y);
  PaintBox1->Canvas->LineTo(Cube[4].x,h-Cube[4].y);

  PaintBox1->Canvas->MoveTo(Cube[1].x,h-Cube[1].y);
  PaintBox1->Canvas->LineTo(Cube[5].x,h-Cube[5].y);
  PaintBox1->Canvas->MoveTo(Cube[2].x,h-Cube[2].y);
  PaintBox1->Canvas->LineTo(Cube[6].x,h-Cube[6].y);
  PaintBox1->Canvas->MoveTo(Cube[3].x,h-Cube[3].y);
  PaintBox1->Canvas->LineTo(Cube[7].x,h-Cube[7].y);

//Now, here is my rotation routines.
void TOKRightDlg::xrot(double phi,double (* Matrix)[4][4])
{
  //TODO: Add your source code here
  double Identity[4][4]={{1,0,0,0},
                         {0,1,0,0},
                         {0,0,1,0},
                         {0,0,0,1}};
  Identity[1][1]= cos(phi);
  Identity[1][2]= sin(phi);
  Identity[2][2]= Identity[1][1];
  Identity[2][1]= -Identity[1][2];
  MultiplyMatrix(Matrix,&Identity);

Quote:}

void TOKRightDlg::yrot(double theta,double (* Matrix)[4][4])
{
  double Identity[4][4]={{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
  Identity[0][0]= cos(theta);
  Identity[2][0]= sin(theta);
  Identity[2][2]= Identity[0][0];
  Identity[0][2]= -Identity[2][0];
  MultiplyMatrix(Matrix,&Identity);

Quote:}

void TOKRightDlg::zrot(double theta,double (* Matrix)[4][4])
{
  double Identity[4][4]={{1,0,0,0},
                         {0,1,0,0},
                         {0,0,1,0},
                         {0,0,0,1}};
  Identity[0][0]= cos(theta);
  Identity[0][1]= sin(theta);
  Identity[1][1]= Identity[0][0];
  Identity[1][0]= -Identity[0][1];
  MultiplyMatrix(Matrix,&Identity);

Quote:}

void TOKRightDlg::project(int axis,double (* Matrix)[4][4])
{
  double Identity[4][4]={{1,0,0,0},
                         {0,1,0,0},
                         {0,0,1,0},
                         {0,0,0,1}};
  switch(axis)
  {
     case 1:
       Identity[0][0]=0.0;
       break;
     case 2:
       Identity[1][1]=0.0;
       break;
     case 3:
       Identity[2][2]=0.0;
       break;
     default:
       return;
  }
  MultiplyMatrix(Matrix,&Identity);

Quote:}

void TOKRightDlg::MultiplyMatrix(double (*dest)[4][4],double (*src)[4][4])
{
  double Mat[4][4];
  for(int row=0;row<4;row++)
  {
     for(int column=0;column<4;column++)
     {
       Mat[row][column] = (*dest)[row][0] * (*src)[0][column] +
                          (*dest)[row][1] * (*src)[1][column] +
                          (*dest)[row][2] * (*src)[2][column] +
                          (*dest)[row][3] * (*src)[3][column];
     }
  }
  for(int row=0;row<4;row++)
  {
     for(int column=0;column<4;column++)
     {
       (*dest)[row][column] = Mat[row][column];
     }
  }
Quote:}

 
 
 

How do I get a 3d representation on a 2d window surface.

Post by Gernot Hoffma » Mon, 07 Jul 2003 02:41:06



> Hello,

> I have been trying to take 3d lines,vectors and project them onto a screen
> 2d surface, but can't get it right.

...

James

The isometric projection problem was discussed here recently (though more as
a monologue than a discussion).
Please search by Google, this group, one month back, "isometric projection".

Best regards --Gernot Hoffmann

 
 
 

1. Converting 3D surface to 2D surface

A coworker and I have been discussing transferring a 3D sphere shape
to paper so that a pattern can be made.

For instance how would you make a pattern for say a hot air baloon in
2D? When you assemble the pieces it becomes a 3D object.

Any thoughts??

Jeff LeBlanc

 Pursuant to US Code, Title 47, Chapter 5, Subchapter II, 227,
 any and all nonsolicited commercial E-mail sent to this address
 is subject to a download and archival fee in the amount of $500
 US.  E-mailing denotes acceptance of these terms.

2. Jaz on PC & Amiga

3. 3D surface representation

4. faq... is there one?

5. Getting a job doing 3D...

6. Using Pen/Stylus as input; Graphics scaling

7. 3D surface from 2D countours/3D Reconstruction

8. Can't open EPS files on PC

9. Doing 2D with 3D Apps

10. Getting a 3d distance from two 2d images?

11. getting 3D info from a rotated 2D rectangle

12. Getting 2D-Coordinates from 3D-Object...