Skip to content

Instantly share code, notes, and snippets.

@nervoussystem
Last active April 22, 2016 14:20
Show Gist options
  • Save nervoussystem/7279387 to your computer and use it in GitHub Desktop.
Save nervoussystem/7279387 to your computer and use it in GitHub Desktop.
Calculate the volume of a mesh stored in a VBO-like structure (vertex data in an array and triangle data in another array). Uses gl-matrix
var volume = 0;
var v1 = vec3.create(),
v2 = vec3.create(),
v3 = vec3.create();
var i,index1,index2,index3;
for(i=0;i<numIndices;) {
index1 = indices[i++]*3;
index2 = indices[i++]*3;
index3 = indices[i++]*3;
vec3.set(v1,vertices[index1],vertices[index1+1],vertices[index1+2]);
vec3.set(v2,vertices[index2],vertices[index2+1],vertices[index2+2]);
vec3.set(v3,vertices[index3],vertices[index3+1],vertices[index3+2]);
vec3.cross(v1,v1,v2);
volume += vec3.dot(v1,v3);
}
volume = Math.abs(volume/6.0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment