在OSG中嵌入OpenGL代码

6 篇文章 1 订阅

看了下网上的方法, 基本上可分为两种:

1. 在osg中创建结点, 比如Geometry对象, 然后给该对象添加DrawCallback, 在这个DrawCallback中使用opengl进行绘制

2. 创建一个Drawable的派生类, 在该类中重写drawImplementation()函数, 在该函数中使用opengl代码

 

第一种方法(转):

(1)继承osg::Drawable::DrawCallback,在

drawImplementation(osg::RenderInfo& renderInfo,const osg::Drawable* drawable)函数里添加opengl代码。

(2)Drawable设置绘制回调函数,并注意要把显示列表设置为false:

geometry->setUseDisplayList(false);

geometry->setDrawCallback(new DrawCallback);

――――――――――――

参考代码如下(改编自OSG的例子程序osgSpaceWarp):

#include "stdafx.h"

#include <osgViewer/Viewer>

#include <osg/Group>

#include <osg/Geometry>

#pragma comment( lib, "osgd.lib"); //.在Debug版本下的库名都加d,如"osgd.lib"

#pragma comment( lib, "osgDBd.lib")

#pragma comment( lib, "osgViewerd.lib");

#pragma comment( lib, "osgUtild.lib");

//opengl的头文件和库文件

#include <gl/gl.h>

#include <gl/glu.h>

#pragma comment(lib,"opengl32.lib")

#pragma comment(lib,"glu32.lib")

class DrawCallback : public osg::Drawable::DrawCallback

{

virtual void drawImplementation(osg::RenderInfo& renderInfo,const osg::Drawable* drawable) const

{

static double dColor= 0;//颜色

glColor3f( dColor, 0, 0);

glBegin(GL_TRIANGLES);//在OSG中画一个opengl三角形

glVertex3f( 0.0, 0.0, -2.0);

glVertex3f( 0.2, 0.0, -2.0);

glVertex3f( 0.0, 0.4, -2.0);

glEnd();

dColor += 0.01;//颜色渐变

if ( dColor > 1.0)

{

dColor= 0.0;

}

}

};

int main( int argc, char **argv )

{

osgViewer::Viewer viewer;

osg::Geometry* geometry = new osg::Geometry;

//此处一定要把显示列表设置为false,

//否则DrawCallback的drawImplementation()函数只会调用一次,而不是在画一帧时都动态更新opengl图形

geometry->setUseDisplayList(false);

geometry->setDrawCallback(new DrawCallback);//Drawable设置动态更新opengl图形

osg::Geode* geode = new osg::Geode;

geode->addDrawable(geometry);

geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);

osg::Group* group = new osg::Group;

group->addChild(geode);

viewer.setSceneData( group);

//return viewer.run();

osg::Matrix mt;

mt.makeIdentity();

while ( !viewer.done())

{

viewer.getCamera()->setViewMatrix( mt);

viewer.frame();

}

}

 

第二种方法, 差不太多的:
class MyDrawable : public osg::Drawable
{
public:
 MyDrawable()
 {
 }

 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
 MyDrawable(const MyDrawable& latlonline, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) :
 osg::Drawable(latlonline,copyop)
 {
 }

 META_Object(osg, MyDrawable)

 virtual void drawImplementation(osg::RenderInfo &renderInfo) const
 {

  glColor3f(1.0f, 1.0f, 1.0f);

  glBegin(GL_LINE_LOOP);
   float x,y;
   double radious =osg::WGS_84_RADIUS_EQUATOR/10000 + 10;           //radious为圆半径       
   radious = 0.3; 
   double step  =  0.01f;                 //step为画圆的步长       
   for(double angle   =   0.0f;   angle   <=  osg::PI *2;   angle  +=  step)     
   {
    x   =   radious   *sin(angle);
    y   =   radious   *cos(angle);
    glVertex3f(x,   y,  -2.0);
   }
  glEnd();

}
};

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的osgEarth嵌入Qt程序的示例代码: ```cpp #include <osgViewer/Viewer> #include <osgEarth/Map> #include <osgEarth/MapNode> #include <osgEarthUtil/EarthManipulator> #include <osgEarthQt/ViewerWidget> int main(int argc, char** argv) { QApplication app(argc, argv); // 创建一个 osgViewer::Viewer 对象 osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer; // 创建一个 osgEarth::Map 对象 osg::ref_ptr<osgEarth::Map> map = new osgEarth::Map; // 创建一个 osgEarth::MapNode 对象 osg::ref_ptr<osgEarth::MapNode> mapNode = new osgEarth::MapNode(map); // 设置地球操作器 osg::ref_ptr<osgEarth::Util::EarthManipulator> manipulator = new osgEarth::Util::EarthManipulator; manipulator->setHomePosition(osgEarth::Util::Viewpoint(30.0, 120.0, 0.0, 0.0, -90.0, 0.0), 0.0, 0.0, true); viewer->setCameraManipulator(manipulator); // 将 osgEarth::MapNode 添加到 osgViewer::Viewer viewer->setSceneData(mapNode); // 创建一个 osgEarthQt::ViewerWidget 对象 osg::ref_ptr<osgEarthQt::ViewerWidget> widget = new osgEarthQt::ViewerWidget(viewer); // 设置 osgEarthQt::ViewerWidget 的窗口大小 widget->setMinimumSize(640, 480); widget->setMaximumSize(1024, 768); // 显示窗口 widget->show(); // 运行 Qt 应用程序 return app.exec(); } ``` 这个程序创建了一个 `osgViewer::Viewer` 对象,一个 `osgEarth::Map` 对象和一个 `osgEarth::MapNode` 对象。然后它设置了一个地球操作器,并将 `osgEarth::MapNode` 添加到 `osgViewer::Viewer` 。接下来,它创建了一个 `osgEarthQt::ViewerWidget` 对象,并将 `osgViewer::Viewer` 对象传递给它。最后,它显示了 `osgEarthQt::ViewerWidget` 窗口并运行 Qt 应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值