2003-12-16 00:46:06 +08:00
|
|
|
// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL.
|
|
|
|
//
|
|
|
|
// Simple example of use of Producer::RenderSurface to create an OpenGL
|
|
|
|
// graphics window, and OSG for rendering.
|
|
|
|
|
|
|
|
#include <Producer/RenderSurface>
|
2006-09-26 00:25:53 +08:00
|
|
|
|
2003-12-16 00:46:06 +08:00
|
|
|
#include <osgUtil/SceneView>
|
|
|
|
#include <osgDB/ReadFile>
|
2006-11-02 20:27:15 +08:00
|
|
|
#include <osgViewer/SimpleViewer>
|
2003-12-16 00:46:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
if (argc<2)
|
|
|
|
{
|
|
|
|
std::cout << argv[0] <<": requires filename argument." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load the scene.
|
|
|
|
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
|
|
|
|
if (!loadedModel)
|
|
|
|
{
|
|
|
|
std::cout << argv[0] <<": No data loaded." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the window to draw to.
|
|
|
|
osg::ref_ptr<Producer::RenderSurface> renderSurface = new Producer::RenderSurface;
|
|
|
|
renderSurface->setWindowName("osgsimple");
|
|
|
|
renderSurface->setWindowRectangle(100,100,800,600);
|
|
|
|
renderSurface->useBorder(true);
|
|
|
|
renderSurface->realize();
|
|
|
|
|
2006-09-26 00:25:53 +08:00
|
|
|
|
2006-11-02 20:27:15 +08:00
|
|
|
osgViewer::SimpleViewer viewer;
|
2006-09-26 00:25:53 +08:00
|
|
|
viewer.setSceneData(loadedModel.get());
|
2003-12-16 00:46:06 +08:00
|
|
|
|
|
|
|
// initialize the view to look at the center of the scene graph
|
|
|
|
const osg::BoundingSphere& bs = loadedModel->getBound();
|
|
|
|
osg::Matrix viewMatrix;
|
|
|
|
viewMatrix.makeLookAt(bs.center()-osg::Vec3(0.0,2.0f*bs.radius(),0.0),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
|
|
|
|
|
|
|
|
// main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
|
|
|
|
while( renderSurface->isRealized() )
|
2006-09-26 00:25:53 +08:00
|
|
|
{
|
|
|
|
// update the window dimensions, in case the window has been resized.
|
|
|
|
viewer.getEventQueue()->windowResize(0,0,renderSurface->getWindowWidth(),renderSurface->getWindowHeight());
|
2003-12-16 00:46:06 +08:00
|
|
|
|
2006-09-26 00:25:53 +08:00
|
|
|
// set the view
|
|
|
|
viewer.getCamera()->setViewMatrix(viewMatrix);
|
2003-12-16 00:46:06 +08:00
|
|
|
|
2006-09-26 00:25:53 +08:00
|
|
|
// advance the frame, handle events, update the scene and render it.
|
|
|
|
viewer.frame();
|
2003-12-16 00:46:06 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
// Swap Buffers
|
|
|
|
renderSurface->swapBuffers();
|
2003-12-16 00:46:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|