Do I have to include SDL_opengl.h after GL/glew.h?
Asked Answered
I

1

7

I would like to create an OpenGL project with SDL2. I usually start my OpenGL projects by including GL/glew.h first. Do I have to include SDL_opengl.h afterwards? It also contains the OpenGL API headers, so It seems to me that it is not necessary to include both.

Itagaki answered 15/6, 2019 at 15:51 Comment(1)
Including both will allow you to use SDL's audio, event handling, threads and times in your OpenGL applications (a function often performed by GLUT).Carlsbad
V
4

SDL can be told to render using OpenGL. But you can use SDL for other things and do OpenGL stuff on your own.

In the second case you need a way to define and load GL's API function pointers from the driver. GLEW is such a function loader.

In the first case, using SDL rendering, SDL will #include all OGL headers needed by #include SDL_opengl.h. For example, it's included from SDL_render_gl.c

Getting the OGL function pointers twice, including both glew.h and SDL_opengl.h is not the way to go. If you include SDL_opengl.h before glew.h likely you get a compiler assertion failure.
If you first include glew.h perhaps the compiler allows it, I haven't tested it and nothing in SDL headers seems to check this case.

But the real point is that you get nothing using GLEW with SDL (*) and this is prune to fail.

(*) It's true that you may need some "new" feature that is contained in last version of glew but not in SDL. In this case, avoid all OGL drawing feature provided by SDL.

EDIT

@HolyBlackCat's comment about SDL supporting only GL 1.x is not completely true. But this requires more explanation.

SDL provides its own API. To draw a line you call SDL_RenderDrawLine, which internally and depending on the renderer used calls a gl-command or another command for another API (OS, DirectX, etc). By gl-command I mean a command as OpenGL specs define it (glDrawArrays, glBindVertexBuffer, etc).

All gl-whatever must not only be defined, but for OGL>1.1 the gl-functions must also be loaded from the driver. SDL loads the gl-functions it uses, admittedly most of GL1.1 and a bunch of GL2.1.
I've found this SDL+OGL3 example which seems to work with no extern loader (like glew), only SDL. I've not tested it, but I can't find in the SDL source where some OGL functions used in this example are loaded.

What I do can see in SDL_opengl_glext.h (included from SDL_opengl.h) is an almost-copy of the headers provided by Khronos, which do define the prototipes with recent version (4.4?). These headers are also part of glew files; glew updates them as soon as possible.

And then we arise to the matter of this question:

  1. Am I going to use only SDL API? If yes, no GLEW is needed.
  2. Am I going to use OGL 1.x on my own, but also using SDL GL rendering? If yes, again, no GLEW is needed.
  3. I want to do some OGL>=2 on my own. If you use the functions already loaded by SDL (good luck to find them out), then no GLEW is needed.
  4. I want full use of full OGL functions. Then don't use SDL for rendering in the windows you do GL rendering. But use SDL to create the window and the context.

I'm in case 4., Which headers do I include?
First of all, remember you need a "gl-functions loader". Because SDL doesn't load all OGL functions (but just a bunch), you need another loader, like GLEW.

Then if you are going to use GLEW anyhow, just include glew.h instead of SDL_opengl.h. Using both has no profit, and you may meet some error.

Vagarious answered 10/6, 2020 at 16:15 Comment(1)
From what I can see, SDL_opengl.h supports GL 1.x only. If "you may need some "new" feature that is contained in last version of glew but not in SDL" Only if you consider GL 2.x to be "new". :PScopoline

© 2022 - 2024 — McMap. All rights reserved.