launch glut/opengl window from max object
I am trying to make a C/C++ plugin for MaxMSP (with the MaxSDK-5.0.8 on OS X with Max 5.0.8 - build 40079). I want the plugin to open a GLUT openGL window when it is sent a bang. For those that are curious, inside this opengl window is a music notation display I am programming in C++. No, I do not want to use Jitter, Javascript, the LCD panel object, Jgraphics.h, etc. etc. I have gotten the MaxSDK & my notation GLUT OpenGL code to compile, but when I try to launch my opengl window the plugin severely freezes or crashes Max.
This is what I tried
1. Using XCode, I compiled and linked my new maxmsp plugin; copied new plugin into the /Applications/Max5/plugins folder
2. (Re)started MaxMSP
3. created object in the patcher and tried to send it a bang
This is where the problem gets a little complex:
a) So long as I don't try to launch a GLUT window (eg send a bang) while the plugin is running inside maxmsp, the plugin works fine. The inlets, outlets and some basic calculations worked fine, so I believe the .mxo is compiled and linked correctly. The plugin is loaded in Max and works fine -- again, so long as no GLUT windows are opened. I just want to be clear this isn't a compiling/linking problem, its a runtime error specifically related to GLUT or OpenGL and MaxMSP.
b) I narrowed down the openGL / Glut culprit that causes the crash: the function "glutMainLoop();". glutMainLoop(); is the GLUT call which opens a GLUT window and starts the GLUT event loop. If this line is present in the code, Max will quasi-lock up, where the GUI is still live but the keyboard won't work.
The console.app output looks like this when I open max, create my object, and send it a bang:
10/6/09 10:29:52 PM [0x0-0x24b24b].com.cycling74.MaxMSP[86679] Max: db
update complete
10/6/09 10:30:18 PM MaxMSP[86679] GLUT Warning: glutInit being called
a second time.
(As one final step: because there was a quasi-lock up in Max, I tried running the glutMainLoop(); in a posix thread - I didn't know if this would make a difference, so I tried. Unfortunately, caused GLUT to complain that it needs to run in the "main thread" to run.)
Therefore, two questions arise...:
1. How can I make this work? What is the problem?
2. Why does GLUT think GLUT is being launched a second time?
I could make a 100% separate standalone app for the GLUT window, and communicate between Max and the standalone via a socket, OSC, or something like that. However, I really would rather not have to program all of that. I would like this to work as a plugin from inside max. THanks
I don't have any experience with Max/MSP in this area, but the OSX implementation of GLUT wants to be in the main thread of your application, as you found out. FreeGLUT doesn't have this restriction, but you may run into other issues...
Best, Charles
Well, I'm not sure about the double- glutInit, but this isn't your only problem. glutMainLoop() is a blocking call - it will take control of the main thread and not return.
I'm pretty sure that means that GLUT is not suitable for an application plugin like a max external.