Creating an OpenGL 3.2/3.x context in SDL 1.3
Asked Answered
I

2

10

I'm facing a problem where SDL says it does not support OpenGL 3.x contexts. I am trying to follow this tutorial: Creating a Cross Platform OpenGL 3.2 Context in SDL (C / SDL). I am using GLEW in this case, but I couldn't get gl3.h to work with this either. This is the code I ended up with:

#include <glew.h>
#include <SDL.h>    

int Testing::init()
        {
            if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
            {
                DEBUGLINE("Error initializing SDL.");
                printSDLError();
                system("pause");
                return 1; // Error
            }

            //Request OpenGL 3.2 context.
            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

            //set double buffer
            SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

            //Create window
            window = SDL_CreateWindow("OpenGL 3.2 test",
                SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                600, 400, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
            if(window == NULL) return 3; // Error

            //Print errors to console if there are any
            printSDLError(__LINE__);

            //Set up OpenGL context.
            glContext = SDL_GL_CreateContext(window);
            printSDLError(__LINE__);
            if(glContext == NULL)
            {
                DEBUGLINE("OpenGL context could not be created.");
                system("pause");
                return 4;
            }

            //Initialize glew
            GLenum err = glewInit();
            if(err != GLEW_OK)
            {
                DEBUGLINE("GLEW unable to be initialized: " << glewGetErrorString(err));
                system("pause");
                return 2;
            }

            return 0; // OK code, no error.
        }

The only problem that is reported is after trying to call SDL_GL_CreateContext(window), where SDL reports "GL 3.x is not supported". However, both the tutorial and this sample pack (which I have not bothered to test with) report success in combining SDL 1.3 and OpenGL 3.2. I am aware that SDL 1.3 is in the middle of development, but I somewhat doubt that even unintentional support would be removed.

A context is still created, and GLEW is able to initialize just fine. (I can't figure out for the life of me how to see the version of the context that was created, since it's supposed to be the core profile, and I don't know how to find that either. According to the tutorial, SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) doesn't actually do anything, in which case I have no clue how to get the appropriate context created or change the default context.)

EDIT: After some testing thanks to the helpful function Nicol gave me, I have found that, regardless of the parameters I pass to SDL_GL_SetAttribute, the context is always version 1.1. However, putting in any version below 3.0 doesn't spit out an error saying it is not supported. So the problem is that the "core" version SDL sees is only 1.1.

For the record, I am using Visual C++ 2010 express, GLEW 1.7.0, and the latest SDL 1.3 revision. I am fairly new to using all three of these, and I had to manually build the SDL libraries for both 32 and 64 bit versions, so there's a lot that could go wrong. So far however, the 32 and 64 bit versions are doing the exact same thing.

EDIT: I am using an nVidia 360M GPU with the latest driver, which OpenGL Extension Viewer 4.04 reports to have full compatibility up to OpenGL 3.3.

Any help is appreciated.

UPDATE: I have managed to get SDL to stop yelling at me that it doesn't support 3.x contexts. The problem was that the SDL_GL_SetAttribute must be set BEFORE SDL_Init is called:

//Request OpenGL 3.2 context.
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

//Initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
    DEBUGLINE("Error initializing SDL.");
    return 1; // Error
}

Unfortunately, GLEW still refuses to acknowledge anything higher than OpenGL 1.1 (only GLEW_VERSION_1_1 returns true), which still has me puzzled. glGetString(GL_VERSION) also reports 1.1.0. It seems that my program simply doesn't know of any higher versions, as if I don't have them installed at all.

Ireneirenic answered 13/11, 2011 at 6:32 Comment(8)
Have you considered that it is your graphics card/drivers that don't support GL 3.x? In any case, you can use the usual methods to get what the context's GL version is (glGetString(GL_VERSION)).Irina
I have, I forgot to mention this important detail. Edited in, I'm using an nVidia 360M, which supports OpenGL 3.3. I'll add in the context it's creating with the function you gave me, thanks.Ireneirenic
After digging around a bit more, it seems that my program only sees OpenGL 1.1. Which might just be the software fallback for everything. So this problem is actually just a symptom of a much larger problem.Ireneirenic
And what happens if you remove the SDL attributes and just try to create a GL context normally?Irina
@NicolBolas The same thing occurs, a 1.1 context is created. I ran the glewinfo.exe which came with GLEW 1.7, and it also reports that it supports OpenGL 3.3. Literally, everything on my computer is saying it should work but it only fails in my program. Could it be I'm linking something incorrectly?Ireneirenic
It's probably something going wrong with SDL. Since 1.3 isn't done yet, it's possible that it's just buggy and not creating a decent pixel format.Irina
So, have you managed to get it working finally?Orcinol
For Mac OS X I found one more line needed, as per answer in #11961616 . Needed SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); after versioning 3,2.Mitra
R
14

since I don't know if you already found a solution, here is mine:

I struggled around a lot today and yesterday with this stuff. Advanced GL functions couldn't be used, so I even debugged into opengl32.dll just to see it really works and wraps the calls into the hardware-specific OpenGL DLL (nvoglnt.dll). So there must have been another cause. There were even tips in the internet to link to opengl32.lib before all other libraries, because ChoosePixelFormat and some other functions are overwritten by each other.

But that wasn't the cause, too. My solution was to enable the accelerated visuals here:

// init SDL
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_HAPTIC | SDL_INIT_TIMER) < 0) {       
    fprintf(stderr, "Could not init SDL");
    return 1;
}

// we must wish our OpenGL Version!!
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);

because in the current SDL revision (Dec 15, 2011) he checks for it in SDL_windowsopengl.c

if (_this->gl_config.accelerated >= 0) {
       *iAttr++ = WGL_ACCELERATION_ARB;
        *iAttr++ = (_this->gl_config.accelerated ? WGL_FULL_ACCELERATION_ARB :
                                                   WGL_NO_ACCELERATION_ARB);
   }

and this attribute is initialized to -1 if you did not define it on your own.

And: Never set the version attributes before initializing SDL, because settings attributes needs the video backend to be initialized properly!

I hope this helps.

Rodeo answered 15/12, 2011 at 8:48 Comment(1)
Thanks I had exactly the same problem and this worked like a charm for me.Scram
C
3

I followed this tutorial. Everything works fine on windowz and linux. http://people.cs.uct.ac.za/~aflower/tutorials.html

Cater answered 14/11, 2011 at 4:4 Comment(1)
Well, the tutorial itself didn't help me at all. However, the code it provided gave me a new SDL.dll, which was a lot different from the one I compiled myself. Using the one provided rather than my self-built one, I managed to create a 3.2 context! (However, Glew only reports success up to 3.0 for some reason). Turns out, I'm not building my SDL DLL correctly. But thanks! I'm finally on the right track, and I'll update the main post accordingly.Ireneirenic

© 2022 - 2024 — McMap. All rights reserved.