//-------------------------------------------------------------- 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 ofVbo::updateMesh(const ofMesh & mesh){ ofMesh * nonconstMesh = (ofMesh*)&mesh; if(nonconstMesh->haveVertsChanged()) updateVertexData(mesh.getVerticesPointer(),mesh.getNumVertices()); if(nonconstMesh->haveColorsChanged()) updateColorData(mesh.getColorsPointer(),mesh.getNumColors()); if(nonconstMesh->haveNormalsChanged()) updateNormalData(mesh.getNormalsPointer(),mesh.getNumNormals()); if(nonconstMesh->haveTexCoordsChanged()) updateTexCoordData(mesh.getTexCoordsPointer(),mesh.getNumTexCoords()); }
//---------------------------------------------------------- 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); } }
ofMesh ofxFfd::deformMesh(ofMesh mesh){ ofMesh dst; dst.append(mesh); for ( int i=0; i<mesh.getNumVertices(); i++ ) { ofVec3f vec = mesh.getVertex(i); dst.setVertex(i, deform(vec)); } return dst; }
ofMesh getProjectedMesh(const ofMesh& mesh) { ofMesh projected = mesh; for(std::size_t i = 0; i < mesh.getNumVertices(); i++) { glm::vec3 cur = ofWorldToScreen(mesh.getVerticesPointer()[i]); cur.z = 0; projected.setVertex(i, cur); } return projected; }
//-------------------------------------------------------------- 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 update() { int n = mesh.getNumVertices(); int start = ofMap(mouseX, 0, ofGetWidth(), 0, n, true); controlPoints.clear(); controlPoints.setMode(OF_PRIMITIVE_POINTS); for(int i = start; i < n; i++) { unsigned int index = rankedCorners[i]; controlPoints.addVertex(mesh.getVertex(index)); } }
void buildNormalsFaces(ofMesh& mesh) { mesh.clearNormals(); for(int i = 0; i < mesh.getNumVertices(); i += 3) { int i0 = i + 0, i1 = i + 1, i2 = i + 2; ofVec3f normal = getNormal(mesh.getVertices()[i0], mesh.getVertices()[i1], mesh.getVertices()[i2]); for(int j = 0; j < 3; j++) { mesh.addNormal(normal); } } }
float getNearestVertex(const ofMesh& mesh, const ofVec2f& target, int& vertexIndex) { float bestDistance = 0; for(int i = 0; i < mesh.getNumVertices(); i++) { float distance = target.distance(mesh.getVertex(i)); if(distance < bestDistance || i == 0) { bestDistance = distance; vertexIndex = i; } } return bestDistance; }
void MeshHelper::appendMesh( ofMesh& targetMesh, const ofMesh& toAppend, bool fuse ) { ofIndexType indexOffset = targetMesh.getNumVertices(); targetMesh.addVertices( toAppend.getVertices() ); // append indices const vector<ofIndexType>& indices = toAppend.getIndices(); for ( int i=0; i<indices.size(); i++ ) { targetMesh.addIndex( indices[i]+indexOffset ); } if ( fuse ) fuseNeighbours(targetMesh); }
// adjacency list of triangulation vector< set<size_t> > ofxDelaunay2D::adjacencyForTriMesh(ofMesh &triMesh) { vector< set<size_t> > adjacency; adjacency.reserve(triMesh.getNumVertices()); for(size_t i=0; i<(size_t)(triMesh.getNumVertices()); ++i) { adjacency.push_back(set<size_t>()); } for(int i0=0; i0<(int)triMesh.getIndices().size() - 2; i0+=3) { size_t i = triMesh.getIndex(i0); size_t j = triMesh.getIndex(i0 + 1); size_t k = triMesh.getIndex(i0 + 2); adjacency[i].insert(j); adjacency[j].insert(i); adjacency[i].insert(k); adjacency[k].insert(i); adjacency[j].insert(k); adjacency[k].insert(j); } return adjacency; }
//---------------------------------------------------------- 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); } }
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)); } } } }
ofMesh convertToIndices(const ofMesh& mesh){ ofMesh result; 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 < vertices; i++){ // check if this vertex already exists. //int who = isPointInVector( } }
void Body::computeMeshFromProtoAndTF( ofMesh &m, const ofMesh* protoMesh, ofMatrix4x4 T ) { m = *protoMesh; ofVec3f* vtp = m.getVerticesPointer(); int NumVts = m.getNumVertices(); for(int i=0;i<NumVts;i++) { ofVec3f& vt = *(vtp+i); vt = vt*T; } }
void testApp::addMessage(string address, ofMesh data) { ofxOscMessage msg; msg.setAddress(address); int numOfVertices = data.getNumVertices(); for (int i = 0; i < numOfVertices; i++) { ofVec3f vertex = data.getVertex(i); msg.addFloatArg(vertex.x); msg.addFloatArg(vertex.y); msg.addFloatArg(vertex.z); } bundle.addMessage(msg); }
void buildNormalsAverage(ofMesh& mesh) { vector<ofIndexType>& indices = mesh.getIndices(); vector<ofVec3f> normals(mesh.getNumVertices()); for(int i = 0; i < indices.size(); i += 3) { int i0 = indices[i + 0], i1 = indices[i + 1], i2 = indices[i + 2]; ofVec3f normal = getNormal(mesh.getVertices()[i0], mesh.getVertices()[i1], mesh.getVertices()[i2]); normals[i0] += normal; normals[i1] += normal; normals[i2] += normal; } for(int i = 0; i < normals.size(); i++) { normals[i].normalize(); } mesh.addNormals(normals); }
void getBoundingBox(const ofMesh& mesh, ofVec3f& min, ofVec3f& max) { int n = mesh.getNumVertices(); if(n > 0) { min = mesh.getVertex(0); max = mesh.getVertex(0); for(int i = 1; i < n; i++) { const ofVec3f& cur = mesh.getVertices()[i]; min.x = MIN(min.x, cur.x); min.y = MIN(min.y, cur.y); min.z = MIN(min.z, cur.z); max.x = MAX(max.x, cur.x); max.y = MAX(max.y, cur.y); max.z = MAX(max.z, cur.z); } } }
void getBounds(ofMesh& mesh, ofVec3f& lower, ofVec3f& upper) { for(int i = 0; i < mesh.getNumVertices(); i++) { ofVec3f& cur = mesh.getVertices()[i]; if(i == 0) { lower = cur; upper = cur; } else { lower.x = MIN(lower.x, cur.x); lower.y = MIN(lower.y, cur.y); lower.z = MIN(lower.z, cur.z); upper.x = MAX(upper.x, cur.x); upper.y = MAX(upper.y, cur.y); upper.z = MAX(upper.z, cur.z); } } }
void texturedRect(float width, float height) { if(texturedRectMesh.getNumVertices() == 0) { texturedRectMesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP); texturedRectMesh.addTexCoord(ofVec2f(0, 0)); texturedRectMesh.addVertex(ofVec2f(0, 0)); texturedRectMesh.addTexCoord(ofVec2f(0, 1)); texturedRectMesh.addVertex(ofVec2f(0, 1)); texturedRectMesh.addTexCoord(ofVec2f(1, 0)); texturedRectMesh.addVertex(ofVec2f(1, 0)); texturedRectMesh.addTexCoord(ofVec2f(1, 1)); texturedRectMesh.addVertex(ofVec2f(1, 1)); } ofPushMatrix(); ofScale(width, height); texturedRectMesh.drawFaces(); ofPopMatrix(); }
// // This works for OF_PRIMITIVE_TRIANGLES // from https://gist.github.com/patriciogonzalezvivo/5473484 // void ofApp::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).cross( v3 - v1 ) ).normalize(); //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(); norm[i] *= -1; } //Set the normals to mesh mesh.clearNormals(); mesh.addNormals( norm ); }
//---------------------------------------- void ofxSphere(float radius) { if( sphereScratchMesh.getNumVertices() == 0 || !lastGeneratedSphereParams.equals( radius, currentSphereParams.numRings, currentSphereParams.numSegments ) ){ ofGenerateSphereMesh( sphereScratchMesh, radius, currentSphereParams.numRings, currentSphereParams.numSegments ); // Save the parameters of what we just generated lastGeneratedSphereParams.radius = radius; lastGeneratedSphereParams.numRings = currentSphereParams.numRings; lastGeneratedSphereParams.numSegments = currentSphereParams.numSegments; } if(ofGetStyle().bFill) { sphereScratchMesh.drawFaces(); } else { sphereScratchMesh.drawWireframe(); // this won't really work, but leave it in for now. } }
void MeshHelper::calculateAABoundingBox( const ofMesh& mesh, ofVec3f& topLeftBack, ofVec3f& bottomRightFront ) { ofVec3f& tlb = topLeftBack; ofVec3f& brf = bottomRightFront; for ( int i=0; i<mesh.getNumVertices(); i++ ) { ofVec3f vertex = mesh.getVertex(i); if ( i == 0 ){ tlb = brf = vertex; continue; } tlb.x = min(tlb.x,vertex.x); tlb.y = min(tlb.y,vertex.y); tlb.z = min(tlb.z,vertex.z); brf.x = max(brf.x,vertex.x); brf.y = max(brf.y,vertex.y); brf.z = max(brf.z,vertex.z); } }
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 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(); }
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 ); } }
//-------------------------------------------------------------- void ofVbo::setMesh(const ofMesh & mesh, int usage){ setVertexData(mesh.getVerticesPointer(),mesh.getNumVertices(),usage,sizeof(ofVec3f)); setColorData(mesh.getColorsPointer(),mesh.getNumColors(),usage,sizeof(ofColor)); setNormalData(mesh.getNormalsPointer(),mesh.getNumNormals(),usage,sizeof(ofVec3f)); setTexCoordData(mesh.getTexCoordsPointer(),mesh.getNumTexCoords(),usage,sizeof(ofVec2f)); }
//---------------------------------------------------------- void ofGLRenderer::draw(const ofMesh & vertexData, ofPolyRenderMode renderType, bool useColors, bool useTextures, bool useNormals) const{ if (bSmoothHinted) const_cast<ofGLRenderer*>(this)->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){ 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); } 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) const_cast<ofGLRenderer*>(this)->endSmoothing(); }
void ofxTranslate(ofMesh &mesh, ofVec3f pos) { for (int i=0; i<mesh.getNumVertices(); i++) { mesh.getVertices()[i] += pos; } }