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 RenderableObject::Use(VideoDriverPtr pDriver) const
{
	pDriver->Begin();

	const unsigned int n = (unsigned int)m_MeshBuffers.size();
	for (unsigned int i=0; i<n; ++i)
	{
		MeshBufferPtr pMeshBuffer = m_MeshBuffers[i];
		pDriver->SetShaderProgram(m_Materials[i]->GetShaderProgram());
		pDriver->SetVertexDeclaration(pMeshBuffer->GetVertexDeclaration());
		for (unsigned int j=0; j<pMeshBuffer->GetNumVertexBuffers(); j++)
		{
			const MeshBuffer::StreamIndexArray& StreamNumbers = pMeshBuffer->GetStreamIndices();
			const MeshBuffer::DriverVertexBufferArray& DriverVertexBuffers = pMeshBuffer->GetDriverVertexBuffers();
			const MeshBuffer::VertexBufferArray& VertexBuffers = pMeshBuffer->GetVertexBuffers();
			pDriver->SetVertexStream(StreamNumbers[j], DriverVertexBuffers[j], VertexBuffers[j]->GetVertexSize());
			pDriver->DrawIndexedTriangleList(pMeshBuffer->GetDriverIndexBuffer(), VertexBuffers[j]->GetNumVertices(), 
				pMeshBuffer->GetIndexBuffer()->GetNumIndices()/3);
		}
	}

	pDriver->End();
}
ClusterVisualizer::ClusterVisualizer(string filename)
{
	ifstream in(filename.c_str());

	// Check!
	if(!in.good()) return;

	MeshCluster* cluster = new MeshCluster;

	// Read sub meshes
	while(in.good())
	{
		string cluster_name;
		int num_faces;
		int num_vertices;

		// Read 'header'
		in >> cluster_name;
		in >> num_faces >> num_vertices;
		BoundingBox<Vertex<float> > bb;
		// Alloc buffers
		if(num_faces && num_vertices)
		{


			floatArr vertices(new float[3 * num_vertices]);
			floatArr normals(new float[3 * num_vertices]);
			ucharArr colors(new uchar[3 * num_vertices]);
			uintArr indices(new uint[3 * num_faces]);

			// Read indices
			for(int i = 0; i < num_faces; i++)
			{
				int pos = 3 * i;
				uint a, b, c;
				in >> a >> b >> c;
				indices[pos    ] = a;
				indices[pos + 1] = b;
				indices[pos + 2] = c;
			}

			// Read vertices, normals and colors
			for(int i = 0; i < num_vertices; i++)
			{
				int pos = 3 * i;
				float x, y, z, nx, ny, nz;
				int r, g, b;

				in >> x >> y >> z >> nx >> ny >> nz >> r >> g >> b;

				vertices[pos    ] = x;
				vertices[pos + 1] = y;
				vertices[pos + 2] = z;

				normals[pos    ] = nx;
				normals[pos + 1] = ny;
				normals[pos + 2] = nz;

				colors[pos    ] = (uchar)r;
				colors[pos + 1] = (uchar)g;
				colors[pos + 2] = (uchar)b;

				bb.expand(x, y, z);
			}

			// Create Buffer
			MeshBufferPtr* buffer = new MeshBufferPtr(new MeshBuffer);
			buffer->get()->setVertexArray(vertices, num_vertices);
			buffer->get()->setVertexNormalArray(normals, num_vertices);
			buffer->get()->setVertexColorArray(colors, num_vertices);
			buffer->get()->setFaceArray(indices, num_faces);

			cluster->boundingBox()->expand(bb);
			cluster->addMesh(*buffer, cluster_name);

		}

	}
Beispiel #4
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;
 	}
}
  ModelPtr ObjIO::read( string filename ) // TODO: Format correctly
  {
    ifstream f (filename.c_str());
    if (!f.is_open())
      {
	cerr << timestamp << "File '" << filename << "' could not be opened." << endl;
	return ModelPtr( new Model );
      }
    f.close();

    char *fn = const_cast<char *>(filename.c_str());
    objLoader *objData = new objLoader();
    objData->load(fn);

    // Buffer count variables
    size_t numVertices      = objData->vertexCount;
    size_t numVertexNormals = objData->normalCount;
    size_t numFaces         = objData->faceCount;
    size_t numTextures      = objData->textureCount;
    size_t numSpheres       = objData->sphereCount;
    size_t numPlanes        = objData->planeCount;
    size_t numPointLights   = objData->lightPointCount;
    size_t numDiscLights    = objData->lightDiscCount;
    size_t numQuadLights    = objData->lightQuadCount;
    size_t numMaterials     = objData->materialCount;
    size_t numVertexColors  = 0; // TODO: Set

    // Some output
    cout << timestamp << "Number of vertices: "            << numVertices      << endl;
    cout << timestamp << "Number of vertex normals: "      << numVertexNormals << endl;
    cout << timestamp << "Number of vertex colors: "       << numVertexColors  << endl;
    cout << timestamp << "Number of faces: "               << numFaces         << endl;
    cout << timestamp << "Number of texture coordinates: " << numTextures      << endl;
    cout << timestamp << "Number of spheres: "             << numSpheres       << endl;
    cout << timestamp << "Number of planes: "              << numPlanes        << endl;
    cout << timestamp << "Number of point lights: "        << numPointLights   << endl;
    cout << timestamp << "Number of disc lights: "         << numDiscLights    << endl;
    cout << timestamp << "Number of quad lights: "         << numQuadLights    << endl;
    cout << timestamp << "Number of materials: "           << numMaterials     << endl;
    if(objData->camera != NULL)
      {
	cout << timestamp << "Found a camera" << endl;
      }
    cout << endl;

    // Buffers
    floatArr vertices;
    floatArr vertexNormals;
    ucharArr vertexColors;
    uintArr  faceIndices;

    // Allocate memory
    if ( numVertices )
      {
	vertices = floatArr( new float[ numVertices * 3 ] );
      }
    if ( numVertexNormals )
      {
	vertexNormals = floatArr( new float[ numVertices * 3 ] );
      }
    if ( numVertexColors )
      {
	vertexColors = ucharArr( new uchar[ numVertexColors * 3 ] );
      }
    if ( numFaces )
      {
	faceIndices = uintArr( new unsigned int[ numFaces * 3 ] );
      }

    // vertices
    for(int i = 0; i < numVertices; ++i)
      {
        obj_vector *o = objData->vertexList[i];
	vertices[ i * 3 ]     = o->e[ 0 ];
	vertices[ i * 3 + 1 ] = o->e[ 1 ];
	vertices[ i * 3 + 2 ] = o->e[ 2 ];
      }
    
    // normals
    for(int i = 0; i < numVertexNormals; ++i)
      {
        obj_vector *o = objData->normalList[i];
	vertexNormals[ i * 3 ]     = o->e[ 0 ];
	vertexNormals[ i * 3 + 1 ] = o->e[ 1 ];
	vertexNormals[ i * 3 + 2 ] = o->e[ 2 ];
      }

    // colors
    for(int i = 0; i < numVertexColors; ++i)
      {
	vertexColors[ i * 3 ]    = 255;
	vertexColors[ i * 3 + 1] = 0;
	vertexColors[ i * 3 + 2] = 0;
      }
    
    // faces
    for(int i = 0; i < numFaces; ++i)
      {
	obj_face *o              = objData->faceList[ i ];
	faceIndices[ i * 3 ]     = o->vertex_index[ 0 ];
	faceIndices[ i * 3 + 1 ] = o->vertex_index[ 1 ];
	faceIndices[ i * 3 + 2 ] = o->vertex_index[ 2 ];
      }

    // delete obj data object
    delete(objData);

    // Save buffers in model
    MeshBufferPtr mesh;
    if(vertices)
      {
	mesh = MeshBufferPtr( new MeshBuffer );
	mesh->setVertexArray(           vertices,         numVertices );
	mesh->setVertexNormalArray(     vertexNormals,    numVertexNormals );
	mesh->setVertexColorArray(      vertexColors,     numVertexColors );
	mesh->setFaceArray(             faceIndices,      numFaces );
      }

    ModelPtr m( new Model( mesh ) );
    m_model = m;
    return m;
  };