How to draw line in 3D

Started by jumong, September 07, 2010, 03:30:43 PM

Previous topic - Next topic

jumong

I wanna draw line in 3D
How can I do
help

EgonOlsen

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?

paulscode

#2
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):


// 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).

jumong