Hi Kony,

sorry for the delay, needed some time to get a working system again.

On Thu, 2005-03-03 at 15:30 +0100, Kornelia Ganglbauer wrote: 
>  Hallo!
> 
> I have still a big problem and do not find a solution. Perhaps anybody can
> tell me, if what I want is even possible?
> 
> 
> I have the (x/y/z) coordinates of a point in my scene. Now I want to get the
> corresponding coordinates (x1/y1) within the Window. 
> That means I want to get the pixel to the scenevoxel.
> Does any possibility  for that exist? Please help me.

Yup, you can use the getWorldToScreen() method of the Camera, see the
attached example.

Yours

Dirk

-- 
Dirk Reiners <[EMAIL PROTECTED]>
// OpenSG Tutorial Example: Hello World
//
// Minimalistic OpenSG program
// 
// This is the shortest useful OpenSG program 
// (if you remove all the comments ;)
//
// It shows how to use OpenSG together with GLUT to create a little
// interactive scene viewer.
//

// 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>
#include <OpenSG/OSGPassiveWindow.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

using namespace std;

// The SimpleSceneManager to manage simple applications
SimpleSceneManager *mgr;

NodePtr scene;

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

// 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
    PassiveWindowPtr gwin= PassiveWindow::create();
    gwin->init();

    // create the scene
    scene = makeTorus(.5, 2, 16, 16);

    // 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
//

int width,height;

// redraw the window
void display(void)
{
    mgr->redraw();
    
    // Get the Screen space bounding box
    
    Pnt3f wbb[8];
    
    DynamicVolume vol;
    scene->getWorldVolume(vol);

    wbb[0].setValues(vol.getMin()[0], vol.getMin()[1], vol.getMin()[2]);
    wbb[1].setValues(vol.getMax()[0], vol.getMin()[1], vol.getMin()[2]);
    wbb[2].setValues(vol.getMin()[0], vol.getMax()[1], vol.getMin()[2]);
    wbb[3].setValues(vol.getMax()[0], vol.getMax()[1], vol.getMin()[2]);
    wbb[4].setValues(vol.getMin()[0], vol.getMin()[1], vol.getMax()[2]);
    wbb[5].setValues(vol.getMax()[0], vol.getMin()[1], vol.getMax()[2]);
    wbb[6].setValues(vol.getMin()[0], vol.getMax()[1], vol.getMax()[2]);
    wbb[7].setValues(vol.getMax()[0], vol.getMax()[1], vol.getMax()[2]);

    // Get the world to screen matrix to transform world space to screen
    Matrix wtos;
    
    ViewportPtr port = mgr->getWindow()->getPort()[0];
    CameraPtr cam = port->getCamera();

    cam->getWorldToScreen(wtos, *port);
    
    cout << wtos << endl;
    
    // Transform points
    Pnt2f bb[8];
   
    for(UInt16 i = 0; i < 8; ++i)
    {
        Pnt3f p;
        wtos.multFullMatrixPnt(wbb[i],p);
        cout << wbb[i] << ":" << p << endl;
        
        // Points after wtos are [-1,1]x[-1,1]x[-1,1]
        bb[i].setValues(((p[0] * .5) + .5) * width,
                        ((p[1] * .5) + .5) * height);
    }

    // Draw them
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,width,0,height,-1,1);
    glMatrixMode(GL_MODELVIEW);
   
    glLoadIdentity();
    
    glDisable(GL_DEPTH_TEST);
    glColor3f(0,1,0);
    
    UInt8 inds[] = { 0,1, 2,3, 4,5, 6,7, 0,2, 1,3, 4,6, 5,7, 
                     0,4, 1,5, 2,6, 3,7 };
    
    glVertexPointer(2,GL_FLOAT,0,&bb[0]);
    glEnable(GL_VERTEX_ARRAY);
    glDrawElements(GL_LINES, sizeof(inds)/sizeof(UInt8), 
                    GL_UNSIGNED_BYTE, inds);
    glDisable(GL_VERTEX_ARRAY);
    
    glEnable(GL_DEPTH_TEST);
    
    glutSwapBuffers();
}

// react to size changes
void reshape(int w, int h)
{
    mgr->resize(w, h);
    width = w;
    height = 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 27:        
        {
            OSG::osgExit();
            exit(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("OpenSG");
    
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    return winid;
}

Reply via email to