SDGeometryHandle compile_geometry(SDRenderMode mode, SDVec2* vertices, SDuint numVertices, SDuint* indices, SDuint numIndices) { kglt::MeshID new_mesh = stage()->new_mesh(); { auto mesh = stage()->mesh(new_mesh); auto submesh = mesh->new_submesh( stage()->get_material_with_alias("diffuse_render"), (mode == SD_RENDER_MODE_TRIANGLES) ? kglt::MESH_ARRANGEMENT_TRIANGLES : kglt::MESH_ARRANGEMENT_LINES, true ); for(SDuint i = 0; i < numVertices; ++i) { mesh->shared_data().position(vertices[i]); mesh->shared_data().diffuse(kglt::Colour::WHITE); mesh->shared_data().move_next(); } for(SDuint i = 0; i < numIndices; ++i) { mesh->submesh(submesh).index_data().index(indices[i]); } mesh->submesh(submesh).index_data().done(); mesh->shared_data().done(); } kglt::ActorID new_actor = stage()->new_actor(new_mesh); return new_actor.value(); }
void Boid::update_debug_mesh() const { Stage* stage = actor_->stage(); assert(stage); auto mesh = stage->mesh(debug_mesh_).lock(); auto& vd = mesh->submesh(normal_points_mesh_).vertex_data(); auto& id = mesh->submesh(normal_points_mesh_).index_data(); vd.clear(); id.clear(); vd.move_to_start(); int i = 0; for(kglt::Vec3 p: normal_points_) { vd.position(p); vd.diffuse(kglt::Colour::red); vd.tex_coord0(kglt::Vec2()); vd.tex_coord1(kglt::Vec2()); vd.normal(kglt::Vec3()); vd.move_next(); id.index(i++); } id.done(); vd.done(); }
void Debug::draw_line(const Vec3 &start, const Vec3 &end, const Colour &colour, double duration, bool depth_test) { auto mesh = stage_.mesh(mesh_); DebugElement element; element.submesh = mesh->new_submesh(material_, MESH_ARRANGEMENT_LINE_STRIP, /*shares_vertices=*/ false); element.colour = colour; element.duration = duration; element.depth_test = depth_test; element.type = DebugElementType::DET_LINE; auto& submesh = mesh->submesh(element.submesh); submesh.vertex_data().move_to_start(); submesh.vertex_data().position(start); submesh.vertex_data().diffuse(colour); submesh.vertex_data().move_next(); submesh.vertex_data().position(end); submesh.vertex_data().diffuse(colour); submesh.vertex_data().done(); submesh.index_data().index(0); submesh.index_data().index(1); submesh.index_data().done(); elements_.push_back(element); }
const MaterialID SubActor::material_id() const { if(material_) { return material_->id(); } return submesh().material_id(); }
MeshID ResourceManagerImpl::new_mesh_from_vertices(const std::vector<Vec3> &vertices, MeshArrangement arrangement, bool garbage_collect) { //FIXME: THis is literally a copy/paste of the function above, we can templatize this MeshID m = new_mesh(garbage_collect); auto new_mesh = mesh(m); auto smi = new_mesh->new_submesh(arrangement); int i = 0; for(auto v: vertices) { new_mesh->shared_data().position(v); new_mesh->shared_data().diffuse(kglt::Colour::WHITE); new_mesh->shared_data().normal(kglt::Vec3()); new_mesh->shared_data().tex_coord0(kglt::Vec2()); new_mesh->shared_data().move_next(); new_mesh->submesh(smi)->index_data().index(i++); } new_mesh->shared_data().done(); new_mesh->submesh(smi)->index_data().done(); MeshManager::mark_as_uncollected(m); return m; }
void Boid::enable_debug(bool value) { assert(actor_); Stage* stage = actor_->stage(); assert(stage); if(value) { if(!debug_mesh_) { debug_mesh_ = stage->new_mesh(); } auto mesh = stage->mesh(debug_mesh_).lock(); mesh->clear(); auto smi = mesh->new_submesh(kglt::MaterialID(), MESH_ARRANGEMENT_LINE_STRIP, false); normal_points_mesh_ = mesh->new_submesh(kglt::MaterialID(), MESH_ARRANGEMENT_POINTS, false); { auto mat = stage->material(mesh->submesh(normal_points_mesh_).material_id()); mat->technique().pass(0).set_point_size(5); } for(uint32_t i = 0; i < path_.length(); ++i) { mesh->submesh(smi).vertex_data().position(path_.point(i)); mesh->submesh(smi).vertex_data().diffuse(kglt::Colour::blue); mesh->submesh(smi).vertex_data().tex_coord0(kglt::Vec2()); mesh->submesh(smi).vertex_data().tex_coord1(kglt::Vec2()); mesh->submesh(smi).vertex_data().normal(kglt::Vec3()); mesh->submesh(smi).vertex_data().move_next(); mesh->submesh(smi).index_data().index(i); } mesh->submesh(smi).vertex_data().done(); mesh->submesh(smi).index_data().done(); if(!debug_actor_) { debug_actor_ = stage->new_actor(debug_mesh_); } } else { stage->delete_actor(debug_actor_); debug_actor_ = kglt::ActorID(); debug_mesh_ = kglt::MeshID(); } }
bool ModelBackgroundLoader::poll(const TimeFrame& timeFrame) { #if OGRE_THREAD_SUPPORT if (mState == LS_UNINITIALIZED) { //Start to load the meshes for (SubModelDefinitionsStore::const_iterator I_subModels = mModel.getDefinition()->getSubModelDefinitions().begin(); I_subModels != mModel.getDefinition()->getSubModelDefinitions().end(); ++I_subModels) { Ogre::MeshPtr meshPtr = static_cast<Ogre::MeshPtr> (Ogre::MeshManager::getSingleton().getByName((*I_subModels)->getMeshName())); if (meshPtr.isNull() || !meshPtr->isPrepared()) { try { Ogre::BackgroundProcessTicket ticket = Ogre::ResourceBackgroundQueue::getSingleton().prepare(Ogre::MeshManager::getSingleton().getResourceType(), (*I_subModels)->getMeshName(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, false, 0, 0, createListener()); if (ticket) { addTicket(ticket); } } catch (const std::exception& ex) { S_LOG_FAILURE("Could not load the mesh " << (*I_subModels)->getMeshName() << " when loading model " << mModel.getName() << "." << ex); continue; } } } mState = LS_MESH_PREPARING; return poll(timeFrame); } else if (mState == LS_MESH_PREPARING) { if (areAllTicketsProcessed()) { mState = LS_MESH_PREPARED; return poll(timeFrame); } } else if (mState == LS_MESH_PREPARED) { for (SubModelDefinitionsStore::const_iterator I_subModels = mModel.getDefinition()->getSubModelDefinitions().begin(); I_subModels != mModel.getDefinition()->getSubModelDefinitions().end(); ++I_subModels) { Ogre::MeshPtr meshPtr = static_cast<Ogre::MeshPtr> (Ogre::MeshManager::getSingleton().getByName((*I_subModels)->getMeshName())); if (!meshPtr.isNull()) { if (!meshPtr->isLoaded()) { #if OGRE_THREAD_SUPPORT == 1 Ogre::BackgroundProcessTicket ticket = Ogre::ResourceBackgroundQueue::getSingleton().load(Ogre::MeshManager::getSingleton().getResourceType(), meshPtr->getName(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, false, 0, 0, createListener()); if (ticket) { // meshPtr->setBackgroundLoaded(true); addTicket(ticket); } #else if (!timeFrame.isTimeLeft()) { return false; } try { meshPtr->load(); } catch (const std::exception& ex) { S_LOG_FAILURE("Could not load the mesh " << meshPtr->getName() << " when loading model " << mModel.getName() << "." << ex); continue; } #endif } } } mState = LS_MESH_LOADING; return poll(timeFrame); } else if (mState == LS_MESH_LOADING) { if (areAllTicketsProcessed()) { mState = LS_MESH_LOADED; return poll(timeFrame); } } else if (mState == LS_MESH_LOADED) { for (SubModelDefinitionsStore::const_iterator I_subModels = mModel.getDefinition()->getSubModelDefinitions().begin(); I_subModels != mModel.getDefinition()->getSubModelDefinitions().end(); ++I_subModels) { Ogre::MeshPtr meshPtr = static_cast<Ogre::MeshPtr> (Ogre::MeshManager::getSingleton().getByName((*I_subModels)->getMeshName())); if (!meshPtr.isNull()) { if (meshPtr->isLoaded()) { Ogre::Mesh::SubMeshIterator subMeshI = meshPtr->getSubMeshIterator(); while (subMeshI.hasMoreElements()) { Ogre::SubMesh* submesh(subMeshI.getNext()); Ogre::MaterialPtr materialPtr = static_cast<Ogre::MaterialPtr> (Ogre::MaterialManager::getSingleton().getByName(submesh->getMaterialName())); if (materialPtr.isNull() || !materialPtr->isPrepared()) { // S_LOG_VERBOSE("Preparing material " << materialPtr->getName()); Ogre::BackgroundProcessTicket ticket = Ogre::ResourceBackgroundQueue::getSingleton().prepare(Ogre::MaterialManager::getSingleton().getResourceType(),submesh->getMaterialName(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, false, 0, 0, createListener()); if (ticket) { addTicket(ticket); } } } } } for (PartDefinitionsStore::const_iterator I_parts = (*I_subModels)->getPartDefinitions().begin(); I_parts != (*I_subModels)->getPartDefinitions().end(); ++I_parts) { if ((*I_parts)->getSubEntityDefinitions().size() > 0) { for (SubEntityDefinitionsStore::const_iterator I_subEntities = (*I_parts)->getSubEntityDefinitions().begin(); I_subEntities != (*I_parts)->getSubEntityDefinitions().end(); ++I_subEntities) { const std::string& materialName = (*I_subEntities)->getMaterialName(); if (materialName != "") { Ogre::MaterialPtr materialPtr = static_cast<Ogre::MaterialPtr> (Ogre::MaterialManager::getSingleton().getByName(materialName)); if (materialPtr.isNull() || !materialPtr->isPrepared()) { // S_LOG_VERBOSE("Preparing material " << materialName); Ogre::BackgroundProcessTicket ticket = Ogre::ResourceBackgroundQueue::getSingleton().prepare(Ogre::MaterialManager::getSingleton().getResourceType(), materialName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, false, 0, 0, createListener()); if (ticket) { addTicket(ticket); } } } } } } } mState = LS_MATERIAL_PREPARING; return poll(timeFrame); } else if (mState == LS_MATERIAL_PREPARING) { if (areAllTicketsProcessed()) { mState = LS_MATERIAL_PREPARED; return poll(timeFrame); } } else if (mState == LS_MATERIAL_PREPARED) { for (SubModelDefinitionsStore::const_iterator I_subModels = mModel.getDefinition()->getSubModelDefinitions().begin(); I_subModels != mModel.getDefinition()->getSubModelDefinitions().end(); ++I_subModels) { Ogre::MeshPtr meshPtr = static_cast<Ogre::MeshPtr> (Ogre::MeshManager::getSingleton().getByName((*I_subModels)->getMeshName())); Ogre::Mesh::SubMeshIterator subMeshI = meshPtr->getSubMeshIterator(); while (subMeshI.hasMoreElements()) { Ogre::SubMesh* submesh(subMeshI.getNext()); Ogre::MaterialPtr materialPtr = static_cast<Ogre::MaterialPtr> (Ogre::MaterialManager::getSingleton().getByName(submesh->getMaterialName())); if (!materialPtr.isNull() && !materialPtr->isLoaded()) { #if OGRE_THREAD_SUPPORT == 1 Ogre::BackgroundProcessTicket ticket = Ogre::ResourceBackgroundQueue::getSingleton().load(Ogre::MaterialManager::getSingleton().getResourceType(), materialPtr->getName(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, false, 0, 0, createListener()); if (ticket) { // materialPtr->setBackgroundLoaded(true); addTicket(ticket); } #else Ogre::Material::TechniqueIterator techIter = materialPtr->getSupportedTechniqueIterator(); while (techIter.hasMoreElements()) { Ogre::Technique* tech = techIter.getNext(); Ogre::Technique::PassIterator passIter = tech->getPassIterator(); while (passIter.hasMoreElements()) { Ogre::Pass* pass = passIter.getNext(); Ogre::Pass::TextureUnitStateIterator tusIter = pass->getTextureUnitStateIterator(); while (tusIter.hasMoreElements()) { Ogre::TextureUnitState* tus = tusIter.getNext(); unsigned int frames = tus->getNumFrames(); for (unsigned int i = 0; i < frames; ++i) { if (!timeFrame.isTimeLeft()) { return false; } //This will automatically load the texture. // S_LOG_VERBOSE("Loading texture " << tus->getTextureName()); Ogre::TexturePtr texturePtr = tus->_getTexturePtr(i); } } } } if (!timeFrame.isTimeLeft()) { return false; } // S_LOG_VERBOSE("Loading material " << materialPtr->getName()); materialPtr->load(); #endif } } for (PartDefinitionsStore::const_iterator I_parts = (*I_subModels)->getPartDefinitions().begin(); I_parts != (*I_subModels)->getPartDefinitions().end(); ++I_parts) { if ((*I_parts)->getSubEntityDefinitions().size() > 0) { for (SubEntityDefinitionsStore::const_iterator I_subEntities = (*I_parts)->getSubEntityDefinitions().begin(); I_subEntities != (*I_parts)->getSubEntityDefinitions().end(); ++I_subEntities) { const std::string& materialName = (*I_subEntities)->getMaterialName(); if (materialName != "") { Ogre::MaterialPtr materialPtr = static_cast<Ogre::MaterialPtr> (Ogre::MaterialManager::getSingleton().getByName(materialName)); if (!materialPtr.isNull() && !materialPtr->isLoaded()) { #if OGRE_THREAD_SUPPORT == 1 Ogre::BackgroundProcessTicket ticket = Ogre::ResourceBackgroundQueue::getSingleton().load(Ogre::MaterialManager::getSingleton().getResourceType(), materialPtr->getName(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, false, 0, 0, createListener()); if (ticket) { addTicket(ticket); } #else Ogre::Material::TechniqueIterator techIter = materialPtr->getSupportedTechniqueIterator(); while (techIter.hasMoreElements()) { Ogre::Technique* tech = techIter.getNext(); Ogre::Technique::PassIterator passIter = tech->getPassIterator(); while (passIter.hasMoreElements()) { Ogre::Pass* pass = passIter.getNext(); Ogre::Pass::TextureUnitStateIterator tusIter = pass->getTextureUnitStateIterator(); while (tusIter.hasMoreElements()) { Ogre::TextureUnitState* tus = tusIter.getNext(); unsigned int frames = tus->getNumFrames(); for (unsigned int i = 0; i < frames; ++i) { if (!timeFrame.isTimeLeft()) { return false; } //This will automatically load the texture. // S_LOG_VERBOSE("Loading texture " << tus->getTextureName()); Ogre::TexturePtr texturePtr = tus->_getTexturePtr(i); } } } } if (!timeFrame.isTimeLeft()) { return false; } // S_LOG_VERBOSE("Loading material " << materialPtr->getName()); materialPtr->load(); #endif } } } } } } mState = LS_MATERIAL_LOADING; return poll(timeFrame); } else if (mState == LS_MATERIAL_LOADING) { if (areAllTicketsProcessed()) { mState = LS_DONE; return true; } } else { return true; } return false; #else //If there's no threading support, just return here. return true; #endif }
bool CalSaver::saveXmlCoreMesh(const std::string& strFilename, CalCoreMesh *pCoreMesh) { std::stringstream str; vsxTiXmlDocument doc(strFilename); vsxTiXmlElement mesh("MESH"); //mesh.SetAttribute("MAGIC",Cal::MESH_XMLFILE_MAGIC); mesh.SetAttribute("VERSION",Cal::LIBRARY_VERSION); mesh.SetAttribute("NUMSUBMESH",pCoreMesh->getCoreSubmeshCount()); // get the submesh vector std::vector<CalCoreSubmesh *>& vectorCoreSubmesh = pCoreMesh->getVectorCoreSubmesh(); // write all core submeshes int submeshId; for(submeshId = 0; submeshId < (int)vectorCoreSubmesh.size(); ++submeshId) { CalCoreSubmesh *pCoreSubmesh=vectorCoreSubmesh[submeshId]; vsxTiXmlElement submesh("SUBMESH"); submesh.SetAttribute("NUMVERTICES",pCoreSubmesh->getVertexCount()); submesh.SetAttribute("NUMFACES",pCoreSubmesh->getFaceCount()); submesh.SetAttribute("MATERIAL",pCoreSubmesh->getCoreMaterialThreadId()); submesh.SetAttribute("NUMLODSTEPS",pCoreSubmesh->getLodCount()); submesh.SetAttribute("NUMSPRINGS",pCoreSubmesh->getSpringCount()); submesh.SetAttribute("NUMTEXCOORDS",pCoreSubmesh->getVectorVectorTextureCoordinate().size()); // get the vertex, face, physical property and spring vector std::vector<CalCoreSubmesh::Vertex>& vectorVertex = pCoreSubmesh->getVectorVertex(); std::vector<CalCoreSubmesh::Face>& vectorFace = pCoreSubmesh->getVectorFace(); std::vector<CalCoreSubmesh::PhysicalProperty>& vectorPhysicalProperty = pCoreSubmesh->getVectorPhysicalProperty(); std::vector<CalCoreSubmesh::Spring>& vectorSpring = pCoreSubmesh->getVectorSpring(); // get the texture coordinate vector vector std::vector<std::vector<CalCoreSubmesh::TextureCoordinate> >& vectorvectorTextureCoordinate = pCoreSubmesh->getVectorVectorTextureCoordinate(); // write all vertices int vertexId; for(vertexId = 0; vertexId < (int)vectorVertex.size(); ++vertexId) { CalCoreSubmesh::Vertex& Vertex = vectorVertex[vertexId]; vsxTiXmlElement vertex("VERTEX"); vertex.SetAttribute("ID",vertexId); vertex.SetAttribute("NUMINFLUENCES",Vertex.vectorInfluence.size()); // write the vertex data vsxTiXmlElement position("POS"); str.str(""); str << Vertex.position.x << " " << Vertex.position.y << " " << Vertex.position.z; vsxTiXmlText positiondata(str.str()); position.InsertEndChild(positiondata); vertex.InsertEndChild(position); vsxTiXmlElement normal("NORM"); str.str(""); str << Vertex.normal.x << " " << Vertex.normal.y << " " << Vertex.normal.z; vsxTiXmlText normaldata(str.str()); normal.InsertEndChild(normaldata); vertex.InsertEndChild(normal); if(Vertex.collapseId!=-1) { vsxTiXmlElement collapse("COLLAPSEID"); str.str(""); str << Vertex.collapseId; vsxTiXmlText collapseid(str.str()); collapse.InsertEndChild(collapseid); vertex.InsertEndChild(collapse); vsxTiXmlElement collapsecount("COLLAPSECOUNT"); str.str(""); str << Vertex.faceCollapseCount; vsxTiXmlText collapsecountdata(str.str()); collapsecount.InsertEndChild(collapsecountdata); vertex.InsertEndChild(collapsecount); } // write all texture coordinates of this vertex int textureCoordinateId; for(textureCoordinateId = 0; textureCoordinateId < (int)vectorvectorTextureCoordinate.size(); ++textureCoordinateId) { CalCoreSubmesh::TextureCoordinate& textureCoordinate = vectorvectorTextureCoordinate[textureCoordinateId][vertexId]; vsxTiXmlElement tex("TEXCOORD"); str.str(""); str << textureCoordinate.u << " " << textureCoordinate.v; vsxTiXmlText texdata(str.str()); tex.InsertEndChild(texdata); vertex.InsertEndChild(tex); } // write all influences of this vertex int influenceId; for(influenceId = 0; influenceId < (int)Vertex.vectorInfluence.size(); ++influenceId) { CalCoreSubmesh::Influence& Influence = Vertex.vectorInfluence[influenceId]; vsxTiXmlElement influence("INFLUENCE"); influence.SetAttribute("ID",Influence.boneId); str.str(""); str << Influence.weight; vsxTiXmlText influencedata(str.str()); influence.InsertEndChild(influencedata); vertex.InsertEndChild(influence); } // save the physical property of the vertex if there are springs in the core submesh if(pCoreSubmesh->getSpringCount() > 0) { // write the physical property of this vertex if there are springs in the core submesh CalCoreSubmesh::PhysicalProperty& physicalProperty = vectorPhysicalProperty[vertexId]; vsxTiXmlElement physique("PHYSIQUE"); str.str(""); str << physicalProperty.weight; vsxTiXmlText physiquedata(str.str()); physique.InsertEndChild(physiquedata); vertex.InsertEndChild(physique); } submesh.InsertEndChild(vertex); } // write all springs int springId; for(springId = 0; springId < (int)pCoreSubmesh->getSpringCount(); ++springId) { CalCoreSubmesh::Spring& Spring = vectorSpring[springId]; vsxTiXmlElement spring("SPRING"); str.str(""); str << Spring.vertexId[0]<< " " << Spring.vertexId[1]; spring.SetAttribute("VERTEXID",str.str()); str.str(""); str << Spring.springCoefficient; spring.SetAttribute("COEF",str.str()); str.str(""); str << Spring.idleLength; spring.SetAttribute("LENGTH",str.str()); submesh.InsertEndChild(spring); } // write all faces int faceId; for(faceId = 0; faceId < (int)vectorFace.size(); ++faceId) { CalCoreSubmesh::Face& Face = vectorFace[faceId]; vsxTiXmlElement face("FACE"); str.str(""); str << Face.vertexId[0]<< " " << Face.vertexId[1]<< " " << Face.vertexId[2]; face.SetAttribute("VERTEXID",str.str()); submesh.InsertEndChild(face); } mesh.InsertEndChild(submesh); } doc.InsertEndChild(mesh); if(!doc.SaveFile()) { CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename); return false; } return true; }