[osg-users] OSG not drawing CEGUI drawable
James Carson
james.carson.direct at gmail.com
Sat Jan 20 16:18:47 PST 2018
Version Information:
OSG: 3.4.0
CEGUI: 0.8.7
Problem: I am unable to get the osg::Drawable for CEGUI to draw. The
drawImplementation method never even gets called.
What was my starting point: I've been using the two by Rui Wang and Xuelei
Qian to learn osg. I attempted to run through their example on integrating
CEGUI, but it is for version 0.7.5. There are considerable differences
between 0.7.5 and 0.8.7, but I believe that I was able to update the
example using CEGUI porting tips on their website (
http://static.cegui.org.uk/docs/0.8.7/porting7to8.html).
Here is my code, please let me know why you think the drawable isn't being
called.
client_master.cpp: This is where I'm calling the code to create the
drawable object and add it to the scene graph. This is pretty much ripped
right out of the cookbook. Option 1 is right from the cookbook. Option 2
was from a forum post somewhere. Option 2 will paint a debugging cow while
option 1 will not.
osg::Camera* ClientMaster::create_hud_camera( double left, double right,
double bottom, double top ){
int option = 2;
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
camera->setClearMask( GL_DEPTH_BUFFER_BIT );
camera->setRenderOrder( osg::Camera::POST_RENDER );
camera->setAllowEventFocus( true );
camera->getOrCreateStateSet()->setMode( GL_LIGHTING,
osg::StateAttribute::OFF );
if( option == 1 ){
//This option is from the Cookbook. Not even the cow is shown using
this.
camera->setProjectionMatrix( osg::Matrix::ortho2D(left, right,
bottom, top) );
}
else if( option == 2 ){
//This option was found online. It shows the side of cow but not
the gui.
osg::Vec3d eye = osg::Vec3d(0, -1, 0 ); // position of the
camera
osg::Vec3d center = osg::Vec3d(0, 0, 0 ); // where does the
camera look.
osg::Vec3d up = osg::Vec3d(0, 0, 1 ); // the up vector of
the camera
camera->setViewMatrixAsLookAt(eye, center, up);
}
return camera.release();
}
void ClientMaster::initialize_HUD( void ){
bool showing_cow = true;
osg::ref_ptr<Geode> geode = new osg::Geode;
geode->setCullingActive(false);
geode->addDrawable( new ClientCEGUIDrawable );
geode->getOrCreateStateSet()->setAttributeAndModes( new osg::BlendFunc
);
geode->getOrCreateStateSet()->setRenderingHint(
osg::StateSet::TRANSPARENT_BIN );
osg::ref_ptr<osg::Camera> hud_camera = this->create_hud_camera( 0, 800,
0, 600 );
hud_camera->addChild( geode.get() );
if( showing_cow ){
//The cow is for debugging purposes. The cow displaying confirms
that the
//children of the hud are being painted to the screen.
osg::ref_ptr<osg::Node> hud_model =
osgDB::readNodeFile("./cow.osg");
hud_camera->addChild( hud_model.get() );
}
this->_root_group->addChild( hud_camera.get() );
this->_viewer.addEventHandler( new EventHandlerCEGUI( hud_camera.get()
) );
this->_viewer.addEventHandler( new osgViewer::WindowSizeHandler );
this->_viewer.addEventHandler( new osgViewer::StatsHandler );
}
client_cegui_drawable.cpp: This is taken from the cookbook and the syntax
was updated from CEGUI 7 to 8. I also did a little refactoring ( nothing
drastic though ). I haven't yet updated the window resizing part of the
code, but that isn't yet an issue because it never gets called. I also put
some debugging screen outputs into the code. ClientCEGUIDrawable is
inheritted from osg::Drawable.
#include "../headers/client_cegui_drawable.hpp"
ClientCEGUIDrawable::ClientCEGUIDrawable() : _lastSimulationTime(0.0),
_activeContextID(0), _initialized(false) {
cout << "ClientCEGUIDrawable: Initializing" << endl;
setSupportsDisplayList( false );
setDataVariance( osg::Object::DYNAMIC );
getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
getOrCreateStateSet()->setMode( GL_DEPTH_TEST, osg::StateAttribute::OFF
);
cout << "ClientCEGUIDrawable: Initialized" << endl;
}
ClientCEGUIDrawable::ClientCEGUIDrawable( const ClientCEGUIDrawable&
copy,const osg::CopyOp& copyop ) : osg::Drawable(copy, copyop),
_lastSimulationTime( copy._lastSimulationTime ),
_activeContextID( copy._activeContextID ),
_initialized( copy._initialized )
{}
void ClientCEGUIDrawable::initialize_resource_provider( void ){
cout << "ClientCEGUIDrawable: Resource Provider: Initializing" << endl;
CEGUI::DefaultResourceProvider* resource_provider =
static_cast<CEGUI::DefaultResourceProvider*>(
CEGUI::System::getSingleton().getResourceProvider() );
resource_provider->setResourceGroupDirectory( "schemes",
"./datafiles/gui/schemes/" );
resource_provider->setResourceGroupDirectory( "imagesets",
"./datafiles/gui/imagesets/" );
resource_provider->setResourceGroupDirectory( "fonts",
"./datafiles/gui/fonts/" );
resource_provider->setResourceGroupDirectory( "layouts",
"./datafiles/gui/layouts/" );
resource_provider->setResourceGroupDirectory( "looknfeel",
"./datafiles/gui/looknfeel/" );
resource_provider->setResourceGroupDirectory( "lua_scripts",
"./datafiles/gui/lua_scripts/" );
CEGUI::ImageManager::setImagesetDefaultResourceGroup( "imagesets" );
CEGUI::Font::setDefaultResourceGroup( "fonts" );
CEGUI::Scheme::setDefaultResourceGroup( "schemes" );
CEGUI::WidgetLookManager::setDefaultResourceGroup( "looknfeel" );
CEGUI::WindowManager::setDefaultResourceGroup( "layouts" );
CEGUI::ScriptModule::setDefaultResourceGroup( "lua_scripts" );
cout << "ClientCEGUIDrawable: Resource Provider: Initialized" << endl;
}
void ClientCEGUIDrawable::drawImplementation( osg::RenderInfo& renderInfo ){
cout << "ClientCEGUIDrawable: drawImplementation: Started" << endl;
unsigned int contextID = renderInfo.getContextID();
if ( !_initialized )
{
CEGUI::OpenGLRenderer& myRenderer =
CEGUI::OpenGLRenderer::bootstrapSystem();
if ( !CEGUI::System::getSingletonPtr() ) return;
this->initialize_resource_provider();
const_cast<ClientCEGUIDrawable*>(this)->initializeControls();
_activeContextID = contextID;
_initialized = true;
}
else if ( contextID==_activeContextID )
{
osg::State* state = renderInfo.getState();
state->disableAllVertexArrays();
state->disableTexCoordPointer( 0 );
glPushMatrix();
glPushAttrib( GL_ALL_ATTRIB_BITS );
CEGUI::OpenGLRenderer* renderer =
static_cast<CEGUI::OpenGLRenderer*>(
//CEGUI::OpenGL3Renderer* renderer =
static_cast<CEGUI::OpenGL3Renderer*>(
CEGUI::System::getSingleton().getRenderer() );
osg::Viewport* viewport =
renderInfo.getCurrentCamera()->getViewport();
if ( renderer && viewport )
{
//const CEGUI::Size& size = renderer->getDisplaySize();
//if ( size.d_width!=viewport->width() ||
size.d_height!=viewport->height() )
//{
// CEGUI::System::getSingleton().notifyDisplaySizeChanged(
// CEGUI::Size(viewport->width(), viewport->height()) );
//}
}
double currentTime = (state->getFrameStamp() ?
state->getFrameStamp()->getSimulationTime() : 0.0);
CEGUI::System::getSingleton().injectTimePulse( (currentTime -
_lastSimulationTime)/1000.0 );
CEGUI::System::getSingleton().renderAllGUIContexts();
_lastSimulationTime = currentTime;
glPopAttrib();
glPopMatrix();
}
cout << "ClientCEGUIDrawable: drawImplementation: Completed" << endl;
}
void ClientCEGUIDrawable::initializeControls(){
cout << "ClientCEGUIDrawable: Controls: Initializing" << endl;
CEGUI::SchemeManager::getSingleton().createFromFile(
"TaharezLook.scheme" );
CEGUI::FontManager::getSingleton().createFromFile( "DejaVuSans-10.font"
);
CEGUI::System::getSingleton().getDefaultGUIContext().getMouseCursor().setDefaultImage(
"TaharezLook/MouseArrow" );
// For Tooltips when I'm ready
//
CEGUI::System::getSingleton().getDefaultGUIContext().setDefaultTooltipType(
"TaharezLook/Tooltip" );
CEGUI::System::getSingleton().getDefaultGUIContext().setDefaultFont(
"DejaVuSans-10" );
// CEGUI::System::getSingleton().getDefaultFont()->setAutoScaled( false
);
CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
CEGUI::Window* root_window = wmgr.createWindow( "DefaultWindow", "root"
);
CEGUI::System::getSingleton().getDefaultGUIContext().setRootWindow(
root_window );
CEGUI::FrameWindow* frame_window = static_cast<CEGUI::FrameWindow*>(
wmgr.createWindow( "TaharezLook/FrameWindow", "testWindow" ));
// position a quarter of the way in from the top-left of parent.
frame_window->setPosition( CEGUI::UVector2( CEGUI::UDim( 0.25f, 0.0f ),
CEGUI::UDim( 0.25f, 0.0f ) ) );
// set size to be half the size of the parent
frame_window->setSize( CEGUI::USize( CEGUI::UDim( 0.5f, 0.0f ),
CEGUI::UDim( 0.5f, 0.0f ) ) );
//demoWindow->setMinSize( CEGUI::UVector2(cegui_reldim(0.1f),
cegui_reldim(0.1f)) );
frame_window->setText( "Example Dialog" );
CEGUI::PushButton* demoButtonOK = static_cast<CEGUI::PushButton*>(
CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/Button",
"DemoButtonOK") );
demoButtonOK->setPosition( CEGUI::UVector2(cegui_reldim(0.3f),
cegui_reldim(0.75f)) );
demoButtonOK->setSize( CEGUI::USize( CEGUI::UDim( 0.1f, 0.1f ),
CEGUI::UDim( 0.2f, 0.2f ) ) );
demoButtonOK->setText( "OK" );
frame_window->subscribeEvent( CEGUI::FrameWindow::EventCloseClicked,
CEGUI::Event::Subscriber(&ClientCEGUIDrawable::handleClose, this) );
root_window->addChild( demoButtonOK );
root_window->addChild( frame_window );
cout << "ClientCEGUIDrawable: Controls: Initialized" << endl;
}
bool ClientCEGUIDrawable::handleClose( const CEGUI::EventArgs& e ){
cout << "ClientCEGUIDrawable: handleClose: starting" << endl;
//CEGUI::WindowManager::getSingleton().getWindow("DemoWindow")->setVisible(
false );
CEGUI::System::destroy();
//CEGUI::OpenGL3Renderer::destroy(
CEGUI::OpenGLRenderer::destroy(
static_cast<CEGUI::OpenGLRenderer&>(
//Commented out from CEGUI's online tutorial. I just figured it
was talking
//about the renderer.
//*d_renderer
*(CEGUI::System::getSingleton().getRenderer())
)
);
cout << "ClientCEGUIDrawable: handleClose: Completed" << endl;
return true;
}
event_handler_cegui.cpp: This was also taken from the cookbook and updated.
I have one piece left, but the CEGUI code is deprecated as far as I can
tell. If this is where my problem is, then I would be grateful for thoughts
or insights on how to update it since I'm not really sure what it is even
doing.
#include "../headers/event_handler_cegui.hpp"
bool EventHandlerCEGUI::handle( const osgGA::GUIEventAdapter& ea,
osgGA::GUIActionAdapter& aa ) {
cout << "EventHandlerCEGUI: Handle: Handling" << endl;
int x = ea.getX(), y = ea.getY(), width = ea.getWindowWidth(), height =
ea.getWindowHeight();
if (
ea.getMouseYOrientation()==osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS )
cout << " EventHandlerCEGUI: Handle: Y Increasing Upwards?" <<
endl;
y = ea.getWindowHeight() - y;
if ( !CEGUI::System::getSingletonPtr() )
cout << " EventHandlerCEGUI: Handle: No CEGUI Singleton" << endl;
return false;
CEGUI::GUIContext& context =
CEGUI::System::getSingleton().getDefaultGUIContext();
switch ( ea.getEventType() )
{
case osgGA::GUIEventAdapter::PUSH:
cout << " EventHandlerCEGUI: Handle: PUSH" << endl;
context.injectMousePosition( x, y );
context.injectMouseButtonDown( convertMouseButton(ea.getButton()) );
break;
case osgGA::GUIEventAdapter::RELEASE:
cout << " EventHandlerCEGUI: Handle: RELEASE" << endl;
context.injectMousePosition( x, y );
context.injectMouseButtonUp( convertMouseButton(ea.getButton()) );
break;
case osgGA::GUIEventAdapter::SCROLL:
cout << " EventHandlerCEGUI: Handle: SCROLL" << endl;
if ( ea.getScrollingMotion()==osgGA::GUIEventAdapter::SCROLL_DOWN )
context.injectMouseWheelChange( -1 );
else if (
ea.getScrollingMotion()==osgGA::GUIEventAdapter::SCROLL_UP )
context.injectMouseWheelChange( +1 );
break;
case osgGA::GUIEventAdapter::DRAG:
cout << " EventHandlerCEGUI: Handle: DRAG" << endl;
case osgGA::GUIEventAdapter::MOVE:
cout << " EventHandlerCEGUI: Handle: MOVE" << endl;
context.injectMousePosition( x, y );
break;
case osgGA::GUIEventAdapter::RESIZE:
cout << " EventHandlerCEGUI: Handle: RESIZE" << endl;
if ( _camera.valid() )
{
_camera->setProjectionMatrix( osg::Matrixd::ortho2D(0.0, width,
0.0, height) );
_camera->setViewport( 0.0, 0.0, width, height );
}
break;
default:
return false;
}
/*
* No idea what this would actually do for me.
* The code for getting the window is deprecated though.
* So, it would take some work.
*
CEGUI::Window* rootWindow = CEGUI::System::getSingleton().getGUISheet();
if ( rootWindow )
{
CEGUI::Window* anyWindow = rootWindow->getChildAtPosition(
CEGUI::Vector2(x, y) );
if ( anyWindow ) return true;
}*/
cout << "EventHandlerCEGUI: Handle: Not Handled" << endl;
return false;
}
CEGUI::MouseButton EventHandlerCEGUI::convertMouseButton( int button ){
cout << "EventHandlerCEGUI: Convert Mouse: Converting" << endl;
switch ( button )
{
case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
return CEGUI::LeftButton;
case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
return CEGUI::MiddleButton;
case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
return CEGUI::RightButton;
default: break;
}
cout << "EventHandlerCEGUI: Convert Mouse: Converted" << endl;
return static_cast<CEGUI::MouseButton>(button);
}
Thank you in advance for your assistance,
Jay
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20180120/3cb418ac/attachment.html>
More information about the osg-users
mailing list