Irrlicht & wxWidgets

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
george++
Posts: 16
Joined: Mon Sep 28, 2009 5:30 pm
Location: Hellas

Irrlicht & wxWidgets

Post by george++ »

Hi,
Although I know about the Irrlicht engine many years now, recently I decided to play with it. Because I think that I have good nowledge of the wxWidgets library I decided to built a project as a starting point for my needs.
I am sure that there are very good and experienced coders here. I believe that the following sources will help some of you to start with these wonderfull libraries:

The header file

Code: Select all

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

typedef irr::SIrrlichtCreationParameters	wxIrrCreateParam;
typedef irr::video::E_DRIVER_TYPE			wxIrrDriverType;
typedef irr::IrrlichtDevice					wxIrrDevice;
typedef irr::scene::ISceneManager			wxIrrSceneManager;
typedef irr::video::IVideoDriver			wxIrrVideoDriver;

typedef irr::scene::ICameraSceneNode		wxIrrCamera;
typedef irr::scene::ISceneNode				wxIrrNode;

typedef irr::video::SColor					wxIrrColor;
typedef irr::core::dimension2d<irr::s32>	wxIrrDimension2D;
typedef irr::core::vector3df				wxIrrVector3DFloat;
typedef irr::s32							wxIrrS32;
typedef irr::video::E_MATERIAL_FLAG			wxIrrMaterialFlag;

class CIrrApp;
class CIrrDevice;
class CIrrWindow;
class CIrrFrame;

int MyCube=1;

enum
{
	ID_Quit = 1,
	ID_About,
};

class CIrrApp : public wxApp
{
	public:
		virtual bool OnInit(void);
};

class CIrrDevice
{
	public:
		CIrrDevice(CIrrWindow *window, wxIrrDriverType type=irr::video::EDT_OPENGL, bool bResizeable=true);
		~CIrrDevice();

		inline wxIrrDevice *GetHandle(void) { return irrDevice; }
		inline wxIrrSceneManager *GetSceneManager(void) { return irrDevice ? irrDevice->getSceneManager() : NULL; }
		inline wxIrrVideoDriver *GetVideoDriver(void) { return irrDevice ? irrDevice->getVideoDriver() : NULL; }

		wxIrrCamera *AddCamera(wxIrrNode *parent=0, wxIrrVector3DFloat &position=wxIrrVector3DFloat(0.0f,0.0f,0.0f), wxIrrVector3DFloat &lookat=wxIrrVector3DFloat(0.0f,0.0f,1.0f), wxIrrS32 id=1);

	protected:
		wxIrrDevice *irrDevice;
};

class CIrrWindow : public wxWindow
{
	public:
		CIrrWindow(wxWindow *parent, wxWindowID id, long style=wxTAB_TRAVERSAL);
		~CIrrWindow();

		void OnPaint(wxPaintEvent &event);
		void OnSize(wxSizeEvent &event);
		void OnTimer(wxTimerEvent &event);

		void OnSceneUpdate(void);

		inline wxIrrSceneManager *GetSceneManager(void) { return irrDevice ? irrDevice->GetSceneManager() : NULL; }
		inline wxIrrVideoDriver *GetVideoDriver(void) { return irrDevice ? irrDevice->GetVideoDriver() : NULL; }

		inline void SetDevice(CIrrDevice *irrdevice) { irrDevice = irrdevice; }
		inline void SetCamera(wxIrrCamera *irrcam) { irrCameraCurrent = irrcam; }

		wxIrrCamera *AddCamera(wxIrrNode *parent=0, wxIrrVector3DFloat &position=wxIrrVector3DFloat(0.0f,0.0f,0.0f), wxIrrVector3DFloat &lookat=wxIrrVector3DFloat(0.0f,0.0f,1.0f), wxIrrS32 id=1, bool bsetcurrent=true);

	DECLARE_EVENT_TABLE()

	protected:
		CIrrDevice *irrDevice;
		wxIrrCamera *irrCameraCurrent;

	private:
		float fRatio;
		wxTimer m_Timer;
};

class CIrrFrame : public wxFrame
{
	public:
		CIrrFrame(const wxString &title, const wxPoint &pos, const wxSize &size, long style=wxDEFAULT_FRAME_STYLE);

		void OnQuit(wxCommandEvent &event);
		void OnAbout(wxCommandEvent &event);

		DECLARE_EVENT_TABLE()

	protected:
		CIrrDevice *device3D;
		wxIrrCamera *irrCam;
		CIrrWindow *window3D;
};
..and the cpp file:

Code: Select all

#include "wxIrrHello.h"

IMPLEMENT_APP(CIrrApp)

bool CIrrApp::OnInit()
{
	CIrrFrame *frame = new CIrrFrame( _T("wxIrrlicht: Hello World"), wxPoint(50,50), wxSize(450,340) );
	frame->Show(TRUE);
	SetTopWindow(frame);
	return TRUE;
} 

///////////////////////////////////////////////////////////////////////////////

CIrrDevice::CIrrDevice(CIrrWindow *window, wxIrrDriverType type, bool bResizeable)
{
	wxIrrCreateParam param;

	param.WindowId = reinterpret_cast<void *>( (HWND)window->GetHandle() );
	param.DriverType = type;

	irrDevice = irr::createDeviceEx(param);
	window->SetDevice( this );
	if ( irrDevice )
		irrDevice->setResizeAble( bResizeable );
}

CIrrDevice::~CIrrDevice()
{
	if ( irrDevice )
	{
		irrDevice->closeDevice();
		irrDevice->drop();
	}
}

wxIrrCamera *CIrrDevice::AddCamera(wxIrrNode *parent, wxIrrVector3DFloat &position, wxIrrVector3DFloat &lookat, wxIrrS32 id)
{
	wxIrrSceneManager *irrSceneMgr=GetSceneManager();

	if (irrSceneMgr)
		return (wxIrrCamera *)irrSceneMgr->addCameraSceneNode( parent, position, lookat );
	else
		return NULL;
}

///////////////////////////////////////////////////////////////////////////////

BEGIN_EVENT_TABLE(CIrrWindow, wxWindow)
	EVT_PAINT(CIrrWindow::OnPaint)
	EVT_SIZE(CIrrWindow::OnSize)
	EVT_TIMER( wxID_ANY, CIrrWindow::OnTimer)
END_EVENT_TABLE()

CIrrWindow::CIrrWindow(wxWindow *parent, wxWindowID id, long style) : wxWindow( parent, id, wxDefaultPosition, wxDefaultSize, style ), m_Timer(this)
{
	irrDevice = NULL;
	irrCameraCurrent = NULL;
	m_Timer.Start(1000/60);
}

CIrrWindow::~CIrrWindow()
{
}

void CIrrWindow::OnPaint(wxPaintEvent &event)
{
	wxPaintDC dc;

	if ( irrDevice )
	{
		wxIrrVideoDriver *driver=irrDevice->GetVideoDriver();
		wxIrrSceneManager *scenemgr=irrDevice->GetSceneManager();

		if ( driver )
		{
			driver->beginScene(true, true, wxIrrColor(0,0,0,0) );
			if ( scenemgr )
				scenemgr->drawAll();
			driver->endScene();
		}
	}
	event.Skip();
}

void CIrrWindow::OnSize(wxSizeEvent &event)
{
	float w=GetClientSize().GetWidth();
	float h=GetClientSize().GetHeight();

	fRatio = (float)w/(float)h;
	if ( irrDevice )
	{
		wxIrrVideoDriver *driver=irrDevice->GetVideoDriver();

		if ( driver )
		{	
			driver->OnResize( wxIrrDimension2D(w, h) );
			if ( irrCameraCurrent )
				irrCameraCurrent->setAspectRatio( fRatio );
			if ( !m_Timer.IsRunning() )
				Update();
		}
	}
	event.Skip();
}

void CIrrWindow::OnTimer(wxTimerEvent &event)
{
	OnSceneUpdate();
	Refresh( false );
	event.Skip();
}

//
// Do the update of your scene here
//
void CIrrWindow::OnSceneUpdate(void)
{
	wxIrrSceneManager *scene=GetSceneManager();

	if ( scene )
	{
		wxIrrNode *cube=scene->getSceneNodeFromId( MyCube );

		if (cube)
		{
			wxIrrVector3DFloat yaw=cube->getRotation();

			cube->setRotation( wxIrrVector3DFloat(0.0f, yaw.Y+1.0f, 0.0f) );
		}
	}
}

wxIrrCamera *CIrrWindow::AddCamera(wxIrrNode *parent, wxIrrVector3DFloat &position, wxIrrVector3DFloat &lookat, wxIrrS32 id, bool bsetcurrent)
{
	wxIrrCamera *newcamera=NULL;

	if (irrDevice)
	{
		newcamera = irrDevice->AddCamera(parent, position, lookat, id);
		if ( bsetcurrent )
			irrCameraCurrent = newcamera;
	}
	return newcamera;
}

///////////////////////////////////////////////////////////////////////////////

BEGIN_EVENT_TABLE(CIrrFrame, wxFrame)
	EVT_MENU(ID_Quit, CIrrFrame::OnQuit)
	EVT_MENU(ID_About, CIrrFrame::OnAbout)
END_EVENT_TABLE()

CIrrFrame::CIrrFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style) : wxFrame((wxFrame *)NULL, -1, title, pos, size, style)
{
	this->SetSizeHints( wxDefaultSize, wxDefaultSize );

	wxBoxSizer* bSizer1;
	bSizer1 = new wxBoxSizer( wxVERTICAL );
	
	window3D = new CIrrWindow( this, wxID_ANY );
	bSizer1->Add( window3D, 1, wxEXPAND, 5 );
	
	this->SetSizer( bSizer1 );
	this->Layout();

	wxMenu *menuFile = new wxMenu;

	menuFile->Append( ID_About, _T("&About...") );
	menuFile->AppendSeparator();
	menuFile->Append( ID_Quit, _T("E&xit") );

	wxMenuBar *menuBar = new wxMenuBar;
	menuBar->Append( menuFile, _T("&File") );

	SetMenuBar( menuBar );

	CreateStatusBar();
	SetStatusText( _T("Welcome to wxIrrlich!") );

	device3D = new CIrrDevice( window3D );
	window3D->AddCamera();

	wxIrrNode *cube=window3D->GetSceneManager()->addCubeSceneNode( 1.0f, 0, MyCube );

	cube->setMaterialTexture(0, window3D->GetVideoDriver()->getTexture("../media/crate.jpg"));
	cube->setMaterialFlag( irr::video::EMF_LIGHTING, false );
	cube->setPosition( wxIrrVector3DFloat(0.0f, 0.0f, 3.0f) );
}

void CIrrFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
	Close(TRUE);
}

void CIrrFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
	wxMessageBox( _T("This is a wxIrrlicht Hello world sample"),	_T("About Hello World"), wxOK|wxICON_INFORMATION, this );
}
If you need some clarifications, I am not sure that I will have the time to tell you, but the above code requires that you have good (but not advanced) knowledge of both libraries and C++ in general
May the software be with you
netpipe
Posts: 670
Joined: Fri Jun 06, 2008 12:50 pm
Location: Edmonton, Alberta, Canada
Contact:

Post by netpipe »

http://tecan.ath.cx/wxIrrHello.7z

here is a CB project and some mods to get it compiling under linux.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Thanks for this, sounds really useful. I want to collect all such integrations into a larger "Example 14", which for now only features MFC integration. May I add this code to the engine examples?
For now I move this topic to Code snippets, too.
george++
Posts: 16
Joined: Mon Sep 28, 2009 5:30 pm
Location: Hellas

Post by george++ »

May I add this code to the engine examples?
Sure, feel free to add this code to the engine examples :)
I could demonstrate a bit more object oriented code, for displaying multiple viewports (a top and a side view for example) if someone could show me (if this is possible) how to use a common (irrlicht) scene with two different (irrlicht) devices.
May the software be with you
Jake-GR
Posts: 41
Joined: Wed Jan 07, 2009 12:32 am
Location: Colorado
Contact:

Post by Jake-GR »

@george++:
endScene() can take in an argument to render to a specified HWND, problem is it only works with software and DirectX. this is how IrrEdit renders the multiple viewports.

not sure if any advancements have been made to irrlicht since then that will allow it to be cross-rendering compatible or not. because i too would love to be able to render to different windows, but still be able to run opengl and cross-platform.
ecsos
Posts: 30
Joined: Mon Feb 04, 2008 8:02 am

Post by ecsos »

pretty sure there is a patch for mrt's that nadro made: http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=25496
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

MRTs has nothing to do with multi-device rendering. The OpenGL changes have not been made so far, sorry.
george++
Posts: 16
Joined: Mon Sep 28, 2009 5:30 pm
Location: Hellas

Post by george++ »

Hi guys!
Well... Irrlicht and wxWidgets rocks!
Take a look to the following sample and let me know what you think

http://www.moraldigames.com/Temp/wxIrrl ... wports.zip

If there is interesting about viewports in a wxWindow then I will gladly post a download link for the project (MS Visual C++ 2008 Express Edition)

Oops! I forgot a screen shot:
Image
May the software be with you
Jake-GR
Posts: 41
Joined: Wed Jan 07, 2009 12:32 am
Location: Colorado
Contact:

Post by Jake-GR »

yes im interested in viewports esspecially if its cross-platform compatible ;)

any idea how to get rid of the menu flickering when it drops down?
george++
Posts: 16
Joined: Mon Sep 28, 2009 5:30 pm
Location: Hellas

Post by george++ »

Here is the link:
http://www.moraldigames.com/Temp/IrrlichtTutorials.zip
any idea how to get rid of the menu flickering when it drops down?
Nop.

Please take into account that this link will be no available for a long period. If someone wants to host these files then I will be grateful if there is a reference to me and my site.
Thanks.
May the software be with you
ceyron
Posts: 63
Joined: Tue Mar 03, 2009 5:10 pm
Location: Bucuresti, România

Post by ceyron »

Wow just what i was looking for, thanks alot George, truly awesome code. If my project will ever be completed i'll be sure to mention your name and your site. 8)
Image
Jake-GR
Posts: 41
Joined: Wed Jan 07, 2009 12:32 am
Location: Colorado
Contact:

Post by Jake-GR »

just looked at the code briefly, but it looks like you are making the entire wxWidget window the HWND of createDeviceEx... then just using the frame area as camera viewports (so essentially the entire window is still just one device with the frame borders drawn over the top?

like i said, just briefly skimmed the code... but hopefully this is the case because it should be cross-platform, and actually the idea i had, but not the time to check it out myself (still learning the wx api)
george++
Posts: 16
Joined: Mon Sep 28, 2009 5:30 pm
Location: Hellas

Post by george++ »

@Jake-GR:
The most importand class is the wxIWindow class. It has been derived from a wxWindow and irr::IrrlichtDevice.
It contains a m_viewportmanager object. This is an instance of a wxCIViewportManager class.
Because I don't know much about the Irrlicht SDK all I can say is:
If the Irrlicht SDK is a cross platform library then the above viewport system is cross platform also.
May the software be with you
ceyron
Posts: 63
Joined: Tue Mar 03, 2009 5:10 pm
Location: Bucuresti, România

Post by ceyron »

did anyone managed to get this compiled under linux, i'm having some problems; i've included the wx library and compiled a simple hello program just fine..

Code: Select all

/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/adrian/Proiecte/NetBeans/Test or Debug Projects/wxWidgetsTest'
/usr/bin/make  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/wxwidgetstest
make[2]: Entering directory `/home/adrian/Proiecte/NetBeans/Test or Debug Projects/wxWidgetsTest'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/wxIWindow.o.d
g++ `wx-config --cxxflags`    -c -g -I/usr/include/wx-2.8 -I../../Irrlicht\ 1.6/include -MMD -MP -MF build/Debug/GNU-Linux-x86/wxIWindow.o.d -o build/Debug/GNU-Linux-x86/wxIWindow.o wxIWindow.cpp
In file included from wxIWindow.cpp:1:
wxIWindow.h:14: error: expected `)' before ‘hwnd’
wxIWindow.h:17: error: expected ‘;’ before ‘(’ token
wxIWindow.h:23: error: default argument for parameter of type ‘wxIVector3DFloat&’ has type ‘wxIVector3DFloat’
wxIWindow.h:23: error: default argument for parameter of type ‘wxIVector3DFloat&’ has type ‘wxIVector3DFloat’
wxIWindow.h:46: error: default argument for parameter of type ‘wxIVector3DFloat&’ has type ‘wxIVector3DFloat’
wxIWindow.h:46: error: default argument for parameter of type ‘wxIVector3DFloat&’ has type ‘wxIVector3DFloat’
wxIWindow.h: In member function ‘void wxCIWindow::StopUpdate()’:
wxIWindow.h:45: error: second operand to the conditional operator is of type ‘void’, but the third operand is neither a throw-expression nor of type ‘void’
wxIWindow.cpp: At global scope:
wxIWindow.cpp:8: error: expected `)' before ‘hwnd’
wxIWindow.cpp:29: error: ‘wxIDevice* wxCIDevice::Create’ is not a static member of ‘class wxCIDevice’
wxIWindow.cpp:29: error: ‘HWND’ was not declared in this scope
wxIWindow.cpp:29: error: expected primary-expression before ‘type’
wxIWindow.cpp:29: error: expected primary-expression before ‘bool’
wxIWindow.cpp:29: error: initializer expression list treated as compound expression
wxIWindow.cpp:30: error: expected ‘,’ or ‘;’ before ‘{’ token
wxIWindow.cpp: In constructor ‘wxCIWindow::wxCIWindow(wxWindow*, wxWindowID, long int, wxIDriverType, bool)’:
wxIWindow.cpp:71: error: ‘Create’ is not a member of ‘wxCIDevice’
wxIWindow.cpp:71: error: ‘HWND’ was not declared in this scope
make[2]: *** [build/Debug/GNU-Linux-x86/wxIWindow.o] Error 1
make[2]: Leaving directory `/home/adrian/Proiecte/NetBeans/Test or Debug Projects/wxWidgetsTest'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/adrian/Proiecte/NetBeans/Test or Debug Projects/wxWidgetsTest'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
First error occurs here in wxIWindow.h, seems to complain about HWND type (doesn't linux have a hwnd type defined ?)

Code: Select all

wxCIDevice(HWND hwnd, wxIDriverType type=irr::video::EDT_OPENGL, bool bResizeable=true);
Image
george++
Posts: 16
Joined: Mon Sep 28, 2009 5:30 pm
Location: Hellas

Post by george++ »

@ceyron
Sorry but I can't help cause I'm not a linux guy... :oops:

EDIT: Wait a minute... Did you upgrade the Irrlicht SDK to 1.6?
May the software be with you
Post Reply