js-openctm is a JavaScript library for reading OpenCTM files.
OpenCTM is a file format for storing 3D triangle meshes created by Marcus Geelnard.
Some parts of this library are a direct traslation to JavaScript from original C source code found on the OpenCTM SDK.
1) Retrieve your .ctm file from the web, as usual:
function retrieve(url){
var request = new XMLHttpRequest();
request.open("GET", url, false);
request.overrideMimeType("text/plain; charset=x-user-defined");
request.send();
if ( (200 !== request.status) && (0 !== request.status) ){
throw new Error(request.status + " Retrieving " + url);
}
return request;
};
var request = retrieve("<your .ctm filename url here>");
2) Create a CTM.Stream object from the retrieved file:
var stream = new CTM.Stream(request.responseText);
3) Create a CTM.File object from the stream:
var file = new CTM.File(stream);
CTM.File objects have two properties:
-
header, an object with the following properties:fileFormat: File format version (always 5)compressionMethod: Compression method (RAW, MG1 or MG2)vertexCount: Vertex counttriangleCount: Triangle countuvMapCount: UV map countattrMapCount: Attribute map countflags: Boolean flags (bit 0 means that normals are present)comment: File comment
-
body, an object with the following properties:indices: AUint32Arraybuffer with 3 xheader.triangleCountelementsvertices: AFloat32Arraybuffer withheader.vertexCountelementsnormals: (optional) AFloat32Arraybuffer withheader.vertexCountelementsuvMaps: (optional) An array withheader.uvMapCountelements, each of them with the following properties:name: UV map namefilename: UV map ?le nameuv: AFloat32Arraybuffer with 2 xheader.vertexCountelements
attrMaps: (optional) A array withheader.attrMapCountelements, each of them with the following properties:name: Attribute map nameattr: AFloat32Arraybuffer with 4 xheader.vertexCountelements
Using WebGL, of course.
...
indicesBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indicesBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(file.body.indices), gl.STATIC_DRAW);
verticesBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, verticesBuffer);
gl.bufferData(gl.ARRAY_BUFFER, file.body.vertices, gl.STATIC_DRAW);
if (file.body.normals){
normalsBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, normalsBuffer);
gl.bufferData(gl.ARRAY_BUFFER,file.body.normals, gl.STATIC_DRAW);
}
if (file.body.uvMaps){
uvBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, uvBuffer);
gl.bufferData(gl.ARRAY_BUFFER, file.body.uvMaps[0].uv, gl.STATIC_DRAW);
}
...
No idea? Try http://learningwebgl.com.
js-lzma: A port of the LZMA decompression algorithm to JavaScript.
-
Textures are not packed on
.ctmfiles, you must retrieve them on your own. -
Indices are exposed like a
Uint32Arraybuffer, but please, remember that WebGLdrawElementsfunction works withUint16Array, you must split them on your own.
Sorry, don't. This library is part of another project of mine that only needed the reader algorithm.