bool ModelOverlay::addToScene(Overlay::Pointer overlay, const render::ScenePointer& scene, render::Transaction& transaction) { Volume3DOverlay::addToScene(overlay, scene, transaction); _model->addToScene(scene, transaction); processMaterials(); emit DependencyManager::get<scriptable::ModelProviderFactory>()->modelAddedToScene(getID(), NestableType::Overlay, _model); return true; }
void ModelOverlay::update(float deltatime) { if (_updateModel) { _updateModel = false; _model->setSnapModelToCenter(true); Transform transform = evalRenderTransform(); if (_scaleToFit) { _model->setScaleToFit(true, transform.getScale() * getDimensions()); } else { _model->setScale(transform.getScale()); } _model->setRotation(transform.getRotation()); _model->setTranslation(transform.getTranslation()); _model->setURL(_url); _model->simulate(deltatime, true); } else { _model->simulate(deltatime); } _isLoaded = _model->isActive(); if (isAnimatingSomething()) { if (!jointsMapped()) { mapAnimationJoints(_model->getJointNames()); } animate(); } // check to see if when we added our model to the scene they were ready, if they were not ready, then // fix them up in the scene render::ScenePointer scene = qApp->getMain3DScene(); render::Transaction transaction; if (_model->needsFixupInScene()) { emit DependencyManager::get<scriptable::ModelProviderFactory>()->modelRemovedFromScene(getID(), NestableType::Overlay, _model); _model->removeFromScene(scene, transaction); _model->addToScene(scene, transaction); auto newRenderItemIDs{ _model->fetchRenderItemIDs() }; transaction.updateItem<Overlay>(getRenderItemID(), [newRenderItemIDs](Overlay& data) { auto modelOverlay = static_cast<ModelOverlay*>(&data); modelOverlay->setSubRenderItemIDs(newRenderItemIDs); }); processMaterials(); emit DependencyManager::get<scriptable::ModelProviderFactory>()->modelAddedToScene(getID(), NestableType::Overlay, _model); } if (_visibleDirty) { _visibleDirty = false; // don't show overlays in mirrors or spectator-cam unless _isVisibleInSecondaryCamera is true _model->setVisibleInScene(getVisible(), scene, render::ItemKey::TAG_BITS_0 | (_isVisibleInSecondaryCamera ? render::ItemKey::TAG_BITS_1 : render::ItemKey::TAG_BITS_NONE), false); } if (_drawInFrontDirty) { _drawInFrontDirty = false; _model->setLayeredInFront(getDrawInFront(), scene); } if (_drawInHUDDirty) { _drawInHUDDirty = false; _model->setLayeredInHUD(getDrawHUDLayer(), scene); } scene->enqueueTransaction(transaction); if (!_texturesLoaded && _model->getGeometry() && _model->getGeometry()->areTexturesLoaded()) { _texturesLoaded = true; _model->updateRenderItems(); } }
void OBJModel::loadFromFile( const std::string& filePath ) { std::string dirPath; if (filePath.find_last_of('\\') != std::string::npos) { dirPath = filePath.substr(0, filePath.find_last_of('\\')+1); } std::ifstream f(filePath.c_str()); if (!f.is_open()) { std::cout << "File open failed : " << filePath << "\n"; return; } std::vector<std::string> lines; char buffer[256]; while (!f.eof()) { f.getline(buffer, 256); lines.push_back(std::string(buffer)); } f.close(); std::string mtlFilePath; for (std::vector<std::string>::const_iterator it = lines.begin(),eit = lines.end(); it != eit; ++it) { if ((*it).empty()) continue; if (startsWith("mtllib", *it) == true) { char mtlFileName[256]; sscanf((*it).c_str(), "mtllib %s", mtlFileName); mtlFilePath = dirPath + std::string(mtlFileName); break; } } processMaterials(dirPath, mtlFilePath); std::vector<std::string> positions; std::vector<std::string> normals; std::vector<std::string> texcoords; std::vector<std::string> triangleGroup; std::map<std::string, std::vector<std::string> > triangles; std::string currentMaterialName; for (std::vector<std::string>::iterator it = lines.begin(),eit = lines.end(); it != eit; ++it) { std::string s = *it; if (s.empty()) continue; if (startsWith("# ", s) == true) continue; if (startsWith("v ", s) == true) { positions.push_back(s); } else if (startsWith("vt ", s) == true) { texcoords.push_back(s); } else if (startsWith("vn ", s) == true) { normals.push_back(s); } else if (startsWith("f ", s) == true) { triangleGroup.push_back(s); } else if (startsWith("usemtl ", s) == true) { char mtlName[256]; sscanf(s.c_str(), "usemtl %s", mtlName); if (!currentMaterialName.empty()) { triangles[currentMaterialName] = triangleGroup; triangleGroup.clear(); } currentMaterialName = mtlName; } } triangles[currentMaterialName] = triangleGroup; unsigned short index = 0; for (std::map<std::string, std::vector<std::string> >::iterator it = triangles.begin(),eit = triangles.end(); it != eit; ++it) { std::string materialName = it->first; std::vector<std::string>& tris = it->second; std::vector<unsigned short> indices; for (std::vector<std::string>::iterator itS = tris.begin(), itE = tris.end(); itS != itE; ++itS) { std::string& s = *itS; int p1,p2,p3, t1,t2,t3, n1,n2,n3; sscanf(s.c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &p1, &t1, &n1, &p2, &t2, &n2, &p3, &t3, &n3 ); { Vertex v; std::string& pos = positions[--p1]; sscanf(pos.c_str(), "v %f %f %f", &v.position[0], &v.position[1], &v.position[2]); std::string& nom = normals[--n1]; sscanf(nom.c_str(), "vn %f %f %f", &v.normal[0], &v.normal[1], &v.normal[2]); std::string& tex = texcoords[--t1]; sscanf(tex.c_str(), "vt %f %f", &v.texcoord[0], &v.texcoord[1]); m_vertices.push_back(v); indices.push_back(index++); } { Vertex v; std::string& pos = positions[--p2]; sscanf(pos.c_str(), "v %f %f %f", &v.position[0], &v.position[1], &v.position[2]); std::string& nom = normals[--n2]; sscanf(nom.c_str(), "vn %f %f %f", &v.normal[0], &v.normal[1], &v.normal[2]); std::string& tex = texcoords[--t2]; sscanf(tex.c_str(), "vt %f %f", &v.texcoord[0], &v.texcoord[1]); m_vertices.push_back(v); indices.push_back(index++); } { Vertex v; std::string& pos = positions[--p3]; sscanf(pos.c_str(), "v %f %f %f", &v.position[0], &v.position[1], &v.position[2]); std::string& nom = normals[--n3]; sscanf(nom.c_str(), "vn %f %f %f", &v.normal[0], &v.normal[1], &v.normal[2]); std::string& tex = texcoords[--t3]; sscanf(tex.c_str(), "vt %f %f", &v.texcoord[0], &v.texcoord[1]); m_vertices.push_back(v); indices.push_back(index++); } } m_indices[materialName] = indices; } }