com.jmex.subdivision
Class Subdivision

java.lang.Object
  extended by com.jmex.subdivision.Subdivision
Direct Known Subclasses:
SubdivisionButterfly

public abstract class Subdivision
extends java.lang.Object

An abstract class for subdivision of surfaces.
Implementing classes must implement prepare() and doSubdivide() and preferrably override computeNormals(TriMesh batch)

Usage of subclass SubdivisionButterfly:

TriMesh mesh = {some trimesh};
Subdivision subdivision = new SubdivisionButterfly(mesh.getBatch(0)); // prepare for subdivision
subdivision.subdivide(); // subdivide
subdivision.apply(); // Applies the new subdivided buffers to the batch
subdivision.computeNormals(); // calculate new normals


Or you can use it without giving it a batch:

Subdivision subdivision = new SubdivisionButterfly();
subdivision.setVertexBuffer(batch.getVertexBuffer());
subdivision.setIndexBuffer(batch.getIndexBuffer());
subdivision.addToBufferList(batch.getTextureBuffer(0), Subdivision.BufferType.TEXTUREBUFFER);
subdivision.addToBufferList(batch.getTextureBuffer(1), Subdivision.BufferType.TEXTUREBUFFER);
subdivision.addToBufferList(batch.getColorBuffer(), Subdivision.BufferType.COLORBUFFER);
subdivision.subdivide(); // subdivide
subdivision.apply(mesh.getBatch(0)); // Applies the new subdivided buffers to the batch
subdivision.computeNormals(mesh.getBatch(0)); // calculate new normals

Author:
Tobias (tobbe.a removethisoryourclientgoesape gmail.com)

Nested Class Summary
static class Subdivision.BufferType
          Buffer types.
protected  class Subdivision.SubdivisionBuffer
           
 
Field Summary
protected  java.util.ArrayList<Subdivision.SubdivisionBuffer> buffers
           
protected  java.nio.IntBuffer indexBuffer
           
protected  java.util.ArrayList<Subdivision.SubdivisionBuffer> newBuffers
           
protected  java.nio.IntBuffer newIndexBuffer
           
protected  java.nio.FloatBuffer newVertexBuffer
           
protected  java.nio.FloatBuffer vertexBuffer
           
 
Constructor Summary
Subdivision()
          Constructor for Subdivision
Subdivision(java.nio.FloatBuffer vertexBuffer, java.nio.IntBuffer indexBuffer)
          Constructor for Subdivision
Subdivision(TriMesh batch)
          Constructor for Subdivision
 
Method Summary
 void addToBufferList(java.nio.FloatBuffer buffer, int elemSize, boolean linear, Subdivision.BufferType type)
          Add a buffer to the list of buffers who are to be subdivided
 void addToBufferList(java.nio.FloatBuffer buffer, Subdivision.BufferType type)
          Add a buffer to the list of buffers who are to be subdivided
 boolean apply()
          Applies the buffers to the set batch
 boolean apply(TriMesh batch)
          Applies the buffers to the given batch
 void clearBufferList()
          Clear the list of buffers to be interpolated
 void computeNormals()
          Computes the normals for the set batch
 void computeNormals(TriMesh batch)
          Computes normals for the given batch.
protected abstract  boolean doSubdivide()
          The actual subdivision, places the new buffers in the members newVertexBuffer, newIndexBuffer, newBuffers
 TriMesh getBatch()
           
 java.nio.IntBuffer getIndexBuffer()
           
 java.nio.FloatBuffer getVertexBuffer()
           
 int getVertexCount()
          Get the vertex count of the set vertex buffer (capacity / 3)
 boolean isValid()
          Checks whether the buffers submitted to this Subdivision are valid (i.e.
abstract  boolean prepare()
          Whichever preparations are needed are to be performed here
 void removeBuffer(java.nio.FloatBuffer buffer)
          Removes a buffer from the bufferlist
 void setBatch(TriMesh batch)
          Sets a batch that this Subdivision should subdivide.
 void setIndexBuffer(java.nio.IntBuffer indexBuffer)
           
 void setVertexBuffer(java.nio.FloatBuffer vertexBuffer)
           
 boolean subdivide()
          Performs the subdivision after checking that everything is ok
 void unsetBatch()
          Unsets the batch.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

vertexBuffer

protected java.nio.FloatBuffer vertexBuffer

newVertexBuffer

protected java.nio.FloatBuffer newVertexBuffer

indexBuffer

protected java.nio.IntBuffer indexBuffer

newIndexBuffer

protected java.nio.IntBuffer newIndexBuffer

buffers

protected java.util.ArrayList<Subdivision.SubdivisionBuffer> buffers

newBuffers

protected java.util.ArrayList<Subdivision.SubdivisionBuffer> newBuffers
Constructor Detail

Subdivision

public Subdivision()
Constructor for Subdivision


Subdivision

public Subdivision(java.nio.FloatBuffer vertexBuffer,
                   java.nio.IntBuffer indexBuffer)
Constructor for Subdivision

Parameters:
vertexBuffer - The vertex buffer to use when subdividing. Specify other buffers (normals,colors,texcoords) with addToBufferList
indexBuffer - The index buffer to use when subdividing

Subdivision

public Subdivision(TriMesh batch)
Constructor for Subdivision

Parameters:
batch - The TriMesh that are to be subdivided
Method Detail

prepare

public abstract boolean prepare()
Whichever preparations are needed are to be performed here

Returns:
true if the preparation succeeded

doSubdivide

protected abstract boolean doSubdivide()
The actual subdivision, places the new buffers in the members newVertexBuffer, newIndexBuffer, newBuffers

Returns:
true if the subdivision succeeded

setBatch

public void setBatch(TriMesh batch)
Sets a batch that this Subdivision should subdivide. Adds color buffer and texture buffers to the Subdivision if they are present in the batch. You can also add buffers manually with addToBufferList(FloatBuffer buffer, int elemSize, boolean linear) if you don't use this method. Removes all other buffers. Replaces a previous batch. Normals are not interpolated by default, since it gives poor results, call computeNormals() after subdividing instead. However, to interpolate the normals as well, just call addToBufferList() with the normal buffer after calling this method.

Parameters:
batch - The batch to set.

subdivide

public boolean subdivide()
Performs the subdivision after checking that everything is ok

Returns:
true if the subdivision succeeded

apply

public boolean apply()
Applies the buffers to the set batch

Returns:
true if applied

apply

public boolean apply(TriMesh batch)
Applies the buffers to the given batch

Parameters:
batch - The batch to apply the buffers to
Returns:
true if applied

computeNormals

public void computeNormals()
Computes the normals for the set batch


computeNormals

public void computeNormals(TriMesh batch)
Computes normals for the given batch. Taken from com.jmex.model.XMLparser.Converters.AseToJme.java (and optimized). Should be overridden since there are methods for each subdivision scheme to compute new normals, which give better results.

Parameters:
batch -

unsetBatch

public void unsetBatch()
Unsets the batch. Does not clear the buffer lists.


addToBufferList

public void addToBufferList(java.nio.FloatBuffer buffer,
                            int elemSize,
                            boolean linear,
                            Subdivision.BufferType type)
Add a buffer to the list of buffers who are to be subdivided

Parameters:
buffer - The buffer to be added
elemSize - The size of the elements in the buffer (e.g. 3 for normal buffers, 2 for texture buffers, 4 for color buffers)
linear - Whether or not the buffer should be linearly interpolated (for example, it doesn't make sense to use higher-order interpolation on texture coordinates). Should probably be false in most cases
type - The type of buffer, needed when applying buffers back to the batch

addToBufferList

public void addToBufferList(java.nio.FloatBuffer buffer,
                            Subdivision.BufferType type)
Add a buffer to the list of buffers who are to be subdivided

Parameters:
buffer - The buffer to be added
type - The type of buffer, needed when applying buffers back to the batch

removeBuffer

public void removeBuffer(java.nio.FloatBuffer buffer)
Removes a buffer from the bufferlist

Parameters:
buffer - The buffer to remove from the list of buffers to be subdivided

clearBufferList

public void clearBufferList()
Clear the list of buffers to be interpolated


isValid

public boolean isValid()
Checks whether the buffers submitted to this Subdivision are valid (i.e. if they have as many elements as there are vertices in the vertexbuffer)

Returns:
true if valid

getBatch

public TriMesh getBatch()
Returns:
Returns the batch.

getIndexBuffer

public java.nio.IntBuffer getIndexBuffer()
Returns:
Returns the indexBuffer.

setIndexBuffer

public void setIndexBuffer(java.nio.IntBuffer indexBuffer)
Parameters:
indexBuffer - The indexBuffer to set.

getVertexBuffer

public java.nio.FloatBuffer getVertexBuffer()
Returns:
Returns the vertexBuffer.

setVertexBuffer

public void setVertexBuffer(java.nio.FloatBuffer vertexBuffer)
Parameters:
vertexBuffer - The vertexBuffer to set.

getVertexCount

public int getVertexCount()
Get the vertex count of the set vertex buffer (capacity / 3)

Returns:
The vertex count