Ejemplo n.º 1
0
void OpenGLWidget::setSurfaceMeshToRender(const PolyVox::SurfaceMesh<PositionMaterialNormal>& surfaceMesh)
{
	if((surfaceMesh.getNoOfIndices() == 0) || (surfaceMesh.getNoOfVertices() == 0))
	{
		//We don't have a valid mesh
		return;
	}

	//Convienient access to the vertices and indices
	const vector<uint32_t>& vecIndices = surfaceMesh.getIndices();
	const vector<PositionMaterialNormal>& vecVertices = surfaceMesh.getVertices();

	//Build an OpenGL index buffer
	glGenBuffers(1, &indexBuffer);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
	const GLvoid* pIndices = static_cast<const GLvoid*>(&(vecIndices[0]));		
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, vecIndices.size() * sizeof(uint32_t), pIndices, GL_STATIC_DRAW);

	//Build an OpenGL vertex buffer
	glGenBuffers(1, &vertexBuffer);
	glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
	const GLvoid* pVertices = static_cast<const GLvoid*>(&(vecVertices[0]));	
	glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(PositionMaterialNormal), pVertices, GL_STATIC_DRAW);

	m_uBeginIndex = 0;
	m_uEndIndex = vecIndices.size();
	noOfIndices = surfaceMesh.getNoOfIndices();
}
Ejemplo n.º 2
0
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;
        }

    }

}