Ideally I'd be doing it on the CPU, but the particular objects I'm picking
are skinned meshes drawn in a 3rd party library, so I don't think there's
any reasonable way to do that, since the vertex positions aren't known on
the CPU.

Max


On Fri, Sep 27, 2013 at 2:00 PM, Robert Osfield <[email protected]>wrote:

> Hi Max,
>
> I would not recommend trying to to use rendering to do picking, it
> requires a round trip to the graphics card making it a very slow way to do
> picking.  If you can do picking on the CPU.
>
> Robert.
>
>
> On 27 September 2013 17:34, Max Bandazian <[email protected]> wrote:
>
>> Hi,
>>
>> I'm trying to implement picking via rendering a single frame with unique
>> colors set on each object in the scene, and getting segfaults in the
>> graphics driver. I'm about out of debugging ideas, so any help would be
>> appreciated. The general outline is I create a viewer, give it a shared
>> context, set it to render to an FBO and add an image, and render one frame,
>> at which point it immediately segfaults.
>>
>> When I run this in gDEBugger, I can see that there are textures allocated
>> in the shared context, but none of them have any data; this is probably
>> what's causing the crash, but since the corresponding textures in the main
>> context do have data, I'm not sure what the problem is. Source is below,
>> simplified a little for readability; hopefully I'm just doing something
>> dumb and easily spotted.
>>
>> Thanks,
>> Max Bandazian
>>
>>
>>    osg::Image* image = new osg::Image;
>>    {
>>       mainViewer->stopThreading();
>>
>>       osg::GraphicsContext* mainContext =
>> mainViewer->getCamera()->getGraphicsContext();
>>       osg::ref_ptr<osg::GraphicsContext::Traits> traits = new
>> osg::GraphicsContext::Traits;
>>       traits->doubleBuffer = false;
>>       traits->vsync = false;
>>       traits->x = 0;
>>       traits->y = 0;
>>       traits->width = widthPx;
>>       traits->height = heightPx;
>>       traits->windowDecoration = false;
>>       traits->pbuffer = true;
>>       traits->red = 8;
>>       traits->green = 8;
>>       traits->blue = 8;
>>       traits->alpha = 8;
>>       traits->depth = 24;
>>       traits->sharedContext = mainContext;
>>
>>       osg::ref_ptr<osg::GraphicsContext> pickContext =
>> osg::GraphicsContext::createGraphicsContext(traits.get());
>>
>>
>>       osg::Texture2D* tex = new osg::Texture2D;
>>       tex->setTextureSize(PICK_TEXTURE_DIMENSION, PICK_TEXTURE_DIMENSION);
>>
>>       // Set up the viewer
>>       osg::ref_ptr<osgViewer::Viewer> pickView = new osgViewer::Viewer();
>>       pickView->setThreadingModel(osgViewer::ViewerBase::SingleThreaded);
>>
>>       osg::Camera* pickCamera = pickView->getCamera();
>>       pickCamera->setGraphicsContext(pickContext.get());
>>
>> pickCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
>>
>>       pickCamera->setProjectionMatrix(sceneCamera->getProjectionMatrix());
>>       pickCamera->setViewport(0, 0, PICK_TEXTURE_DIMENSION,
>> PICK_TEXTURE_DIMENSION);
>>
>>       pickCamera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
>>       pickCamera->setClearMask( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT
>> );
>>
>>       osg::Shader* vs = new osg::Shader(osg::Shader::VERTEX);
>>       vs->loadShaderSourceFromFile("pick_shader.vert");
>>       osg::Shader* fs = new osg::Shader(osg::Shader::FRAGMENT);
>>       fs->loadShaderSourceFromFile("pick_shader.frag");
>>
>>       osg::Program* program = new osg::Program;
>>       program->addBindAttribLocation("attr_weight_array",
>> osg::Drawable::ATTRIBUTE_6);
>>       program->addBindAttribLocation("attr_index_array",
>>  osg::Drawable::ATTRIBUTE_7);
>>       program->addBindAttribLocation("attr_tangent",
>>  osg::Drawable::SECONDARY_COLORS);
>>       program->addShader(vs);
>>       program->addShader(fs);
>>
>>       pickView->setSceneData(skinnedMeshRoot);
>>
>>       pickCamera->getOrCreateStateSet()->setAttributeAndModes(program,
>> osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
>>
>>       osg::ShadeModel* sm = new osg::ShadeModel(osg::ShadeModel::FLAT);
>>       pickCamera->getOrCreateStateSet()->setAttributeAndModes(sm,
>> osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
>>
>>       // attach an image to the view
>>       image->allocateImage(PICK_TEXTURE_DIMENSION,
>> PICK_TEXTURE_DIMENSION, 1, GL_RGBA, GL_UNSIGNED_BYTE);
>>       pickCamera->attach(osg::Camera::COLOR_BUFFER, image);
>>
>>       // finalize and render
>>       pickView->realize();
>>       pickView->setCameraManipulator(0);
>>       pickView->frame();
>>    }
>>    mainViewer()->startThreading();
>>
>>    return image;
>>
>> _______________________________________________
>> osg-users mailing list
>> [email protected]
>> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>>
>>
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to