3D Objects (need help)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: 3D Objects (need help)

  1. #1
    Join Date
    Dec 2005
    Posts
    10

    3D Objects (need help)

    The following won't work, if anyone can find the bug, please notify me on this post. What I want to come out is a rectangle with pyramids at each end.

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.Math;

    class Point3D {
    public int x, y, z;
    public Point3D( int X, int Y, int Z ) {
    x = X; y = Y; z = Z;
    }
    }

    class Edge {
    public int a, b;
    public Edge( int A, int B ) {
    a = A; b = B;
    }
    }

    public class WireframeViewer extends Applet
    implements MouseListener, MouseMotionListener {

    int width, height;
    int mx, my;
    Image backbuffer;
    Graphics backg;

    int azimuth = 40, elevation = 35;

    Point3D[] vertices;
    Edge[] edges;

    public void init() {
    width = getSize().width;
    height = getSize().height;

    vertices = new Point3D[ 8 ];
    vertices[0] = new Point3D( -2, -1, -1 );
    vertices[1] = new Point3D( -2, -1, 1 );
    vertices[2] = new Point3D( -2, 1, -1 );
    vertices[3] = new Point3D( -2, 1, 1 );
    vertices[4] = new Point3D( 2, -1, -1 );
    vertices[5] = new Point3D( 2, -1, 1 );
    vertices[6] = new Point3D( 2, 1, -1 );
    vertices[7] = new Point3D( 2, 1, 1 );
    vertices[8] = new Point3D( 3, 0, 0 );
    vertices[9] = new Point3D( -3, 0, 0 );

    edges = new Edge[ 12 ];
    edges[ 0] = new Edge( 0, 1 );
    edges[ 1] = new Edge( 0, 2 );
    edges[ 2] = new Edge( 0, 4 );
    edges[ 3] = new Edge( 1, 3 );
    edges[ 4] = new Edge( 1, 5 );
    edges[ 5] = new Edge( 2, 3 );
    edges[ 6] = new Edge( 2, 6 );
    edges[ 7] = new Edge( 3, 7 );
    edges[ 8] = new Edge( 4, 5 );
    edges[ 9] = new Edge( 4, 6 );
    edges[10] = new Edge( 5, 7 );
    edges[11] = new Edge( 6, 7 );
    edges[12] = new Edge( 0, 9 );
    edges[13] = new Edge( 1, 9 );
    edges[14] = new Edge( 2, 9 );
    edges[15] = new Edge( 3, 9 );
    edges[16] = new Edge( 4, 8 );
    edges[17] = new Edge( 5, 8 );
    edges[18] = new Edge( 6, 8 );
    edges[19] = new Edge( 7, 8 );

    backbuffer = createImage( width, height );
    backg = backbuffer.getGraphics();
    drawWireframe( backg );

    addMouseListener( this );
    addMouseMotionListener( this );
    }

    void drawWireframe( Graphics g ) {

    double theta = Math.PI * azimuth / 180.0;
    double phi = Math.PI * elevation / 180.0;
    float cosT = (float)Math.cos( theta ), sinT = (float)Math.sin( theta );
    float cosP = (float)Math.cos( phi ), sinP = (float)Math.sin( phi );
    float cosTcosP = cosT*cosP, cosTsinP = cosT*sinP,
    sinTcosP = sinT*cosP, sinTsinP = sinT*sinP;

    Point[] points;
    points = new Point[ vertices.length ];
    int j;
    int scaleFactor = width/4;
    float near = 3; // distance from eye to near plane
    float nearToObj = 1.5f; // distance from near plane to center of object
    for ( j = 0; j < vertices.length; ++j ) {
    int x0 = vertices[j].x;
    int y0 = vertices[j].y;
    int z0 = vertices[j].z;

    float x1 = cosT*x0 + sinT*z0;
    float y1 = -sinTsinP*x0 + cosP*y0 + cosTsinP*z0;

    float z1 = cosTcosP*z0 - sinTcosP*x0 - sinP*y0;
    x1 = x1*near/(z1+near+nearToObj);
    y1 = y1*near/(z1+near+nearToObj);

    points[j] = new Point(
    (int)(width/2 + scaleFactor*x1 + 0.5),
    (int)(height/2 - scaleFactor*y1 + 0.5)
    );
    }

    g.setColor( Color.black );
    g.fillRect( 0, 0, width, height );
    g.setColor( Color.white );
    for ( j = 0; j < edges.length; ++j ) {
    g.drawLine(
    points[ edges[j].a ].x, points[ edges[j].a ].y,
    points[ edges[j].b ].x, points[ edges[j].b ].y
    );
    }
    }

    public void mouseEntered( MouseEvent e ) { }
    public void mouseExited( MouseEvent e ) { }
    public void mouseClicked( MouseEvent e ) { }
    public void mousePressed( MouseEvent e ) {
    mx = e.getX();
    my = e.getY();
    e.consume();
    }
    public void mouseReleased( MouseEvent e ) { }
    public void mouseMoved( MouseEvent e ) { }
    public void mouseDragged( MouseEvent e ) {
    int new_mx = e.getX();
    int new_my = e.getY();

    azimuth -= new_mx - mx;
    elevation += new_my - my;

    drawWireframe( backg );

    mx = new_mx;
    my = new_my;

    repaint();
    e.consume();
    }

    public void update( Graphics g ) {
    g.drawImage( backbuffer, 0, 0, this );
    showStatus("Elev: "+elevation+" deg, Azim: "+azimuth+" deg");
    }

    public void paint( Graphics g ) {
    update( g );
    }
    }

  2. #2
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Why wont it work mate? What errors are you getting?

  3. #3
    Join Date
    Dec 2005
    Posts
    10

    Line 8

    -=Line 8 not in bounds=-

    The code should just make it so that the screen resizes and fits the object in there.

  4. #4
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Code:
    vertices = new Point3D[ 8 ];
    vertices[0] = new Point3D( -2, -1, -1 );
    vertices[1] = new Point3D( -2, -1, 1 );
    vertices[2] = new Point3D( -2, 1, -1 );
    vertices[3] = new Point3D( -2, 1, 1 );
    vertices[4] = new Point3D( 2, -1, -1 );
    vertices[5] = new Point3D( 2, -1, 1 );
    vertices[6] = new Point3D( 2, 1, -1 );
    vertices[7] = new Point3D( 2, 1, 1 );
    vertices[8] = new Point3D( 3, 0, 0 );
    vertices[9] = new Point3D( -3, 0, 0 );
    
    edges = new Edge[ 12 ];
    edges[ 0] = new Edge( 0, 1 );
    edges[ 1] = new Edge( 0, 2 );
    edges[ 2] = new Edge( 0, 4 );
    edges[ 3] = new Edge( 1, 3 );
    edges[ 4] = new Edge( 1, 5 );
    edges[ 5] = new Edge( 2, 3 );
    edges[ 6] = new Edge( 2, 6 );
    edges[ 7] = new Edge( 3, 7 );
    edges[ 8] = new Edge( 4, 5 );
    edges[ 9] = new Edge( 4, 6 );
    edges[10] = new Edge( 5, 7 );
    edges[11] = new Edge( 6, 7 );
    edges[12] = new Edge( 0, 9 );
    edges[13] = new Edge( 1, 9 );
    edges[14] = new Edge( 2, 9 );
    edges[15] = new Edge( 3, 9 );
    edges[16] = new Edge( 4, 8 );
    edges[17] = new Edge( 5, 8 );
    edges[18] = new Edge( 6, 8 );
    edges[19] = new Edge( 7, 8 );
    That could be the problem... the arrays' lengths aren't long enough.
    change:
    Code:
    vertices = new Point3D[ 8 ];
    to:
    Code:
    vertices = new Point3D[ 10 ];
    and change:
    Code:
    edges = new Edge[ 12 ];
    to:
    Code:
    edges = new Edge[ 20 ];
    hope that helps

  5. #5
    Join Date
    Dec 2005
    Posts
    10
    Worked like a charm, thank you.

    I'm suprised I didn't see that...

    Now, how would one color in a side of that figure?

Similar Threads

  1. Bug with Creation of Objects in SQL *Plus Worksheet
    By Michael Cole in forum Database
    Replies: 2
    Last Post: 04-02-2003, 03:06 PM
  2. Bug with Creation of Objects in SQL *Plus Worksheet
    By Michael Cole in forum oracle.general
    Replies: 0
    Last Post: 02-20-2003, 10:25 PM
  3. Analogy between Beans and Activated Objects
    By Peter Leong in forum .NET
    Replies: 5
    Last Post: 05-21-2002, 08:02 AM
  4. Thin Objects for Better Performance
    By Dave Fleischman in forum Enterprise
    Replies: 3
    Last Post: 08-18-2000, 01:10 PM
  5. Components vs Objects
    By Oliver Lennon in forum Enterprise
    Replies: 3
    Last Post: 04-18-2000, 12:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center