Author Topic: How to draw line in 3D  (Read 3287 times)

Offline jumong

  • byte
  • *
  • Posts: 19
    • View Profile
How to draw line in 3D
« on: September 07, 2010, 03:30:43 pm »
I wanna draw line in 3D
How can I do
help

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: How to draw line in 3D
« Reply #1 on: September 07, 2010, 09:18:28 pm »
That's simple: You can't! You might be able to emulate it with a simple Object3D, like i did in this thread to draw outlines: http://www.jpct.net/forum2/index.php/topic,1656.0.html

But maybe there's a better solution...what do you need these lines for?

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: How to draw line in 3D
« Reply #2 on: September 07, 2010, 10:00:31 pm »
A "quick and dirty" way of drawing lines is with perpendicular intersecting quads.  Note that these "lines" will have an apparent width, i.e. they appear thicker when close to the viewer and thinner when further away.  Also note that if the line is super-long the ordering may turn out incorrect (in which case dividing it into smaller line-segments should work better).  Here is one possible way to create such a line.  I'm writing it off the top of my head, so there may be typos, but it should be enough to give you the general idea.  Also, for jPCT AE, you'll need to use whatever is the equivalent to java.awt.Color (I haven't written anything for the AE version yet):

Code: [Select]
// note: you will need to define a texture ahead of time.  For a solid-color line, you could use something like:
// TextureManager.getInstance().addTexture( "Red", new Texture( 8, 8, java.awt.Color.Red ) );

public Object3D createLine (SimpleVector pointA, SimpleVector pointB, float width, String textureName)
{
    Object3D line = new Object3D( 8 );
    float offset = width / 2.0f;

    // Quad A:
    line.addTriangle( new SimpleVector( pointA.x, pointA.y - offset, pointA.z ), 0, 0,
                     new SimpleVector( pointA.x, pointA.y + offset, pointA.z ), 0, 1,
                     new SimpleVector( pointB.x, pointB.y + offset, pointB.z ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    line.addTriangle( new SimpleVector( pointB.x, pointB.y + offset, pointB.z ), 0, 0,
                     new SimpleVector( pointB.x, pointB.y - offset, pointB.z ), 0, 1,
                     new SimpleVector( pointA.x, pointA.y - offset, pointA.z ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    // Quad A, back-face:
    line.addTriangle( new SimpleVector( pointB.x, pointB.y - offset, pointB.z ), 0, 0,
                     new SimpleVector( pointB.x, pointB.y + offset, pointB.z ), 0, 1,
                     new SimpleVector( pointA.x, pointA.y + offset, pointA.z ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    line.addTriangle( new SimpleVector( pointA.x, pointA.y + offset, pointA.z ), 0, 0,
                     new SimpleVector( pointA.x, pointA.y - offset, pointA.z ), 0, 1,
                     new SimpleVector( pointB.x, pointB.y - offset, pointB.z ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    // Quad B:
    line.addTriangle( new SimpleVector( pointA.x, pointA.y, pointA.z + offset ), 0, 0,
                     new SimpleVector( pointA.x, pointA.y, pointA.z - offset ), 0, 1,
                     new SimpleVector( pointB.x, pointB.y, pointB.z - offset ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    line.addTriangle( new SimpleVector( pointB.x, pointB.y, pointB.z - offset ), 0, 0,
                     new SimpleVector( pointB.x, pointB.y, pointB.z + offset ), 0, 1,
                     new SimpleVector( pointA.x, pointA.y, pointA.z + offset ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    // Quad B, back-face:
    line.addTriangle( new SimpleVector( pointB.x, pointB.y, pointB.z + offset ), 0, 0,
                     new SimpleVector( pointB.x, pointB.y, pointB.z - offset ), 0, 1,
                     new SimpleVector( pointA.x, pointA.y, pointA.z - offset ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );
    line.addTriangle( new SimpleVector( pointA.x, pointA.y, pointA.z - offset ), 0, 0,
                     new SimpleVector( pointA.x, pointA.y, pointA.z + offset ), 0, 1,
                     new SimpleVector( pointB.x, pointB.y, pointB.z + offset ), 1, 1,
                     TextureManager.getInstance().getTextureID( textureName ) );

    // If you don't want the line to react to lighting:
    line.setLighting( Object3D.LIGHTING_NO_LIGHTS );
    line.setAdditionalColor( java.awt.Color.White );

    // done
    return line;
}

Upon consideration, this method might also have trouble with lines that run more or less straight along the y-axis when viewed from the z-axis, and vice-versa.  A better way would be to create a line with the proper length along say the x-axis, and then rotate/translate it into place (I'm not ambitious enough to write that at the moment).
« Last Edit: September 07, 2010, 10:05:21 pm by paulscode »

Offline jumong

  • byte
  • *
  • Posts: 19
    • View Profile
Re: How to draw line in 3D
« Reply #3 on: September 08, 2010, 06:03:02 am »
Thank you for you help !!!