示例#1
0
void ofxMesh::toMesh(ofMesh & mesh){
    mesh.clear();
    if (hasVertices()) {
        mesh.getVertices()=getVertices();
    }
    if (hasColors()) {
        mesh.getColors()=getColors();
    }
    if (hasNormals()) {
        mesh.getNormals()=getNormals();
    }
    if (hasTexCoords()) {
        mesh.getTexCoords()=getTexCoords();
    }
    if (hasIndices()) {
        mesh.getIndices()=getIndices();
    }
}
void ofxOBJModel::loadVbo() {
	int count = 0;
	for(int i = 0; i < meshes.size(); i++) {
		for(int j = 0; j < meshes[i]->faces.size(); j++) {
			
			int pointCount = meshes[i]->faces[j]->points.size();
			
			if(pointCount==3) {
				count += 3;
			} else {
				// if the poly has more than 3 sides, 
				// we're gonna do a triangle fan.
				// 4 points goes to 6 points
				// 5 points goes to 9 points
				// 6 points goes to 12 points
				count += (pointCount - 2)*3;
			}
			
		}
	}
	coords = new ofVec3f[count];
	ofVec3f *normals = NULL;
	ofVec2f *texCoords = NULL;
	if(hasNormals()) normals = new ofVec3f[count];
	if(hasTexCoords()) texCoords = new ofVec2f[count];
	int pos = 0; 
	for(int i = 0; i < meshes.size(); i++) {
		for(int j = 0; j < meshes[i]->faces.size(); j++) {
			
			int pointCount = meshes[i]->faces[j]->points.size();
			if(pointCount==3) {
				for(int k = 0; k < pointCount; k++) {
					coords[pos] = meshes[i]->faces[j]->points[k];
				
					if(normals!=NULL) {
						normals[pos] = meshes[i]->faces[j]->normals[k];
					}
					if(texCoords!=NULL) {
						texCoords[pos] = meshes[i]->faces[j]->texCoords[k];
					}
					
					pos++;
				}
			} else {
				// triangle fan
				int triCount = pointCount - 2;
				for(int tri = 0; tri < triCount; tri++) {
					for(int p = 0; p < 3; p++) {
						coords[pos] = meshes[i]->faces[j]->points[tri+p];
						
						if(normals!=NULL) {
							normals[pos] = meshes[i]->faces[j]->normals[tri+p];
						}
						if(texCoords!=NULL) {
							texCoords[pos] = meshes[i]->faces[j]->texCoords[tri+p];
						}
						pos++;
					}
				}
			}
		}
	}
	
	vbo.clear();
	
	vbo.setVertexData(coords, count, GL_STATIC_DRAW_ARB);
	
	if(normals!=NULL) vbo.setNormalData(normals, count, GL_STATIC_DRAW_ARB);
	if(texCoords!=NULL) vbo.setTexCoordData(texCoords, count, GL_STATIC_DRAW_ARB);
	
	//delete [] coords;
	if(normals!=NULL) delete [] normals;
	if(texCoords!=NULL) delete [] texCoords;
	vboCoordCount = count;
}