osgViewer.GraphicsHandleX11
类封装了一个基于X11的窗口,用于将OpenSceneGraph渲染结果展示到屏幕上。
GraphicsHandleX11(unsigned int width, unsigned int height, int screenNum = 0, Window parent = 0, bool createOwnWindow = true);
width
: 窗口宽度height
: 窗口高度screenNum
: 屏幕编号,默认为0parent
: 父窗口句柄,默认为0,表示无父窗口createOwnWindow
: 是否创建X11窗口,默认为truegetWindowHandle()
获取X11窗口句柄。
Window getWindowHandle() const;
resized(width, height)
更改窗口大小。
void resized(unsigned int width, unsigned int height);
width
: 新的窗口宽度height
: 新的窗口高度makeCurrent()
将OpenGL渲染上下文设置为当前上下文。
void makeCurrent() const;
swapBuffers()
交换前后缓冲,显示渲染结果。
void swapBuffers() const;
#include <osgViewer/GraphicsHandleX11>
#include <osgViewer/Viewer>
osg::ref_ptr<osgViewer::GraphicsHandleX11> createWindow(int width, int height)
{
osg::ref_ptr<osgViewer::GraphicsHandleX11> graphicsHandle = new osgViewer::GraphicsHandleX11(width, height);
if (!graphicsHandle.valid())
{
osg::notify(osg::FATAL) << "Failed to create graphics window" << std::endl;
return nullptr;
}
return graphicsHandle;
}
int main(int argc, char** argv)
{
int width = 800;
int height = 600;
// 创建X11窗口
auto graphicsHandle = createWindow(width, height);
if (!graphicsHandle.valid())
return 1;
// 创建OSG查看器
osgViewer::Viewer viewer;
viewer.getCamera()->setGraphicsContext(graphicsHandle);
// 加载模型
osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("cow.osg");
if (!model.valid())
{
osg::notify(osg::FATAL) << "Failed to load model" << std::endl;
return 1;
}
viewer.setSceneData(model);
// 渲染循环
while (!viewer.done())
{
viewer.frame();
}
return 0;
}