irr::scene::IMesh* CGWIC_Cell::TerrainToMesh(int LOD) { SMesh* out = new SMesh(); CDynamicMeshBuffer* buff = new CDynamicMeshBuffer(EVT_2TCOORDS,EIT_16BIT); terrain->getMeshBufferForLOD(*buff,LOD); const u32 vertCnt = buff->getVertexCount(); S3DVertex2TCoords* mbv = reinterpret_cast<S3DVertex2TCoords*> (buff->getVertexBuffer().getData()); vector3df scale = terrain->getScale(); for (u32 i=0; i<vertCnt; i++) mbv[i].Pos *= scale; out->addMeshBuffer(buff); out->recalculateBoundingBox(); buff->drop(); terrain->setPosition(terrain->getPosition()); return out; }
//! reads a mesh sections and creates a mesh buffer from it IMeshBuffer* CIrrMeshFileLoader::readMeshBuffer(io::IXMLReader* reader) { CDynamicMeshBuffer* buffer = 0; core::stringc verticesSectionName = "vertices"; core::stringc bbSectionName = "boundingBox"; core::stringc materialSectionName = "material"; core::stringc indicesSectionName = "indices"; core::stringc bufferSectionName = "buffer"; bool insideVertexSection = false; bool insideIndexSection = false; int vertexCount = 0; int indexCount = 0; video::SMaterial material; if (!reader->isEmptyElement()) while(reader->read()) { if (reader->getNodeType() == io::EXN_ELEMENT) { const wchar_t* nodeName = reader->getNodeName(); if (bbSectionName == nodeName) { // inside a bounding box, ignore it for now because // we are calculating this anyway ourselves later. } else if (materialSectionName == nodeName) { //we've got a material io::IAttributes* attributes = FileSystem->createEmptyAttributes(SceneManager->getVideoDriver()); attributes->read(reader, true, L"material"); SceneManager->getVideoDriver()->fillMaterialStructureFromAttributes(material, attributes); attributes->drop(); } else if (verticesSectionName == nodeName) { // vertices section const core::stringc vertexTypeName1 = "standard"; const core::stringc vertexTypeName2 = "2tcoords"; const core::stringc vertexTypeName3 = "tangents"; const wchar_t* vertexType = reader->getAttributeValue(L"type"); vertexCount = reader->getAttributeValueAsInt(L"vertexCount"); insideVertexSection = true; video::E_INDEX_TYPE itype = (vertexCount > 65536)?irr::video::EIT_32BIT:irr::video::EIT_16BIT; if (vertexTypeName1 == vertexType) { buffer = new CDynamicMeshBuffer(irr::video::EVT_STANDARD, itype); } else if (vertexTypeName2 == vertexType) { buffer = new CDynamicMeshBuffer(irr::video::EVT_2TCOORDS, itype); } else if (vertexTypeName3 == vertexType) { buffer = new CDynamicMeshBuffer(irr::video::EVT_TANGENTS, itype); } buffer->getVertexBuffer().reallocate(vertexCount); buffer->Material = material; } else if (indicesSectionName == nodeName) { // indices section indexCount = reader->getAttributeValueAsInt(L"indexCount"); insideIndexSection = true; } } // end if node type is element else if (reader->getNodeType() == io::EXN_TEXT) { // read vertex data if (insideVertexSection) { readMeshBuffer(reader, vertexCount, buffer); insideVertexSection = false; } // end reading vertex array else if (insideIndexSection) { readIndices(reader, indexCount, buffer->getIndexBuffer()); insideIndexSection = false; } } // end if node type is text else if (reader->getNodeType() == io::EXN_ELEMENT_END) { if (bufferSectionName == reader->getNodeName()) { // end of buffer section reached, cancel out break; } } } // end while reader->read(); if (buffer) buffer->recalculateBoundingBox(); return buffer; }
IMeshBuffer* CIrrBMeshFileLoader::createMeshBuffer(u32 idx) { CDynamicMeshBuffer* buffer = 0; struct IrrbVertex *pivb; u32 *pindices; video::E_INDEX_TYPE iType=video::EIT_16BIT; struct IrrbMeshBufInfo& mbi=MBuffer[idx]; pivb = &VBuffer[mbi.iVertStart]; pindices = &IBuffer[mbi.iIndexStart]; if(mbi.iIndexCount > 65536) iType = video::EIT_32BIT; buffer = new CDynamicMeshBuffer((video::E_VERTEX_TYPE)mbi.iVertexType, iType); scene::IVertexBuffer& Vertices = buffer->getVertexBuffer(); buffer->Material = Materials[mbi.iMaterialIndex]; for(idx=0; idx<mbi.iVertCount; idx++) { video::S3DVertex vtx0; video::S3DVertex2TCoords vtx1; video::S3DVertexTangents vtx2; video::S3DVertex* vtx=0; if(mbi.iVertexType == irr::video::EVT_2TCOORDS) vtx = &vtx1; else if(mbi.iVertexType == irr::video::EVT_TANGENTS) vtx = &vtx2; else vtx = &vtx0; // set common data vtx->Pos.X = pivb->vPos.x; vtx->Pos.Y = pivb->vPos.y; vtx->Pos.Z = pivb->vPos.z; vtx->Normal.X = pivb->vNormal.x; vtx->Normal.Y = pivb->vNormal.y; vtx->Normal.Z = pivb->vNormal.z; vtx->Color = pivb->vColor; vtx->TCoords.X = pivb->vUV1.x; vtx->TCoords.Y = pivb->vUV1.y; if(mbi.iVertexType == irr::video::EVT_2TCOORDS) { vtx1.TCoords2.X = pivb->vUV2.x; vtx1.TCoords2.Y = pivb->vUV2.y; Vertices.push_back(vtx1); } else if(mbi.iVertexType == irr::video::EVT_TANGENTS) { vtx2.Tangent.X = pivb->vTangent.x; vtx2.Tangent.Y = pivb->vTangent.y; vtx2.Tangent.Z = pivb->vTangent.z; vtx2.Binormal.X = pivb->vBiNormal.x; vtx2.Binormal.Y = pivb->vBiNormal.y; vtx2.Binormal.Z = pivb->vBiNormal.z; Vertices.push_back(vtx2); } else { Vertices.push_back(vtx0); } ++pivb; } scene::IIndexBuffer& Indices = buffer->getIndexBuffer(); for(idx=0; idx<mbi.iIndexCount; idx++) { Indices.push_back(*pindices); ++pindices; } core::aabbox3df mbb(mbi.ibbMin.x,mbi.ibbMin.y,mbi.ibbMin.z, mbi.ibbMax.x,mbi.ibbMax.y,mbi.ibbMax.z); buffer->setBoundingBox(mbb); return buffer; }