Do I need glew?

Hey all I just want to see if I can get some clarification on using glew? In most tutorials for SDL2 + OpenGL they use Glew to get OpenGL support. However I’m having trouble getting it to work and I was wondering if it’s even needed. I’ve come across some code that just uses SDL2.h and SDL_OpenGL.h with modern OpenGL and it seems to be working just fine.

Here’s an example: Minimal SDL2 + OpenGL3 example. · GitHub

The basic idea I’m getting by searching through google is that SDL2 will include most of OpenGL but you need Glew to get the full scope of OpenGL. Is this correct?

If so, how do I know if I’ll even need it. Can I get by just be using the provided OpenGL? Or will I just create more work for myself later if I don’t include Glew now?

I’m just starting OpenGL and I don’t think I’m too likely to be doing anything advanced yet.

libGL or opengl32.dll aren’t guaranteed to export all opengl functions you may want to use.
I think opengl32.dll only exports those for (ancient) OpenGL 1.1, you must get all other functions as function pointers with SDL_GL_GetProcAdress() (or the platform specific function that SDL uses under the hood).
glew just helps to figure out which version of OpenGL and which extensions are supported and loads their function pointers so you can easily use them.

So no, you don’t have to use glew, you can also get the function pointers yourself (the types are in SDL_opengl.h and SDL_opengl_glext.h and have names like PFNGLBUFFERDATAPROC for glBufferData) - or you can use another lib to manage the extensions, for example glad: https://glad.dav1d.de/

Short version: you want to use GLEW or GLAD (really, just GLAD)

Long version:

On *nix systems, you don’t have to manually load core OpenGL functionality at runtime. Just linking against libGL will give you what you need, so long as you stick to a version of OpenGL old enough to be supported on every OS version you support. You don’t have to manually load extensions either IIRC, but it’s a good idea to do it anyway so if an extension isn’t available your program can either use an alternate rendering path or tell the user it can’t run, vs the user’s OS not being able to start your program because it can’t link the extension (and probably not providing a user-friendly explanation why, if any at all).

On Windows, it’s a whole 'nother ballgame. You’ve got to manually load basically everything. The Windows OpenGL DLL that your program links to only supports, like, OpenGL 1.1 (OpenGL 1.4 on Windows Vista and later, IIRC), so even with the latest hardware and the latest driver you’re stuck with OpenGL 1.1 unless you manually load all functionality past that, and all extensions.

Stuff like GLEW and GLAD make manually loading functionality and extensions easier, by doing it for you. One of the nice things about GLAD is that it lets you specify the extensions you’re using, and it won’t try to load any that you aren’t.

1 Like