环境说明
代码使用osg3.6版本。不同版本的API 会有变化,可能编译不过或没有效果。如果要运行代码,请确定使用osg3.6版本。
本文以Qt作为渲染窗口载体,osgqt的编译实现不在介绍范围内,默认已经编译通过。
简介
上一章聊过了HUDCamera,主要是对相机设置为正交投影。有了相机,还要有相机的子节点,也就是相机对什么数据做剪裁和渲染。
因为是正交投影,所以HUDCamera要剪裁和渲染的对象是一个矩形几何节点。矩形的范围就是屏幕范围,但做了归一化。从左到右,从上到下,都是0到1的范围。
矩形几何节点上,需要提供几个能力:
1:根据左下角位置,和长,宽创建;
2:能够设置纹理,也就是后面要用到的视频帧;
3:如果没有纹理,也需要提供一个默认的颜色数组;
4:支持运行时动态改变位置;
代码封装
不废话,直接上代码,声明一个HUDQuad,实现对四边形的封装。
#pragma once
#include <Windows.h>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Drawable>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/ref_ptr>
#include <osg/Referenced>
/*
* HUD四边形
*/
class HUDQuad : public osg::Referenced
{
bool m_bUseTexture;
public:
//根据左右位置占比和纹理创建HUD四边形,自动计算UV
HUDQuad(float perLeft, float perRight, osg::ref_ptr<osg::Texture2D> texture);
//根据左右位置占比和颜色创建HUD四边形
HUDQuad(float perLeft, float perRight, osg::Vec4 color);
//根据左下角和长宽创建四边形
HUDQuad(osg::Vec3 leftBottom, osg::Vec3 w, osg::Vec3 h, osg::ref_ptr<osg::Texture2D> texture);
virtual ~HUDQuad();
//占比改变
void changePercentage(float perNewLeft, float perNewRight);
public:
osg::ref_ptr<osg::Geode> m_ptrQuadGeode;
private:
//转换位置占比到顶点和UV
osg::ref_ptr<osg::Vec2Array> transPercentageWithUV(float perLeft, float perRight, osg::Vec3& leftBottom, osg::Vec3& w, osg::Vec3& h);
//转换位置占比到顶点
void transPercentage(float perLeft, float perRight, osg::Vec3& leftBottom, osg::Vec3& w, osg::Vec3& h);
};
#include \”HUDQuad.h\”
#include <osg/ShapeDrawable>
HUDQuad::HUDQuad(float perLeft, float perRight, osg::ref_pt
