> 文章列表 > osgwidget 使用 方法以及案例分享

osgwidget 使用 方法以及案例分享

osgwidget 使用 方法以及案例分享

 osgwidget 使用 方法以及案例分享

按钮 一个常见的 osg::Widget 就是按钮。下面的代码展示了如何使用 osg::Switch 和 osgText 创建一个简单的按钮:
osg::ref_ptr<osg::Switch> buttonSwitch = new osg::Switch();
osg::ref_ptr<osgText::Text> buttonText = new osgText::Text();
buttonText->setText("Click me!");
buttonText->setCharacterSize(30);
buttonText->setPosition(osg::Vec3(0, 0, 0));
buttonText->setAlignment(osgText::Text::CENTER_CENTER);
buttonSwitch->addChild(buttonText.get());标签页 另一个常见的 osg::Widget 就是标签页。下面的代码展示了如何使用 osg::Group、osg::PositionAttitudeTransform 和 osgText 创建一个简单的标签页:
osg::ref_ptr<osg::Group> tabGroup = new osg::Group();
osg::ref_ptr<osg::PositionAttitudeTransform> tabTransform = new osg::PositionAttitudeTransform();
tabTransform->setPosition(osg::Vec3(-50, 0, 0));
osg::ref_ptr<osgText::Text> tabText = new osgText::Text();
tabText->setText("Tab 1");
tabText->setCharacterSize(20);
tabText->setPosition(osg::Vec3(0, 0, 0));
tabTransform->addChild(tabText.get());
tabGroup->addChild(tabTransform.get());文本框 最后一个例子是文本框。下面的代码展示了如何使用 osg::Geode、osgText 和 osg::Geometry 创建一个简单的文本框:
osg::ref_ptr<osg::Geode> textBoxGeode = new osg::Geode();
osg::ref_ptr<osgText::Text> textBoxText = new osgText::Text();
textBoxText->setText("Type here!");
textBoxText->setCharacterSize(20);
textBoxText->setPosition(osg::Vec3(0, 0, 0));
osg::ref_ptr<osg::Geometry> textBoxGeometry = osg::createTexturedQuadGeometry(osg::Vec3(-50, -20, 0), osg::Vec3(100, 0, 0), osg::Vec3(0, 40, 0));
osg::ref_ptr<osg::Texture2D> textBoxTexture = new osg::Texture2D();
// 设置纹理
textBoxGeode->addDrawable(textBoxGeometry.get());
textBoxGeode->addDrawable(textBoxText.get());

osgWidget是OpenSceneGraph(OSG)的一个扩展库,提供了一个基于OpenGL的2D GUI框架。它允许用户创建和管理图形用户界面元素,例如按钮、标签、滑块、文本框等,并将它们嵌入到OSG场景中。

osgWidget的主要特点包括:

  1. 支持多个窗口,每个窗口都可以包含任意数量的部件。

  2. 可以使用纹理作为部件的背景,支持透明度和平铺模式。

  3. 组件可以响应鼠标事件,例如单击、双击、拖动等。

  4. 可以自定义部件的外观和行为,例如字体、颜色、大小等。

osgWidget的使用方法类似于其他GUI框架,需要创建一个GUI环境对象并添加所需的部件。下面是一个简单的osgWidget代码示例,用于创建一个带有两个按钮和一个标签的窗口:

#include <osgWidget/WindowManager>
#include <osgWidget/Box>// 创建GUI环境对象和窗口管理器
osg::ref_ptr<osgWidget::WindowManager> wm = new osgWidget::WindowManager(graphicsContext->getTraits());// 创建窗口
osg::ref_ptr<osgWidget::Box> window = new osgWidget::Box("window");// 创建按钮
osg::ref_ptr<osgWidget::Button> button1 = new osgWidget::Button("button1");
osg::ref_ptr<osgWidget::Button> button2 = new osgWidget::Button("button2");// 创建标签
osg::ref_ptr<osgWidget::Label> label = new osgWidget::Label("label");// 将按钮和标签添加到窗口中
window->addWidget(button1.get());
window->addWidget(button2.get());
window->addWidget(label.get());// 添加窗口到窗口管理器
wm->addChild(window.get());osgWidget是一个功能强大的GUI框架,可以方便地将用户界面集成到OSG场景中,提供更丰富和交互性的应用程序体验。
osg::MatrixTransform 和 osgWidget::Box 都是 OpenSceneGraph(OSG)中的类,但它们用途不同。osg::MatrixTransform 是 OSG 中的一个节点类,用于在场景图中对节点进行平移、旋转、缩放等变换操作,可以将其子节点沿着给定的变换矩阵进行变换。通常用于表示模型的空间变换和动画效果。例如,我们可以使用以下代码将一个模型沿X轴平移10个单位:osg::ref_ptr<osg::MatrixTransform> modelTransform = new osg::MatrixTransform();
modelTransform->setMatrix(osg::Matrix::translate(10.0, 0.0, 0.0));
modelTransform->addChild(modelNode);其中 modelNode 是待变换的模型节点,modelTransform 是应用了平移变换的 MatrixTransform 节点。而 osgWidget::Box 则是 OSGWidget 库中的一个部件类,用于创建一个矩形区域作为容器,可以将其他部件添加到其中。通常用于创建用户界面的布局结构。例如,我们可以使用以下代码创建一个 Box 容器,并将两个按钮添加到其中:osg::ref_ptr<osgWidget::Box> container = new osgWidget::Box("container");
osg::ref_ptr<osgWidget::Button> button1 = new osgWidget::Button("button1");
osg::ref_ptr<osgWidget::Button> button2 = new osgWidget::Button("button2");
container->addWidget(button1.get());
container->addWidget(button2.get());其中,container 是创建的 Box 容器,button1 和 button2 是添加到容器中的按钮部件。综上所述,osg::MatrixTransform 用于场景图节点的变换操作,而 osgWidget::Box 用于用户界面的布局组件。

osg::MatrixTransform 和 osgWidget::Box 测试代码

644283b4af53bb284e470d99

以下是一个简单的测试代码,其中创建了一个osg::MatrixTransform节点来控制osgWidget::Box组件的位置和方向。在这个例子中,osgWidget::Box组件被添加到场景图中,然后放置在osg::MatrixTransform节点上,之后通过调整osg::MatrixTransform节点的变换来改变osgWidget::Box组件的位置和方向。

#include <osg/MatrixTransform>
#include <osgWidget/Box>
#include <osgViewer/Viewer>int main()
{// 创建场景根节点osg::ref_ptr<osg::Group> root = new osg::Group;// 创建MatrixTransform节点osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;// 创建osgWidget::Box组件osg::ref_ptr<osgWidget::Box> box = new osgWidget::Box;box->setMargin(1); // 设置边界大小box->setColor(1.0f, 0.0f, 0.0f, 1.0f); // 设置颜色为红色// 将Box组件添加到MatrixTransform节点上mt->addChild(box);// 将MatrixTransform节点添加到场景中root->addChild(mt);// 设置初始变换osg::Matrix m;m.setTrans(osg::Vec3(0, 0, 0)); // 设置平移mt->setMatrix(m); // 应用平移变换// 创建OSG Viewer窗口osgViewer::Viewer viewer;viewer.setSceneData(root);viewer.realize();// 进入主循环while (!viewer.done()){osg::Matrix transMat;transMat.makeTranslate(osg::Vec3(1.0f, 0.0f, 0.0f)); // 计算新的平移矩阵mt->setMatrix(transMat); // 平移Box组件viewer.frame();}return 0;
}

这段代码的效果是每次循环时将osgWidget::Box组件向右平移1.0f的距离。通过这个例子可以发现,osg::MatrixTransform和osgWidget::Box可以非常容易地结合使用来实现3D交互式界面。