|
Project Information
|
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. DemoVideo
How to use?Three steps are required: 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); What you get?CTM.File objects have two properties:
How to draw the mesh?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. Dependenciesjs-lzma: A port of LZMA decompression algorithm to JavaScript. Limitations- Textures are not packed on .ctm files, you must retrieve them on your own. - Indices are exposed like a Uint32Array buffer, but please, remember that WebGL drawElements function works with Uint16Array, you must split them on your own. What about writer function?Sorry, don't. This library is part of another project of mine that only needed the reader algorithm. |