HEAD-UP DISPLAY, 即是抬头显示功能,在opengl编程中,我们有时需要一种屏幕区域,这个区域并不会随着三维场景视角表换而发生改变,也可以讲,是在三维场景中实现了一个二维子场景,故常用来显示提示信息。下面这个小例子实现了GL3版本中的HUD功能(OSG编译选项里面,我们选择了编译GLcore版本,因为我们想要这个功能应用在osgEarth3.0以后的版本中,不熟悉什么叫GLCore的童靴可以参考我的另一篇博文《opengl版本发展史及各种概念的厘清》)。

首先,我们要创建一个相机

    osg::Camera* camera = new osg::Camera;camera->setName("Hud Camera");// set the projection matrixcamera->setProjectionMatrixAsOrtho2D(0, screen_width, 0, screen_heigh);camera->setViewport(0, 0, screen_width, screen_heigh);// set the view matrixcamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);camera->setViewMatrix(osg::Matrix::identity());camera->setClearColor(osg::Vec4(1, 0.5, 0.5, 0.5));

将相机的投影方式设置为正射投影(Ortho2D);视口设置为屏幕大小;现在我们看到的界面是整个屏幕;设置参考帧为绝对。

创建完了相机,接下来我们创建要在屏幕上显示的内容。一般情况下我们会创建一个矩形,在这个矩形上贴纹理,或绘制图案,作为HUD的显示内容,下面的代码,创建一个白色的矩形。


osg::Geometry* createQuad(int screen_width, int screen_heigh, float z) {osg::Geometry* polyGeom = new osg::Geometry();polyGeom->setSupportsDisplayList(false);osg::Vec3Array* vertices = new osg::Vec3Array;osg::Vec2Array* texcoords = new osg::Vec2Array;osg::Vec4Array* colors = new osg::Vec4Array;/*four vertex*/vertices->push_back(osg::Vec3d(0, 0, z));texcoords->push_back(osg::Vec2f(0, 0));vertices->push_back(osg::Vec3d(screen_width, 0, z));texcoords->push_back(osg::Vec2f(1, 0));vertices->push_back(osg::Vec3d(screen_width, screen_heigh, z));texcoords->push_back(osg::Vec2f(1, 1));vertices->push_back(osg::Vec3d(0, screen_heigh, z));texcoords->push_back(osg::Vec2f(0, 1));/*four vertex*/colors->push_back(osg::Vec4f(1.0,1.0,1.0,1.0));colors->setBinding(osg::Array::BIND_OVERALL);/*set vertex array and tex coord array*/polyGeom->setVertexArray(vertices);polyGeom->setTexCoordArray(0, texcoords);polyGeom->setColorArray(colors);polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN, 0, vertices->size()));return polyGeom;
}

创建一个viewer,并且取得窗口的宽高。

    // initialize a viewer:osgViewer::Viewer viewer;    viewer.setUpViewAcrossAllScreens();osgViewer::Viewer::Windows windows;viewer.getWindows(windows);int screen_width = windows[0]->getTraits()->width;int screen_heigh = windows[0]->getTraits()->height;

然后创建一个场景树中的叶子节点,将createQuad函数创建的矩形包含在geode中:

    /*create a geode*/osg::Geode* _geode = new osg::Geode();_geode->setName("GeodeOfHUD");osg::Geometry* polyGeom = createQuad(screen_width, screen_heigh, -100);polyGeom->setName("PolyGeomOfHUD_");_geode->addDrawable(polyGeom);

最后,我们将场景加入到view中

  camera->addChild(_geode);viewer.setSceneData(camera);

这样,我们就完成了整个HUD效果的创建。

完整的代码如下

osg::Geometry* createQuad(int screen_width, int screen_heigh, float z) {osg::Geometry* polyGeom = new osg::Geometry();polyGeom->setSupportsDisplayList(false);osg::Vec3Array* vertices = new osg::Vec3Array;osg::Vec2Array* texcoords = new osg::Vec2Array;osg::Vec4Array* colors = new osg::Vec4Array;/*four vertex*/vertices->push_back(osg::Vec3d(0, 0, z));texcoords->push_back(osg::Vec2f(0, 0));vertices->push_back(osg::Vec3d(screen_width, 0, z));texcoords->push_back(osg::Vec2f(1, 0));vertices->push_back(osg::Vec3d(screen_width, screen_heigh, z));texcoords->push_back(osg::Vec2f(1, 1));vertices->push_back(osg::Vec3d(0, screen_heigh, z));texcoords->push_back(osg::Vec2f(0, 1));/*four vertex*/colors->push_back(osg::Vec4f(1.0,1.0,1.0,1.0));colors->setBinding(osg::Array::BIND_OVERALL);/*set vertex array and tex coord array*/polyGeom->setVertexArray(vertices);polyGeom->setTexCoordArray(0, texcoords);polyGeom->setColorArray(colors);polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN, 0, vertices->size()));return polyGeom;
}int
main(int argc, char** argv)
{// initialize a viewer:osgViewer::Viewer viewer;/*得到窗口的宽高*/viewer.setUpViewAcrossAllScreens();osgViewer::Viewer::Windows windows;viewer.getWindows(windows);int screen_width = windows[0]->getTraits()->width;int screen_heigh = windows[0]->getTraits()->height;/*创建HUD相机*/osg::Camera* camera = new osg::Camera;camera->setName("Hud Camera");// set the projection matrixcamera->setProjectionMatrixAsOrtho2D(0, screen_width, 0, screen_heigh);camera->setViewport(0, 0, screen_width, screen_heigh);// set the view matrixcamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);camera->setViewMatrix(osg::Matrix::identity());camera->setClearColor(osg::Vec4(1, 0.5, 0.5, 0.5));/*create quad with texture from rtt*/osg::Geode* _geode = new osg::Geode();_geode->setName("GeodeOfHUD");osg::Geometry* polyGeom = createQuad(screen_width, screen_heigh, -100);polyGeom->setName("PolyGeomOfHUD_");_geode->addDrawable(polyGeom);camera->addChild(_geode);viewer.setSceneData(camera);int r = viewer.run();return r;
}

osg中实现HUD(OSG初级篇1)相关推荐

  1. osg中运用Shader(osg初级篇2)

    关于shader的概念,可以参见我的另一篇博客<opengl版本发展史及各种概念的厘清>,这里列举一个例子,用来实现一个特效,屏幕的左半部显示为红色.首先我们准备好两个shader程序: ...

  2. android在主程序中调用图片,009android初级篇之APP中使用系统相机相册等集成应用...

    009android初级篇之APP中使用系统相机相册等集成应用 android应用中使用相机功能,大致有两种方式实现: 直接调用系统内部的相机程序,显示的也是系统预设的界面(简单,只有简单的拍照功能) ...

  3. 初探OSG+Opencascade在qt上的实践值获取模型颜色并在OSG中显示

    很久没有正式写过一篇博客,之前因为工作原因,致使个人项目一直停滞,在出差回来后,决定利用晚上的空闲时间重新对之前的知识进行梳理和补充,前版本中实现了如何利用opencascade读取stp模型,并显示 ...

  4. osg中三维模型的位置变换

    对模型的平移和旋转等操作是我们在做三维软件开发时必然会解决的事情.但是由于基本变换默认是以世界坐标系的原点为变换中心,所以我们在进行平移旋转等操作时往往会发现其并非如我们想象的那样去执行.举个简单的例 ...

  5. 在Eclipse中使用JUnit4进行单元测试(初级篇)

    转载自   在Eclipse中使用JUnit4进行单元测试(初级篇) 本文绝大部分内容引自这篇文章: http://www.devx.com/Java/Article/31983/0/page/1 我 ...

  6. cacheinterceptor第二次访问没被调用_访问者设计模式在OSG中的应用

    为什么要谈谈访问者设计模式呢?因为OSG整个引擎就是用访问者设计模式建立起来的,不论是遍历节点图,还是做各种实用的功能,都需要大量的用到访问者设计模式. 先谈谈访问者设计模式的定义. 1:什么是访问者 ...

  7. 3DMAX文件导入到OSG中。

    插件地址在 http://sourceforge.net/projects/osgmaxexp/files/OpenSceneGraph%20Max%20Exporter/1.0.2/ 有win2版本 ...

  8. 7、osg中响应键盘鼠标事件以及鼠标和键盘编码表

    1.首先定义一个类,该类继承与osgGA::GUIEventHandler,在此类中存在一个handle函数,所有的事件都在此函数中进行处理,如下类的代码: //声明类UseEventHandler, ...

  9. OSG中使用png图片显示透明效果

    常见的几种图片格式中只有png格式和gif格式的图片会有透明效果,其他图片格式都会使用白色作为底色.下面是使用OSG实现png纹理透明效果的代码,使用gif格式的图片也可以,注意:图片必须首先有透明的 ...

最新文章

  1. wpf在presenter(VM)中异步更新viewer中数据
  2. drf解决跨域问题 使用 django-corse-headers扩展
  3. Spring MVC HttpMessageConverter对象
  4. C++(STL):30 ---关联式容器map的operator[]和insert效率对比
  5. 再来一波不错的学习资源
  6. Tensorflow实践:用神经网络训练分类器
  7. Java 9 揭秘(9. 打破模块封装)
  8. Activiti进阶(七)——排他网关(ExclusiveGateWay)
  9. 千字搞定数据产品选型!报表、BI、大数据平台、中台都在这了
  10. 震惊!雷军表示要出千元5G手机!
  11. [AHOI 2012]树屋阶梯
  12. 华为ENSP安装教程
  13. 堪称神器的图片无损放大缩小工具
  14. 淘宝电商项目落地,从零开始搭建亿级系统架构笔记
  15. 论文阅读:《POI: Multiple Object Tracking with High Performance Detection and Appearance Feature 》
  16. 医院信息化成功的关键=本质+方法+工具
  17. Editplus文件在linux下乱码,EditPlus中文乱码怎么办-解决EditPlus中文出现乱码的方法 - 河东软件园...
  18. 【运维探讨】RPA落地实践,提升IT运维工作效能!
  19. Nginx+Lua 实现灰度发布详细步骤
  20. 生成1-30的随机数,生成-30到30的随机数

热门文章

  1. Latex中设置字体颜色
  2. 北大BBS2008年毕业生晒工资
  3. Form_通过FND_FNDFLUPL标准功能上传CSV控件(案例)
  4. Linux下显示IP地址所在地信息的小工具——nali
  5. 发现了lucene一个bug
  6. Java基础篇:IO流
  7. 图像技术在上亿规模实拍图片中的应用(算法好文)
  8. 只要努力搞,没有KPI搞不垮的团队?
  9. 一文把Redis主从复制、哨兵、Cluster三种模式摸透
  10. 月薪30K+的程序员都会啥,通过3000字告诉你……