Discussion:
Can I check that an openGl context exist?
(too old to reply)
m***@yahoo.com
2006-08-10 05:08:26 UTC
Permalink
Hi guys, when a program tries for example to create a glsl shader
before an openGl context is created it seems to cause the program to
crash (which is not so cool!). But anyway as all the calls I am doing
are encapsulated in a class, I was wondering if there's a way I can
check that an openGL context exists before I do a call to
glCreateShader for example. Thank you, Mark -
Latti
2006-08-10 07:32:23 UTC
Permalink
Mark,

if you are using Windows, try calling wglGetCurrentContext(). If it returns
NULL, no context is current.
- Latti
Post by m***@yahoo.com
Hi guys, when a program tries for example to create a glsl shader
before an openGl context is created it seems to cause the program to
crash (which is not so cool!). But anyway as all the calls I am doing
are encapsulated in a class, I was wondering if there's a way I can
check that an openGL context exists before I do a call to
glCreateShader for example. Thank you, Mark -
fungus
2006-08-10 10:02:37 UTC
Permalink
Post by m***@yahoo.com
Hi guys, when a program tries for example to create a glsl shader
before an openGl context is created it seems to cause the program to
crash (which is not so cool!). But anyway as all the calls I am doing
are encapsulated in a class, I was wondering if there's a way I can
check that an openGL context exists before I do a call to
glCreateShader for example. Thank you, Mark -
How about wglGetCurrentContext()....?
--
<\___/>
/ O O \
\_____/ FTB. For email, remove my socks.

In science it often happens that scientists say, 'You know
that's a really good argument; my position is mistaken,'
and then they actually change their minds and you never
hear that old view from them again. They really do it.
It doesn't happen as often as it should, because scientists
are human and change is sometimes painful. But it happens
every day. I cannot recall the last time something like
that happened in politics or religion.

- Carl Sagan, 1987 CSICOP keynote address
Wolfgang Draxinger
2006-08-10 16:19:04 UTC
Permalink
Post by m***@yahoo.com
Hi guys, when a program tries for example to create a glsl
shader before an openGl context is created it seems to cause
the program to crash (which is not so cool!).
Which happens, because Shaders are usually exposed through
extensions, which are accessed by function pointers you get with
wglGetProcAddress. Since wglProcAddress only works with a
context being current those function pointers will be NULL
without being properly initialized, which requires of course a
current context.

Anyway you should check extension function not being NULL
explicitly, if you want to make your program more stable.

Wolfgang Draxinger
--
m***@yahoo.com
2006-08-10 23:00:55 UTC
Permalink
Post by Wolfgang Draxinger
Anyway you should check extension function not being NULL
explicitly, if you want to make your program more stable.
Thanks everyone for the answers. I am still trying to get my way around
openGl extensions at the moment and as I just started really playing
with those things they are still quite a few things I don't understand.

As I couldn't find examples on how to use extensions directly I ended
up using glew. I still do not undestand what glew does for me but
apparently initialise all these extensions so they become available in
the program. Although indeed when I look at some examples that compile
under Windows, it seems that extensions are initialized using
wglGetProcAddress but I should left that to another thread. It's
frustrating because despite all the docs/books and tutorials which are
out there, there's none that seem to explain you clearly step by step
how those things work. For example the orange book tells you how to
write GlSl shaders but doesn't really provide you with a complete
openGl c program that shows you how to use them in the context of a
functional program.

I compile the code under Linux so it seems that I might not be able to
use the function you mentionned. Alhough I am assuming there's the same
thing for Linux ? If you have an idea on how to get that working on
Linux it would be great.

Many thanks, Mark
Wolfgang Draxinger
2006-08-11 01:27:41 UTC
Permalink
Post by m***@yahoo.com
As I couldn't find examples on how to use extensions directly I
ended up using glew. I still do not undestand what glew does
for me but apparently initialise all these extensions so they
become available in the program. Although indeed when I look at
some examples that compile under Windows, it seems that
extensions are initialized using wglGetProcAddress but I should
left that to another thread.
It fit's here IMHO. An extension to OpenGL consists of several
additions and changes to the OpenGL specification, introducing
new Tokens (a token are OpenGL enumeration constants like
GL_TEXTURE_1D or GL_TEXTURE_MIN_FILTER or, GL_LIGHT1 or
GL_LIGHTING), new Procedures and dependencies within OpenGL
itself.

To access an extension you only need the Tokens and the
Procedures.

The Tokens are added by e.g.
#define GL_FOOBAR_BLA_EXT 0x....

A Procedure is accessed by a function pointer type, e.g.

typedef GLvoid (*PFNGLFOOFOO_BLAPROC)(GLenum foo, GLuint bar);
typedef GLvoid (*PFNGLBARBAR_BLAPROC)(GLint foo, GLuint bar);

and the pointer itself

PFNGLFOOFOO_BLAPROC glFooFooBLA = NULL;
PFNGLBARBAR_BLAPROC glBarBarBLA = NULL;

After getting a valid OpenGL context and testing for presence of
the extension by checking the string returened by
glGetString(GL_EXTENSIONS) you retrieve the address of the
procedure with wglGetProcAddress (Windows) glXGetProcAddress
(X11 based Systems, i.e. Linux, all *nix, *BSD),
aglGetProcAddress (Apple).

if(strstr(glGetString(GL_EXTENTIONS), "GL_BLA_foo")!=NULL)
{
glFooFooBLA = wglGetProcAddress("glFooFooBLA");
glBarBarBLA = wglGetProcAddress("glBarBarBLA");
}
Post by m***@yahoo.com
It's frustrating because despite
all the docs/books and tutorials which are out there, there's
none that seem to explain you clearly step by step how those
things work. For example the orange book tells you how to write
GlSl shaders but doesn't really provide you with a complete
openGl c program that shows you how to use them in the context
of a functional program.
Appendix C (in the 3rd Edition) of the red book tells you
everything you need to know.

Wolfgang Draxinger
--
m***@yahoo.com
2006-08-11 02:52:10 UTC
Permalink
Post by Wolfgang Draxinger
Appendix C (in the 3rd Edition) of the red book tells you
everything you need to know.
Excellent thank you. I just checked and started to read it! Thanks a
lot, Mark -

Continue reading on narkive:
Loading...