osg模型调整
osg模型調(diào)整光照以及模型閃爍問題[z-lighting]
0 模型閃爍原因推測
- 1 關(guān)于模型閃爍的問題,很可能是由于坐標(biāo)的值的有效位數(shù)超過了7位,目前的opengl的gpu渲染(老一點的顯卡以及gl)都是以單精度來渲染點的位置的,所以如果坐標(biāo)很大,很可能導(dǎo)致單精度無法精確表示有效位數(shù)超過7位的數(shù)據(jù),然后發(fā)生截斷。
- 2 關(guān)于z-lighting的問題,嘗試了網(wǎng)上大多數(shù)的方法:可以使用osg封裝的polygonOffset來避免重疊面片交替出現(xiàn)的問題,當(dāng)然最好的方案是找的模型本身重疊面片就少
1 meshlab手動調(diào)模型
filters->Normals,curve,orientations->transform…
2 使用osg降低模型的點的坐標(biāo)大小
osg::Vec3f center1 = TerrainM1->getBound().center();float radius1 = TerrainM1->getBound().radius();osg::Vec3f center2 = TerrainM2->getBound().center();float radius2 = TerrainM2->getBound().radius();TerrainM1->setMatrix(osg::Matrix::translate(-center1.x(), -center1.y(), -center1.z()) *osg::Matrix::rotate(osg::DegreesToRadians(180.0), 1, 0, 0) *osg::Matrix::translate(center1.x(), center1.y(), center1.z()) *osg::Matrix::translate(0, 0, radius2 * 0.80 + radius1));// to modify the model's points render valuemRoot->getChild(0)->asTransform()->asMatrixTransform()->setMatrix(osg::Matrix::translate(-center1.x(), -center1.y(), -center1.z()));osgDB::writeNodeFile(*(mRoot->getChild(0)->asNode()), "sc.obj");3 關(guān)于材質(zhì),光照,以及模型讀寫旋轉(zhuǎn)平移的例子
關(guān)于材料的顏色光照等可以參考https://learnopengl-cn.readthedocs.io/zh/latest/02%20Lighting/03%20Materials/
// mRoot是一個osg::ref_ptr<osg::Group>auto TerrainM1 = new osg::MatrixTransform;auto terrain1 = new osg::PositionAttitudeTransform;auto TerrainM2 = new osg::MatrixTransform;auto terrain2 = new osg::PositionAttitudeTransform;osgDB::Options *a = new osgDB::Options(std::string("noRotation")); // 關(guān)掉模型優(yōu)化繪制(OSG在加載obj模型的時候,會默認(rèn)將模型繞x軸逆時針旋轉(zhuǎn)90度,此處設(shè)置不旋轉(zhuǎn))// setting materialosg::Material *material = new osg::Material;material->setDiffuse(osg::Material::FRONT, osg::Vec4(0.75, 0.80, 0.75, 1.0));material->setAmbient(osg::Material::FRONT, osg::Vec4(0.75, 0.80, 0.75, 1.0));// material->setShininess(osg::Material::FRONT, 90.0);// turn off light effectauto Model1 = osgDB::readNodeFile(part1Path, a);auto pState1 = Model1->getOrCreateStateSet();pState1->setMode(GL_LIGHTING, osg::StateAttribute::ON);pState1->setAttribute(material);// pState1->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);// pState1->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);// set polygon offsetosg::ref_ptr<osg::PolygonOffset> polyOff = new osg::PolygonOffset();float mFactor = 1.0, mUnits = 1.0;polyOff->setFactor(mFactor);polyOff->setUnits(mUnits);pState1->setAttributeAndModes(new osg::PolygonOffset(-1.0f,-1.0f),osg::StateAttribute::ON);material->setEmission(osg::Material::FRONT, osg::Vec4(0.75, 0.80, 0.75, 1.0));// pState1->setAttributeAndModes(polyOff.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);// auto pGeom1 = Model1->asGeode()->asGeometry();// pGeom1->getOrCreateStateSet()->setMode(GL_NV_framebuffer_multisample_coverage, osg::StateAttribute::ON | osg::StateAttribute::PROTECTED | osg::StateAttribute::OVERRIDE);TerrainM1->addChild(Model1);TerrainM1->setName(modelPrefix + "1");auto Model2 = osgDB::readNodeFile(part2Path, a);auto pState2 = Model2->getOrCreateStateSet();pState2->setMode(GL_LIGHTING, osg::StateAttribute::ON);pState2->setAttribute(material);pState2->setMode(GL_BLEND, osg::StateAttribute::ON);TerrainM2->addChild(Model2);TerrainM2->setName(modelPrefix + "2");// rotate to make sure the building is standing on the groundosg::Vec3f center1 = TerrainM1->getBound().center();float radius1 = TerrainM1->getBound().radius();osg::Vec3f center2 = TerrainM2->getBound().center();float radius2 = TerrainM2->getBound().radius();TerrainM1->setMatrix(osg::Matrix::translate(-center1.x(), -center1.y(), -center1.z()) *osg::Matrix::rotate(osg::DegreesToRadians(180.0), 1, 0, 0) *osg::Matrix::translate(center1.x(), center1.y(), center1.z()) *osg::Matrix::translate(0, 0, radius2 * 0.80 + radius1));mRoot->addChild(TerrainM1);TerrainM2->setMatrix(osg::Matrix::translate(-center2.x(), -center2.y(), -center2.z()) *osg::Matrix::rotate(osg::DegreesToRadians(180.0), 1, 0, 0) *osg::Matrix::translate(center2.x(), center2.y(), center2.z()) *osg::Matrix::translate(0, 0, 0.5 * radius2));mRoot->addChild(TerrainM2);// to modify the model's points render valuemRoot->getChild(0)->asTransform()->asMatrixTransform()->setMatrix(osg::Matrix::translate(-center1.x(), -center1.y(), -center1.z()));mRoot->getChild(1)->asTransform()->asMatrixTransform()->setMatrix(osg::Matrix::translate(-center1.x(), -center1.y(), -center1.z()));mRoot->getChild(4)->asTransform()->asMatrixTransform()->setMatrix(osg::Matrix::translate(-center1.x(), -center1.y(), -center1.z()));mRoot->getChild(5)->asTransform()->asMatrixTransform()->setMatrix(osg::Matrix::translate(-center1.x(), -center1.y(), -center1.z()));osgDB::writeNodeFile(*(mRoot->getChild(0)->asNode()), "sc.obj");osgDB::writeNodeFile(*(mRoot->getChild(1)->asNode()), "de.obj");osgDB::writeNodeFile(*(mRoot->getChild(4)->asNode()), "par1.obj");osgDB::writeNodeFile(*(mRoot->getChild(5)->asNode()), "par2.obj");總結(jié)
- 上一篇: C/C++百行代码实现热门游戏-消消乐
- 下一篇: CAS学习笔记四:CAS单点登出流程