glmodel
Class GL_Mesh

java.lang.Object
  extended by glmodel.GL_Mesh

public class GL_Mesh
extends java.lang.Object

Holds a mesh object containing triangles and vertices. Verts and Triangles are stored as ArrayLists for flexibility and also as arrays for speedier processing. The rebuild() function converts the ArrayLists to arrays, assigns neighbor triangles to each vert, and recalculates normals. Handles normal smoothing, and preserves sharp edges. see projectVerts() for setting screen positions (Zdepth) of verts see sortTriangles() for Z depth sorting see regenerateNormals() to calculate smoothed normals oct 2008: added funcs to handle groups sep 2008: changed gluProject() params due to lwjgl 2.0 changes jun 2006: add makeclone()


Field Summary
 java.lang.String materialLibeName
           
 GLMaterial[] materials
           
 java.lang.String name
           
 int numTriangles
           
 int numVertices
           
static int SIZE_FLOAT
           
 java.util.ArrayList triangleData
           
 GL_Triangle[] triangles
           
 java.util.ArrayList vertexData
           
 GL_Vertex[] vertices
           
 
Constructor Summary
GL_Mesh()
           
 
Method Summary
 void addTriangle(GL_Triangle newTriangle)
           
 void addTriangle(GL_Triangle newTriangle, int groupNum, int triangleNum)
          add triangle to given group.
 void addTriangle(GL_Vertex a, GL_Vertex b, GL_Vertex c)
           
 void addTriangle(int v1, int v2, int v3)
           
 void addVertex(float x, float y, float z)
           
 void addVertex(GL_Vertex newVertex)
           
static java.nio.FloatBuffer allocFloats(int howmany)
           
 void calcZDepths()
          Calculate the average Z depth of each triangle.
 GL_Vector getCenter()
          Return the center point of the mesh.
 GL_Vector getDimension()
          Returns the dimensions of this mesh.
 GL_Triangle[] getGroupFaces(int g)
           
 java.lang.String getGroupMaterialName(int g)
           
 java.lang.String getGroupName(int g)
           
 GL_Triangle getTriangle(float x, float y)
          Return the front-most triangle that contains the given screen x,y position, or null if no triangle contains the x,y position.
static GL_Triangle getTriangle(float x, float y, GL_Triangle[] _triangles)
          Given a screen point and a triangles array, return the front-most triangle that contains the given screen x,y position, or null if no triangle contains the x,y position.
 GL_Vertex[] getVertexArray()
          return the vertex array
 void initGroup(int groupNum, java.lang.String name, java.lang.String materialName, int numTriangles)
          allocate triangle array for a group.
 GL_Mesh makeClone()
           
 void makeGroups(int num)
          Allocate arrays to hold groups.
 GL_Vector max()
          Return maximum point in the mesh.
 GL_Vector min()
          Return minimum point in the mesh.
 int numGroups()
           
static boolean pointInTriangle(float px, float py, float x1, float y1, float x2, float y2, float x3, float y3)
          Return true if the point px,py is inside the triangle created by the three triangle coordinates.
 void projectVerts(GL_Mesh obj, java.nio.FloatBuffer modelMatrix, java.nio.FloatBuffer projectionMatrix, java.nio.IntBuffer viewport)
          "Project" the verts to find their screen coords.
 void rebuild()
          Copy vertex and triangle data into arrays for faster access.
 void regenerateNormals()
          Recalculate normals for each vertex in each triangle.
 void registerSmoothNeighbors(java.util.ArrayList neighborTris, GL_Vertex v, GL_Triangle t)
          For the given Vert in the given Triangle, make a list of triangles that are neighbors to the given Triangle.
 void removeTriangle(GL_Triangle t)
           
 void removeTriangleAt(int pos)
           
 void removeVertex(GL_Vertex v)
           
 void removeVertexAt(int pos)
           
 void selectGroup(int g)
           
 void setSmoothingAngle(float maxSmoothingAngle)
          the smoothing angle determines how smooth or faceted a model will appear.
 void sortTriangles()
          Z sort the triangles of this mesh.
 GL_Triangle[] sortTriangles(GL_Triangle[] tri, int L, int R)
          Z sort the given triangle array.
 java.lang.String toString()
           
 GL_Triangle triangle(int id)
           
 GL_Vertex vertex(int id)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

vertexData

public java.util.ArrayList vertexData

triangleData

public java.util.ArrayList triangleData

vertices

public GL_Vertex[] vertices

triangles

public GL_Triangle[] triangles

numVertices

public int numVertices

numTriangles

public int numTriangles

name

public java.lang.String name

materialLibeName

public java.lang.String materialLibeName

materials

public GLMaterial[] materials

SIZE_FLOAT

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

GL_Mesh

public GL_Mesh()
Method Detail

setSmoothingAngle

public void setSmoothingAngle(float maxSmoothingAngle)
the smoothing angle determines how smooth or faceted a model will appear. If the angle between the normals of two neighboring faces is greater than the smoothing angle then the faces will be smoothed (the vertex normals at the edge of the faces will be averaged.
               N1        example: the angle between face normals 1 and 2
              |                   is 90 degrees.  If maxSmoothingAngle is 80
         _____|___                these faces will have a hard edge.  If
      face1       |___ N2         maxSmoothingAngle is 100 they will be smoothed.
                  |
                  |
              face2
 

Parameters:
maxSmoothingAngle -

vertex

public GL_Vertex vertex(int id)

triangle

public GL_Triangle triangle(int id)

addVertex

public void addVertex(GL_Vertex newVertex)

addTriangle

public void addTriangle(GL_Triangle newTriangle)

addTriangle

public void addTriangle(int v1,
                        int v2,
                        int v3)

addTriangle

public void addTriangle(GL_Vertex a,
                        GL_Vertex b,
                        GL_Vertex c)

removeVertex

public void removeVertex(GL_Vertex v)

removeTriangle

public void removeTriangle(GL_Triangle t)

removeVertexAt

public void removeVertexAt(int pos)

removeTriangleAt

public void removeTriangleAt(int pos)

rebuild

public void rebuild()
Copy vertex and triangle data into arrays for faster access. Find the neighbor triangles for each vertex. This data should not change once the mesh is loaded, so we call rebuild() only when the object is imported.


registerSmoothNeighbors

public void registerSmoothNeighbors(java.util.ArrayList neighborTris,
                                    GL_Vertex v,
                                    GL_Triangle t)
For the given Vert in the given Triangle, make a list of triangles that are neighbors to the given Triangle. Only count as neighbors those triangles that form a smooth surface with this triangle, meaning the angle between this triangle and the neighbor triangle is > 90 degrees (the actual min degrees value is in cos_angle). Requires that rebuild() has been run so that the vertex has a list of neighbor triangles populated (see addNeighborTri()), and the triangle face normals have been calculated. (see GL_Triangle.regenerateNormal()).


addVertex

public void addVertex(float x,
                      float y,
                      float z)

regenerateNormals

public void regenerateNormals()
Recalculate normals for each vertex in each triangle. This allows a vertex to have a different normal for each triangle it's in (so we can have sharp edges or smooth surfaces). Requires that neighoring triangles have already been set.

See Also:
rebuild(), registerNeighbors(), setSmoothingAngle()

min

public GL_Vector min()
Return minimum point in the mesh.


max

public GL_Vector max()
Return maximum point in the mesh.


getCenter

public GL_Vector getCenter()
Return the center point of the mesh.


getDimension

public GL_Vector getDimension()
Returns the dimensions of this mesh.


getVertexArray

public GL_Vertex[] getVertexArray()
return the vertex array


projectVerts

public void projectVerts(GL_Mesh obj,
                         java.nio.FloatBuffer modelMatrix,
                         java.nio.FloatBuffer projectionMatrix,
                         java.nio.IntBuffer viewport)
"Project" the verts to find their screen coords. Store these screen coords in the vertex.posS vector. GLU.gluProject() will create screen x,y coords and a z depth value between 0 and 1. NOTE: sept2008: lwjgl 2.0 changed args for GLU.gluProject()

See Also:
sortTriangles()

calcZDepths

public void calcZDepths()
Calculate the average Z depth of each triangle. Used by sortTriangles() below.


sortTriangles

public void sortTriangles()
Z sort the triangles of this mesh. Call projectVerts() and calcZDepths() first to set correct Z depth of all verts and triangles.


sortTriangles

public GL_Triangle[] sortTriangles(GL_Triangle[] tri,
                                   int L,
                                   int R)
Z sort the given triangle array. Call projectVerts() first to set correct Z depth of all verts and triangles.


getTriangle

public GL_Triangle getTriangle(float x,
                               float y)
Return the front-most triangle that contains the given screen x,y position, or null if no triangle contains the x,y position. Requires that projectVerts() has been run to populate the vertex screen positions.

Parameters:
x - cursor x (screen coordinates)
y - cursor y (screen coordinates)
Returns:
the triangle at the cursor x,y
See Also:
projectVerts()

getTriangle

public static GL_Triangle getTriangle(float x,
                                      float y,
                                      GL_Triangle[] _triangles)
Given a screen point and a triangles array, return the front-most triangle that contains the given screen x,y position, or null if no triangle contains the x,y position. Requires that projectVerts() has been run to populate the vertex screen positions.

Parameters:
x - cursor x (screen coordinates)
y - cursor y (screen coordinates)
Returns:
the triangle at the cursor x,y
See Also:
projectVerts()

pointInTriangle

public static boolean pointInTriangle(float px,
                                      float py,
                                      float x1,
                                      float y1,
                                      float x2,
                                      float y2,
                                      float x3,
                                      float y3)
Return true if the point px,py is inside the triangle created by the three triangle coordinates.


makeClone

public GL_Mesh makeClone()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

allocFloats

public static java.nio.FloatBuffer allocFloats(int howmany)

numGroups

public int numGroups()

getGroupName

public java.lang.String getGroupName(int g)

getGroupMaterialName

public java.lang.String getGroupMaterialName(int g)

getGroupFaces

public GL_Triangle[] getGroupFaces(int g)

selectGroup

public void selectGroup(int g)

makeGroups

public void makeGroups(int num)
Allocate arrays to hold groups. The GL_OBJ_Importer calls this once, after it has loaded the OBJ and knows how many groups it has. Use initGroups() to allocate triangle arrays for each group.

Parameters:
num - number of groups to allocate
See Also:
initGroup()

initGroup

public void initGroup(int groupNum,
                      java.lang.String name,
                      java.lang.String materialName,
                      int numTriangles)
allocate triangle array for a group. Also set name and material name.

Parameters:
groupNum - number of group to init
name - group name
materialName - group's material
numTriangles - number of triangles in the group
See Also:
makeGroups()

addTriangle

public void addTriangle(GL_Triangle newTriangle,
                        int groupNum,
                        int triangleNum)
add triangle to given group. used by Importer to load mesh groups. Groups had to be allocated first by makeGroups() and inited with initGroup().

Parameters:
newTriangle -
groupNum -
triangleNum -
See Also:
makeGroups(), setGroup()