//---------------------------------------------------------- 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 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); }
//-------------------------------------------------------------- void ofVboByteColor::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 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 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 ofxBulletSoftTriMesh::updateMesh( ofMesh& aMesh ) { int totalNodes = getNumNodes(); auto& tverts = aMesh.getVertices(); if( _cachedMesh.getMode() == OF_PRIMITIVE_TRIANGLES ) { if( tverts.size() != totalNodes ) { tverts.resize( totalNodes ); } auto& tnormals = aMesh.getNormals(); if( aMesh.getNumNormals() != totalNodes ) { tnormals.resize( totalNodes ); } for( int i = 0; i < totalNodes; i++ ) { tverts[i].x = _softBody->m_nodes[i].m_x.x(); tverts[i].y = _softBody->m_nodes[i].m_x.y(); tverts[i].z = _softBody->m_nodes[i].m_x.z(); tnormals[i].x = _softBody->m_nodes[i].m_n.x(); tnormals[i].y = _softBody->m_nodes[i].m_n.y(); tnormals[i].z = _softBody->m_nodes[i].m_n.z(); } } _lastMeshUpdateFrame = ofGetFrameNum(); }
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; }
//---------------------------------------------------------- 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 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); } }
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 ofxBulletSoftBody::updateMesh( ofMesh& aMesh ) { int totalNodes = getNumNodes(); int totalFaces = getNumFaces(); auto& tverts = aMesh.getVertices(); if( _cachedMesh.getMode() == OF_PRIMITIVE_TRIANGLES ) { if( tverts.size() != totalFaces * 3 ) { tverts.resize( totalFaces * 3 ); } auto& tnormals = aMesh.getNormals(); if( aMesh.getNumNormals() != totalFaces * 3 ) { tnormals.resize( totalFaces * 3 ); } for (int i = 0; i < totalFaces; i += 1 ) { for (int j = 0; j < 3; ++j) { tverts[ i * 3 + j ].x = _softBody->m_faces.at(i).m_n[j]->m_x.x(); tverts[ i * 3 + j ].y = _softBody->m_faces.at(i).m_n[j]->m_x.y(); tverts[ i * 3 + j ].z = _softBody->m_faces.at(i).m_n[j]->m_x.z(); tnormals[ i * 3 + j ].x = _softBody->m_faces.at(i).m_n[j]->m_n.x(); tnormals[ i * 3 + j ].y = _softBody->m_faces.at(i).m_n[j]->m_n.y(); tnormals[ i * 3 + j ].z = _softBody->m_faces.at(i).m_n[j]->m_n.z(); } } } else if( _cachedMesh.getMode() == OF_PRIMITIVE_LINE_STRIP ) { if( tverts.size() != totalNodes ) { tverts.resize( totalNodes ); } for( int i = 0; i < totalNodes; i++ ) { tverts[i].x = _softBody->m_nodes[i].m_x.x(); tverts[i].y = _softBody->m_nodes[i].m_x.y(); tverts[i].z = _softBody->m_nodes[i].m_x.z(); } } _lastMeshUpdateFrame = ofGetFrameNum(); }
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 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 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; } } }