Hello Renan,

    I've put an osg::notify(osg::NOTICE) inside the 'for' nest and outside
the if(drawable.valid()) nest and I saw that it never entered the 'for'...
Just in case you've erased my previous messages, I'm copying the new code
with the appear-if-worked messages (that didn't appear).

I understand you've just started with OSG, but are you just starting C++ as well? No offense intended, but if so, you might want to read a good book and try some non-graphical programming first, to get a handle on how it works. We will not always be around to debug your code.

For now, I'll show you. You have two bugs in your code.

A) If you're passing the intersections list to another function, and want that function to fill it, then you need to pass it by reference.

So this:

bool PickHandler::Intersecao(osgViewer::Viewer* viewer,
    const osgGA::GUIEventAdapter& ea,
    osgUtil::LineSegmentIntersector::Intersections intersections)

becomes this:

bool PickHandler::Intersecao(osgViewer::Viewer* viewer,
    const osgGA::GUIEventAdapter& ea,
    osgUtil::LineSegmentIntersector::Intersections &intersections)

(note the ampersand (&) - you'll also have to add it in the method declaration in the PickHandler class)

That will make it so the viewer->computeIntersections will fill the intersections list, and it will still be the same list once you return from the function. Otherwise, you were passing by value, which meant that it was making a copy of the list local to the called function (Intersecao), and it was lost once the function returned to handle().

B) If you want to enter the if (which is in the for loop), you need a valid drawable. You're testing if it is NOT valid, so you will never enter the if.

So this:

                        if(!hitr->drawable.valid())

becomes this:

                        if(hitr->drawable.valid())

Once those two bugs are fixed, I get this on the console:

SELECAO
ENTERED THE FOR
ENTERED HERE

and the sphere becomes red once it's picked. I'm including a modified osgpick.cpp for you to look at. See the comments starting with JSG for what I changed in your code.

As I said, these two bugs are straight C++ and logic bugs, so you cannot expect us to always help you with them. I hope this will get you started, though.

Good luck,

J-S
--
______________________________________________________
Jean-Sebastien Guay     [EMAIL PROTECTED]
                        http://whitestar02.webhop.org/

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

/* OpenSceneGraph example, osgpick.
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/

/* osgpick sample
* demonstrate use of osgUtil/PickVisitor for picking in a HUD or
* in a 3d scene,
*/

#include <osgUtil/Optimizer>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>

#include <osg/Material>
#include <osg/Geode>
#include <osg/BlendFunc>
#include <osg/Depth>
#include <osg/Projection>
#include <osg/MatrixTransform>
#include <osg/Camera>
#include <osg/io_utils>

#include <osgText/Text>
#include <osg/ShapeDrawable>

#include <sstream>

// class to handle events with a pick
class PickHandler : public osgGA::GUIEventHandler {
public: 

    PickHandler(osgText::Text* updateText):
        _updateText(updateText) {}
        
    ~PickHandler() {}
    
    bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);

    virtual bool Intersecao(osgViewer::Viewer* viewer, const 
osgGA::GUIEventAdapter& ea,
                            osgUtil::LineSegmentIntersector::Intersections 
&intersections);    // JSG fixed

    void setLabel(const std::string& name)
    {
        if (_updateText.get()) _updateText->setText(name);
    }
    
protected:

    osg::ref_ptr<osgText::Text>  _updateText;
};

bool PickHandler::handle(const osgGA::GUIEventAdapter& ea, 
osgGA::GUIActionAdapter& aa)
{
    osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>( &aa );
    osgUtil::LineSegmentIntersector::Intersections intersections;

    if(!viewer)
    { return false; }

    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::PUSH):
        {
            if(Intersecao(viewer, ea, intersections))
            {
                osg::notify(osg::NOTICE)<< "SELECAO" << std::endl;
                for(osgUtil::LineSegmentIntersector::Intersections::iterator 
hitr =
                    intersections.begin(); hitr != intersections.end(); ++hitr)
                {
                    osg::notify(osg::NOTICE)<< "ENTERED THE FOR" << std::endl;
                    if(hitr->drawable.valid())    // JSG fixed
                    {
                        osg::notify(osg::NOTICE)<< "ENTERED HERE" << std::endl;
                        osg::ShapeDrawable* shapedrawable = 
dynamic_cast<osg::ShapeDrawable*>(hitr-> drawable.get());
                        shapedrawable->setColor(osg::Vec4(1.0, 0.0, 0.0, 1.0));
                        return true;    // JSG added
                    }
                }
            }
        }
    }

    return false;    // JSG added
}

bool PickHandler::Intersecao(osgViewer::Viewer* viewer, const 
osgGA::GUIEventAdapter& ea,
                             osgUtil::LineSegmentIntersector::Intersections 
&intersections)    // JSG fixed
{
    return (viewer->computeIntersections(ea.getX(),ea.getY(),intersections));
}

osg::Node* createHUD(osgText::Text* updateText)
{

    // create the hud. derived from osgHud.cpp
    // adds a set of quads, each in a separate Geode - which can be picked 
individually
    // eg to be used as a menuing/help system!
    // Can pick texts too!

    osg::Camera* hudCamera = new osg::Camera;
    hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    hudCamera->setProjectionMatrixAsOrtho2D(0,1280,0,1024);
    hudCamera->setViewMatrix(osg::Matrix::identity());
    hudCamera->setRenderOrder(osg::Camera::POST_RENDER);
    hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
    
    std::string timesFont("fonts/times.ttf");
    
    // turn lighting off for the text and disable depth test to ensure its 
always ontop.
    osg::Vec3 position(150.0f,800.0f,0.0f);
    osg::Vec3 delta(0.0f,-60.0f,0.0f);
    
    {
        osg::Geode* geode = new osg::Geode();
        osg::StateSet* stateset = geode->getOrCreateStateSet();
        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
        stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
        geode->setName("simple");
        hudCamera->addChild(geode);
        
        osgText::Text* text = new  osgText::Text;
        geode->addDrawable( text );
        
        text->setFont(timesFont);
        text->setText("Picking in Head Up Displays is simple!");
        text->setPosition(position);
        
        position += delta;
    }    
    
    
    for (int i=0; i<5; i++) {
        osg::Vec3 dy(0.0f,-30.0f,0.0f);
        osg::Vec3 dx(120.0f,0.0f,0.0f);
        osg::Geode* geode = new osg::Geode();
        osg::StateSet* stateset = geode->getOrCreateStateSet();
        const char *opts[]={"One", "Two", "Three", "January", "Feb", "2003"};
        osg::Geometry *quad=new osg::Geometry;
        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
        stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
        std::string name="subOption";
        name += " ";
        name += std::string(opts[i]);
        geode->setName(name);
        osg::Vec3Array* vertices = new osg::Vec3Array(4); // 1 quad
        osg::Vec4Array* colors = new osg::Vec4Array;
        colors = new osg::Vec4Array;
        colors->push_back(osg::Vec4(0.8-0.1*i,0.1*i,0.2*i, 1.0));
        quad->setColorArray(colors);
        quad->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);
        (*vertices)[0]=position;
        (*vertices)[1]=position+dx;
        (*vertices)[2]=position+dx+dy;
        (*vertices)[3]=position+dy;
        quad->setVertexArray(vertices);
        quad->addPrimitiveSet(new 
osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
        geode->addDrawable(quad);
        hudCamera->addChild(geode);
        
        position += delta;
    }    
    
    
    
    { // this displays what has been selected
        osg::Geode* geode = new osg::Geode();
        osg::StateSet* stateset = geode->getOrCreateStateSet();
        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
        stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
        geode->setName("The text label");
        geode->addDrawable( updateText );
        hudCamera->addChild(geode);
        
        updateText->setCharacterSize(20.0f);
        updateText->setFont(timesFont);
        updateText->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
        updateText->setText("");
        updateText->setPosition(position);
        updateText->setDataVariance(osg::Object::DYNAMIC);
        
        position += delta;
    }    
    
    return hudCamera;

}

int main( int argc, char **argv )
{

    // use an ArgumentParser object to manage the program arguments.
    osg::ArgumentParser arguments(&argc,argv);

    // construct the viewer.
    osgViewer::Viewer viewer;
/*
    // read the scene from the list of file specified commandline args.
    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
     
    // if not loaded assume no arguments passed in, try use default mode 
instead.
    if (!scene) scene = osgDB::readNodeFile("fountain.osg");

    osg::ref_ptr<osg::Group> group = dynamic_cast<osg::Group*>(scene.get());
    if (!group)
    {
        group = new osg::Group;
        group->addChild(scene.get());
    }
*/
    osg::ref_ptr<osg::Group> group = new osg::Group;
    osg::ref_ptr<osg::ShapeDrawable> sphere = new osg::ShapeDrawable(
        new osg::Sphere(osg::Vec3(0,0,0), 0.5));
    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    geode->addDrawable(sphere.get());
    group->addChild(geode.get());

    osg::ref_ptr<osgText::Text> updateText = new osgText::Text;

    // add the HUD subgraph.    
    group->addChild(createHUD(updateText.get()));

    // add the handler for doing the picking
    viewer.addEventHandler(new PickHandler(updateText.get()));

    // set the scene to render
    viewer.setSceneData(group.get());

    return viewer.run();
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to