embedding Ogre 1.7 into wxWidgets 2.9 cross platform

well, its harder than you might think, but i finally found this to be working quite well:

wxutils.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * File:   wxutils.h
 * Author: thomas
 *
 * Created on May 12, 2010, 9:23 AM
 */


#ifndef _WXUTILS_H
#define _WXUTILS_H

#include <wx/wx.h>
#include <string.h>

std::string getWindowHandle(wxWindow *window);

#endif  /* _WXUTILS_H */

wxutils.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "wxutils.h"

#include <iostream>
#include <sstream>

// this is in a separate file to reduce the clutter in the global namespace and their conflicts

#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
# ifdef __WXGTK__
#include <gdk/gdk.h>
#include <gtk/gtk.h> // just this should suffice as it should include gdk.h itself
/* Seems to be needed under Linux */
//#include <wx/gtk/win_gtk.h>
#include "wx/gtk/private/win_gtk.h"
#include <gdk/gdkx.h>
#include <GL/glx.h>

#if  wxCHECK_VERSION(2, 9, 0)
#define piz(wxwin) WX_PIZZA((wxwin)->m_wxwindow)
#define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
        GDK_WINDOW_XWINDOW(((GtkWidget*)piz(wxwin))->window) : \
        GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)

#else
#define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
        GDK_WINDOW_XWINDOW(GTK_PIZZA((wxwin)->m_wxwindow)->bin_window) : \
        GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)

#endif

#ifdef __WXX11__
#include "wx/x11/privx.h"
#define GetXWindow(wxwin)   ((Window)(wxwin)->GetHandle())
#endif

# endif //__WXGTK__
#endif //OGRE_PLATFORM_LINUX

std::string getWindowHandle(wxWindow *window)
{
#if WIN32
    std::stringstream sstr;
    size_t hWnd = (size_t)window->GetHandle();
    sstr << hWnd;
    std::string result = sstr.str();
    return result;
#else

    // TODO: Someone test this. you might to use "parentWindowHandle" if this
    // does not work.  Ogre 1.2 + Linux + GLX platform wants a string of the
    // format display:screen:window, which has variable types ulong:uint:ulong.
    GtkWidget* widget = window->GetHandle();
    //gtk_widget_set_double_buffered (widget, FALSE);
    gtk_widget_realize( widget );   // Mandatory. Otherwise, a segfault happens.
    //XSync(GDK_WINDOW_XDISPLAY(widget->window), False);
   
    Display* display = GDK_WINDOW_XDISPLAY( widget->window );

   
    //Window wid = GetXWindow(window);
    Window wid = GDK_WINDOW_XWINDOW( widget->window );

    std::stringstream sstr;
    /* Get the right display (DisplayString() returns ":display.screen") */
    std::string displayStr = DisplayString( display );
    displayStr = displayStr.substr( 1, ( displayStr.find( ".", 0 ) - 1 ) );

    int screenNum = DefaultScreen( display );

    /* Put all together */
    printf("using display: %s\n", displayStr.c_str());
    printf("using screen: %d\n", screenNum);
    printf("using window: 0x%x\n", wid);

    // old format:
    sstr << displayStr << ':' << screenNum << ':' << wid;
    std::string result = sstr.str();

    printf("getWindowHandle() = %s\n", result.c_str());
    return result;
#endif
}

how to use (panel_viewport is the widget that should show the 3d content):

1
2
3
4
5
6
...
mWindow = mRoot->initialise(false);
Ogre::NameValuePairList params;
params["externalWindowHandle"] = getWindowHandle(panel_viewport);
mWindow = Ogre::Root::getSingleton().createRenderWindow("OgreRenderWindow", 640, 480, false, &params);
...

works great so far under windows 7 and ubuntu 9.10 with Ogre 1.7 and wxWidgets 2.9

4 thoughts on “embedding Ogre 1.7 into wxWidgets 2.9 cross platform

  1. Hi there,

    Thanks for posting this, it seems to be the most recent info with the most recent versions of ogre and wx. Can you provide a little more info about how to use the third code snippet? Where does mRoot come from? How can I use this in a simple little hello world wx app?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by sweetCaptcha