Flight Club


flightclub.framework3d
Class Obj3d

java.lang.Object
  |
  +--flightclub.framework3d.Obj3d
All Implemented Interfaces:
CameraSubject
Direct Known Subclasses:
Obj3dDir

public class Obj3d
extends java.lang.Object
implements CameraSubject

This class implements a 3d object made up of polygons. We have a list of points (x, y, z) in model space and a second list of the those points after they have been mapped into screen space (x_, y_, z_). The x_ coord is handy for fogging and sorting; it represents the 'depth' of the projected point. (y_, z_) is the location of the point in screen space.

The mapping from model space to screen space is as follows: 1. A translation such that the camera focus becomes the origin. 2. A rotation such that the camera eye is on the +ve x_ axis. 3. Forshorten y_ and z_ by one over x_, the depth of the point. 4. Scale y_ and z_ according to the height of the display area in pixels. We use a flat array to store the coords as follows: (x0, y0, z0, x1, y1, z1, x2, ...). If npoints * 3 exceeds the array size then we double the array size and do a System.copyarray(). I got the flat array idea from the WireFrame demo applet.


Field Summary
static int CONCAVE
           
static int CONVEX
           
protected  ModelViewer modelViewer
           
 
Constructor Summary
Obj3d(ModelViewer modelViewer, int npolygons)
          Creates an Obj3d and registers it with the 3d object manager.
Obj3d(ModelViewer modelViewer, int npolygons, boolean register)
           
Obj3d(Obj3d from, boolean register)
          Creates a copy of obj.
Obj3d(java.io.StreamTokenizer st, ModelViewer modelViewer, boolean register)
          Parses a text file and creates an Obj3d.
 
Method Summary
 int addPolygon(float[][] vs)
          Adds a single sided gray polygon.
 int addPolygon(float[][] vs, java.awt.Color c)
          Adds a polygon with one side visible.
 int addPolygon(float[][] vs, java.awt.Color c, boolean doubleSided)
          Adds a polygon.
 int addPolygon2(float[][] vs)
          Adds a gray polygon that is visible from both sides.
 int addPolygon2(float[][] vs, java.awt.Color c)
          Adds a polygon that is visible from both sides.
 void addPolygonBent(float[][] ps, java.awt.Color color, int concaveFlag)
          Adds a bent polygon (made out of two triangles).
 void destroyMe()
           
 void draw(java.awt.Graphics g)
          Draws a 2d representation of the object onto the screen.
 float getDepthMax()
           
 float getDepthMin()
           
 float[] getEye()
           
 float[] getFocus()
           
 int getPointIndex(int poly, int vertex)
          Gets the index of a point on a polygon.
static Obj3d makeCube(ModelViewer modelViewer)
          Creates a unit cube whose centre of mass lies at the origin.
static Obj3d parseFile(java.io.InputStream is, ModelViewer modelViewer, boolean register)
          This method is a hack.
 void scaleBy(float s)
           
 void setBB()
          Creates unit axes at the origin.
 void setColor(java.awt.Color c)
          Gives all polygons the specifed color.
 void setNormals()
          Loops thru' surfaces setting their dirtyNormal flags so that the surface normals will be computed again.
 void setPoint(int index, float x, float y, float z)
          Sets the co-ords of a point.
 java.lang.String toString()
          Dumps the data of this 3d shape.
 void transform()
          Uses the camera to map all points from model space, (x, y, z) to screen space (x_, y_, z_).
 void translateBy(float dx, float dy, float dz)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

modelViewer

protected ModelViewer modelViewer

CONCAVE

public static final int CONCAVE
See Also:
Constant Field Values

CONVEX

public static final int CONVEX
See Also:
Constant Field Values
Constructor Detail

Obj3d

public Obj3d(ModelViewer modelViewer,
             int npolygons,
             boolean register)

Obj3d

public Obj3d(ModelViewer modelViewer,
             int npolygons)
Creates an Obj3d and registers it with the 3d object manager.


Obj3d

public Obj3d(Obj3d from,
             boolean register)
Creates a copy of obj.


Obj3d

public Obj3d(java.io.StreamTokenizer st,
             ModelViewer modelViewer,
             boolean register)
      throws java.io.IOException,
             FileFormatException
Parses a text file and creates an Obj3d. The file format is as follows:
 
         # a simple kite made of two triangular polygons
         # by foo bar, 27 Aug 2002
         2
         f CC99FF 12.1 2.4 3.5, 12.4 2.2 3.5, 3.2 5 7.4, 
         t 336699 9.7 0 3.5, 2.2 2.2 3.5, 3.2 5 7.3,
 
         
The above represents an Obj3d with two polygons, the second of which is double sided; each is a differnt color; each is made up of 3 points.

Method Detail

parseFile

public static Obj3d parseFile(java.io.InputStream is,
                              ModelViewer modelViewer,
                              boolean register)
                       throws java.io.IOException,
                              FileFormatException
This method is a hack. I really want another constructor.

java.io.IOException
FileFormatException

destroyMe

public void destroyMe()

draw

public void draw(java.awt.Graphics g)
Draws a 2d representation of the object onto the screen.

See Also:
ModelCanvas.paintModel()

transform

public void transform()
Uses the camera to map all points from model space, (x, y, z) to screen space (x_, y_, z_). Remember that x_ represents the depth of the point and (y_, z_) maps to the screen (x, y) co-ords of the point.

See Also:
CameraMan

translateBy

public void translateBy(float dx,
                        float dy,
                        float dz)

scaleBy

public void scaleBy(float s)

setColor

public void setColor(java.awt.Color c)
Gives all polygons the specifed color.


addPolygon

public int addPolygon(float[][] vs,
                      java.awt.Color c,
                      boolean doubleSided)
Adds a polygon. Note that the vertices of the polygon are passed using a float[][] and NOT a float[]. Eg. {{x0, y0, z0}, {x1, y1, z1}, ...} We *flatten* the data once it is encapsulated inside this class; outside this class we want clarity; inside this class we want speed !


addPolygon

public int addPolygon(float[][] vs,
                      java.awt.Color c)
Adds a polygon with one side visible. Which of the two sides is visible is determined by the order of the points. If as you look at the polygon the points go clockwise then the normal points away from you.


addPolygon

public int addPolygon(float[][] vs)
Adds a single sided gray polygon.


addPolygon2

public int addPolygon2(float[][] vs,
                       java.awt.Color c)
Adds a polygon that is visible from both sides.


addPolygon2

public int addPolygon2(float[][] vs)
Adds a gray polygon that is visible from both sides.


addPolygonBent

public void addPolygonBent(float[][] ps,
                           java.awt.Color color,
                           int concaveFlag)
Adds a bent polygon (made out of two triangles). We are passed four points that do NOT lie in a plane.
 
        p3 .-------.  p2
           |     / |
           |   /   |
           | /     |
        p0 .-------.  p1
 
        


makeCube

public static Obj3d makeCube(ModelViewer modelViewer)
Creates a unit cube whose centre of mass lies at the origin.


setBB

public void setBB()
Creates unit axes at the origin. We color code as follows: x - red, y - green, z - blue.


getEye

public float[] getEye()
Specified by:
getEye in interface CameraSubject

getFocus

public float[] getFocus()
Specified by:
getFocus in interface CameraSubject

getPointIndex

public int getPointIndex(int poly,
                         int vertex)
Gets the index of a point on a polygon.


setPoint

public void setPoint(int index,
                     float x,
                     float y,
                     float z)
Sets the co-ords of a point.


getDepthMin

public final float getDepthMin()

getDepthMax

public final float getDepthMax()

toString

public java.lang.String toString()
Dumps the data of this 3d shape.

Overrides:
toString in class java.lang.Object

setNormals

public void setNormals()
Loops thru' surfaces setting their dirtyNormal flags so that the surface normals will be computed again.

See Also:
Obj3dDir.updateRotated

Dan Burton <danb@dircon.co.uk> | latest update: 20 Sep 2002;