Stage3d – AGAL experiments



BINARY FILE IMPORT

Download importer exemple – Moussa Dembélé - www.mousman.com- mousman@hotmail.com

Download compact importer exemple

 

Now we have a binary file containing all the model data.
But how to use it ?

Let’s see that :
You will find sources in the import directory.

We’ll use the Ferrari model as we already know it, it will be clearer to see differences when using the binary file.

The file is embeded line 50

[Embed(source="../lib/mesh/ferrari.mdm", mimeType="application/octet-stream")]
private var _meshData:Class;

When shaders are defined (same as before),
the file is uncompress,
then we instanciate a new object : MObjImportParser to parse the file.

// parse the OBJ file and create buffers
_meshBA = ByteArrayAsset(new _meshData()); 
_meshBA.uncompress();
_parser = new MObjImportParser(_context3D, _shaderStore);
_mesh = _parser.parse(_meshBA);

Rest of the class remains the same as previously seen.

What about MObjImportParser :
The parse methode requests a ByteArrayAsset which corresponds to the binary file.
Parsing principle is still the same : create a MObjMesh.
The difference is the data origin. Previously they came from two files , a mtl file and an obj file.
Now they come from a single ByteArray and in fact things are easier now for parsing, because we just have to follow the binary file description
to get our data.
Firt we read the header (line 96) , nothing special here as for now this function is permissive and no control is done…. (that will probably change in the futur),
then we read the content and return the MObjMesh instance created.

private function parseObj(meshData:ByteArrayAsset):void {
var buffer3D:VertexBuffer3D;
var index3D:IndexBuffer3D;
var object3Dset:MObject3D;
var mPolygonsGroup:MPolygoneGroup3D;
var totalVertices:int;
var startObjPos:uint;
var endObjPos:uint;
var size:uint;

readHeader(meshData);
var nbObj:uint = meshData.readUnsignedInt();
//trace("nbObj : " + nbObj);

for (var i:int = 0; i < nbObj; i++) {
	var object3D:MObject3D = readObj(meshData);
	_mesh.add(object3D);
}
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
private function readObj(meshData:ByteArrayAsset):MObject3D 
{
var objSize:uint = meshData.readUnsignedInt();
var id:String = meshData.readUTF();
//trace("-----------------------");
//trace("MObject3D : " + id);
var object3D:MObject3D = new MObject3D(id);
var nbPGroups:uint = meshData.readUnsignedInt();
//trace("nbPGroups : " + nbPGroups);
var polygonsGroups:Vector.<MPolygoneGroup3D> = new Vector.<MPolygoneGroup3D>(nbPGroups,true);
object3D.polygonsGroups = polygonsGroups;

for (var i:int = 0; i < nbPGroups; i++) {
	var polygoneGroup3D:MPolygoneGroup3D = readPGroup(meshData);
	polygonsGroups[i] = polygoneGroup3D;
}

return object3D;
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
private function readPGroup(meshData:ByteArrayAsset):MPolygoneGroup3D 
{
var groupSize:uint = meshData.readUnsignedInt();
var id:String = meshData.readUTF();
trace("   MPolygoneGroup3D : " + id);

var polygoneGroup3D:MPolygoneGroup3D;
polygoneGroup3D = new MPolygoneGroup3D(id);

var nbVertices:uint = meshData.readUnsignedInt();
polygoneGroup3D.nbVertices = nbVertices;
var smoothShading:Number = meshData.readDouble();
polygoneGroup3D.smoothShading = smoothShading;

var mtlSize:uint = meshData.readUnsignedInt();
var mtlId:String = meshData.readUTF();
var mtlSet:MMtlSet = new MMtlSet(mtlId);
polygoneGroup3D.mtlSet = mtlSet;
mtlSet.diffuseColor = meshData.readUnsignedInt();
mtlSet.ambiantColor = meshData.readUnsignedInt();
mtlSet.specularColor = meshData.readUnsignedInt();
mtlSet.specularCoef= meshData.readDouble();
mtlSet.emissiveColor = meshData.readUnsignedInt();
mtlSet.dissolved = meshData.readDouble();
mtlSet.illumination = meshData.readDouble();
var aTexture:String = meshData.readUTF();
if (aTexture !="")
	mtlSet.ambiantTexture = aTexture;
var dTexture:String = meshData.readUTF();
if (dTexture !="")
	mtlSet.diffuseTexture = dTexture;
var isTexture:Boolean = meshData.readBoolean();
var nbBufferSets:uint = meshData.readUnsignedInt();
for (var i:int = 0; i < nbBufferSets; i++) {
	var bufferSet:MPolygonGroupBuffers = readBufferSet(meshData);
	polygoneGroup3D.addPolygonBuffers(bufferSet);
}		

return polygoneGroup3D;			
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
private function readBufferSet(meshData:ByteArrayAsset):MPolygonGroupBuffers 
{
var bSize:uint = meshData.readUnsignedInt();
var id:String = meshData.readUTF();
//trace("       MPolygonGroupBuffers : " + id);
var bufferSet:MPolygonGroupBuffers = new MPolygonGroupBuffers(id);
var dataPerVertex:int = meshData.readInt();
var nbVertices:int = meshData.readInt();
var verticesBA:ByteArray = new ByteArray();
//trace("       nbVertices : " + nbVertices + " , dataPerVertex: " + dataPerVertex);
meshData.readBytes(verticesBA, 0, nbVertices * dataPerVertex * 4);
var nbIndices:int = meshData.readInt();
//trace("       nbIndices : " + nbIndices );
var indices:Vector.<uint> = new Vector.<uint>();
for (var i:int = 0; i < nbIndices; i++) {
	indices.push(meshData.readUnsignedInt());
}		

var vertexBuffer:VertexBuffer3D = _context3D.createVertexBuffer(nbVertices, dataPerVertex);
//trace("creating " + nbVertices +" vertex and index");
vertexBuffer.uploadFromByteArray(verticesBA,0, 0, nbVertices);
bufferSet.vertexBuffer = vertexBuffer;

// index
var indexBuffer:IndexBuffer3D = _context3D.createIndexBuffer(nbIndices);
indexBuffer.uploadFromVector(indices, 0, nbIndices);
bufferSet.indexBuffer = indexBuffer;

return bufferSet;
}

And it’s done !!

Any questions, any comments, don’t hesitate !

What next and references

 

No Responses Yet

Leave a Reply




Articles récents


Pages



Switch to our desktop site