com.microcrowd.loader.java3d.max3ds
Class ChunkChopper

java.lang.Object
  extended by com.microcrowd.loader.java3d.max3ds.ChunkChopper

public class ChunkChopper
extends java.lang.Object

A singleton flyweight factory responsible for chopping the data up and sending it to the corresponding chunks(which are flyweights ala the flyweight pattern) for processing. This will sequentially read a 3ds file, load or skip chunks and subchunks and initialize the data for the chunks.

Retrieved data may be stored as state in the ChunkChopper via pushData(java.lang.Object, java.lang.Object) for use by other chunks.

Features not supported; unknown chunks are skipped.


Field Summary
static boolean debug
          This should be turned on by Loader3DS to view debugging information.
static Chunk debugChunk
          Current chunk for which debugging info is viewed if debug == true
 
Constructor Summary
ChunkChopper()
          private singleton constructor.
 
Method Summary
 void addBehaviorNode(Behavior behavior)
          Adds a behavior to the scene base.
 void addLightNode(Light light)
          Adds a light to the scene base.
 void addObject(java.lang.String name, TransformGroup group)
          Adds a group to the choppers scene group and sets the current name and group.
 void addViewGroup(TransformGroup viewGroup)
          Adds a camera transform to the scene base.
 Texture createTexture(java.lang.String textureImageName)
          Loads the image to server as a texture.
protected  void debug(Chunk parentChunk, int level, java.lang.Integer chunkID, long chunkLength, int position, long limit)
          prints some handy information...
 void get(byte[] bytes)
          Fills bytes with data from the chunk buffer.
 java.nio.ByteBuffer getByteBuffer(java.nio.channels.ReadableByteChannel channel, int channelSize)
          Allocates and loads a byte buffer from the channel
 byte[] getChunkBytes()
          Reads a number of bytes corresponding to the number of bytes left in the current chunk and returns an array containing them.
 float getFloat()
          reads a float from the chunkBuffer.
 TransformGroup getGroup()
          Gets the group for the current object the chopper is working with.
 java.lang.Integer getID()
          Gets the id of the current chunk.
 int getInt()
          Reads an int and returns it
 KeyFramer getKeyFramer()
          Gets the key framer chunk These should be their own objects instead of chunks.
 long getLong()
          Gets a long from the chunk Buffer
 java.lang.Object getNamedObject(java.lang.String key)
          Sets a named Object in the loader.
 TransformGroup getNamedTransformGroup(java.lang.String key)
          Gets and cast the named object for the key provided.
 java.lang.String getObjectName()
          Gets the name of the current object the chopper is working with.
 Point3f getPoint()
          Reads 3 floats x,z,y from the chunkbuffer.
 int getShort()
          Reads a short and returns it as a signed int.
 java.lang.String getString()
          This reads bytes until it gets 0x00 and returns the corresponding string.
 int getUnsignedByte()
          Reads a byte, unsigns it, returns the corresponding int.
 int getUnsignedInt()
          Reads an int and returns it unsigned, any ints greater than MAX_INT will break.
 int getUnsignedShort()
          Reads a short and returns it as an unsigned int.
 Vector3f getVector()
          Reads 3 floats x,z,y from the chunkbuffer.
 boolean hasLights()
          Returns true if there have been lights loaded.
 SceneBase loadSceneBase(java.io.InputStream inputStream, Loader3DS loader, int modelSize)
          This sequentially parses the chunks out of the input stream and constructs the 3D entities represented within.
protected  void loadSubChunks(Chunk parentChunk, int level)
          The base class Chunk takes care of loading subchunks for all chunks types.
 java.lang.Object popData(java.lang.Object key)
          Gets a datum that had been retrieved and stored via pushData(java.lang.Object, java.lang.Object) earlier and removes it.
 void pushData(java.lang.Object key, java.lang.Object data)
          Used to store data that may later be used by another chunk.
 void setDataMap(java.util.HashMap dataMap)
          Sets the data map used to store values that chunks may need to retrieve later.
 void setNamedObject(java.lang.String key, java.lang.Object value)
          Sets a named object in the loader.
 void setObjectName(java.lang.String name)
          Sets the name of the current object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debug

public static boolean debug
This should be turned on by Loader3DS to view debugging information.


debugChunk

public static Chunk debugChunk
Current chunk for which debugging info is viewed if debug == true

Constructor Detail

ChunkChopper

public ChunkChopper()
private singleton constructor.

Method Detail

loadSceneBase

public SceneBase loadSceneBase(java.io.InputStream inputStream,
                               Loader3DS loader,
                               int modelSize)
This sequentially parses the chunks out of the input stream and constructs the 3D entities represented within. A Chunk is a little endian data structure consists of a 6 byte header followed by subchunks and or data. The first short int(little endian) represent the id of the chunk. The next int represent the total length of the chunk(total of data, subchunks and chunk header).

The first chunk is the main chunk (id=4D4D) and its length is always the length of the file. It only contains sub chunks. Other chunks may contain data, subchunks or both. If the format of a chunk is unknown skipped.

Subclasses of chunk will all automagically load the subchunks. It is the programmers responsibility to ensure that the data preceeding the subchunks is loaded or skipped as required and that something useful is done with the data. If data from the subchunks is needed in order to initialize components then that code should be placed in Chunk.initialize(com.microcrowd.loader.java3d.max3ds.ChunkChopper). Otherwise the data may be dealt with in Chunk.loadData(com.microcrowd.loader.java3d.max3ds.ChunkChopper). Also, if a chunk has data preceeding its subchunks it communicates how many bytes long that data is by returning it from loadData.

This chopper reads a file in order from beginning to end

Parameters:
inputStream - the stream with the data to be parsed.
loader - the loader that will be configured from the data.
modelName - name of the model file for display purposes.
modelSize - size in bytes of the file to read.

getByteBuffer

public java.nio.ByteBuffer getByteBuffer(java.nio.channels.ReadableByteChannel channel,
                                         int channelSize)
                                  throws java.io.IOException
Allocates and loads a byte buffer from the channel

Parameters:
channel - the file channel to load the data from
Returns:
a direct byte buffer containing all the data of the channel at position 0
Throws:
java.io.IOException

loadSubChunks

protected void loadSubChunks(Chunk parentChunk,
                             int level)
                      throws CannotChopException
The base class Chunk takes care of loading subchunks for all chunks types. It occurs as follows:
  1. The chunk id (short) is read
  2. The chunk length(int) is read
  3. A subchunk is looked up from the map of publish subchunk types of the current chunk.
  4. If it isn't found during the lookup it is skipped.
  5. Otherwise it is requested to pushData(java.lang.Object, java.lang.Object)
  6. The return value, if there is one, is used to determine where its next subchunk is. A return value of 0 signifies that the next subchunk is nigh.
  7. The chunk's subchunks are then loaded.
  8. The chunks initialize method is called.

Throws:
CannotChopException

getKeyFramer

public KeyFramer getKeyFramer()
Gets the key framer chunk These should be their own objects instead of chunks.


addObject

public void addObject(java.lang.String name,
                      TransformGroup group)
Adds a group to the choppers scene group and sets the current name and group.

Parameters:
the - name of the object to add which will also be the current name of the object the chopper is working with.
group - the current group that the chopper will be adding things too.

getObjectName

public java.lang.String getObjectName()
Gets the name of the current object the chopper is working with. The value returned from this generally is either set by a NamedObjectChunk and is the name of the name of the last object added or is the name set by setObjectName.

Returns:
the name of the current object being constructed.

setObjectName

public void setObjectName(java.lang.String name)
Sets the name of the current object. The name of the current object can also be set with addObject(java.lang.String, TransformGroup)

Parameters:
name - the name that the current object should be set to.

getGroup

public TransformGroup getGroup()
Gets the group for the current object the chopper is working with. The value returned from this generally gets set by a NamedObjectChunk and is the name of the last object added.

Returns:
the group for the current object being constructed.

pushData

public void pushData(java.lang.Object key,
                     java.lang.Object data)
Used to store data that may later be used by another chunk.

Parameters:
key - the look up key.
data - the data to store.

popData

public java.lang.Object popData(java.lang.Object key)
Gets a datum that had been retrieved and stored via pushData(java.lang.Object, java.lang.Object) earlier and removes it.

Parameters:
key - the key used to store the datum earlier.

setNamedObject

public void setNamedObject(java.lang.String key,
                           java.lang.Object value)
Sets a named object in the loader.

Parameters:
key - the key name of the object
value - the named Object.

hasLights

public boolean hasLights()
Returns true if there have been lights loaded.

Returns:
true if there are lights.

addBehaviorNode

public void addBehaviorNode(Behavior behavior)
Adds a behavior to the scene base.

Parameters:
behavior - the behavior to add to the scene base.

addLightNode

public void addLightNode(Light light)
Adds a light to the scene base.

Parameters:
light - the light to add to the scene base.

addViewGroup

public void addViewGroup(TransformGroup viewGroup)
Adds a camera transform to the scene base.

Parameters:
viewGroup - the transform group to add as a view.

getNamedObject

public java.lang.Object getNamedObject(java.lang.String key)
Sets a named Object in the loader.

Parameters:
key - the key used as the name for which the object will be returned

getNamedTransformGroup

public TransformGroup getNamedTransformGroup(java.lang.String key)
Gets and cast the named object for the key provided. Its an error if its not a transform group.


getLong

public long getLong()
Gets a long from the chunk Buffer


getShort

public int getShort()
Reads a short and returns it as a signed int.


getUnsignedShort

public int getUnsignedShort()
Reads a short and returns it as an unsigned int.


getFloat

public float getFloat()
reads a float from the chunkBuffer.


getVector

public Vector3f getVector()
Reads 3 floats x,z,y from the chunkbuffer. Since 3ds has z as up and y as pointing in whereas java3d has z as pointing forward and y as pointing up; this returns new Vector3f(x,-z,y)


getPoint

public Point3f getPoint()
Reads 3 floats x,z,y from the chunkbuffer. Since 3ds has z as up and y as pointing in whereas java3d has z as pointing forward and y as pointing up; this returns new Point3f(x,-z,y)


getInt

public int getInt()
Reads an int and returns it

Returns:
the int read

getUnsignedInt

public int getUnsignedInt()
Reads an int and returns it unsigned, any ints greater than MAX_INT will break.


getUnsignedByte

public int getUnsignedByte()
Reads a byte, unsigns it, returns the corresponding int.

Returns:
the unsigned int corresponding to the read byte.

getChunkBytes

public byte[] getChunkBytes()
Reads a number of bytes corresponding to the number of bytes left in the current chunk and returns an array containing them.

Returns:
an array containing all the bytes for the current chunk.

get

public void get(byte[] bytes)
Fills bytes with data from the chunk buffer.

Parameters:
bytes - the array to fill with data.

setDataMap

public void setDataMap(java.util.HashMap dataMap)
Sets the data map used to store values that chunks may need to retrieve later.

Parameters:
dataMap - the hashmap that will be used to store and retrieve values for use by chunks.

getString

public java.lang.String getString()
This reads bytes until it gets 0x00 and returns the corresponding string.


getID

public java.lang.Integer getID()
Gets the id of the current chunk.

Returns:
id of the current chunk as read from the chunkBuffer. It will be a signed short.

createTexture

public Texture createTexture(java.lang.String textureImageName)
Loads the image to server as a texture.

Parameters:
textureImageName - name of the image that is going to be set to be the texture.

debug

protected void debug(Chunk parentChunk,
                     int level,
                     java.lang.Integer chunkID,
                     long chunkLength,
                     int position,
                     long limit)
prints some handy information... the chunk hierarchy.



Copyright © 2000-2011 Microcrowd. All Rights Reserved.