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(); }
void VoxelTerrain::mesh() { PolyVox::SurfaceMesh<PolyVox::PositionMaterialNormal> mesh; PolyVox::Region region(PolyVox::Vector3DInt32(0,0,0), PolyVox::Vector3DInt32(120, 60, 120)); //PolyVox::Vector3DInt32(120, 130, 120)); std::cout << "Begin extracting surface." << std::endl; //PolyVox::MarchingCubesSurfaceExtractor<PolyVox::LargeVolume<BYTE> > PolyVox::CubicSurfaceExtractorWithNormals<PolyVox::LargeVolume<BYTE> > surface_extractor(mVolumeData, region, &mesh); surface_extractor.execute(); std::cout << "Done extracting surface." << std::endl; begin("Terrain", Ogre::RenderOperation::OT_TRIANGLE_LIST); { const std::vector<PolyVox::PositionMaterialNormal>& vecVertices = mesh.getVertices(); const std::vector<uint32_t>& vecIndices = mesh.getIndices(); unsigned int uLodLevel = 0; int beginIndex = mesh.m_vecLodRecords[uLodLevel].beginIndex; int endIndex = mesh.m_vecLodRecords[uLodLevel].endIndex; for(int index = beginIndex; index < endIndex; ++index) { const PolyVox::PositionMaterialNormal& vertex = vecVertices[vecIndices[index]]; const PolyVox::Vector3DFloat& v3dVertexPos = vertex.getPosition(); const PolyVox::Vector3DFloat& v3dVertexNormal = vertex.getNormal(); const PolyVox::Vector3DFloat v3dFinalVertexPos = v3dVertexPos + static_cast<PolyVox::Vector3DFloat>(mesh.m_Region.getLowerCorner()); position(v3dFinalVertexPos.getX(), v3dFinalVertexPos.getY(), v3dFinalVertexPos.getZ()); normal(v3dVertexNormal.getX(), v3dVertexNormal.getY(), v3dVertexNormal.getZ()); //colour(1, 0, 0, 0.5); textureCoord(v3dFinalVertexPos.getX()/4.0f, v3dFinalVertexPos.getY()/4.0f, v3dFinalVertexPos.getZ()/4.0f); } } end(); }
// Called when the plugin is loaded. void create() { // Load Shader Graphics::ShaderAsset* shaderAsset = Link.Graphics.getShaderAsset("PolyVox:renderShader"); if (shaderAsset) mShader = shaderAsset->getProgram(); // Generate polyvox sphere. createSphereInVolume(volData, 30); PolyVox::SurfaceMesh<PolyVox::PositionMaterialNormal> surfaceMesh; PolyVox::CubicSurfaceExtractorWithNormals< PolyVox::SimpleVolume<U8> > surfaceExtractor(&volData, volData.getEnclosingRegion(), &surfaceMesh); surfaceExtractor.execute(); // Verts const std::vector<PolyVox::PositionMaterialNormal>& vecVertices = surfaceMesh.getVertices(); mVerts = new PosUVNormalVertex[vecVertices.size()]; for (U32 i = 0; i < vecVertices.size(); ++i) { mVerts[mVertCount].m_x = vecVertices[i].position.getX(); mVerts[mVertCount].m_y = vecVertices[i].position.getY(); mVerts[mVertCount].m_z = vecVertices[i].position.getZ(); mVerts[mVertCount].m_u = 0.0f; mVerts[mVertCount].m_v = 0.0f; mVerts[mVertCount].m_normal_x = vecVertices[i].normal.getX(); mVerts[mVertCount].m_normal_y = vecVertices[i].normal.getY(); mVerts[mVertCount].m_normal_z = vecVertices[i].normal.getZ(); mVertCount++; } // Indices const std::vector<U32>& vecIndices = surfaceMesh.getIndices(); mIndices = new U32[vecIndices.size()]; for (U32 i = 0; i < vecIndices.size(); ++i) { mIndices[mIndexCount] = vecIndices[mIndexCount]; mIndexCount++; } refresh(); }
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; } } }