日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

osg模型调整

發布時間:2024/1/1 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 osg模型调整 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

osg模型調整光照以及模型閃爍問題[z-lighting]

0 模型閃爍原因推測

  • 1 關于模型閃爍的問題,很可能是由于坐標的值的有效位數超過了7位,目前的opengl的gpu渲染(老一點的顯卡以及gl)都是以單精度來渲染點的位置的,所以如果坐標很大,很可能導致單精度無法精確表示有效位數超過7位的數據,然后發生截斷。
  • 2 關于z-lighting的問題,嘗試了網上大多數的方法:可以使用osg封裝的polygonOffset來避免重疊面片交替出現的問題,當然最好的方案是找的模型本身重疊面片就少

1 meshlab手動調模型

filters->Normals,curve,orientations->transform…

2 使用osg降低模型的點的坐標大小

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 關于材質,光照,以及模型讀寫旋轉平移的例子

關于材料的顏色光照等可以參考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")); // 關掉模型優化繪制(OSG在加載obj模型的時候,會默認將模型繞x軸逆時針旋轉90度,此處設置不旋轉)// 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");

總結

以上是生活随笔為你收集整理的osg模型调整的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。