Osg使用osgQt与Qt结合使用(附项目)(1)

概述

在使用Qt成功编译出来Osg之后下一个将编写Qt应用,首先要应用的就是使用osgQt实现将Osg放入一个控件之中


Osg使用osgQt与Qt结合使用在网上有很多教程,但是都是一些代码,没有成完整的体系,这里后续将可运行的项目附上方便使用

开始的环境配置可以参考(win7也可使用):
https://blog.csdn.net/weixin_40331125/article/details/80654081

这里附上两个Demo,一个用于验证Osg编译是否成功,一个用于验证osgQt的可用性:


结果示例:

  • Osg编译是否成功
    这里写图片描述
  • osgQt的可用性
    这里写图片描述

代码 :

  • 对于验证Osg编译是否成功:

    • 如果使用是之前的教程进行的安装应该会将lib已经添加到了环境变量中,这里只需要在项目中直接进行添加库即可:
      这里写图片描述
  • 同时按照这个结构添加代码,main文件如下:
#include <QCoreApplication>
#include "drawtwoball.h"
#include "pickhandler.h"
int main()
{
    osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer();
    PickHandler* p=new PickHandler();
    viewer->addEventHandler(p);
    osg::ref_ptr<osg::Group> root=new osg::Group();

    //读取模型
    osg::ref_ptr<osg::Node> node=osgDB::readNodeFile("cow.osg");

    //创建位置变换位置节点pat2
    osg::ref_ptr<osg::PositionAttitudeTransform> pat2=new osg::PositionAttitudeTransform();
    pat2->setPosition(osg::Vec3(10.0f,0.0f,0.0f));
    pat2->addChild(node.get());

    root->addChild(node.get());
    root->addChild(pat2.get());

    printf_s("X:%lf,Y:%lf,Z:%lf\n\n",node->getBound().center().x(),
             node->getBound().center().y(),
             node->getBound().center().z());
    printf_s("X:%lf,Y:%lf,Z:%lf",pat2->getBound().center().x(),
             pat2->getBound().center().y(),
             pat2->getBound().center().z());

    viewer->setUpViewInWindow(100,100,800,600);

    //优化场景数据
    osgUtil::Optimizer op;
    op.optimize(root.get());

    viewer->setSceneData(root.get());
    viewer->realize();
    viewer->run();

    return 0;

}
  • drawtwoball.h
#ifndef DRAWTWOBALL_H
#define DRAWTWOBALL_H

#endif // DRAWTWOBALL_H



#include <osgViewer/Viewer>

#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>

#include <osgDB/ReadFile>
#include <osgDB/WriteFile>

#include <osgUtil/Optimizer>

#include "libs.h"
  • libs.h (这个比较关键)
 #ifndef LIBS_H
#define LIBS_H

#endif // LIBS_H


#ifdef _DEBUG
    #pragma comment(lib,"OpenThreadsd.lib")
    #pragma comment(lib,"osgAnimationd.lib")
    #pragma comment(lib,"osgd.lib")
    #pragma comment(lib,"osgDBd.lib")
    #pragma comment(lib,"osgFXd.lib")
    #pragma comment(lib,"osgGAd.lib")
    #pragma comment(lib,"osgManipulatord.lib")
    #pragma comment(lib,"osgParticled.lib")
    #pragma comment(lib,"osgPresentationd.lib")
    #pragma comment(lib,"osgShadowd.lib")
    #pragma comment(lib,"osgSimd.lib")
    #pragma comment(lib,"osgTerraind.lib")
    #pragma comment(lib,"osgTextd.lib")
    #pragma comment(lib,"osgUId.lib")
    #pragma comment(lib,"osgUtild.lib")
    #pragma comment(lib,"osgViewerd.lib")
    #pragma comment(lib,"osgVolumed.lib")
    #pragma comment(lib,"osgWidgetd.lib")
#else
    #pragma comment(lib,"OpenThreads.lib")
    #pragma comment(lib,"osgAnimation.lib")
    #pragma comment(lib,"osg.lib")
    #pragma comment(lib,"osgDB.lib")
    #pragma comment(lib,"osgFX.lib")
    #pragma comment(lib,"osgGA.lib")
    #pragma comment(lib,"osgManipulator.lib")
    #pragma comment(lib,"osgParticle.lib")
    #pragma comment(lib,"osgPresentation.lib")
    #pragma comment(lib,"osgShadow.lib")
    #pragma comment(lib,"osgSim.lib")
    #pragma comment(lib,"osgTerrain.lib")
    #pragma comment(lib,"osgText.lib")
    #pragma comment(lib,"osgUI.lib")
    #pragma comment(lib,"osgUtil.lib")
    #pragma comment(lib,"osgViewer.lib")
    #pragma comment(lib,"osgVolume.lib")
    #pragma comment(lib,"osgWidget.lib")
#endif
  • pickhandler.h
#ifndef PICKHANDLER_H
#define PICKHANDLER_H

#endif // PICKHANDLER_H

#include "libs.h"

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>

#include <osgDB/ReadFile>
#include <osgDB/WriteFile>

#include <osgFX/Scribe>

#include <osgGA/GUIEventHandler>

#include <osgUtil/Optimizer>

#include <iostream>

#include <osgGA/TrackballManipulator>
#include <osgGA/CameraManipulator>

//对象选取事件处理器
class PickHandler:public osgGA::GUIEventHandler
{
public:
    PickHandler();
    ~PickHandler();

    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
    void pick(osg::ref_ptr<osgViewer::View> view,float x,float y);
    //鼠标位置
    float _mx;
    float _my;
};

其余的代码在项目:
https://github.com/Outliwer/osgDemo/tree/master
已有注明

猜你喜欢

转载自blog.csdn.net/qq_34861102/article/details/80786031
今日推荐