QtOgre on Mac OS X?

Problems building or running the engine, queries about how to use features etc.
meteors
Gnoblar
Posts: 18
Joined: Thu Nov 30, 2006 3:32 am

QtOgre on Mac OS X?

Post by meteors »

Hi all,

Has anyone succeeded in embedding Ogre in a Qt window on the mac?

I have modified the QtOgre simple project, and it seems to want to compile except for this line:

params["externalWindowHandle"] = Ogre::StringConverter::toString((size_t)(HWND)winId());

What is the correct way to write that on OS X 10.4? HWND is Win32 dependent.



Thanks in advance! Regards,
-joshua
joshua@spacescience.com
User avatar
Game_Ender
Ogre Magi
Posts: 1269
Joined: Wed May 25, 2005 2:31 am
Location: Rockville, MD, USA

Post by Game_Ender »

What version of Ogre are you using? Dagon 1.2 with the SDL, Dagon 1.2 with the Cocoa backends (Will's Framework), or are you using the 1.3 CVS version? To the best of my knowledge none of those version support the external/parent window handle methods. I am sure patches against head would be welcome. I might be wrong about the SDL ones, but I think SDL doesn't like to play nicely with others and many people have trouble using embedding Ogre using the SDL backend.
meteors
Gnoblar
Posts: 18
Joined: Thu Nov 30, 2006 3:32 am

Post by meteors »

Hey,

Thanks for the follow-up.

I'm using Dagon, downloaded from the Ogre site, and the:

Dagon_Mac_Dependencies_10.4_Universal_v4

So it's impossible to do this with OS X for now? That's a *real* bummer.


Thanks again,
-joshua
joshua@spacescience.com
meteors
Gnoblar
Posts: 18
Joined: Thu Nov 30, 2006 3:32 am

Post by meteors »

Hi again,

I just spoke with Justin on Freenode, and he points out:

"...QT is carbon based, so this should be possible thanks to the carbon back end that is in development."


Does anyone have anything to add to this?



Thanks again! Best,
-joshua
joshua@spacescience.com
Mans
Gnoblar
Posts: 24
Joined: Wed Mar 28, 2007 10:47 am

Post by Mans »

I'm working with Ogre 1.4, and I can't manage to create a QT (4.2.3) widget embedding Ogre.
I've tried loads of combinations like :
params["externalWindowHandle"] = Ogre::StringConverter::toString((size_t)winId());
or, with Carbon :
params["externalWindowHandle"] = Ogre::StringConverter::toString(HIViewGetWindow(HIViewRef(winId()));

But none of them is working : the QT widget is always empty...
Has anyone been able to do something like that ??
User avatar
JustinWalsh
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 209
Joined: Mon Sep 05, 2005 8:11 pm
Location: Phoenix, AZ
Contact:

Post by JustinWalsh »

The reason the Qt widget is empty is because Qt is taking over the context and hiding it. I had some discussion with the Qt folks and the response was to take a look at the QVTKWidget. This widget has some custom case code for the mac to allow the context to be displayed properly.

If you get something working please let me know, I haven't had the time to create the custom widget as of yet, but it is on my TODO list.
Mans
Gnoblar
Posts: 24
Joined: Wed Mar 28, 2007 10:47 am

Post by Mans »

I didn't have time to post here before, but I managed to make my program work last week. I didn't use the QVTKWidget (as I haven't seen your post by then), and I had to modify Ogre Source to make it work.
In fact, if I use the following code to create the renderWindow :
params["externalWindowHandle"] = Ogre::StringConverter::toString((size_t)winId());
a_ogreRenderWindow = a_ogreRoot->createRenderWindow("View", width(), height(), true, &params);

some very strange things happen : the QT widget is not empty, it loads the ogre display but only a part of it. And if I resize the widget, I notice that the Ogre display resizes as well, but inverted on the y axis.
Therefore, I had to modify the Ogre code so the resize goes well.
I did the following modification in OgreOSXCarbonWindow.cpp :

void OSXCarbonWindow::windowMovedOrResized()
{
// External windows will call this method.
if(mView != NULL)
{
//update our drawing region
::Rect ctrlBounds;
GetControlBounds(mView, &ctrlBounds);

//////////////////////////// working with all kind of windows including brushed metal window ////////////////////////
// compensate y inverted axis
HIRect viewBounds, winBounds;
HIViewGetBounds(mView, &viewBounds);
HIViewRef root = HIViewGetRoot(HIViewGetWindow(mView));
HIViewRef content_root;
HIViewFindByID(root, kHIViewWindowContentID, &content_root);

HIViewGetBounds(content_root, &winBounds);
HIViewConvertRect(&viewBounds, mView, content_root);
GLint bufferRect[4] = { GLint(viewBounds.origin.x),
GLint((winBounds.size.height) - (viewBounds.origin.y + viewBounds.size.height)),
GLint(viewBounds.size.width),
GLint(viewBounds.size.height) };
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

aglSetInteger(mAGLContext, AGL_BUFFER_RECT, bufferRect);
aglEnable (mAGLContext, AGL_BUFFER_RECT);
swapBuffers(true);

mWidth = ctrlBounds.right - ctrlBounds.left;
mHeight = ctrlBounds.bottom - ctrlBounds.top;
mLeft = ctrlBounds.left;
mTop = ctrlBounds.top;

}

for (ViewportList::iterator it = mViewportList.begin(); it != mViewportList.end(); ++it)
{
(*it).second->_updateDimensions();
}
}


I found the code on the internet, and now my program works fine on OSX. It is not a proper solution, but at least it's working... If you have any idea why I get this strange behaviour, and a better way to solve it, please tell me !
meteors
Gnoblar
Posts: 18
Joined: Thu Nov 30, 2006 3:32 am

Ogre/Qt OSX Application

Post by meteors »

Hey Mans,

Is there any way I could see the full source code for your application?

(Or maybe you could put together a very simple app for us?)


Thanks in advance!! Best,
-joshua
User avatar
djbe
Gnoblar
Posts: 6
Joined: Tue Oct 23, 2007 4:05 am

Post by djbe »

Hey, I was wondering if anyone got this to work? Because I've been trying for a while, and only get a blank screen... I don't want to modify the ogre source code (I'm not the only person who is going to use this) and I tried taking a look at QVTKWidget but couldn't really get what they are doing (they also have a lot of compatibility code with older qt versions and such, which I don't need). Anyway, here is a piece of the code I'm using:

Code: Select all

	// Create the renderwindow
	Ogre::NameValuePairList params;
	
#if defined(Q_WS_MAC)
	WId wid = (size_t) HIViewGetWindow(HIViewRef(winId()));
#else
	WId wid = winId();
#endif
	
	params["externalWindowHandle"] = Ogre::StringConverter::toString((size_t) wid);
	params["vsync"] = "no";
#if !defined(Q_WS_WIN)
	char buf[64];
	sprintf(buf, "SDL_WINDOWID=0x%lx", (size_t) HIViewGetWindow(HIViewRef(winId())));
	putenv(buf);
#endif
	
	m_renderWindow = m_ogreRoot->createRenderWindow("View", width(), height(), false, &params);
	
	// not sure if this part is needed:
	WId ogreWinId = 0x0;
	m_renderWindow->getCustomAttribute("WINDOW", &ogreWinId);
	std::cout << "ogreWinId: " << ogreWinId << "\n";
	// why is ogreWinId 0? it should have another value I think
	create(ogreWinId);
If needed, you can find all the code here: http://david.thesneaky.com/Test or more conveniently in a zip file here: http://david.thesneaky.com/Test.zip

Thanks in advance,
David

edit: Yes I searched before posting, here and on the qt-interest archive, and yes ogre itself works fine here (tested all the examples out of curiosity :P )
meteors
Gnoblar
Posts: 18
Joined: Thu Nov 30, 2006 3:32 am

We need...

Post by meteors »

Ogre/Qt on OS X!!!!

On strike!



-joshua

PS - maybe we could pool together and pay someone to come up with a reliable method? -j
lexchou
Gnoblar
Posts: 20
Joined: Thu Nov 15, 2007 2:14 am

Post by lexchou »

Neither Qt nor Ogre's source should be changed.

it works fine here.

I only add these code:

Code: Select all

setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_NoSystemBackground);
when create a widget for ogre rendering

and

Code: Select all

Ogre::NameValuePairList pl;
pl["externalWindowHandle"]=Ogre::StringConverter::toString((size_t)winId());
QSize size=this->size();
window=root->createRenderWindow(windowName.toStdString(),size.width(),size.height(),false,&pl);
to make a render window.

in the widget's paintEvent ask the Ogre::RenderWindow to update
meteors
Gnoblar
Posts: 18
Joined: Thu Nov 30, 2006 3:32 am

Post by meteors »

lexchou:

Is there any way you could zip up and post a whole example project somewhere?

I would be immensely grateful!!


Best regards,
-joshua
User avatar
djbe
Gnoblar
Posts: 6
Joined: Tue Oct 23, 2007 4:05 am

Post by djbe »

lexchou: already tried that :wink:
User avatar
djbe
Gnoblar
Posts: 6
Joined: Tue Oct 23, 2007 4:05 am

Post by djbe »

After fiddling around, I noted the following things:

Code: Select all

#if !defined(Q_WS_WIN) 
   char buf[64]; 
   sprintf(buf, "SDL_WINDOWID=0x%lx", (size_t) HIViewGetWindow(HIViewRef(winId()))); 
   putenv(buf); 
#endif 
and

Code: Select all

   WId ogreWinId = 0x0; 
   m_renderWindow->getCustomAttribute("WINDOW", &ogreWinId); 
   create(ogreWinId);
don't do anything...

Depending on which value I pass to externalWindowHandle, I get one of the following results:
  • winId() ==> Case 1
  • dynamic_cast<QWidget*>(parent())->winId() ==> Case 1
  • HIViewGetWindow(HIViewRef(winId())) ==> Case 2
  • HIViewGetWindow(HIViewRef(dynamic_cast<QWidget*>(parent())->winId())) ==> Case 2
Case 1:
Image
Case 2:
Image

In case 1, it seems everything is being rendered in the titlebar (weird). In case 2, you can't see anything (that's what I had). Oh, and using parentWindowHandle instead of externalWindowHandle creates a 2nd window behind the qt window, where I can see the correct ogre scene. Btw, here it is recommended to use HIViewGetWindow() and HIViewRef().
User avatar
djbe
Gnoblar
Posts: 6
Joined: Tue Oct 23, 2007 4:05 am

Post by djbe »

Aaaaaaaah, I just love what all nighters do for you... They just get things done :P
meteors, you said you were gonna pay someone to come up with a reliable method? I'm a poor poor student and any money is welcome :wink:

So I've got qt and ogre working together here, without modifying ogre source code. The only steps you need to take are:
  • get the code here (or download the whole thing here for QOgreWidget (getting the .pro file is recommended)
  • from the ogre source, add these files to the headers dir in the ogre framework: OgreGLContext.h, OgreGLPrerequisites.h and GL/glew.h . You'll find them in {ogresourcedir}/RenderSystems/GL/include. In my opinion this is a bug in the ogre compile xcodeproject, I mean why does for example 'OgreOSXContext.h' get copied (wich depends on ogreglcontext.h)??
  • subclass the QOgreWidget class (like in TestWidget)
  • done! :D
If you have any questions, tips or find bugs, please mention them

Notes:
- Linux and windows are untested. I added the compatibility code I found on the forums/wiki.
- This was tested on OSX 10.5.1 with Ogre 1.4.5
Last edited by djbe on Sat Jul 05, 2008 7:05 pm, edited 1 time in total.
meteors
Gnoblar
Posts: 18
Joined: Thu Nov 30, 2006 3:32 am

Post by meteors »

Hey dude,

Nice going :-)

(I hope it works on 10.4, but if it doesn't, no worries.)

Do you take PayPal? Please message me with your email address. I want to send you $20 right now. I know that's such a small amount as to almost be insulting, but its all I have at the moment. We can arrange more after the holidays, etc.

Again, I know its not a lot but maybe you can buy someone *something* for Christmas on Friday.


Thanks man!! Best,
-joshua
User avatar
djbe
Gnoblar
Posts: 6
Joined: Tue Oct 23, 2007 4:05 am

Post by djbe »

lol I was just kidding :P I don't want any money for it, just some feedback on it to see if it works correctly on all platforms. Oh and about Tiger, there shouldn't be any problems, as long as you follow the steps I explained (and check the .pro file for which things to add to your project file).

So if people could just check if this works on windows and linux (and tiger, but that one should work :) ), and maybe find a tip for the following: in the .pro file, I have to add this to the includepath:
INCLUDEPATH += /Library/Frameworks/Ogre.framework/Versions/A/
I was wondering if anyone knows of a better way to do this (it is needed because otherwise OgreGLPrerequisites.h complains. This could be fixed by changing the line #include <GL/glew.h> to #include "GL/glew.h" in that file
Paladi
Kobold
Posts: 32
Joined: Mon Nov 19, 2007 6:24 pm
x 1

Post by Paladi »

I'm using Leopard too and I was finding ogre integration with QT or WxWidgets for made an editor.

I have tested your template and works GREAT!!!!

THANKS!!!!

If someone can try it on linux would be nice :)
alanic
Gnoblar
Posts: 14
Joined: Wed Dec 05, 2007 9:40 pm

Post by alanic »

*Here comes the linux guy!*

SWEET! I was about to start my own since I was disappointed with not finding a clean QOgreWidget around. I'm glad I gave the forums another look today:) The widget is very nice and reusable, so thanks a lot.

I made it work in Linux and this required small touches, mostly on the qmake file. Here is the patch, here is the patched tarball and here is my blog entry, with the explanations of the changes I made.

Again, thanks a lot. I also edited the QtOgre wiki page to add a link to here. http://www.ogre3d.org/wiki/index.php/QtOgre
Paladi
Kobold
Posts: 32
Joined: Mon Nov 19, 2007 6:24 pm
x 1

Post by Paladi »

I'm trying to made a render to texture on mac os x, and using this widget.

Always, when I try to update the texture the program crashes.

Has anyone tried to do a RTT with this wiget ? Where did you update the texture on the code?

thanks.
User avatar
xibo
Gnoblar
Posts: 16
Joined: Wed Oct 10, 2007 9:26 pm
Location: Tyrol, Austria

Post by xibo »

I've played around a little bit with the widget on my linux box. Now I have a question: Why does the Ogre widget must have a parent?

Code: Select all

winHandle += Ogre::StringConverter::toString((unsigned long)(this->parentWidget()->winId()));
//this code is also on the wiki
If there is no parent, this gives a seg fault. But it's not working with the following code either:

Code: Select all

winHandle += Ogre::StringConverter::toString((unsigned long)(this->winId()));
X Error: BadWindow (invalid Window parameter) 3
Major opcode: 18 (X_ChangeProperty)
Resource id: 0x400000a
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Extension: 144 (Uknown extension)
Minor opcode: 9 (Unknown request)
Resource id: 0x400000a
X Error: BadWindow (invalid Window parameter) 3
Major opcode: 40 (X_TranslateCoords)
Resource id: 0x400000a
edit:
Maybe I should have posted more code ^^. Here it is:

Code: Select all

void QOgreWidget::createRenderWindow() {
	Ogre::NameValuePairList params;

	if (m_renderWindow)
		return;
	configure();   //creates Ogre::Root

#if defined(Q_WS_MAC) || defined(Q_WS_WIN)
	params["externalWindowHandle"] = Ogre::StringConverter::toString((size_t) winId());
#else
	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()));
// 	winHandle += Ogre::StringConverter::toString((unsigned long)(this->window()->winId()));
	params["parentWindowHandle"] = winHandle;
#endif

	m_renderWindow = m_ogreRoot->createRenderWindow(
			"View" + Ogre::StringConverter::toString((unsigned long) this),
			width(), height(), false, &params);

#if defined(Q_WS_MAC)
	// store context for hack
	Ogre::OSXContext *context;
	m_renderWindow->getCustomAttribute("GLCONTEXT", &context);
	context->setCurrent();
	m_aglContext = aglGetCurrentContext();
	resizeRenderWindow();
#endif

	// take over ogre window
#if !defined(Q_WS_MAC)
	WId ogreWinId = 0x0;
	m_renderWindow->getCustomAttribute("WINDOW", &ogreWinId);
	assert(ogreWinId);
	create(ogreWinId);
#endif
}
User avatar
shams
Halfling
Posts: 96
Joined: Mon May 21, 2007 2:30 pm

Post by shams »

hello!
I tried to run Qt and Ogre on my mac... Done with your program and indications. But I tried to run the famous "showMesh" program and I have an error with QX11Info.... Do you have any idea from where it could come?

Thank you very much
ekarulf
Gnoblar
Posts: 5
Joined: Sun Apr 27, 2008 12:51 pm

Post by ekarulf »

shams wrote:hello!
I tried to run Qt and Ogre on my mac... Done with your program and indications. But I tried to run the famous "showMesh" program and I have an error with QX11Info.... Do you have any idea from where it could come?

Thank you very much
Try my patch mentioned here.
User avatar
shams
Halfling
Posts: 96
Joined: Mon May 21, 2007 2:30 pm

Post by shams »

hey!
I've got another big problem with the QOgreWidget. All works prefectly, except that my ogre widget is into a QTabWidget, and when i change the current tab, the Ogre widget is still displayed....!!
It seems that it is mac os related, because there is no bug under linux.
Have you the same problem?
User avatar
djbe
Gnoblar
Posts: 6
Joined: Tue Oct 23, 2007 4:05 am

Post by djbe »

Could you post your code/project with the problem please? (preferred a mini project with only the needed code).
Post Reply