osg高亮显示模型轮廓线及网格化模型

本文介绍了如何利用osgFX库在3D场景中实现模型的高亮显示和网格化效果。代码示例展示了如何创建一个轮廓线以突出模型,并应用网格化效果。通过设置宽度、颜色等参数,可以自定义高亮效果。同时,网格化模型展示了将3D模型表面划分为线条网格的方法。

目录

1. 高亮显示

2. 网格化模型


1. 高亮显示

代码如下:

// -*-c++-*-

/*
 * Draw an outline around a model.
 */

#include <osg/Group>
#include <osg/PositionAttitudeTransform>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgFX/Outline>


int main(int argc, char** argv)
{
	osg::ArgumentParser arguments(&argc, argv);
	arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName() + " [options] <file>");
	arguments.getApplicationUsage()->addCommandLineOption("-h or --help", "Display this information");


	// load outlined object
	std::string modelFilename = arguments.argc() > 1 ? arguments[1] : "dumptruck.osgt";
	osg::ref_ptr<osg::Node> outlineModel = osgDB::readRefNodeFile(modelFilename);
    if(nullptr == outlineModel)
    {
         return -1;
    }

    // create scene
    osg::ref_ptr<osg::Group> pRoot = new osg::Group;
    
    // create outline effect
    osg::ref_ptr<osgFx::Outline> pOutLine = new osgFx::Outline;
    pRoot->addChild(pOutLine);
   
    pOutLine->setWidth(8);
    pOutLine->setColor(osg::Vec4(1, 1, 0, 1));
    pOutLine->addChild(outlineModel);

	// must have stencil buffer...
	osg::DisplaySettings::instance()->setMinimumNumStencilBits(1);

	// construct the viewer
	osgViewer::Viewer viewer;
	viewer.setSceneData(root);

	// must clear stencil buffer...
	unsigned int clearMask = viewer.getCamera()->getClearMask();
	viewer.getCamera()->setClearMask(clearMask | GL_STENCIL_BUFFER_BIT);
	viewer.getCamera()->setClearStencil(0);

	return viewer.run();
}

运行结果如下:

本文仅仅只是对高亮显示的简单描述,更具有项目实战意义的高亮显示参见如下博文:

osg高亮显示选中的模型 

2. 网格化模型

代码如下:

#include<osgViewer/viewer>
#include<osgDB/ReadFile>
#include<osgFX/Scribe>
int main()
{
    auto pCowNode = osgDB::readNodeFile("cow.osg");
    if (nullptr == pCowNode)
    {
        OSG_WARN << "node is nullptr";
        return 1;
    }

    osgViewer::Viewer viewer;
 
    auto pRoot = new osg::Group;

    // 网格化
    auto pScribe = new osgFX::Scribe();
    pScribe->addChild(pCowNode);
    pRoot->addChild(pScribe );

    viewer.setSceneData(pRoot);

    return viewer.run();
}

 

 效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值