Hello Theodore,

Hall, Theodore wrote:
On Sunday, February 01, 2009 19:56
Carsten Neumann [mailto:[email protected]] wrote
yes, please send them either to me directly or over the list (there is a limit on attachment size, but if you tar.gz the files it should not be a problem).

OK, I'm attaching a .zip file with the C++ source code and data files.
To see the behavior in question:
*  Compile and execute the program.
*  The setAutoFrustum and setVolumeDrawing are initially true.
*  The z translation of the scene is initially non-zero (-2, to be
   precise).
*  Note the white X through the viewport that seems to be a frustum
   or bounding box.  It's not part of the scene graph.
*  Toggle F3 to switch the scene translation between zero and non-zero.
   Note how the white box comes and goes.
*  With the white box visible, "walk around" the scene by holding the
   'A' key and tapping the right arrow (to translate left and rotate right).
   Note how the white box translates with the camera but does not rotate
   with it.  It generally seems to contain no geometry.

I fixed a bug affecting the drawing of the camera frustum (the last matrix from processing the draw tree was left on the matrix stack, so the frustum was drawn in that coordinate system instead of world coordinates). Now there is no difference when you hit 'F3'/'3'. However, it is still possible to see the camera frustum when moving around, still looking into what is causing that. If anybody else is curious and wants to play with this ( ;) ), I've attached a version of the program that uses glut.

        Cheers,
                Carsten
#include <OpenSG/OSGBaseFunctions.h>
#include <OpenSG/OSGCoredNodePtr.h>
#include <OpenSG/OSGGeometry.h>
#include <OpenSG/OSGGL.h>
#include <OpenSG/OSGGLUT.h>
#include <OpenSG/OSGGroup.h>
#include <OpenSG/OSGMatrix.h>
#include <OpenSG/OSGNode.h>
#include <OpenSG/OSGNodePtr.h>
#include <OpenSG/OSGPassiveBackground.h>
#include <OpenSG/OSGPassiveViewport.h>
#include <OpenSG/OSGPassiveWindow.h>
#include <OpenSG/OSGPerspectiveCamera.h>
#include <OpenSG/OSGPointLight.h>
#include <OpenSG/OSGRenderAction.h>
#include <OpenSG/OSGSceneFileHandler.h>
#include <OpenSG/OSGSimpleStatisticsForeground.h>
#include <OpenSG/OSGSwitch.h>
#include <OpenSG/OSGTransform.h>


OSG::RenderAction* render_action;

bool auto_frustum   = true;
bool volume_drawing = true;

int vp_x = 0;
int vp_y = 0;
int vp_w = 800;
int vp_h = 600;

//!! A non-zero translation of the scene reveals the bug.
//!! A zero translation does not.
//!! Key F3 toggles this.
float         scene_ztran_init = -2.f;  //!! Try -2.f
float         scene_ztran      = scene_ztran_init;
OSG::Matrix4f scene_mtx;

double time_0 = 0.0;
double time_1 = 0.0;
double delta_t;
OSG::Real32 pitch = 0.f;
OSG::Real32 yaw   = 0.f;
OSG::Vec3f tran_0, tran, x_axis, z_axis, v_axis;
OSG::Quaternion rot_0, rot;
OSG::Matrix4f mtx;

OSG::PassiveWindowPtr window;
OSG::TransformPtr     user_core;
OSG::TransformPtr     scene_root_core;

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

int main(int argc, char *argv[])
{
    const OSG::BitVector core_mask     = OSG::Node::CoreFieldMask;
    const OSG::BitVector children_mask = OSG::Node::ChildrenFieldMask;

    // OSG init
    OSG::ChangeList::setReadWriteDefault();
    OSG::osgInit(argc,argv);

    int glutWinId = setupGLUT(&argc, argv);
    time_0        = glutGet(GLUT_ELAPSED_TIME);

                      user_core = OSG::Transform::create();
    OSG::NodePtr      user      = OSG::Node::create();
    OSG::beginEditCP(user, core_mask);
        user->setCore(user_core);
    OSG::endEditCP(user, core_mask);

                      scene_root_core = OSG::Transform::create();
    OSG::NodePtr      scene_root      = OSG::Node::create();
    OSG::beginEditCP(scene_root, core_mask);
        scene_root->setCore(scene_root_core);
    OSG::endEditCP(scene_root, core_mask);

    OSG::PointLightPtr light_core = OSG::PointLight::create();
    OSG::NodePtr       light      = OSG::Node::create();
    OSG::beginEditCP(light_core, OSG::Light::BeaconFieldMask);
        light_core->setBeacon(user);
    OSG::endEditCP(light_core, OSG::Light::BeaconFieldMask);

    OSG::beginEditCP(light, core_mask | children_mask);
        light->setCore(light_core);
        light->addChild(scene_root);
    OSG::endEditCP(light, core_mask | children_mask);   // added children mask

    OSG::GroupPtr root_core = OSG::Group::create();
    OSG::NodePtr  root      = OSG::Node::create();
    OSG::beginEditCP(root, core_mask | children_mask);
        root->setCore(root_core);
        root->addChild(user);
        root->addChild(light);
    OSG::endEditCP(root, core_mask | children_mask);

    // Set up run-time statistics.
    OSG::SimpleStatisticsForegroundPtr runtime_stats = OSG::SimpleStatisticsForeground::create();
    OSG::beginEditCP(runtime_stats, OSG::StatisticsForeground::ElementIDsFieldMask);
        runtime_stats->addElement(OSG::RenderAction::statNGeometries, "Nodes Drawn: %d");
        runtime_stats->addElement(OSG::Drawable::statNTriangles, "Triangles: %d");
        runtime_stats->addElement(OSG::Drawable::statNPrimitives, "Batches: %d");
        runtime_stats->addElement(OSG::RenderAction::statNOcclusionTests, "Occlusion Tests: %d"); 
        runtime_stats->addElement(OSG::RenderAction::statNOcclusionCulled, "Occluded Objects: %d"); 
    OSG::endEditCP(runtime_stats, OSG::StatisticsForeground::ElementIDsFieldMask);

    // Set up the render action.
    render_action = OSG::RenderAction::create();
    render_action->setStatistics(&runtime_stats->editCollector());
    render_action->setZWriteTrans(true);
    render_action->setSortTrans(true);
    render_action->setAutoFrustum(auto_frustum);
    render_action->setStateSorting(true);
    render_action->setCorrectTwoSidedLighting(true);
    render_action->setLocalLights(true);
    render_action->setVolumeDrawing(volume_drawing);
    render_action->setFrustumCulling(true);
    render_action->setSmallFeatureCulling(true);
    render_action->setSmallFeaturePixels(0);
    render_action->setSmallFeatureThreshold(50);

    // removed check for extension GL_ARB_occlusion_query
    render_action->setOcclusionCullingMode(OSG::RenderAction::OcclusionHierarchicalMultiFrame);
    render_action->setOcclusionCullingThreshold(0);
    render_action->setOcclusionCullingPixels(10);


    // Create a camera.
    const OSG::BitVector cam_mask =
        OSG::PerspectiveCamera::FovFieldMask |
        OSG::Camera::BeaconFieldMask |
        OSG::Camera::NearFieldMask |
        OSG::Camera::FarFieldMask;
    OSG::PerspectiveCameraPtr camera = OSG::PerspectiveCamera::create();
    OSG::beginEditCP(camera, cam_mask);
        camera->setFov(OSG::deg2rad(45.0));
        camera->setBeacon(user);
        camera->setNear(0.1f);
        camera->setFar(100.f);
    OSG::endEditCP(camera, cam_mask);

    // Create a viewport.
    const OSG::BitVector viewport_mask =
            OSG::Viewport::LeftFieldMask | OSG::Viewport::RightFieldMask |
            OSG::Viewport::BottomFieldMask | OSG::Viewport::TopFieldMask |
            OSG::Viewport::CameraFieldMask |
            OSG::Viewport::RootFieldMask |
            OSG::Viewport::BackgroundFieldMask |
            OSG::Viewport::ForegroundsFieldMask;
    OSG::PassiveBackgroundPtr background = OSG::PassiveBackground::create();
    OSG::PassiveViewportPtr   viewport   = OSG::PassiveViewport::create();
    OSG::beginEditCP(viewport, viewport_mask);
        viewport->setSize(0.f, 0.f, 1.f, 1.f);
        viewport->setCamera(camera);
        viewport->setRoot(root);
        viewport->setBackground(background);
        viewport->editMFForegrounds()->push_back(runtime_stats);
    OSG::endEditCP(viewport, viewport_mask);

    // Create an OpenSG window.
    window = OSG::PassiveWindow::create();
    OSG::beginEditCP(window, OSG::Window::PortFieldMask); 
        window->addPort(viewport);
    OSG::endEditCP(window, OSG::Window::PortFieldMask);
    window->init();
    
    //!! Read the scene and attach it to the scene graph.
    //!! Compare rendering of ".wrl" and ".obj" files.
    const std::string scenefile("data/tetra.obj");  //!! Here.
    OSG::NodePtr scene = OSG::SceneFileHandler::the().read(scenefile.c_str());
    if(scene == OSG::NullFC)
    {
        std::cerr << "Failed: couldn't read " << scenefile << std::endl;
        return EXIT_FAILURE;
    }
    OSG::beginEditCP(scene_root, children_mask);
        scene_root->addChild(scene);
    OSG::endEditCP(scene_root, children_mask);

    scene_mtx.setTransform(OSG::Vec3f(0.f, 0.f, scene_ztran));  
    OSG::beginEditCP(scene_root_core, OSG::Transform::MatrixFieldMask);
        scene_root_core->setMatrix(scene_mtx);
    OSG::endEditCP(scene_root_core, OSG::Transform::MatrixFieldMask);
    
    //!! A non-zero translation of the camera beacon is OK.
    //!! The bug is not sensitive to this.
    //!! It's convenient to start this a few units in front of the scene.
    tran_0.setValue(OSG::Vec3f(0.f, 0.f, 4.f));  //!! Try z = 4.f
    rot_0.setIdentity();
    tran = tran_0;
    rot = rot_0;

    mtx.setTransform(tran, rot);

    root->updateVolume();

    // Print basic instructions.
    std::cout
        << "\n"
        << " Press 'S' to move the camera back.\n"
        << " Press 'W' to move the camera forward.\n"
        << " Press 'A' to move the camera left.\n"
        << " Press 'D' to move the camera right.\n"
        << " Press 'Q' to move the camera down.\n"
        << " Press 'E' to move the camera up.\n"
        << "\n"
        << " Press the arrow keys to pitch and yaw the camera.\n"
        << "\n"
        << " Press 'R' to reset the camera position.\n"
        << "\n"
        << " Press '1' to toggle OSG::RenderAction::setAutoFrustum\n"
        << " Press '2' to toggle OSG::RenderAction::setVolumeDrawing\n"
        << " Press '3' to toggle the scene z-translation\n"
        << "\n"
        << " Press 'Esc' to exit the program." << std::endl;

    glutMainLoop();
}




// react to keys
void keyboard(unsigned char k, int , int )
{
    bool printPos = false;

    x_axis.setValue(mtx[0]);
    z_axis.setValue(mtx[2]);
    v_axis.setValue(osg::Vec3f(0.f, 1.f, 0.f));

    switch(k)
    {
    case 27:
    {
        OSG::osgExit();
        exit(0);
    }
    break;

    case '1':
    {
        auto_frustum = !auto_frustum;
        render_action->setAutoFrustum(auto_frustum);
        std::cout << "auto_frustum: " << auto_frustum << std::endl;
    }
    break;

    case '2':
    {
        volume_drawing = !volume_drawing;
        render_action->setVolumeDrawing(volume_drawing);
        std::cout << "volume_drawing: " << volume_drawing << std::endl;
    }
    break;
    
    case '3':
    {
        scene_ztran = (scene_ztran == 0.f) ? scene_ztran_init: 0.f;
        scene_mtx.setTranslate(OSG::Vec3f(0.f, 0.f, scene_ztran));

        OSG::beginEditCP(scene_root_core, OSG::Transform::MatrixFieldMask);
            scene_root_core->setMatrix(scene_mtx);
        OSG::endEditCP(scene_root_core, OSG::Transform::MatrixFieldMask);

        printPos = true;
    }
    break;
    
    case 'w':
        tran -= z_axis * 0.5f;
        printPos = true;
        break;
    case 's':
        tran += z_axis * 0.5f;
        printPos = true;
        break;
    
    case 'a':
        tran -= x_axis * 0.5f;
        printPos = true;
        break;
    case 'd':
        tran += x_axis * 0.5f;
        printPos = true;
        break;
        
    case 'q':
        tran -= v_axis * 0.5f;
        printPos = true;
        break;
        
    case 'e':
        tran += v_axis * 0.5f;
        printPos = true;
        break;

    case 'u':
        pitch += OSG::deg2rad(5.f);
        printPos = true;
        break;
    case 'j':
        pitch -= OSG::deg2rad(5.f);
        printPos = true;
        break;

    case 'h':
        yaw += OSG::deg2rad(5.f);
        printPos = true;
        break;
    case 'k':
        yaw -= OSG::deg2rad(5.f);
        printPos = true;
        break;

    case 'r':
    {
        tran  = tran_0;
        rot   = rot_0;
        pitch = 0.f;
        yaw   = 0.f;

        printPos = true;
    }
    break;
    
    case 'p':
    {
        printPos = true;
    }

    }
    
    // Update the camera beacon position.
    rot =  OSG::Quaternion(OSG::Vec3f(0.f, 1.f, 0.f), yaw);
    rot *= OSG::Quaternion(OSG::Vec3f(1.f, 0.f, 0.f), pitch);
    mtx.setTransform(tran, rot);
    OSG::beginEditCP(user_core, OSG::Transform::MatrixFieldMask);
        user_core->setMatrix(mtx);
    OSG::endEditCP(user_core, OSG::Transform::MatrixFieldMask);

    if(printPos == true)
    {
        std::cout << "scene_root_core:\n" << scene_root_core->getMatrix() << std::endl;
        std::cout << "user_core:\n"       << user_core->getMatrix() << std::endl;
        std::cout << "frustum:\n"         << render_action->getFrustum() << std::endl;
    }

    glutPostRedisplay();
}

void display(void)
{
    time_1  = glutGet(GLUT_ELAPSED_TIME);
    delta_t = time_1 - time_0;
    time_0  = time_1;

    // Render.
    glViewport(0, 0, vp_w, vp_h);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glDrawBuffer(GL_BACK); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    window->frameInit();
    window->render(render_action);

    glutSwapBuffers();
    window->frameExit();

    OSG::Thread::getCurrentChangeList()->clearAll();
}

// react to size changes
void reshape(int w, int h)
{
    vp_w = w;
    vp_h = h;

    window->resize(w, h);
}

void idle(void)
{
    glutPostRedisplay();
}

// 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);
    glutIdleFunc(idle);
    glutKeyboardFunc(keyboard);

    return winid;
}
------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to