Error when retrieving glViewport()

Hello everyone,
I want to ask you how I can retrieve glViewport and all other gl functions without using any other libraries like GLEW.

I have tried it like this:

typedef void (*GL_VIEWPORT_PTR) (GLint, GLint, GLsizei, GLsizei);

GL_VIEWPORT_PTR glViewportF = (GL_VIEWPORT_PTR)glfwGetProcAddress("glViewport");

It compiles without any errors but I get Segmentation fault (core dumped) when executing the program and the window draws for a fraction of a second so something must be wrong with the pointers. Could someone please tell me the correct way to retrieve the functions? Thank you in advance!

There’s a page one the wiki that has information on how to do it. Instead of the platform specific functions ({wgl|glX}GetProcAddress) you can use the one from glfw.
Note that (at least) on Windows you must have a valid OpenGL context before attempting to retrieve function pointers - the wiki page has more details on this as well.

1 Like

I’ve read that page already and in the code I’ve posted I actually used glfwGetProcAddress. But I just don’t know what is wrong with the pointer. If I remove the call of glViewport from my code the program executes, so the problem has to have something to do with my function retrieval.

Hmm, well with the information/code you’ve provided I’m not sure what to tell you beyond that you’ll have to debug your program :wink: Some things to investigate:

  • where does your program crash: when calling glfwGetProcAddress or when attempting to make a call through glViewportF (i.e. your function pointer variable)?
  • is the function pointer initialized before the first attempt to make a call through it?
  • do you get a reasonably looking function pointer from glfwGetProcAddress (not NULL nor some smallish integer value)?

glViewport is a core OpenGL 1.0 function, and is exported from opengl32.dll. It’s not clear from Microsoft’s documentation of wglGetProcAddress whether it’s valid to use it to obtain function pointers for core functions. It’s only intended to be used for functions which aren’t part of the OpenGL 1.1 API.

As a general rule, it does not. I assume glfwGetProcAddress implements the technique I’ve seen (and written) in other libraries: if the value you get is NULL (or equivalent), then get the OpenGL32.dll and load the procedure directly from there.

The wiki even shows this off, as well as the reasoning behind it.

If you still want to use your own function pointer, on windows you might just do:

glViewportF = glViewport;

I am sorry, I just was stupid there. You are right carsten, I didn’t call glfwMakeContextCurrent() and all that beforehand, so I had no valid OpenGL context! Just moving the code fixed it, thank you :slight_smile:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.