[osg-users] Set Viewport background color as transparent

1,147 views
Skip to first unread message

shekhar vishwa

unread,
Jun 2, 2012, 7:30:51 AM6/2/12
to osg-...@lists.openscenegraph.org
Hi,
 
I am using the OSG, but I unable to set the viewport or view backgraund color as transparent.
 
Please help me to resolve this issue.
 
Thanks
Vishwa

Sebastian Messerschmidt

unread,
Jun 2, 2012, 4:20:53 PM6/2/12
to OpenSceneGraph Users
Hello Vishwa,

Can you please elaborate what you are trying to achieve?
You can set the viewers camera's clear color with an alpha value less than one. (.setClearColor(r,g,b,a))
But I (and possibly others) don't understand what you are trying to do with this, as the background is basically drawn first (i.e. the viewport is set to this color).

cheers
Sebastian
_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

shekhar vishwa

unread,
Jun 4, 2012, 2:21:57 AM6/4/12
to osg-...@lists.openscenegraph.org
Hi,
 
I am trying to implement the 3D overlay above current view. I am trying to set the camera background color as transparent. I am using following code.
 
osg::Node* node= osgDB::readNodeFile( "Models/cessna.osg"); 
osg::Camera* camera = new osg::Camera;
camera->setViewport( 0, 0, 200, 200 );
camera->setClearColor(osg::Vec4(1.0f,1.0,0.8f,0.2f));
osg::StateSet* stateset = camera1->getOrCreateStateSet();
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
camera->setStateSet(stateset);
camera->setRenderOrder( osg::Camera::POST_RENDER );
camera->setAllowEventFocus( true );
camera->setClearMask( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
camera->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
camera->addChild( node1 );
 
 
Above code only set the white background. Please help to set the camera backgroud color as transparent.
 
Thanks
Vishwa

Gio

unread,
Jun 4, 2012, 3:31:08 AM6/4/12
to OpenSceneGraph Users
Hi Shekhar,

to set a transparent background you have to enable alpha when you set your traits 

When you set the graphic context traits 
    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
you can just add the line to your configuration:
    traits->alpha = 1;
after you create a graphiccontext and associate your traits
    osg::ref_ptr<osg::GraphicsContext> graphicsContext = osg::GraphicsContext::createGraphicsContext(traits.get());
you can set it in your camera like this
    _viewer = new osgViewer::Viewer();
    _viewer->getCamera()->setGraphicsContext(graphicsContext);


Robert Osfield

unread,
Jun 4, 2012, 4:52:28 AM6/4/12
to OpenSceneGraph Users
Hi Shekhar,

On 2 June 2012 12:30, shekhar vishwa <vishwa....@gmail.com> wrote:
> I am using the OSG, but I unable to set the viewport or view backgraund
> color as transparent.

Do you actually mean that you want it completely transparent? As in
disable the clear of the colour buffer completely? If so then use the
Camera's ClearMask property set to GL_DEPTH_BUFFER_BIT rather than the
normal default of GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT

Robert.

shekhar vishwa

unread,
Jun 4, 2012, 3:49:12 PM6/4/12
to osg-...@lists.openscenegraph.org
Hi,
 
Thanks for help. Now I can make the camera background as transparent. What parameters need to set the camera background as semi-transparent.
 
Please help me.
 
Thanks
Vishwa 

Robert Osfield

unread,
Jun 4, 2012, 3:53:51 PM6/4/12
to OpenSceneGraph Users
Hi Vishwa,

On 4 June 2012 20:49, shekhar vishwa <vishwa....@gmail.com> wrote:
> Thanks for help. Now I can make the camera background as transparent. What
> parameters need to set the camera background as semi-transparent.

What do you mean by semi-transparent background? Everyone has given
you as much guidance as can be given with what you've said about what
you are trying to do so there is nothing else we can add till you
explain what you are actually after.

Gio

unread,
Jun 4, 2012, 5:11:11 PM6/4/12
to OpenSceneGraph Users

I thin you can try to set the clear mask alpha channel to a value of 0.5
cheers,
John

shekhar vishwa

unread,
Jun 5, 2012, 12:01:29 AM6/5/12
to osg-...@lists.openscenegraph.org
Hi,
 
I want to show the models on semi-transparent backgroud color.  I have implemented following code

camera1->setViewport( 0, 0, 200, 200 );

camera1->setClearColor( osg::Vec4(0.0f, 1.0f, 1.0f, 0.5f) );

camera1->setRenderOrder( osg::Camera::POST_RENDER );

camera1->setAllowEventFocus(

true );

camera1->setClearMask(GL_DEPTH_BUFFER_BIT );

//| GL_DEPTH_BUFFER_BIT

camera1->setReferenceFrame( osg::Transform::ABSOLUTE_RF );

But above code is set the background as complete transparent.
 
Please help me to make the camera background as semi-transparent.
 
Thanks
Vishwa

Sergey Polischuk

unread,
Jun 5, 2012, 4:46:34 AM6/5/12
to OpenSceneGraph Users
Hi
 
Draw semitransparent quad in ortho projection before your models. With clear color disabled.
 
Cheers.
 
05.06.2012, 08:01, "shekhar vishwa" <vishwa....@gmail.com>:

Robert Osfield

unread,
Jun 5, 2012, 6:01:18 AM6/5/12
to OpenSceneGraph Users
Hi Vishwa,

You still don't explain what you mean by a semi-transparent background
colour, given what replies you've already had and you've tried it
suggest to me that you really are talking about something different as
what you are asking really doesn't make sense in an normal graphics
application.

In a normal graphics application objects can be drawn semi-transparent
on top of the background, and multiple Camera's can be layered on top
of each one can have the front Camera's disable the clear of the
colour buffer so they are rendered on top (see osghud), one can even
set the an RGBA colour buffer when doing render to texture as this can
be of use when reusing the texture to do appropriate blending.
However it really doesn't seem like you are asking to do any of these
scenario's, or perhaps you are.

The only way I can make sense of what you are asking is that perhaps
you might be wanting to render window that is transparent so you can
see the desktop below it. Is this correct?

Robert.

Gio

unread,
Jun 6, 2012, 12:10:03 AM6/6/12
to OpenSceneGraph Users
I think Vishwa wants to do something similar to what I am currently doing in iOS. I set transparent background in order to see some layer that is below, which in my case is the image coming from the videocamera. Anyway it would be helpful if Vishwa you can explain us better the purpose of your application and the platform where you are running.
Cheers,
John

shekhar vishwa

unread,
Jun 6, 2012, 12:46:09 AM6/6/12
to osg-...@lists.openscenegraph.org
Hi,
 
I am trying to implement the multiple views in same window. Each view render the diffrent models.User can set the view position at run time by mouse dragging. If dragged view is overlapped with any view then dragged view backgroud should be semi-trasparent.
 
Please suggest me to resolve this.
 
 
Thanks
Vishwa

Robert Osfield

unread,
Jun 6, 2012, 4:58:13 AM6/6/12
to OpenSceneGraph Users
Hi Vishwa,
When you say background of the view being moved do you simply mean the
clear colour that provides the normal background colour of a viewport
needs to be disabled? This is an important point as there isn't
really any concept of "background" in OpenGL or the OSG. Also be
specific about what you mean by semi-transparent. I'm guessing that
you want to see the underlying views being the viewport, but what
blending do you intend between the view at the front and the back?

Robert.

Gio

unread,
Jun 6, 2012, 1:43:12 PM6/6/12
to OpenSceneGraph Users
Hi Vishwa,
the only way I can help you is giving you my code of initialization:
Try to adapt it for non your platform.
variables of instance:
osg::ref_ptr<osgViewer::Viewer> _viewer;
osg::ref_ptr<osg::MatrixTransform> _root;


- (osg::ref_ptr<osg::GraphicsContext>)createGraphicContext
{
    //create our graphics context directly so we can pass our own window 
    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;

    

    // probably you need to change this because it is for iOS
    osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowIOS::WindowData(self.view, osgViewer::GraphicsWindowIOS::WindowData::IGNORE_ORIENTATION);

    

    // Setup the traits parameters
    traits->x = 0;
    traits->y = 0;
    traits->width//put here the width of the window view
    traits->height//put here the height of the window view
    traits->depth = 24; //keep memory down, you can put 16
    traits->windowDecoration = false;
    traits->alpha = 1;
    traits->doubleBuffer = true;
    traits->sharedContext = 0;
    traits->setInheritedWindowPixelFormat = true;
    //traits->windowName = "osgViewer";

    

    traits->inheritedWindowData = windata;

    

    // Create the Graphics Context
    osg::ref_ptr<osg::GraphicsContext> graphicsContext = osg::GraphicsContext::createGraphicsContext(traits.get());

    

    osg::setNotifyLevel(osg::INFO);

    

    return graphicsContext;

    

    

}

This is the function that initialize everything:
//here I call the function I previously defined to create graphics context 
osg::ref_ptr<osg::GraphicsContext> graphicsContext = [self createGraphicContext];

    

    //create the viewer    
    _viewer = new osgViewer::Viewer();
    //if the context was created correctly then attach to our viewer
    if(graphicsContext)
    {
        //suppress annoying opengl warnings //attention please, you can comment the lines to debug
        graphicsContext->getState()->setCheckForGLErrors(osg::State::NEVER_CHECK_GL_ERRORS);
        _viewer->getCamera()->setGraphicsContext(graphicsContext);
        _viewer->getCamera()->setViewport(new osg::Viewport(0, 0, graphicsContext->getTraits()->width, graphicsContext->getTraits()->height));
    }
    else
    {
        std::cout << "ERROR: I can't create graphic context" <<std::endl;
    }

    

    //create root of the scenegraph tree
    _root = new osg::MatrixTransform();

    //what I do here is simply create new nodes representing what i want to draw and add them as child of _root

    [self initModels];

    

    //YOU PROBABLY DON'T NEED TO DO THIS part about shaders
    std::cout << "initing shaders" <<std::endl;
    //initialize the shaders and attach them to _root
    [self initShaders];

    


    std::cout << "setting scene data to root" <<std::endl;
    //create scene and attach it to _viewer
    _viewer->setSceneData(_root.get());

    


    //Set the threading model for the viewer,
    //Possible options are: SingleThreaded || DrawThreadPerContext
    _viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);

    

    std::cout << "setup camera" <<std::endl;
    //I setup the camera parameters such as clear color, clear mask, render order ...
    [self setupCamera];

    

    _viewer->frame();


Then you call back _viewer->frame in a way I think you know.

The function [self setupCamera] is this:

- (void)setupCamera
{
    //set the clear color of the camera to be semitransparent
    _viewer->getCamera()->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f, 0.75f)); 
    //set the clear mask for the camera
    _viewer->getCamera()->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    

    _viewer->getCamera()->setRenderOrder(osg::CameraNode::PRE_RENDER);    
    _viewer->getCamera()->setComputeNearFarMode(osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR);    

    

}


I personally tried the semi-transparency (with that value of 0.75) and it is working.

I hope this helps.
Just remember to attach a Camera manipulator or manually set ViewMatrix and ProjectionMatrix

Cheers,
John

shekhar vishwa

unread,
Jun 10, 2012, 11:24:23 PM6/10/12
to osg-...@lists.openscenegraph.org


Hi,
 
I have implemented above code  but it does not make the viewport background transparent. I want to see the underlying views being the viewport.
 
Thanks
Vishwa
Reply all
Reply to author
Forward
0 new messages