Hello!
I'm trying to form a translation by using functions.
So I wrote this function to make an easy transformation along the y axis:

void animation_vertical(Vec3f cube_init_vec, Vec3f cube_end_vec, int 
frame_total){
     
    Matrix cube_move;
    cube_move.setIdentity();
    for (frame_current=0; frame_current<=frame_total; frame_current++){
         beginEditCP(cube_trans, Transform::MatrixFieldMask);
                int cube_move_y = 
cube_init_vec.getValues()[1]+(osgabs(cube_end_vec.getValues()[1]-cube_init_vec.getValues()[1])/frame_total*frame_current);
                cube_move.setTranslate(0,cube_move_y,0);
                cube_trans->setMatrix(cube_move);
        endEditCP(cube_trans, Transform::MatrixFieldMask);
    }
}

With the keyboard function I'm activating the type of the animation (because 
there will be some more animations) and the running of the animation itself. In 
addition I set the duration of the animation to zero:

void keyboard(unsigned char k, int x, int y){
    switch (k){
        case '1' : run = true;
                        typ_animation = 1;
                  dauer = 0;
                        break;
        }
}

After that I use the display function to let the animation go on. As I have 
understood this method, this function is being called and called by 
glutDisplayFunc(display);  in the SetupGlut. (Sorry, after adapting to french 
my english gets more and more worse :-/) So, that's why I thought it will 
redraw each new frame position of the animation. 

void display(void){

    if(run==true && dauer<300){
         switch(typ_animation){
               case 1: animation_vertical(Vec3f(0,5,0), Vec3f(0,20,0), dauer);
                break;
           }
        dauer++;
        }
        mgr->redraw();
    }
}

All in all, it changes the positions, but it does not show the animation 
between. Why?! What do I have to change?
Thanks a lot!
Greetings, alex


// GLUT is used for window handling
#include <OpenSG/OSGGLUT.h>

// General OpenSG configuration, needed everywhere
#include <OpenSG/OSGConfig.h>

// Methods to create simple geometry: boxes, spheres, tori etc.
#include <OpenSG/OSGSimpleGeometry.h>

// The GLUT-OpenSG connection class
#include <OpenSG/OSGGLUTWindow.h>

// A little helper to simplify scene management and interaction
#include <OpenSG/OSGSimpleSceneManager.h>

// Activate the OpenSG namespace
// This is not strictly necessary, you can also prefix all OpenSG symbols
// with OSG::, but that would be a bit tedious for this example
OSG_USING_NAMESPACE

// The SimpleSceneManager to manage simple applications
SimpleSceneManager *mgr;
// Hilfsvariablen
UInt32 frame_current = 0;
UInt8 typ_animation=1;
UInt32 dauer=0;
bool run = false;


// Global Transformations
TransformPtr cube_trans;

// forward declaration so we can have the interesting stuff upfront
int setupGLUT( int *argc, char *argv[] );


void animation_vertical(Vec3f cube_init_vec, Vec3f cube_end_vec, int 
frame_total){

        Matrix cube_move;
        cube_move.setIdentity();

        for (frame_current=0; frame_current<=frame_total; frame_current++){

                beginEditCP(cube_trans, Transform::MatrixFieldMask);
                        //int cube_move_y = 
(cube_end_vec.getValues()[1]-cube_init_vec.getValues()[1])/frame_total*frame_current;
                        int cube_move_y = 
cube_init_vec.getValues()[1]+(osgabs(cube_end_vec.getValues()[1]-cube_init_vec.getValues()[1])/frame_total*frame_current);
                        cube_move.setTranslate(0,cube_move_y,0);
                        cube_trans->setMatrix(cube_move);
                endEditCP(cube_trans, Transform::MatrixFieldMask);
        }
        return;

}

void animation_initial(){

        Matrix i;

        beginEditCP(cube_trans, Transform::MatrixFieldMask);
            i.setIdentity();
                i.setTranslate(Vec3f(0,5,0));
                cube_trans->setMatrix(i);
        endEditCP(cube_trans, Transform::MatrixFieldMask);

}

NodePtr createScenegraph(void){

        //Geometrie erstellen
        GeometryPtr wurzel = makeBoxGeo(10, 5, 10, 1, 1, 1);
        NodePtr cube = makeBox(10, 10, 10, 1, 1, 1);

        cube_trans = Transform::create();

        Matrix m;

        // Transformierung erstellen
        beginEditCP(cube_trans, Transform::MatrixFieldMask);
                m.setIdentity();
                m.setTranslate(Vec3f(0,5,0));
                cube_trans->setMatrix(m);
        endEditCP(cube_trans, Transform::MatrixFieldMask);

        NodePtr cube_node= Node::create();
        // Geometrie und Transformierung an einen Node hängen
        beginEditCP(cube_node, Node::CoreFieldMask | Node::ChildrenFieldMask);
                cube_node->addChild(cube);
                cube_node->setCore(cube_trans);
        endEditCP(cube_node, Node::CoreFieldMask | Node::ChildrenFieldMask);

        NodePtr root = Node::create();

        // Root-Node erstellen und erstellte Nodes anhängen
        beginEditCP(root, Node::CoreFieldMask | Node::ChildrenFieldMask);
                root->setCore(wurzel);
                root->addChild(cube_node);
        endEditCP(root, Node::CoreFieldMask | Node::ChildrenFieldMask);

        return root;

}

// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{
    // OSG init
    osgInit(argc,argv);

    // GLUT init
    int winid = setupGLUT(&argc, argv);

    // the connection between GLUT and OpenSG
    GLUTWindowPtr gwin= GLUTWindow::create();
    gwin->setId(winid);
    gwin->init();

        NodePtr scene = createScenegraph();

    // create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    // tell the manager what to manage
    mgr->setWindow(gwin );
    mgr->setRoot  (scene);

    // show the whole scene
    mgr->showAll();
    
    // GLUT main loop
    glutMainLoop();

    return 0;
}
//
// GLUT callback functions
//

// redraw the window
void display(void)
{
        Real32 time = glutGet(GLUT_ELAPSED_TIME);

        if(run==false){
                animation_initial();
        }

        if(run==true && dauer<300){
                switch(typ_animation){
                        case 1: animation_vertical(Vec3f(0,5,0), Vec3f(0,20,0), 
dauer);

                                break;
                                
                }
                dauer++;
        }
        

        mgr->redraw();
        

}

// react to size changes
void reshape(int w, int h)
{
    mgr->resize(w, h);
    glutPostRedisplay();
}

// react to mouse button presses
void mouse(int button, int state, int x, int y)
{
    if (state)
        mgr->mouseButtonRelease(button, x, y);
    else
        mgr->mouseButtonPress(button, x, y);
        
    glutPostRedisplay();
}

// react to mouse motions with pressed buttons
void motion(int x, int y)
{
    mgr->mouseMove(x, y);
    glutPostRedisplay();
}

// react to keys
void keyboard(unsigned char k, int x, int y){
    switch (k){
                case '0' :      run = false;
                                        typ_animation = 0;
                                        break;
        case '1' :      run = true;
                                        typ_animation = 1;
                                        dauer = 0;
                                        break;
    }
}

// setup the GLUT library which handles the windows for us
int setupGLUT(int *argc, char *argv[])
{
    glutInit(argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    
    int winid = glutCreateWindow("Cube_Animation with OpenSG");
    
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
        glutIdleFunc(display);

    return winid;
}
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to