Detect if 'inside' a glBegin (and what mode)?

Hi all - I’m writing some opengl code as part of a plugin, and as the plugin documentation isn’t very thorough, I’m wondering if there’s a way to programatically determine

a) if we’re ‘inside’ a glBegin(mode)
b) if so, what the mode is (ie, GL_QUADS, GL_TRIANGLE_STRIP, etc)

(For context, it looks like in some instances my plugin, callback may be called ‘inside’ a glBegin, but I’m not sure under what situations this happens… if I could detect what the current mode is, it would give me some clues…)


The only hacky idea i can think of, is to do something that is not allowed inside glBegin/glEnd and check glGetError for whether it complained.

Other than that i don’t know.


The ‘inside Begin’ and current primitive type is explicitly internal state, and can’t be accessed with GL state-retrieval function calls.

That being said, along the lines of Jan’s suggestion, you can detect if you are inside glBegin by issuing your own glBegin (bracketed with glGetError). If your glBegin generates an error, then you were inside glBegin. If your glBegin doesn’t generate an error, you weren’t inside glBegin (but you are now), and you should call glEnd.

If you want to find the type of primitive, I suggest either using an intercept program that logs all OpenGL calls (such as GLintercept:, and making some “unusual” OpenGL call to mark your plugin execution.

Or you could write your own custom interceptor that tracked the begin/end state.

An easy hack is to wrap glBegin and glEnd commands with your own:


currentPrimType = prim;
isInside_glBegin = true;

isInside_glBegin = false;

This should do the job.

I don’t think you got the point of his question glfreak.

Kelvin is right, i forgot that you want the primitive type also. In that case intercepting the gl-calls is probably the only way to get to that information, though that is not easy.

The question is, why would a program want to call a plugin INSIDE a glBegin call, without telling it how to do its work?? I mean, i assume your plugin is supposed to render stuff (and not do something else entirely)? In that case the app must tell the plugin how to go about that. Are you sure there are no other ways, that you can retrieve such vital information from the application?


Gotcha! I see now what you mean…I missed that plugin part sorry.

So you plugin can issue drawing calls, but you ae not sure if the main application is already in glBegin/End pair trying to render some stuff?

And I assume the application is asynchronous with the plugin, so both can render at the same time.

In this case as far as both use different gl contexts, then you are safe.

If the program is providing the context to the plugin, then it should be safe as well since the main application should guarantee that drawing commands are issued in sequence and asynchronously.

I’m not sure about intercepting of gl commands, but what application you working on? is it Maya or Lightwave3D, or in house?