//---------------------------------------------------------- void ofGLRenderer::draw(ofMesh & vertexData, ofPolyRenderMode renderType, bool useColors, bool useTextures, bool useNormals){ if (bSmoothHinted) startSmoothing(); #ifndef TARGET_OPENGLES glPushAttrib(GL_POLYGON_BIT); glPolygonMode(GL_FRONT_AND_BACK, ofGetGLPolyMode(renderType)); draw(vertexData,useColors,useTextures,useNormals); glPopAttrib(); //TODO: GLES doesnt support polygon mode, add renderType to gl renderer? #else if(vertexData.getNumVertices()){ glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, sizeof(ofVec3f), vertexData.getVerticesPointer()); } if(vertexData.getNumNormals() && useNormals){ glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, 0, vertexData.getNormalsPointer()); } if(vertexData.getNumColors() && useColors){ glEnableClientState(GL_COLOR_ARRAY); glColorPointer(4,GL_FLOAT, sizeof(ofFloatColor), vertexData.getColorsPointer()); } if(vertexData.getNumTexCoords() && useTextures){ glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, vertexData.getTexCoordsPointer()); } GLenum drawMode; switch(renderType){ case OF_MESH_POINTS: drawMode = GL_POINTS; break; case OF_MESH_WIREFRAME: drawMode = GL_LINES; break; case OF_MESH_FILL: drawMode = ofGetGLPrimitiveMode(vertexData.getMode()); break; default: drawMode = ofGetGLPrimitiveMode(vertexData.getMode()); break; } if(vertexData.getNumIndices()){ glDrawElements(drawMode, vertexData.getNumIndices(),GL_UNSIGNED_SHORT,vertexData.getIndexPointer()); }else{ glDrawArrays(drawMode, 0, vertexData.getNumVertices()); } if(vertexData.getNumColors() && useColors){ glDisableClientState(GL_COLOR_ARRAY); } if(vertexData.getNumNormals() && useNormals){ glDisableClientState(GL_NORMAL_ARRAY); } if(vertexData.getNumTexCoords() && useTextures){ glDisableClientState(GL_TEXTURE_COORD_ARRAY); } #endif if (bSmoothHinted) endSmoothing(); }
void MeshHelper::fuseNeighbours( ofMesh& mesh, float fuseDistance ) { //@todo tex coords, normals assert( mesh.getMode() == OF_PRIMITIVE_TRIANGLES ); int oldNumVerts = mesh.getNumVertices(); int oldNumIndices = mesh.getNumIndices(); if ( fuseDistance < 0 ) { // fuse close-enough vertices // first define 'close enough' as 1/10000 of smallest dimension of the bounding box width/height/depth ofVec3f tlb, brf; // top left back, bottom right front calculateAABoundingBox( mesh, tlb, brf ); float minDimension = min(brf.x-tlb.x,min(brf.y-tlb.y, brf.z-tlb.z)); fuseDistance = minDimension * 0.00001f; } // now fuse map<ofIndexType,ofIndexType> fused; vector<ofVec3f> newVertices; vector<ofIndexType> remove; for ( ofIndexType i=0; i<mesh.getNumVertices(); i++ ) { const ofVec3f& vertex = mesh.getVertex(i); // look at all the earlier vertices bool didFuse = false; for ( ofIndexType j=0; j<newVertices.size(); j++ ) { if ( (vertex-newVertices[j]).length()<fuseDistance ) { // fuse i to j fused[i] = j; remove.push_back(i); didFuse = true; break; } } if ( !didFuse ) { newVertices.push_back( vertex ); fused[i] = newVertices.size()-1; } } // update indices for ( int i=0; i<mesh.getNumIndices(); i++ ) { ofIndexType originalIndex = mesh.getIndex(i); assert( fused.find( originalIndex ) != fused.end() ); if ( fused.find(originalIndex) != fused.end() ) { mesh.getIndices()[i] = fused[originalIndex]; } } // remove the fused for ( int i=remove.size()-1; i>=0; i-- ) { mesh.removeVertex( remove[i] ); } ofLogNotice("MeshHelper") << "fuseNeighbours inplace: input " << oldNumVerts << " vertices/" << oldNumIndices << " indices, output " << mesh.getNumVertices() << " vertices/" << mesh.getNumIndices() << " indices"; }
void ofxObjLoader::save(string path, ofMesh& mesh){ path = ofToDataPath(path); GLuint writeMode = GLM_NONE; GLMmodel* m = new GLMmodel(); if(mesh.getNumVertices() > 0){ m->numvertices = mesh.getNumVertices(); m->vertices = new GLfloat[m->numvertices*3+1]; memcpy(&m->vertices[3], &mesh.getVertices()[0].x, sizeof(ofVec3f) * mesh.getNumVertices()); } else { ofLogError("ofxObjLoader::save -- No vertices to save!"); return; } if(mesh.getNumNormals() > 0){ m->numnormals = mesh.getNumNormals(); m->normals = new GLfloat[m->numnormals*3+1]; memcpy(&m->normals[3], &mesh.getNormals()[0].x, sizeof(ofVec3f)*mesh.getNumNormals()); writeMode |= GLM_SMOOTH; } if(mesh.getNumTexCoords() > 0){ m->numtexcoords = mesh.getNumTexCoords(); m->texcoords = new GLfloat[m->numtexcoords*2+1]; memcpy(&m->texcoords[2], &mesh.getTexCoords()[0].x, sizeof(ofVec2f)*mesh.getNumTexCoords()); writeMode |= GLM_TEXTURE; } if(mesh.getNumIndices() > 0){ //create triangles m->numtriangles = mesh.getNumIndices()/3; m->triangles = new GLMtriangle[m->numtriangles]; //add them all to one group m->groups = new GLMgroup(); m->groups->next = NULL; m->groups->material = NULL; string name = "ofMesh"; m->groups->name = (char*)malloc(sizeof(char) * name.length()+1); strcpy(m->groups->name, name.c_str()); m->groups->numtriangles = mesh.getNumIndices()/3; m->groups->triangles = new GLuint[m->groups->numtriangles]; m->numgroups = 1; for(int i = 0; i < mesh.getNumIndices()/3; i++){ memcpy(m->triangles[i].vindices, &mesh.getIndices()[i*3], sizeof(GLuint)*3); memcpy(m->triangles[i].nindices, &mesh.getIndices()[i*3], sizeof(GLuint)*3); memcpy(m->triangles[i].tindices, &mesh.getIndices()[i*3], sizeof(GLuint)*3); m->groups->triangles[i] = i; } } glmWriteOBJ(m, (char*)path.c_str(), writeMode); glmDelete(m); }
int getClosestTripletOnMesh(const ofMesh& objectMesh,const ofMesh& imageMesh, float x, float y, float* distance) { float bestDistance = numeric_limits<float>::infinity(); int bestChoice = 0; for(int i = 0; i < objectMesh.getNumIndices()/3; i++) { ofVec3f cur = ofVec3f(0,0,0); for (int j=0;j<3;j++) { cur+=imageMesh.getVerticesPointer()[objectMesh.getIndexPointer()[3*i+j]]; } cur/=3.0; float dx = x - cur.x; float dy = y - cur.y; float dz = 0 - cur.z; float curDistance = dx * dx + dy * dy + dz * dz; if(curDistance < bestDistance) { bestDistance = curDistance; bestChoice = i; } } return bestChoice; }
void PyramidBrush::setNormals(ofMesh& mesh) { int nV = mesh.getNumVertices(); int nT = mesh.getNumIndices() / 3; vector<ofPoint> norm(nV); for(int t=0; t < nT; t++) { int i1 = mesh.getIndex(3*t); int i2 = mesh.getIndex(3*t + 1); int i3 = mesh.getIndex(3*t + 2); const ofPoint &v1 = mesh.getVertex(i1); const ofPoint &v2 = mesh.getVertex(i2); const ofPoint &v3 = mesh.getVertex(i3); ofPoint dir = ( (v2 - v1).crossed(v3 - v1)).normalized(); norm[i1] += dir; norm[i2] += dir; norm[i3] += dir; } for(int i = 0; i < nV; i++) { norm[i].normalize(); } mesh.clearNormals(); mesh.addNormals(norm); }
//-------------------------------------------------------------- void ofVbo::setMesh(const ofMesh & mesh, int usage, bool useColors, bool useTextures, bool useNormals){ if(mesh.getVertices().empty()){ ofLogWarning("ofVbo") << "setMesh(): ignoring mesh with no vertices"; return; } setVertexData(mesh.getVerticesPointer(),mesh.getNumVertices(),usage); if(mesh.hasColors() && useColors){ setColorData(mesh.getColorsPointer(),mesh.getNumColors(),usage); enableColors(); }else{ disableColors(); } if(mesh.hasNormals() && useNormals){ setNormalData(mesh.getNormalsPointer(),mesh.getNumNormals(),usage); enableNormals(); }else{ disableNormals(); } if(mesh.hasTexCoords() && useTextures){ setTexCoordData(mesh.getTexCoordsPointer(),mesh.getNumTexCoords(),usage); enableTexCoords(); }else{ disableTexCoords(); } if(mesh.hasIndices()){ setIndexData(mesh.getIndexPointer(), mesh.getNumIndices(), usage); enableIndices(); }else{ disableIndices(); } }
//-------------------------------------------------------------- void ofVboByteColor::setMesh(const ofMesh & mesh, int usage){ setVertexData(mesh.getVerticesPointer(),mesh.getNumVertices(),usage); setColorData(mesh.getColorsPointer(),mesh.getNumColors(),usage); setNormalData(mesh.getNormalsPointer(),mesh.getNumNormals(),usage); setTexCoordData(mesh.getTexCoordsPointer(),mesh.getNumTexCoords(),usage); setIndexData(mesh.getIndexPointer(), mesh.getNumIndices(), usage); }
//-------------------------------------------------------------- void ofxBulletTriMeshShape::updateMesh( btDiscreteDynamicsWorld* a_world, ofMesh& aMesh ) { if( aMesh.getNumVertices() != totalVerts || aMesh.getNumIndices() != totalIndices ) { ofLogWarning() << "updateMesh :: the verts or the indices are not the correct size, not updating"; return; } auto& tverts = aMesh.getVertices(); btVector3 aabbMin(BT_LARGE_FLOAT,BT_LARGE_FLOAT,BT_LARGE_FLOAT); btVector3 aabbMax(-BT_LARGE_FLOAT,-BT_LARGE_FLOAT,-BT_LARGE_FLOAT); for( int i = 0; i < totalVerts; i++ ) { auto& v = tverts[i]; bullet_vertices[i].setValue( v.x, v.y, v.z ); aabbMin.setMin( bullet_vertices[i] ); aabbMax.setMax( bullet_vertices[i] ); } btBvhTriangleMeshShape* triShape = (btBvhTriangleMeshShape*)_shape; // triShape->partialRefitTree( aabbMin, aabbMax ); triShape->refitTree( aabbMin, aabbMax ); //clear all contact points involving mesh proxy. Note: this is a slow/unoptimized operation. a_world->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs( getRigidBody()->getBroadphaseHandle(), a_world->getDispatcher()); }
ofMesh convertFromIndices(const ofMesh& mesh) { ofMesh result; // have to do a const_cast because ofMesh::get*() is not const correct ofMesh& cmesh = const_cast<ofMesh&>(mesh); int vertices = mesh.getNumVertices(); int colors = mesh.getNumColors(); int normals = mesh.getNumNormals(); int texcoords = mesh.getNumTexCoords(); int indices = mesh.getNumIndices(); for(int i = 0; i < indices; i++) { int cur = cmesh.getIndex(i); if(vertices > 0) { result.addVertex(cmesh.getVertex(cur)); } if(colors > 0) { result.addColor(cmesh.getColor(cur)); } if(normals > 0) { result.addNormal(cmesh.getNormal(cur)); } if(texcoords > 0) { result.addTexCoord(cmesh.getTexCoord(cur)); } } return result; }
//Universal function which sets normals for the triangle mesh void ofxOcean::setNormals( ofMesh &mesh ){ //The number of the vertices int nV = mesh.getNumVertices(); //The number of the triangles int nT = mesh.getNumIndices() / 3; vector<ofPoint> norm( nV ); //Array for the normals //Scan all the triangles. For each triangle add its //normal to norm's vectors of triangle's vertices for (int t=0; t<nT; t++) { //Get indices of the triangle t int i1 = mesh.getIndex( 3 * t ); int i2 = mesh.getIndex( 3 * t + 1 ); int i3 = mesh.getIndex( 3 * t + 2 ); //Get vertices of the triangle const ofPoint &v1 = mesh.getVertex( i1 ); const ofPoint &v2 = mesh.getVertex( i2 ); const ofPoint &v3 = mesh.getVertex( i3 ); //Compute the triangle's normal ofPoint dir = ( (v2 - v1).getCrossed( v3 - v1 ) ).getNormalized(); //Accumulate it to norm array for i1, i2, i3 norm[ i1 ] += dir; norm[ i2 ] += dir; norm[ i3 ] += dir; } //Normalize the normal's length for (int i=0; i<nV; i++) { norm[i].normalize(); } //Set the normals to mesh mesh.clearNormals(); mesh.addNormals( norm ); }
//-------------------------------------------------------------- //Universal function which sets normals for the triangle mesh void setNormals( ofMesh &mesh ){ int nV = mesh.getNumVertices();//640 int nT = mesh.getNumIndices() / 3;//213 vector<ofPoint> norm( nV ); for (int t=0; t<nT; t++) { int i1 = mesh.getIndex( 3 * t ); int i2 = mesh.getIndex( 3 * t + 1 ); int i3 = mesh.getIndex( 641 ); const ofPoint &v1 = mesh.getVertex( i1 ); const ofPoint &v2 = mesh.getVertex( i2 ); const ofPoint &v3 = mesh.getVertex( i3 ); //Compute the triangle's normal ofPoint dir = ( (v2 - v1).crossed( v3 - v1 ) ).normalized(); norm[ i1 ] += dir; norm[ i2 ] += dir; norm[ i3 ] += dir; } //Normalize the normal's length for (int i=0; i<nV; i++) { norm[i].normalize(); } //Set the normals to mesh mesh.clearNormals(); mesh.addNormals( norm ); }
void buildNormals(ofMesh& mesh) { if(mesh.getNumIndices() > 0) { buildNormalsAverage(mesh); } else { buildNormalsFaces(mesh); } }
//---------------------------------------------------------- void ofGLRenderer::draw(const ofMesh & vertexData, bool useColors, bool useTextures, bool useNormals) const{ if(vertexData.getNumVertices()){ glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, sizeof(ofVec3f), &vertexData.getVerticesPointer()->x); } if(vertexData.getNumNormals() && useNormals){ glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, sizeof(ofVec3f), &vertexData.getNormalsPointer()->x); } if(vertexData.getNumColors() && useColors){ glEnableClientState(GL_COLOR_ARRAY); glColorPointer(4,GL_FLOAT, sizeof(ofFloatColor), &vertexData.getColorsPointer()->r); } if(vertexData.getNumTexCoords() && useTextures){ set<int>::iterator textureLocation = textureLocationsEnabled.begin(); for(;textureLocation!=textureLocationsEnabled.end();textureLocation++){ glActiveTexture(GL_TEXTURE0+*textureLocation); glClientActiveTexture(GL_TEXTURE0+*textureLocation); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, sizeof(ofVec2f), &vertexData.getTexCoordsPointer()->x); } glActiveTexture(GL_TEXTURE0); glClientActiveTexture(GL_TEXTURE0); } if(vertexData.getNumIndices()){ #ifdef TARGET_OPENGLES glDrawElements(ofGetGLPrimitiveMode(vertexData.getMode()), vertexData.getNumIndices(),GL_UNSIGNED_SHORT,vertexData.getIndexPointer()); #else glDrawElements(ofGetGLPrimitiveMode(vertexData.getMode()), vertexData.getNumIndices(),GL_UNSIGNED_INT,vertexData.getIndexPointer()); #endif }else{ glDrawArrays(ofGetGLPrimitiveMode(vertexData.getMode()), 0, vertexData.getNumVertices()); } if(vertexData.getNumColors() && useColors){ glDisableClientState(GL_COLOR_ARRAY); } if(vertexData.getNumNormals() && useNormals){ glDisableClientState(GL_NORMAL_ARRAY); } if(vertexData.getNumTexCoords() && useTextures){ glDisableClientState(GL_TEXTURE_COORD_ARRAY); } }
void MeshHelper::fuseNeighbours( ofMesh& outputMesh, const ofMesh& sourceMesh, float fuseDistance ) { //@todo tex coords, normals assert( sourceMesh.getMode() == OF_PRIMITIVE_TRIANGLES ); if ( fuseDistance < 0 ) { // fuse close-enough vertices // first define 'close enough' as 1/10000 of smallest dimension of the bounding box width/height/depth ofVec3f tlb, brf; // top left back, bottom right front calculateAABoundingBox( sourceMesh, tlb, brf ); float minDimension = min(brf.x-tlb.x,min(brf.y-tlb.y, brf.z-tlb.z)); fuseDistance = minDimension * 0.00001f; } // now fuse map<int,int> fused; vector<ofVec3f> vertices; for ( int i=0; i<sourceMesh.getNumVertices(); i++ ) { const ofVec3f& vertex = sourceMesh.getVertex(i); //vertex.rotate(10, 10, 10); bool didFuse = false; for ( int j=0; j<vertices.size(); j++ ) { if ( (vertex-vertices[j]).length()<fuseDistance ) { // fuse i to j fused[i] = j; didFuse = true; break; } } if ( !didFuse ) { vertices.push_back( vertex ); fused[i] = vertices.size()-1; } } // build the output mesh outputMesh.clear(); outputMesh.addVertices(vertices); if ( sourceMesh.getNumIndices() > 0 ) { // walk through indices to build up the new mesh const vector<ofIndexType>& indices = sourceMesh.getIndices(); for ( int i=0; i<indices.size(); i+=3 ) { assert( fused.find( indices[i] ) != fused.end() ); assert( fused.find( indices[i+1] ) != fused.end() ); assert( fused.find( indices[i+2] ) != fused.end() ); outputMesh.addTriangle( fused[indices[i]], fused[indices[i+1]], fused[indices[i+2]] ); } } else { // triangles are just triples of vertices for ( int i=0; i<sourceMesh.getNumVertices(); i+=3 ) { outputMesh.addTriangle( fused[i], fused[i+1], fused[i+2] ); } } ofLogNotice("MeshHelper") << "fuseNeighbours: input " << sourceMesh.getNumVertices() << " vertices/" << sourceMesh.getNumIndices() << " indices, output " << outputMesh.getNumVertices() << " vertices/" << outputMesh.getNumIndices() << " indices"; }
//-------------------------------------------------------------- void ofxBulletSoftTriMesh::create( ofxBulletWorldSoft* a_world, ofMesh& aMesh, btTransform &a_bt_tr, float a_mass ) { if(a_world == NULL) { ofLogError("ofxBulletSoftTriMesh") << "create(): a_world param is NULL"; return; } if( aMesh.getMode() != OF_PRIMITIVE_TRIANGLES ) { ofLogError("ofxBulletSoftTriMesh") << " only excepts meshes that are triangles"; return; } _world = a_world; _cachedMesh.clear(); _cachedMesh = aMesh; if( bullet_vertices != NULL ) { delete bullet_vertices; bullet_vertices = NULL; } int vertStride = sizeof(btVector3); int indexStride = 3*sizeof(int); int totalVerts = (int)aMesh.getNumVertices(); int totalIndices = (int)aMesh.getNumIndices(); bullet_vertices = new btScalar[ totalVerts * 3 ]; int* bullet_indices = new int[ totalIndices ]; auto& tverts = aMesh.getVertices(); vector< ofIndexType >& tindices = aMesh.getIndices(); for( int i = 0; i < totalVerts; i++ ) { bullet_vertices[i*3+0] = tverts[i].x; bullet_vertices[i*3+1] = tverts[i].y; bullet_vertices[i*3+2] = tverts[i].z; } for( int i = 0; i < totalIndices; i++ ) { bullet_indices[i] = tindices[i]; } _softBody = btSoftBodyHelpers::CreateFromTriMesh( _world->getInfo(), bullet_vertices, bullet_indices, totalIndices/3 ); _softBody->transform( a_bt_tr ); setMass( a_mass, true ); setCreated(_softBody); createInternalUserData(); delete [] bullet_indices; }
//---------------------------------------------------------- void ofGLRenderer::draw(ofMesh & vertexData){ if(vertexData.getNumVertices()){ glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, sizeof(ofVec3f), vertexData.getVerticesPointer()); } if(vertexData.getNumNormals()){ glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, 0, vertexData.getNormalsPointer()); } if(vertexData.getNumColors()){ glEnableClientState(GL_COLOR_ARRAY); glColorPointer(4,GL_FLOAT, sizeof(ofColor), vertexData.getColorsPointer()); } if(vertexData.getNumTexCoords()){ glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, vertexData.getTexCoordsPointer()); } if(vertexData.getNumIndices()){ #ifdef TARGET_OPENGLES glDrawElements(ofGetGLPrimitiveMode(vertexData.getMode()), vertexData.getNumIndices(),GL_UNSIGNED_SHORT,vertexData.getIndexPointer()); #else glDrawElements(ofGetGLPrimitiveMode(vertexData.getMode()), vertexData.getNumIndices(),GL_UNSIGNED_INT,vertexData.getIndexPointer()); #endif }else{ glDrawArrays(ofGetGLPrimitiveMode(vertexData.getMode()), 0, vertexData.getNumVertices()); } if(vertexData.getNumColors()){ glDisableClientState(GL_COLOR_ARRAY); } if(vertexData.getNumNormals()){ glDisableClientState(GL_NORMAL_ARRAY); } if(vertexData.getNumTexCoords()){ glDisableClientState(GL_TEXTURE_COORD_ARRAY); } }
// Returns true iff the mesh contains two index defined traingles that intersect. bool check_triangle_intersections(ofMesh &mesh) { int len = mesh.getNumIndices(); for(int i = 0; i < len; i += 3) { ofVec3f v1 = mesh.getVertex(mesh.getIndex(i)); ofVec3f v2 = mesh.getVertex(mesh.getIndex(i + 1)); ofVec3f v3 = mesh.getVertex(mesh.getIndex(i + 2)); for(int j = 0; j < len; j += 3) { ofVec3f p1 = mesh.getVertex(mesh.getIndex(j)); ofVec3f p2 = mesh.getVertex(mesh.getIndex(j + 1)); ofVec3f p3 = mesh.getVertex(mesh.getIndex(j + 2)); int b = Intersecting(p1, p2, v1, v2, v3); int b2 = Intersecting(p2, p3, v1, v2, v3); int b3 = Intersecting(p3, p1, v1, v2, v3); if(b == 1 || b2 == 1 || b3 == 1) { return true; } if(point_triangle_intersection(v1, v2, v3, p1)|| point_triangle_intersection(v1, v2, v3, p2)|| point_triangle_intersection(v1, v2, v3, p3)) { return true; } } } return false; }
Handle <Device::RTShape> Renderer::addMesh(Device::RTMaterial material, const ofMesh & meshPr, const ofMatrix4x4& transform) { int vertsNum = meshPr.getNumVertices(); int vertsSize = vertsNum * sizeof(ofVec3f); Device::RTData positions = g_device->rtNewData("immutable_managed", vertsSize, meshPr.getVerticesPointer()); int normalsNum = meshPr.getNumNormals(); int normalsSize = normalsNum * sizeof(ofVec3f); Device::RTData normals = g_device->rtNewData("immutable_managed", normalsSize, meshPr.getNormalsPointer()); int textcoordsNum = meshPr.getNumTexCoords(); int textcoordsSize = textcoordsNum * sizeof(ofVec2f); Device::RTData texcoords = g_device->rtNewData("immutable_managed", textcoordsSize, meshPr.getTexCoordsPointer()); int indicesNum = meshPr.getNumIndices(); int indicesSize = indicesNum * sizeof(ofIndexType); Device::RTData triangles = g_device->rtNewData("immutable_managed", indicesSize, meshPr.getIndexPointer()); Handle <Device::RTShape> mesh = device->rtNewShape("trianglemesh"); if(vertsNum) { g_device->rtSetArray(mesh, "positions", "float3", positions, vertsNum, sizeof(ofVec3f), 0); } if(normalsNum) { g_device->rtSetArray(mesh, "normals", "float3", normals, normalsNum, sizeof(ofVec3f), 0); } if(textcoordsNum) { g_device->rtSetArray(mesh, "texcoords", "float2", texcoords, textcoordsNum, sizeof(ofVec2f), 0); } if(indicesNum) { g_device->rtSetArray(mesh, "indices", "int3", triangles, indicesNum / 3, 3 * sizeof(ofIndexType), 0); } device->rtCommit(mesh); device->rtClear(mesh); return addShape(material, mesh, transform); }
void ofSaveMesh(const ofMesh& mesh, string filename) { ofFile ply; if (ply.open(filename, ofFile::WriteOnly, true)) { int vertexCount = mesh.getNumVertices(); int triangleCount = mesh.getNumVertices() / 3; cout << "saving mesh with " << mesh.getNumIndices() << " indices" << endl; cout << "saving mesh with " << mesh.getNumVertices() << " vertices" << endl; // write the header ply << "ply" << endl; ply << "format binary_little_endian 1.0" << endl; ply << "element vertex " << vertexCount << endl; ply << "property float x" << endl; ply << "property float y" << endl; ply << "property float z" << endl; ply << "element face " << triangleCount << endl; ply << "property list uchar int vertex_index" << endl; ply << "end_header" << endl; // write all the vertices const vector<ofVec3f>& surface = mesh.getVertices(); for(int i = 0; i < surface.size(); i++) { // write the raw data as if it were a stream of bytes ply.write((char*) &surface[i], sizeof(ofVec3f)); } // write all the faces unsigned char faceSize = 3; for(int i = 0; i < vertexCount; i += faceSize) { // write the raw data as if it were a stream of bytes ply.write((char*) &faceSize, sizeof(unsigned char)); for(int j = 0; j < faceSize; j++) { int curIndex = i + j; ply.write((char*) &curIndex, sizeof(int)); } } } }
//-------------------------------------------------------------- void save(const string& path, const ofMesh& mesh) { ofFile file(path, ofFile::WriteOnly, true); int numVerts = mesh.getNumVertices(); file.write((char *)(&numVerts), sizeof(int)); if (numVerts > 0) { file.write((char *)(&(mesh.getVertices())[0]), sizeof(ofPoint) * numVerts); } int numNormals = mesh.getNumNormals(); file.write((char *)(&numNormals), sizeof(int)); if (numNormals > 0) { file.write((char *)(&(mesh.getNormals())[0]), sizeof(ofPoint) * numNormals); } int numTexCoords = mesh.getNumTexCoords(); file.write((char *)(&numTexCoords), sizeof(int)); if (numTexCoords > 0) { file.write((char *)(&(mesh.getTexCoords())[0]), sizeof(ofVec2f) * numTexCoords); } int numColors = mesh.getNumColors(); file.write((char *)(&numColors), sizeof(int)); if (numColors > 0) { file.write((char *)(&(mesh.getColors())[0]), sizeof(ofFloatColor) * numColors); } int numIndices = mesh.getNumIndices(); file.write((char *)(&numIndices), sizeof(int)); if (numIndices > 0) { file.write((char *)(&(mesh.getIndices())[0]), sizeof(ofIndexType) * numIndices); } file.close(); }
vector<pair<btVector3, btConvexHullShape*> > ofxBulletConvexDecomposer::decompose(const ofMesh &meshToDecompose, btVector3 scale ) { assert( meshToDecompose.getMode() == OF_TRIANGLES_MODE ); vector<pair<btVector3, btConvexHullShape*> > convexShapes; int tcount = meshToDecompose.getNumIndices()/3; if ( tcount == 0 ) // nothing to do return convexShapes; // adapted from bullet-2.81-rev2613/Demos/ConvexDecompositionDemo/ConvexDecompositionDemo.cpp /* unsigned int depth = 5; float cpercent = 5; float ppercent = 15; unsigned int maxv = 16; float skinWidth = 0.0; // ConvexDecomposition::WavefrontObj wo; ConvexDecomposition::DecompDesc desc; desc.mVcount = meshToDecompose.getNumVertices(); desc.mVertices = (float*)(meshToDecompose.getVerticesPointer()); desc.mTcount = meshToDecompose.getNumIndices()/3; desc.mIndices = meshToDecompose.getIndexPointer(); desc.mDepth = depth; desc.mCpercent = cpercent; desc.mPpercent = ppercent; desc.mMaxVertices = maxv; desc.mSkinWidth = skinWidth; desc.mCallback = this; */ //----------------------------------------------- // HACD //----------------------------------------------- std::vector< HACD::Vec3<HACD::Real> > points; std::vector< HACD::Vec3<long> > triangles; for(int i=0; i<meshToDecompose.getNumVertices(); i++ ) { ofVec3f meshVert = meshToDecompose.getVertex(i); HACD::Vec3<HACD::Real> vertex( meshVert.x, meshVert.y, meshVert.z ); points.push_back(vertex); } for(int i=0;i<meshToDecompose.getNumIndices(); i+=3 ) { HACD::Vec3<long> triangle(meshToDecompose.getIndex(i), meshToDecompose.getIndex(i+1), meshToDecompose.getIndex(i+2) ); triangles.push_back(triangle); } assert(triangles.size()==tcount); HACD::HACD myHACD; myHACD.SetPoints(&points[0]); myHACD.SetNPoints(points.size()); myHACD.SetTriangles(&triangles[0]); myHACD.SetNTriangles(triangles.size()); myHACD.SetCompacityWeight(0.1); myHACD.SetVolumeWeight(0.0); // HACD parameters // Recommended parameters: 2 100 0 0 0 0 size_t nClusters = 2; double concavity = 100; bool invert = false; bool addExtraDistPoints = false; bool addNeighboursDistPoints = false; bool addFacesPoints = false; myHACD.SetNClusters(nClusters); // minimum number of clusters myHACD.SetNVerticesPerCH(100); // max of 100 vertices per convex-hull myHACD.SetConcavity(concavity); // maximum concavity myHACD.SetAddExtraDistPoints(addExtraDistPoints); myHACD.SetAddNeighboursDistPoints(addNeighboursDistPoints); myHACD.SetAddFacesPoints(addFacesPoints); myHACD.SetCallBack( hacdCallback ); myHACD.Compute(); nClusters = myHACD.GetNClusters(); int totalTriangles = 0; int totalPoints = 0; for (int c=0;c<nClusters;c++) { //generate convex result size_t nPoints = myHACD.GetNPointsCH(c); size_t nTriangles = myHACD.GetNTrianglesCH(c); ofLogVerbose("ofxBulletConvexDecomposer") << "cluster " << c <<"/" << nClusters << " points " << nPoints << " triangles " << nTriangles; float* vertices = new float[nPoints*3]; unsigned int* triangles = new unsigned int[nTriangles*3]; HACD::Vec3<HACD::Real> * pointsCH = new HACD::Vec3<HACD::Real>[nPoints]; HACD::Vec3<long> * trianglesCH = new HACD::Vec3<long>[nTriangles]; myHACD.GetCH(c, pointsCH, trianglesCH); // points for(size_t v = 0; v < nPoints; v++) { vertices[3*v] = pointsCH[v].X(); vertices[3*v+1] = pointsCH[v].Y(); vertices[3*v+2] = pointsCH[v].Z(); } // triangles for(size_t f = 0; f < nTriangles; f++) { triangles[3*f] = trianglesCH[f].X(); triangles[3*f+1] = trianglesCH[f].Y(); triangles[3*f+2] = trianglesCH[f].Z(); } ConvexResult r(nPoints, vertices, nTriangles, triangles); convexShapes.push_back( createConvexHullShapeFromConvexResult(r, scale) ); delete [] pointsCH; delete [] trianglesCH; delete [] vertices; delete [] triangles; totalTriangles += nTriangles; } return convexShapes; }
void ofxPolyvox::polyvoxToOfMesh(const PolyVox::SurfaceMesh<PositionMaterialNormal>& surfaceMesh, ofMesh& polyvxToOfMesh, bool setColor){ //Convienient access to the vertices and indices const vector<uint32_t>& vecIndices = surfaceMesh.getIndices(); const vector<PositionMaterialNormal>& vecVertices = surfaceMesh.getVertices();//surfaceMesh.getRawVertexData(); ofIndexType ofVecIndices; const void* pIndices = static_cast<const void*>(&(vecIndices[0])); int* indices = (int*)pIndices; vector<int> indx; for (int i = 0; i < surfaceMesh.getNoOfIndices(); i++ ){ indx.push_back(indices[i]); //cout << "indices:" << indices[i] << endl; polyvxToOfMesh.addIndex(indx[i]); } ofLog(OF_LOG_NOTICE, "ofMesh: number of indices is %d", polyvxToOfMesh.getNumIndices()); ofVec3f ofVecVertices; for (int i = 0; i < surfaceMesh.getNoOfVertices(); i++ ){ PositionMaterialNormal vert0 = vecVertices[i]; ofVecVertices = ofVec3f(vert0.getPosition().getX(),vert0.getPosition().getY(),vert0.getPosition().getZ()); polyvxToOfMesh.addVertex(ofVecVertices); } ofLog(OF_LOG_NOTICE, "ofMesh: number of vertices is %d", polyvxToOfMesh.getNumVertices()); ofVec3f ofVecNormals; for (int i = 0; i < surfaceMesh.getNoOfVertices(); i++ ){ PositionMaterialNormal vert0 = vecVertices[i]; ofVecNormals = ofVec3f(vert0.getNormal().getX(),vert0.getNormal().getY(),vert0.getNormal().getZ()); polyvxToOfMesh.addNormal(ofVecNormals); } ofLog(OF_LOG_NOTICE, "ofMesh: number of normals is %d", polyvxToOfMesh.getNumNormals()); if(setColor){ for (int i = 0; i < surfaceMesh.getNoOfVertices(); i++ ){ PositionMaterialNormal vert0 = vecVertices[i]; uint8_t material = static_cast<uint8_t>(vert0.getMaterial() + 0.5); //cout << "material:" << int(material) << endl; ofFloatColor colour = convertMaterialIDToColour(material); //cout << colour << endl; polyvxToOfMesh.addColor(colour); bool col = polyvxToOfMesh.hasColors(); //cout << "hasColors:" << col << endl; } } }
//-------------------------------------------------------------- void ofxBulletTriMeshShape::create( btDiscreteDynamicsWorld* a_world, ofMesh& aMesh, btTransform &a_bt_tr, float a_mass, glm::vec3 aAAbbMin, glm::vec3 aAAbbMax ) { if( aMesh.getMode() != OF_PRIMITIVE_TRIANGLES ) { ofLogWarning() << " ofxBulletTriMeshShape :: create : mesh must be using triangles, not creating!!" << endl; return; } if( aMesh.getNumIndices() < 3 ) { ofLogWarning() << " ofxBulletTriMeshShape :: create : mesh must have indices, not creating!" << endl; return; } if( !_bInited || _shape == NULL ) { int vertStride = sizeof(btVector3); int indexStride = 3*sizeof(int); totalVerts = (int)aMesh.getNumVertices(); totalIndices = (int)aMesh.getNumIndices(); const int totalTriangles = totalIndices / 3; if( bullet_indices != NULL ) { removeShape(); } if( bullet_vertices != NULL ) { removeShape(); } if( bullet_indexVertexArrays != NULL ) { removeShape(); } if( _shape != NULL ) { removeShape(); } bullet_vertices = new btVector3[ totalVerts ]; bullet_indices = new int[ totalIndices ]; auto& tverts = aMesh.getVertices(); auto& tindices = aMesh.getIndices(); for( int i = 0; i < totalVerts; i++ ) { bullet_vertices[i].setValue( tverts[i].x, tverts[i].y, tverts[i].z ); } for( int i = 0; i < totalIndices; i++ ) { bullet_indices[i] = (int)tindices[i]; } bullet_indexVertexArrays = new btTriangleIndexVertexArray(totalTriangles, bullet_indices, indexStride, totalVerts, (btScalar*) &bullet_vertices[0].x(), vertStride); // if you are having trouble with objects falling through, try passing in smaller or larger aabbMin and aabbMax // to something closer to the size of your object // // btVector3 aabbMin(-10000,-10000,-10000),aabbMax(10000,10000,10000); if( aAAbbMin.length() > 0 && aAAbbMax.length() > 0 ) { btVector3 aabbMin( aAAbbMin.x, aAAbbMin.y, aAAbbMin.z ); btVector3 aabbMax( aAAbbMax.x, aAAbbMax.y, aAAbbMax.z ); _shape = new btBvhTriangleMeshShape(bullet_indexVertexArrays, true, aabbMin, aabbMax ); } else { _shape = new btBvhTriangleMeshShape(bullet_indexVertexArrays, true, true ); } } ofxBulletRigidBody::create( a_world, _shape, a_bt_tr, a_mass ); createInternalUserData(); updateMesh( a_world, aMesh ); }
void ofCairoRenderer::draw(ofMesh & primitive, bool useColors, bool useTextures, bool useNormals){ if(primitive.getNumVertices() == 0){ return; } if(primitive.getNumIndices() == 0){ ofMesh indexedMesh = primitive; indexedMesh.setupIndicesAuto(); draw(indexedMesh, useColors, useTextures, useNormals); return; } pushMatrix(); cairo_matrix_init_identity(getCairoMatrix()); cairo_new_path(cr); int i = 1; ofVec3f v = transform(primitive.getVertex(primitive.getIndex(0))); ofVec3f v2; cairo_move_to(cr,v.x,v.y); if(primitive.getMode()==OF_PRIMITIVE_TRIANGLE_STRIP){ v = transform(primitive.getVertex(primitive.getIndex(1))); cairo_line_to(cr,v.x,v.y); v = transform(primitive.getVertex(primitive.getIndex(2))); cairo_line_to(cr,v.x,v.y); i=2; } for(; i<primitive.getNumIndices(); i++){ v = transform(primitive.getVertex(primitive.getIndex(i))); switch(primitive.getMode()){ case(OF_PRIMITIVE_TRIANGLES): if((i+1)%3==0){ cairo_line_to(cr,v.x,v.y); v2 = transform(primitive.getVertex(primitive.getIndex(i-2))); cairo_line_to(cr,v2.x,v2.y); cairo_move_to(cr,v.x,v.y); }else if((i+3)%3==0){ cairo_move_to(cr,v.x,v.y); }else{ cairo_line_to(cr,v.x,v.y); } break; case(OF_PRIMITIVE_TRIANGLE_STRIP): v2 = transform(primitive.getVertex(primitive.getIndex(i-2))); cairo_line_to(cr,v.x,v.y); cairo_line_to(cr,v2.x,v2.y); cairo_move_to(cr,v.x,v.y); break; case(OF_PRIMITIVE_TRIANGLE_FAN): /*triangles.addIndex((GLuint)0); triangles.addIndex((GLuint)1); triangles.addIndex((GLuint)2); for(int i = 2; i < primitive.getNumVertices()-1;i++){ triangles.addIndex((GLuint)0); triangles.addIndex((GLuint)i); triangles.addIndex((GLuint)i+1); }*/ break; default:break; } } cairo_move_to(cr,primitive.getVertex(primitive.getIndex(primitive.getNumIndices()-1)).x,primitive.getVertex(primitive.getIndex(primitive.getNumIndices()-1)).y); if(ofGetStyle().lineWidth>0){ cairo_stroke( cr ); } popMatrix(); }
void ofCairoRenderer::draw(const ofMesh & primitive, ofPolyRenderMode mode, bool useColors, bool useTextures, bool useNormals) const{ if(useColors || useTextures || useNormals){ ofLogWarning("ofCairoRenderer") << "draw(): cairo mesh rendering doesn't support colors, textures, or normals. drawing wireframe ..."; } if(primitive.getNumVertices() == 0){ return; } if(primitive.getNumIndices() == 0){ ofMesh indexedMesh = primitive; indexedMesh.setupIndicesAuto(); draw(indexedMesh, mode, useColors, useTextures, useNormals); return; } cairo_new_path(cr); cairo_matrix_t matrix; cairo_matrix_init_identity(&matrix); cairo_new_path(cr); std::size_t i = 1; ofVec3f v = transform(primitive.getVertex(primitive.getIndex(0))); ofVec3f v2; cairo_move_to(cr,v.x,v.y); if(primitive.getMode()==OF_PRIMITIVE_TRIANGLE_STRIP){ v = transform(primitive.getVertex(primitive.getIndex(1))); cairo_line_to(cr,v.x,v.y); v = transform(primitive.getVertex(primitive.getIndex(2))); cairo_line_to(cr,v.x,v.y); i=2; } for(; i<primitive.getNumIndices(); i++){ v = transform(primitive.getVertex(primitive.getIndex(i))); switch(primitive.getMode()){ case(OF_PRIMITIVE_TRIANGLES): if((i+1)%3==0){ cairo_line_to(cr,v.x,v.y); v2 = transform(primitive.getVertex(primitive.getIndex(i-2))); cairo_line_to(cr,v2.x,v2.y); cairo_move_to(cr,v.x,v.y); }else if((i+3)%3==0){ cairo_move_to(cr,v.x,v.y); }else{ cairo_line_to(cr,v.x,v.y); } break; case(OF_PRIMITIVE_TRIANGLE_STRIP): v2 = transform(primitive.getVertex(primitive.getIndex(i-2))); cairo_line_to(cr,v.x,v.y); cairo_line_to(cr,v2.x,v2.y); cairo_move_to(cr,v.x,v.y); break; case(OF_PRIMITIVE_TRIANGLE_FAN): /*triangles.addIndex((GLuint)0); triangles.addIndex((GLuint)1); triangles.addIndex((GLuint)2); for(int i = 2; i < primitive.getNumVertices()-1;i++){ triangles.addIndex((GLuint)0); triangles.addIndex((GLuint)i); triangles.addIndex((GLuint)i+1); }*/ break; default:break; } } cairo_move_to(cr,primitive.getVertex(primitive.getIndex(primitive.getNumIndices()-1)).x,primitive.getVertex(primitive.getIndex(primitive.getNumIndices()-1)).y); if(currentStyle.lineWidth>0){ cairo_stroke( cr ); } }
size_t polygonCount() const { return mesh->getNumIndices() / 3; }
void ofCairoRenderer::draw(ofMesh & primitive){ if(primitive.getNumVertices()==0) return; pushMatrix(); cairo_matrix_init_identity(getCairoMatrix()); cairo_new_path(cr); //if(indices.getNumIndices()){ int i = 1; ofVec3f v = transform(primitive.getVertex(primitive.getIndex(0))); ofVec3f v2; cairo_move_to(cr,v.x,v.y); if(primitive.getMode()==OF_TRIANGLE_STRIP_MODE){ v = transform(primitive.getVertex(primitive.getIndex(1))); cairo_line_to(cr,v.x,v.y); v = transform(primitive.getVertex(primitive.getIndex(2))); cairo_line_to(cr,v.x,v.y); i=2; } for(; i<primitive.getNumIndices(); i++){ v = transform(primitive.getVertex(primitive.getIndex(i))); switch(primitive.getMode()){ case(OF_TRIANGLES_MODE): if((i+1)%3==0){ cairo_line_to(cr,v.x,v.y); v2 = transform(primitive.getVertex(primitive.getIndex(i-2))); cairo_line_to(cr,v2.x,v2.y); cairo_move_to(cr,v.x,v.y); }else if((i+3)%3==0){ cairo_move_to(cr,v.x,v.y); }else{ cairo_line_to(cr,v.x,v.y); } break; case(OF_TRIANGLE_STRIP_MODE): v2 = transform(primitive.getVertex(primitive.getIndex(i-2))); cairo_line_to(cr,v.x,v.y); cairo_line_to(cr,v2.x,v2.y); cairo_move_to(cr,v.x,v.y); break; case(OF_TRIANGLE_FAN_MODE): /*triangles.addIndex((GLuint)0); triangles.addIndex((GLuint)1); triangles.addIndex((GLuint)2); for(int i = 2; i < primitive.getNumVertices()-1;i++){ triangles.addIndex((GLuint)0); triangles.addIndex((GLuint)i); triangles.addIndex((GLuint)i+1); }*/ break; default:break; } } cairo_move_to(cr,primitive.getVertex(primitive.getIndex(primitive.getNumIndices()-1)).x,primitive.getVertex(primitive.getIndex(primitive.getNumIndices()-1)).y); if(ofGetStyle().lineWidth>0){ cairo_stroke( cr ); } popMatrix(); }