2021-01-27

2 篇文章 0 订阅

OSG LOD崩溃

当LOD的孩子共享顶点数据时,会崩溃。LOD的一个孩子是四边形,一个孩子是八边形。按需更新LOD。我这里为了简化就一直更新LOD。当CRASH_LOD 宏是 0,不崩溃,是1时崩溃。代码如下。
也可见 https://github.com/openscenegraph/OpenSceneGraph/issues/1043

/***************************************************************************
* Copyright (C) 2017, Deping Chen, cdp97531@sina.com
*
* All rights reserved.
* For permission requests, write to the author.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
***************************************************************************/
#include <osg/Geode>

#include <osgViewer/Viewer>
#include <osg/LOD>

#include <osgGA/TrackballManipulator>

#define CRASH_LOD 0
class UpdateLOD : public osg::NodeCallback
{
public:
	void operator()(osg::Node* node, osg::NodeVisitor* nv)
	{
		if (nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
		{
			osg::LOD* lod = static_cast<osg::LOD*>(node);
#if CRASH_LOD
			osg::Geometry* lod1 = lod->getChild(0)->asGeometry();
			osg::Geometry* lod2 = lod->getChild(1)->asGeometry();
#else
			lod->removeChildren(0, lod->getNumChildren());
			osg::ref_ptr <osg::Geometry> lod1 = new osg::Geometry();
			osg::ref_ptr <osg::Geometry> lod2 = new osg::Geometry();
			lod->addChild(lod1, 0, 8.f);
			lod->addChild(lod2, 8.f, FLT_MAX);
#endif
			auto posA = new osg::Vec3Array;
			const float sqrt_2_2 = sqrt(2) / 2.0f;
			posA->push_back(osg::Vec3f(0, 0, 0));
			posA->push_back(osg::Vec3f(1, 0, 0));
			posA->push_back(osg::Vec3f(sqrt_2_2, 0, sqrt_2_2));
			posA->push_back(osg::Vec3f(0, 0, 1));
			posA->push_back(osg::Vec3f(-sqrt_2_2, 0, sqrt_2_2));
			posA->push_back(osg::Vec3f(-1, 0, 0));
			posA->push_back(osg::Vec3f(-sqrt_2_2, 0, -sqrt_2_2));
			posA->push_back(osg::Vec3f(0, 0, -1));
			posA->push_back(osg::Vec3f(sqrt_2_2, 0, -sqrt_2_2));
			lod1->setVertexArray(posA);
			lod2->setVertexArray(posA);

			auto colorA = new osg::Vec3Array(osg::Array::Binding::BIND_OVERALL);
			colorA->push_back(osg::Vec3(1, 0, 0));
			lod1->setColorArray(colorA);
			lod2->setColorArray(colorA);

			osg::ref_ptr<osg::UShortArray> indices1 = new osg::UShortArray;
			const unsigned short i1a[] = { 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 7, 0, 7, 8, 0, 8, 1};
			indices1->assign(std::begin(i1a), std::end(i1a));

			osg::ref_ptr<osg::UShortArray> indices2 = new osg::UShortArray;
			const unsigned short i2a[] = { 0, 1, 3, 0, 3, 5, 0, 5, 7, 0, 7, 1 };
			indices2->assign(std::begin(i2a), std::end(i2a));

			lod1->removePrimitiveSet(0, lod1->getNumPrimitiveSets());
			lod1->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES, (unsigned int)indices1->size(), indices1->asVector().data()));
			lod1->dirtyBound();
			lod2->removePrimitiveSet(0, lod2->getNumPrimitiveSets());
			lod2->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES, (unsigned int)indices2->size(), indices2->asVector().data()));
			lod2->dirtyBound();
		}
		// must call any nested node callbacks and continue subgraph traversal.
		NodeCallback::traverse(node, nv);
	}
};

osg::Group* createLOD()
{
	osg::ref_ptr<osg::LOD> lod = new osg::LOD;
#if CRASH_LOD
	osg::ref_ptr <osg::Geometry> lod1 = new osg::Geometry();
	osg::ref_ptr <osg::Geometry> lod2 = new osg::Geometry();
	lod->addChild(lod1, 0, 8.f);
	lod->addChild(lod2, 8.f, FLT_MAX);
#endif
	lod->addUpdateCallback(new UpdateLOD);
	return lod.release();
}

int main(int argc, char **argv)
{
	osgViewer::Viewer viewer;
    osg::ref_ptr<osg::GraphicsContext::Traits> traits =
        new osg::GraphicsContext::Traits;
    traits->x = 50;
    traits->y = 50;
    traits->width = 800;
    traits->height = 600;
    traits->windowDecoration = true;
    traits->doubleBuffer = true;

    osg::ref_ptr<osg::GraphicsContext> gc =
        osg::GraphicsContext::createGraphicsContext(traits.get());
    osg::ref_ptr<osg::Camera> camera = viewer.getCamera();
    camera->setGraphicsContext(gc);
    camera->setViewport(
        new osg::Viewport(0, 0, traits->width, traits->height));

	// add model to viewer.
	viewer.setSceneData(createLOD());
	viewer.realize();

	return viewer.run();
}

改成这样就不崩溃了。

#if CRASH_LOD
	osg::ref_ptr <osg::Geometry> lod1 = new osg::Geometry();
	osg::ref_ptr <osg::Geometry> lod2 = new osg::Geometry();
	
	lod1->setDataVariance(osg::Object::DYNAMIC);
	lod2->setDataVariance(osg::Object::DYNAMIC);
	
	lod->addChild(lod1, 0, 8.f);
	lod->addChild(lod2, 8.f, FLT_MAX);
#endif

但是我真实的程序还是崩溃,真让人崩溃。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值