What does GLEW do and why do I need it?
Asked Answered
T

2

29

Okay, so I already know why I need GLEW, but only up to a point.

If I am using modern OpenGL stuff, probably from version 3.0 onwards, and/or am using the Core Profile, then GLEW is required as without it compilation produced error such as glGenVertexArrays was not declared.

So GLEW does a bit of background work including the modern OpenGL functions we would like to use, probably.

Apart from that, does it do anything else? Also, how does it work.

As an example, does it improve cross-platform compilation? (Require less modifications to code if moving from Linux to Windows or OS X for example?

Does it also improved the "cross-platform-ness" of graphics hardware? For example, say we had two identical computers, including the OS. A program is compiled using OpenGL 4.3 commands on one system. If the other system has a graphics card or driver which only supports OpenGL 3.3, does GLEW help with that? (Compiling shaders for an older version of OpenGL, perhaps?)

So you can probably see I don't actually know what GLEW does or how it does it.

One last point; does anyone know how to use GLEW with GLFW? I might post that as a separate question actually.

Telluric answered 23/7, 2013 at 11:42 Comment(0)
S
15

GLEW isn't 'required', as you put it. You could use glcorearb.h header, or anything like that. However, if you link with some function - it must exist on target platform, or your program wouldn't launch. GLEW and others are exactly to handle that - you're not linking with GL functions directly, but instead getting function pointers after initialization phase. It allows you to check at runtime which extensions are present and which functions may be used.

The only thing it helps with portability is getting function pointers - it may be wglGetProcAddress/glxGetProcAddress and it's analog for apple OSes. So no, it's not a case. However, the variety of available GL extensions is.

GLEW requires no touch points with your preferred GL initialization library - just call glewInit after creating GL context.

Shortwinded answered 23/7, 2013 at 11:59 Comment(3)
If I create a window and then call glewInit() I get a segfault error?Telluric
@EdwardBird: Did you create an OpenGL context on that window? Is a OpenGL context active when calling glewInit?Octo
Yeah I did, but I've fixed the problem now - thanks anyway (:Telluric
E
6

To correctly init glew an OpenGL context must be created and wglMakeCurrent() must be called before calling glewInit();

I would put this into the comments of keltar's answer, but I don't see how.

Elegance answered 23/7, 2013 at 19:56 Comment(4)
why wglMakeCurrent() is necessary? is this specific to a platform or does every OS need it?Reversioner
Its windows specific and it is to make a rendering contex active so that you can call gl commands without getting an errorElegance
so I assume linux or OS X may not need it? btw, impressive speed of response.Reversioner
They each have their own versionsElegance

© 2022 - 2024 — McMap. All rights reserved.