旋转 3D 对象 (OSG & vc++)

发布于 2024-11-10 08:19:27 字数 488 浏览 0 评论 0原文

我正在使用 VC++ 和 OSG 开发 3D 环境,我需要一些帮助

我正在使用下面的代码为场景的 3D 模型充电

    mueble00Node = osgDB::readNodeFile("Model/mueble_desk.3ds");
    mueble00Transform = new osg::MatrixTransform;
    mueble00Transform->setName("mueble00");
    mueble00Transform->setDataVariance(osg::Object::STATIC);
    mueble00Transform->addChild(mueble00Node);
    sceneRoot->addChild(mueble00Transform);

我尝试使用一些线来旋转 3D 模型,但没有结果

任何人都可以吗向我解释一下如何使模型绕其自己的轴旋转?

I'm developing a 3D environment using VC++ and OSG and I need some help

I'm using this code below to charge the 3D models for the scene

    mueble00Node = osgDB::readNodeFile("Model/mueble_desk.3ds");
    mueble00Transform = new osg::MatrixTransform;
    mueble00Transform->setName("mueble00");
    mueble00Transform->setDataVariance(osg::Object::STATIC);
    mueble00Transform->addChild(mueble00Node);
    sceneRoot->addChild(mueble00Transform);

I've tried with some lines to rotate the 3D models, but with no result

Could anybody explain to me how to rotate the models on its own axis?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅此而已 2024-11-17 08:19:27

使用 MatrixTransform::setMatrix() 更改子节点的方向。

MatrixTransform* transform = new osg::MatrixTransform;
const double angle = 0.8;
const Vec3d axis(0, 0, 1);
transform->setMatrix(Matrix::rotate(angle, axis));

下面是一个完整的程序,它加载并显示添加和不添加变换的 3D 对象。

#include <string>
#include <osg/Object>
#include <osg/Node>
#include <osg/Transform>
#include <osg/Matrix>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgGA/TrackballManipulator>

using namespace osg;

int main(int argc, char** argv)
{
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << "<file>\n";
        exit(1);
    }
    const std::string file = argv[1];

    // Load a node.
    Node* node = osgDB::readNodeFile(file);
    if (!node) {
        std::cerr << "Can't load node from file '" << file << "'\n";
        exit(1);
    }

    // Set a transform for the node.
    MatrixTransform* transform = new osg::MatrixTransform;
    const double angle = 0.8;
    const Vec3d axis(0, 0, 1);
    transform->setMatrix(Matrix::rotate(angle, axis));
    transform->setName(file);
    transform->addChild(node);

    // Add the node with and without the transform.
    Group* scene = new Group();
    scene->addChild(transform);
    scene->addChild(node);

    // Start a scene graph viewer.
    osgViewer::Viewer viewer;
    viewer.setSceneData(scene);
    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
    viewer.realize();
    while (!viewer.done()) viewer.frame();
}

Use MatrixTransform::setMatrix() to change the orientation of the child node.

MatrixTransform* transform = new osg::MatrixTransform;
const double angle = 0.8;
const Vec3d axis(0, 0, 1);
transform->setMatrix(Matrix::rotate(angle, axis));

Below is a complete program that loads and displays a 3D object with and without the transformation added.

#include <string>
#include <osg/Object>
#include <osg/Node>
#include <osg/Transform>
#include <osg/Matrix>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgGA/TrackballManipulator>

using namespace osg;

int main(int argc, char** argv)
{
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << "<file>\n";
        exit(1);
    }
    const std::string file = argv[1];

    // Load a node.
    Node* node = osgDB::readNodeFile(file);
    if (!node) {
        std::cerr << "Can't load node from file '" << file << "'\n";
        exit(1);
    }

    // Set a transform for the node.
    MatrixTransform* transform = new osg::MatrixTransform;
    const double angle = 0.8;
    const Vec3d axis(0, 0, 1);
    transform->setMatrix(Matrix::rotate(angle, axis));
    transform->setName(file);
    transform->addChild(node);

    // Add the node with and without the transform.
    Group* scene = new Group();
    scene->addChild(transform);
    scene->addChild(node);

    // Start a scene graph viewer.
    osgViewer::Viewer viewer;
    viewer.setSceneData(scene);
    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
    viewer.realize();
    while (!viewer.done()) viewer.frame();
}
墨小墨 2024-11-17 08:19:27

您需要使用 quat

http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDocs /a00568.html

它有许多可用于旋转的函数。

You'll want to use a quat

http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDocs/a00568.html

It has a number of functions you can use for rotation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文