void Entity3D::add3DModel() {
	//添加模型
	QString path = _entity->getEntityInfo(EIC_3D_MODEL).toString();
	std::string modelPath = path.toLocal8Bit().data();
	osg::ref_ptr<osg::Node> nodeFile = osgDB::readNodeFile(modelPath);
	osg::ref_ptr<osg::MatrixTransform> nodeMt = new osg::MatrixTransform;//平移矩阵

	osg::ref_ptr<osg::MatrixTransform> nodeMtR = new osg::MatrixTransform;//旋转矩阵
	nodeMt->addChild(nodeMtR);
	osg::ref_ptr<osg::MatrixTransform> nodeMtS = new osg::MatrixTransform;//缩放矩阵
	nodeMtR->addChild(nodeMtS);
	nodeMtS->addChild(nodeFile);

	EarthMapMgr()->getModelRoot()->addChild(nodeMt);

	osg::Matrix m;
	EarthMapMgr()->getMapNode()->getMapSRS()->getEllipsoid()->computeLocalToWorldTransformFromLatLongHeight(
		osg::DegreesToRadians(_entity->getLat()),
		osg::DegreesToRadians(_entity->getLon()),
		_entity->getAlt(),
		m);

	nodeMt->setMatrix(m);
	nodeMt->addDescription("model-3d");
#if 0
	nodeMt->getOrCreateStateSet()->setMode(GL_LIGHT0, osg::StateAttribute::ON);
	nodeMt->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::ON);
	nodeMt->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL, osg::StateAttribute::ON);
#endif

	nodeMt->getOrCreateStateSet()->setMode(GL_LIGHT1, osg::StateAttribute::ON);
	nodeMt->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::ON);
	nodeMt->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL, osg::StateAttribute::ON);
	nodeMt->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
	//设置模型显示比例
	nodeMtS->setMatrix(osg::Matrix::scale(_scale, _scale, _scale));

	_entityNode = nodeMt;
}
}

void Entity3D::createLabel(std::string label, double fontSize) {
	osg::ref_ptr<osgText::Text> textPtr = new osgText::Text;
	textPtr->setFont("./Map/3D/fonts/simsun.ttc");
	textPtr->setCharacterSize(fontSize);
	QString context = QString::fromLocal8Bit(label.data());
	textPtr->setText(context.toStdString(), osgText::String::Encoding::ENCODING_UTF8);

	osg::ref_ptr<osg::Geode> geodePtr = new osg::Geode;
	geodePtr->addDrawable(textPtr);

	osg::ref_ptr<osg::AutoTransform> nodeAT = new osg::AutoTransform;//自动缩放矩阵
	nodeAT->setAutoRotateMode(osg::AutoTransform::AutoRotateMode::ROTATE_TO_SCREEN);//始终朝向屏幕
	nodeAT->setAutoScaleToScreen(true);
	nodeAT->setMinimumScale(1.0);//视角缩放的时候,字体的缩放比例变化 1.0-最小的缩放比例
	nodeAT->addChild(geodePtr);

	//添加到三维模型上
	osg::MatrixTransform* mt = dynamic_cast<osg::MatrixTransform*>(_entityNode.get());
	osg::MatrixTransform* mtR = dynamic_cast<osg::MatrixTransform*>(mt->getChild(0));
	osg::MatrixTransform* mtS = dynamic_cast<osg::MatrixTransform*>(mtR->getChild(0));
	
	mtS->addChild(nodeAT);
}
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐