erik
erik

Reputation: 31

Rotate a 3D object (OSG & vc++)

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?

Upvotes: 3

Views: 10044

Answers (2)

antonakos
antonakos

Reputation: 8361

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();
}

Upvotes: 3

kand
kand

Reputation: 2338

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.

Upvotes: 1

Related Questions