OgreWidget.h 3.43 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "ogrewidget.h"

#define THIS OgreWidget

/**
 * @brief init the object
 * @author kito berg-taylor
 */
void THIS::init( std::string plugins_file,
                 std::string ogre_cfg_file,
                 std::string ogre_log )
{

14 15
    // create the main ogre object
    mOgreRoot = new Ogre::Root( plugins_file, ogre_cfg_file, ogre_log );
pixhawk's avatar
pixhawk committed
16

17 18 19
    // setup a renderer
    Ogre::RenderSystemList *renderers = mOgreRoot->getAvailableRenderers();
    assert( !renderers->empty() ); // we need at least one renderer to do anything useful
pixhawk's avatar
pixhawk committed
20

21 22
    Ogre::RenderSystem *renderSystem;
    renderSystem = chooseRenderer( renderers );
pixhawk's avatar
pixhawk committed
23

24
    assert( renderSystem ); // user might pass back a null renderer, which would be bad!
pixhawk's avatar
pixhawk committed
25

26 27 28 29
    mOgreRoot->setRenderSystem( renderSystem );
    QString dimensions = QString( "%1x%2" )
                         .arg(this->width())
                         .arg(this->height());
pixhawk's avatar
pixhawk committed
30

31
    renderSystem->setConfigOption( "Video Mode", dimensions.toStdString() );
pixhawk's avatar
pixhawk committed
32

33 34 35 36
    // initialize without creating window
    mOgreRoot->getRenderSystem()->setConfigOption( "Full Screen", "No" );
    mOgreRoot->saveConfig();
    mOgreRoot->initialise(false); // don't create a window
pixhawk's avatar
pixhawk committed
37 38 39 40 41 42 43 44
}

/**
 * @brief setup the rendering context
 * @author Kito Berg-Taylor
 */
void THIS::initializeGL()
{
45
    //== Creating and Acquiring Ogre Window ==//
pixhawk's avatar
pixhawk committed
46

47 48 49 50 51 52 53 54
    // Get the parameters of the window QT created
    QX11Info info = x11Info();
    Ogre::String winHandle;
    winHandle  = Ogre::StringConverter::toString((unsigned long)(info.display()));
    winHandle += ":";
    winHandle += Ogre::StringConverter::toString((unsigned int)(info.screen()));
    winHandle += ":";
    winHandle += Ogre::StringConverter::toString((unsigned long)(this->parentWidget()->winId()));
pixhawk's avatar
pixhawk committed
55

56 57
    Ogre::NameValuePairList params;
    params["parentWindowHandle"] = winHandle;
pixhawk's avatar
pixhawk committed
58

59 60 61 62 63
    mOgreWindow = mOgreRoot->createRenderWindow( "QOgreWidget_RenderWindow",
                  this->width(),
                  this->height(),
                  false,
                  &params );
pixhawk's avatar
pixhawk committed
64

65 66 67
    mOgreWindow->setActive(true);
    WId ogreWinId = 0x0;
    mOgreWindow->getCustomAttribute( "WINDOW", &ogreWinId );
pixhawk's avatar
pixhawk committed
68

69
    assert( ogreWinId );
pixhawk's avatar
pixhawk committed
70

71 72 73
    this->create( ogreWinId );
    setAttribute( Qt::WA_PaintOnScreen, true );
    setAttribute( Qt::WA_NoBackground );
pixhawk's avatar
pixhawk committed
74

75 76
    //== Ogre Initialization ==//
    Ogre::SceneType scene_manager_type = Ogre::ST_EXTERIOR_CLOSE;
pixhawk's avatar
pixhawk committed
77

78 79
    mSceneMgr = mOgreRoot->createSceneManager( scene_manager_type );
    mSceneMgr->setAmbientLight( Ogre::ColourValue(1,1,1) );
pixhawk's avatar
pixhawk committed
80

81 82 83 84
    mCamera = mSceneMgr->createCamera( "QOgreWidget_Cam" );
    mCamera->setPosition( Ogre::Vector3(0,1,0) );
    mCamera->lookAt( Ogre::Vector3(0,0,0) );
    mCamera->setNearClipDistance( 1.0 );
pixhawk's avatar
pixhawk committed
85

86 87
    Ogre::Viewport *mViewport = mOgreWindow->addViewport( mCamera );
    mViewport->setBackgroundColour( Ogre::ColourValue( 0.8,0.8,1 ) );
pixhawk's avatar
pixhawk committed
88 89 90 91 92 93 94 95
}

/**
 * @brief render a frame
 * @author Kito Berg-Taylor
 */
void THIS::paintGL()
{
96 97
    assert( mOgreWindow );
    mOgreRoot->renderOneFrame();
pixhawk's avatar
pixhawk committed
98 99 100 101 102 103 104 105
}

/**
 * @brief resize the GL window
 * @author Kito Berg-Taylor
 */
void THIS::resizeGL( int width, int height )
{
106 107
    assert( mOgreWindow );
    mOgreWindow->windowMovedOrResized();
pixhawk's avatar
pixhawk committed
108 109 110 111 112 113 114 115
}

/**
 * @brief choose the right renderer
 * @author Kito Berg-Taylor
 */
Ogre::RenderSystem* THIS::chooseRenderer( Ogre::RenderSystemList *renderers )
{
116 117 118
    // It would probably be wise to do something more friendly
    // that just use the first available renderer
    return *renderers->begin();
pixhawk's avatar
pixhawk committed
119
}