osg.Viewport
是OpenSceneGraph中的视口类,可以用于指定渲染场景的部分区域。
osg::Viewport(int x, int y, int width, int height);
x
:视口矩形的左下角x坐标。y
:视口矩形的左下角y坐标。width
: 视口矩形的宽度。height
:视口矩形的高度。void setViewport(int x, int y, int width, int height);
x
:视口矩形的左下角x坐标。y
:视口矩形的左下角y坐标。width
: 视口矩形的宽度。height
:视口矩形的高度。void setViewport(const osg::Viewport& vp);
vp
:需要设置视口的参数。void setPosition(int x, int y);
x
:视口矩形的左下角x坐标。y
:视口矩形的左下角y坐标。void setSize(int width, int height);
width
: 视口矩形的宽度。height
:视口矩形的高度。int x() const;
获取视口矩形左下角x坐标。
int y() const;
获取视口矩形左下角y坐标。
int width() const;
获取视口矩形宽度。
int height() const;
获取视口矩形高度。
float aspectRatio() const;
获取视口的宽高比。
bool intersects(double x, double y) const;
判断点是否在视口范围内。
x
:点的x坐标。y
:点的y坐标。#include <osg/Viewport>
#include <iostream>
int main()
{
// 创建一个视口对象
osg::Viewport* viewport = new osg::Viewport(0, 0, 800, 600);
// 输出视口参数
std::cout << "x: " << viewport->x() << std::endl;
std::cout << "y: " << viewport->y() << std::endl;
std::cout << "width: " << viewport->width() << std::endl;
std::cout << "height: " << viewport->height() << std::endl;
// 修改视口参数
viewport->setViewport(100, 100, 400, 300);
// 输出修改后的视口参数
std::cout << "x: " << viewport->x() << std::endl;
std::cout << "y: " << viewport->y() << std::endl;
std::cout << "width: " << viewport->width() << std::endl;
std::cout << "height: " << viewport->height() << std::endl;
// 输出视口的宽高比
std::cout << "aspectRatio: " << viewport->aspectRatio() << std::endl;
// 判断一个点是否在视口内
bool isInside = viewport->intersects(200, 400);
std::cout << "isInside: " << isInside << std::endl;
return 0;
}