void StaticMesh::init( MeshBufferPtr mesh ) { size_t n_colors; size_t n_normals; m_lineWidth = 2.0; if(mesh) { m_faceNormals = 0; m_normals = mesh->getVertexNormalArray(n_normals); m_colors = mesh->getVertexColorArray(n_colors); m_vertices = mesh->getVertexArray(m_numVertices); m_faces = mesh->getFaceArray(m_numFaces); m_blackColors = new unsigned char[ 3 * m_numVertices ]; for ( size_t i = 0; i < 3 * m_numVertices; i++ ) { m_blackColors[i] = 0; } m_finalized = true; m_visible = true; m_active = true; m_renderMode = 0; m_renderMode |= RenderSurfaces; m_renderMode |= RenderTriangles; m_boundingBox = new BoundingBox<Vertex<float> >; if(!m_faceNormals) { interpolateNormals(); } else { // cout << "Face normals: " << m_faceNormals << endl; } if(!m_colors) { setDefaultColors(); } if(n_colors == 0) { m_colors = ucharArr( new unsigned char[3 * m_numVertices] ); for( int i = 0; i < m_numVertices; ++i ) { m_colors[3 * i] = 0; m_colors[3 * i + 1] = 255; m_colors[3 * i + 2] = 0; } } } }
void STLIO::save( ModelPtr model, string filename ) { MeshBufferPtr mesh = model->m_mesh; size_t n_vert; size_t n_faces; floatArr vertices = mesh->getVertexArray(n_vert); uintArr indices = mesh->getFaceArray(n_faces); std::string header_info = "Created by LVR"; char head[80]; std::strncpy(head,header_info.c_str(),sizeof(head)-1); char attribute[2] = "0"; std::ofstream myfile(filename.c_str()); myfile.write(head,sizeof(head)); myfile.write((char*)&n_faces,4); if(myfile.good()) { for(int i = 0; i < n_faces; i++) { int a = (int)indices[3 * i]; int b = (int)indices[3 * i + 1]; int c = (int)indices[3 * i + 2]; Vertexf v1; Vertexf v2; Vertexf v3; v1.x = vertices[3 * a]; v1.y = vertices[3 * a + 1]; v1.z = vertices[3 * a + 2]; v2.x = vertices[3 * b]; v2.y = vertices[3 * b + 1]; v2.z = vertices[3 * b + 2]; v3.x = vertices[3 * c]; v3.y = vertices[3 * c + 1]; v3.z = vertices[3 * c + 2]; Normalf normal( (v1 - v2).cross(v1 - v3)); myfile.write( (char*)&normal.x, 4); myfile.write( (char*)&normal.y, 4); myfile.write( (char*)&normal.z, 4); myfile.write( (char*)&v1.x, 4); myfile.write( (char*)&v1.y, 4); myfile.write( (char*)&v1.z, 4); myfile.write( (char*)&v2.x, 4); myfile.write( (char*)&v2.y, 4); myfile.write( (char*)&v2.z, 4); myfile.write( (char*)&v3.x, 4); myfile.write( (char*)&v3.y, 4); myfile.write( (char*)&v3.z, 4); uint16_t u = 0; myfile.write( (char*)attribute, 2 ); } } else { cout << timestamp << "Could not open file " << filename << " for writing." << endl; } }