[osg-users] Problems getting a model to render using OSG in an MFC application

Jesper D. Thomsen jdt at anybodytech.com
Thu Sep 25 01:32:10 PDT 2008

Hi all, first posting here, so be gentle.

I'm trying to integrate OSG in an existing MFC application in place of a temporary custom OpenGL renderer.

I have looked at the osgviewermfc example and based my code on that. So far I have got a viewer up and running with a single master camera. I have tested that the camera is being redrawn by changing the clear-color to a random value every frame. When I assign scenedata to the viewer there is nothing being drawn however.

I am using the following code in my CView::Oncreate:


// Create a trackball manipulator

trackball = new osgGA::TrackballManipulator();

// Create a Manipulator Switcher

keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;

// Add our trackball manipulator to the switcher

keyswitchManipulator->addMatrixManipulator( '1', "Trackball", trackball);

// Init the switcher to the first manipulator (in this case the only manipulator)

keyswitchManipulator->selectMatrixManipulator(0); // Zero based index Value

// Init the main Root Node/Group

mRoot = new osg::Group;

// Load the Model from the model name

mModel = osgDB::readNodeFile("c:/cow.osg");

//// Optimize the model

//osgUtil::Optimizer optimizer;



// Add the model to the scene


// Local Variable to hold window size data

RECT rect;

// Create the viewer for this window

mViewer = new osgViewer::Viewer();

// Add a Stats Handler to the viewer

mViewer->addEventHandler(new osgViewer::StatsHandler);

// Get the current window size

::GetWindowRect(m_hWnd, &rect);

// Init the GraphicsContext Traits

traits = new osg::GraphicsContext::Traits;

// Init the Windata Variable that holds the handle for the Window to display OSG in.

windata = new osgViewer::GraphicsWindowWin32::WindowData(m_hWnd);

// Setup the traits parameters

traits->x = 0;

traits->y = 0;

traits->width = rect.right - rect.left;

traits->height = rect.bottom - rect.top;

traits->windowDecoration = false;

traits->doubleBuffer = true;

traits->sharedContext = 0;

traits->setInheritedWindowPixelFormat = true;

traits->inheritedWindowData = windata;

// Create the Graphics Context

gc = osg::GraphicsContext::createGraphicsContext(traits);

// Init a new Camera (Master for this View)

osg_camera = new osg::Camera;

// Assign Graphics Context to the Camera


// Set the viewport for the Camera

osg_camera->setViewport(new osg::Viewport(traits->x, traits->y, traits->width, traits->height));

// Add the Camera to the Viewer


// Add the Camera Manipulator to the Viewer


// Set the Scene Data


// Realize the Viewer



And in my views OnDraw I use the following:


float r = (float) rand()/RAND_MAX;

float g = (float) rand()/RAND_MAX;

float b = (float) rand()/RAND_MAX;

float a = (float) rand()/RAND_MAX;

osg_camera->setClearColor( osg::Vec4(r,g,b,a) );



As far as I can tell the osgviewermfc uses pretty much the same code.

Any and all help and hint as to what I'm doing wrong would be much appreciated.

Regards, and thanks in advance.

Jesper D. Thomsen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20080925/3376c9c1/attachment-0002.htm>

More information about the osg-users mailing list