Using GLUT in QC custom plugin

LukeNeo's picture

Hi, is it possible to use GLUT library inside a custom QC plugin? I tried to do that in this way:
* add GLUT framework to my project
* add

#import <OpenGL/OpenGL.h>
#import <OpenGL/gl.h>
#import <OpenGL/glu.h>

in my plugin.h

now, if in the execute method I try to make a simple sphere with this simple code..

- (BOOL) execute:(id<QCPlugInContext>)context atTime:(NSTimeInterval)time withArguments:(NSDictionary*)arguments
{
 
[...]
 
GLUquadricObj *quadratic;
quadratic=gluNewQuadric();
gluQuadricNormals(quadratic, GLU_SMOOTH);
gluQuadricTexture(quadratic, GL_TRUE);
gluSphere(quadratic,10.3f,32,32);
 
[...]
 
}

..nothing happens. Where I am wrong?

Thank you, Luke

franz's picture
Re: Using GLUT in QC custom plugin

as a sidenote, GLUT is different from GLU. GLUT is a windowing system, GLU is an utility library. QC plugins should not need GLUT, AFAIK.

Did you add openGL framework as well to your project ?

On the GLU side, I tryied once to get GLUtesselator object in a QC plugin, but never managed to get it working, as well. Having said that I did not persevere more than a few minutes. Should definitly be possible tho'.

LukeNeo's picture
Re: Using GLUT in QC custom plugin

franz wrote:
Did you add openGL framework as well to your project ?

On the GLU side, I tryied once to get GLUtesselator object in a QC plugin, but never managed to get it working, as well. Having said that I did not persevere more than a few minutes. Should definitly be possible tho'.

You're right: I don't need to add GLUT framework. Yes, I added OpenGL framework: I can draw lines and points using simple OpenGL primitives, but glu functions seems to take no effect.

Maybe there is something in the QC plugin structure that don't allow to use them?

cwright's picture
Re: Using GLUT in QC custom plugin

Maybe you need to perform a context switch, or make sure you're using the glu cgl_ctx macro functions. There's nothing that prevents GLU from working -- it's how sphere and cylinder work under the hood.

franz's picture
Re: Using GLUT in QC custom plugin

cool ! thanks for the tip Chris

LukeNeo's picture
Re: Using GLUT in QC custom plugin

cwright wrote:
Maybe you need to perform a context switch, or make sure you're using the glu cgl_ctx macro functions. There's nothing that prevents GLU from working -- it's how sphere and cylinder work under the hood.

Thank ou for the hint!:) I write

#import <OpenGL/gluMacro.h>

instead of

#import <OpenGL/CGLMacro.h>

and now I can see what I draw with glu* functions. But.. is it the right way to use the glu cgl_ctx macro functions?

Another question: I still am not able to use GLUT functions, like glutWireCube() and so on.. if I want to use them, I have to use import another context?

Thank you!

cwright's picture
Re: Using GLUT in QC custom plugin

using cgl_ctx macro functions is the way 100% of QC's internals work (and virtually all of the OS X-bound OpenGL code I've written as well) -- it's the correct way.

GLUT is not GLU. GLUT is problematic for a number of reasons, so I highly advise not using it. A wireframe cube is like 10 lines of GL code. I don't think any GLUT cgl_ctx macros exist (though I could be mistaken?) -- if they don't, you're going to require 2 context switches per patch (yay!).

EDIT: just checked through the glut framework -- no macros exist. Don't use GLUT.

franz's picture
Re: Using GLUT in QC custom plugin

thx chris. got to the same conclusions here. GLUT seems crap anyway --- just browse at openframeworks forum for confirmations: everybody's complaining about GLUT.

how about GLU ? such as GLU tesselator for instance. I tryied to pass cgl_ctx, but it didn't work out. Didn't find any info either on the internet. It seems problematic to tell GLU to draw into a QClugin context. Any info truly welcome.

vade's picture
Re: Using GLUT in QC custom plugin

If the framework you are linking does not respect CGLMacros, you may have to call CGLSetCurrentContext(cgl_ctx);

franz's picture
Re: Using GLUT in QC custom plugin

thx master vade

cwright's picture
Re: Using GLUT in QC custom plugin

vade wrote:
you may have to call CGLSetCurrentContext(cgl_ctx);

Nooooooo! stop the senseless violence ... err, context switching.

#import <OpenGL/gluMacro.h>

should magically make GLU functions correctly utilize the cgl_ctx without costing a switch -- those are super-bad, and some vendors (coughnvidiacough) make them painfully expensive. Just don't do it.

And by "don't", I mean, seriously. If you think "oh, well it's just this once", you are wrong. (There are some non-OS X bound frameworks that can't deal with cgl_ctx stuff, making a switch actually required, but those are the exception, and should probably get some patch action to support it).

For GLU stuff, if you do stuff in -setup:, it won't always work in -execute: (because the context can change on you, and glu doesn't work around shared contexts due to some implementation details -- the radar for that is marked as wont-fix because glu's in maintenance mode, and has been for several years... yeah, writing stuff around code like that is definitely going to be awesome). -- you'll have to do it all in one go (all in -execute:, for example)

vade's picture
Re: Using GLUT in QC custom plugin

Yes of course if you can use CGLMacro.h do it, but there are some libraries where that wont work in my experience.

franz's picture
Re: Using GLUT in QC custom plugin

"should magically make GLU functions correctly"

I love magic !

cwright's picture
Re: Using GLUT in QC custom plugin

yep, those fall under my "some non-OS X bound frameworks that can't deal with cgl_ctx stuff, making a switch actually required, but those are the exception, and should probably get some patch action to support it" category -- I'm not kidding when I suggest patching; context switches are literally non-existent in native OS X code, and for good reason.

vade's picture
Re: Using GLUT in QC custom plugin

Thats interesting. I did not know that. Also interesting that its more expensive on NVidia. Thanks for the info, now to double check everything to make sure Im not accidentally doing something lame :) ha!

franz's picture
Re: Using GLUT in QC custom plugin

unfortunately, it doesn't work "magically". Xcode still complains about gluPerspectiveCTX.

note that i'm using : CGLContextObj cgl_ctx = [context CGLContextObj];

where maybe i should declare cgl_ctx as a gluPerspectiveCTX ???

EDIT: It works, my fault. Didn't link against the framework.