1

I designed a simple java program viewing 3D Cube, but i do not why i guess there is something illogically in that.

the length of rib = 350 the front square depth (z axis) is 0

the back square depth (z axis) is equal to the length of rib,

but the result was very tall cube !!:

I designed a simple java program viewing 3D Cube, but i do not why i guess there is something illogically in that.

the length of rib = 350 the front square depth (z axis) is 0

the back square depth (z axis) is equal to the length of rib, but the result was very tall cube !!:

enter image description here

The code:

public void paint(Graphics g){
    super.paint(g);

    int squareLengthRib  = 350; // Ribs length
    int frontSquareDepth = 0;   // Z value (Depth of front square)
    int backSquareDepth  = frontSquareDepth + squareLengthRib; // Z value (Depth of back square)

    x1 = 300;
    y1 = 300;
    z1 = frontSquareDepth;

    x2 = x1 + squareLengthRib;
    y2 = y1 ;
    z2 = z1;

    x3 = x2;
    y3 = y2 + squareLengthRib;
    z3 = z2;

    x4 = x1;
    y4 = y3;
    z4 = z3;

    /**********************************************/

    x5 = x1;
    y5 = y1;
    z5 = backSquareDepth;

    x6 = x2;
    y6 = y2;
    z6 = z5;

    x7 = x3;
    y7 = y3;
    z7 = z6;

    x8 = x4;
    y8 = y4;
    z8 = z7;

    PerspectiveProjection();

    g.drawLine((int)x1, (int)y1, (int)x2, (int)y2);
    g.drawLine((int)x2, (int)y2, (int)x3, (int)y3);
    g.drawLine((int)x3, (int)y3, (int)x4, (int)y4);
    g.drawLine((int)x4, (int)y4, (int)x1, (int)y1);

    g.drawLine((int)x5, (int)y5, (int)x6, (int)y6);
    g.drawLine((int)x6, (int)y6, (int)x7, (int)y7);
    g.drawLine((int)x7, (int)y7, (int)x8, (int)y8);
    g.drawLine((int)x8, (int)y8, (int)x5, (int)y5);

    g.drawLine((int)x1, (int)y1, (int)x5, (int)y5);
    g.drawLine((int)x2, (int)y2, (int)x6, (int)y6);
    g.drawLine((int)x3, (int)y3, (int)x7, (int)y7);
    g.drawLine((int)x4, (int)y4, (int)x8, (int)y8);



}

private void PerspectiveProjection() {
    double d = 100;
    double xd, yd;

    xd = (x1 * d) / (z1 + d); 
    yd = (y1 * d) / (z1 + d);
    x1 = xd;
    y1 = yd;

    xd = (x2 * d) / (z2 + d); 
    yd = (y2 * d) / (z2 + d);
    x2 = xd;
    y2 = yd;

    xd = (x3 * d) / (z3 + d); 
    yd = (y3 * d) / (z3 + d);
    x3 = xd;
    y3 = yd;

    xd = (x4 * d) / (z4 + d); 
    yd = (y4 * d) / (z4 + d);
    x4 = xd;
    y4 = yd;

    xd = (x5 * d) / (z5 + d); 
    yd = (y5 * d) / (z5 + d);
    x5 = xd;
    y5 = yd;

    xd = (x6 * d) / (z6 + d); 
    yd = (y6 * d) / (z6 + d);
    x6 = xd;
    y6 = yd;

    xd = (x7 * d) / (z7 + d); 
    yd = (y7 * d) / (z7 + d);
    x7 = xd;
    y7 = yd;

    xd = (x8 * d) / (z8 + d); 
    yd = (y8 * d) / (z8 + d);
    x8 = xd;
    y8 = yd;
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Why do you multiply by `d` then divide by `z + d`? I don't think that's what the perspective transform and divide boil down to, but anyhow please give the source of this method. – meowgoesthedog Jan 18 '18 at 22:14
  • DON'T change anything! I like the picture... ;) – zlakad Jan 18 '18 at 22:21
  • https://www.youtube.com/watch?v=3jmiXZlacx0 this is Indian teacher explaining the perspective projection and I used idea to make that cube. – Mohammed K. Abushawish Jan 19 '18 at 03:21
  • 1
    What is your point of view? – MBo Jan 19 '18 at 05:20
  • 1
    check against this [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) the C++ example there does exactly what you do .... the perspective division is a bit different there – Spektre Jan 19 '18 at 08:19
  • Really I'm a new programmer in the 3d graphics approach so some of terms like these, point of view and others I don't know what does it mean!! – Mohammed K. Abushawish Jan 19 '18 at 16:40
  • Thanks @Spectre for your post, but I tried to understand that and unfortunately I failed. According to your experience in 3d Graphics, How can I represent (X, Y, Z) existing the space on 2D panel? – Mohammed K. Abushawish Jan 19 '18 at 16:45
  • To notify user you need to have the nick correctly spelled :) . After applying all the transforms you simply ignore `z` coordinate and use only `x,y` if `z` coordinate passes depth test. For wire frame there is usually none but the z coordinate (in camera space) should be positive or negative (depends what you want to have as viewing direction). So if point is behind camera either ignore such line or clip it by `z_near` plane – Spektre Jan 22 '18 at 09:55

0 Answers0