Question

I'm having a small problem with SDL2 and OpenGL. I've been trying to get VBOs working (specifically ARB objects) and have thus far been unsuccessful. Through various searches around, I've found how ancient the onboard GL.h is, and opted for glew.h instead - thus allowing me to use functions past 1.1.

Whilst attempting to use glGenBuffersARB, I found that even after calling glewInit, the functions pointer was empty.

However, visualinfo reports that OpenGL is 2.1.0, and that GL_ARB_vertex_buffer_object is able to be accessed - the SDL context seems to default to Microsoft's 1.1.0, and doesn't allow me to rectify the null pointer.

I'm (sadly) using an Intel HD Graphics chipset, on an Acer Aspire 5733.

How can I get SDL to recognize the 2.1.0 on the chipset? (Or am I completely wrong in my approach to this?)

EDIT

Here's the code that was requested:

astralmain.cpp (inits SDL + OpenGL context)

bool AstralGame::onInit()
{
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false;
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);

    if ((currWindow = SDL_CreateWindow(ASTRAL_TITLE" "ASTRAL_VERSION, 100, 100, 800, 600, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL)) == nullptr)
    {
        a_ErrorManager::panic("Window couldn't be created\n");
        return false;
    }
    if ((currContext = SDL_GL_CreateContext(currWindow)) == NULL)
    {
        a_ErrorManager::panic("OpenGl context couldn't be created\n");
    }
    printf((char*)glGetString(GL_VERSION));
    glewExperimental = TRUE;
    glewInit();
    SDL_GL_SetSwapInterval(1);
    textureManager->startUp();
    errorManager->startUp();
    renderManager->startUp();
    if (glGenBuffersARB == nullptr) printf("This entire program will not work!");
    cube = new a_GL_Cube(textureManager->getTexture("astral")->texture);
    //Start initting GL functions
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_TEXTURE_2D);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    cube->buildVert(1.0f);
    return true;

}

a_GL3DPrimitives.cpp (builds vertices for cubes, error occurs)

void a_GL_Cube::buildVert(float size)
{
    float pVt[72] = { -size, -size, size, -size, size, size, size, size, size, size, -size, size,
        size, -size, size, size, -size, -size, size, size, -size, size, size, size,
        size, size, size, -size, size, size, -size, size, -size, size, size, -size,
        size, size, -size, size, -size, -size, -size, -size, -size, -size, size, -size,
        -size, size, size, -size, size, -size, -size, -size, -size, -size, -size, size,
        -size, -size, size, -size, -size, -size, size, -size, -size, size, -size, size };
    float pTt[48] = { 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
        0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
        0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
        0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
        0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
        0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0 };

    vboVName = 0;
    vboTName = 0;

    unsigned int* test = &vboVName;
    glGenBuffersARB(1, &vboVName); <======= Null Pointer
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboVName);
    glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(float)* 72, pVt, GL_STATIC_DRAW_ARB);

    glGenBuffersARB(1, &vboTName);
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboTName);
    glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(float)* 48, pTt, GL_STATIC_DRAW_ARB);

    delete[] pVt;
    delete[] pTt;
}
Was it helpful?

Solution 2

Things to try:

  1. Remove all your SDL_GL_SetAttribute() calls. You may be over-constraining the pixel format. Add them back in one at a time to figure out where you overstepped.

  2. Remove only the SDL_GL_CONTEXT_* calls since you aren't trying to request a GL 3.x context.

OTHER TIPS

I think you've misunderstood the purpose of glGetString(GL_VERSION)

First of all you of course need to create an OpenGL Context before you can even call glGetString(). When you're creating an OpenGL Context, you specify the version of OpenGL you want to use.

Then when you call glGetString(GL_VERSION) it will return the OpenGL version of the current OpenGL Context in use.

Extra

As I can see you tagged your question you can use GLEW to see which OpenGL versions your computer supports.

std::cout << "OpenGL 2.0: " << ((GLEW_VERSION_2_0 != 0) ? "Available" : "Unavailable") << std::endl;
std::cout << "OpenGL 2.1: " << ((GLEW_VERSION_2_1 != 0) ? "Available" : "Unavailable") << std::endl;

std::cout << "OpenGL 3.0: " << ((GLEW_VERSION_3_0 != 0) ? "Available" : "Unavailable") << std::endl;
std::cout << "OpenGL 3.1: " << ((GLEW_VERSION_3_1 != 0) ? "Available" : "Unavailable") << std::endl;
std::cout << "OpenGL 3.2: " << ((GLEW_VERSION_3_2 != 0) ? "Available" : "Unavailable") << std::endl;
std::cout << "OpenGL 3.3: " << ((GLEW_VERSION_3_3 != 0) ? "Available" : "Unavailable") << std::endl;

std::cout << "OpenGL 4.0: " << ((GLEW_VERSION_4_0 != 0) ? "Available" : "Unavailable") << std::endl;
std::cout << "OpenGL 4.1: " << ((GLEW_VERSION_4_1 != 0) ? "Available" : "Unavailable") << std::endl;
std::cout << "OpenGL 4.2: " << ((GLEW_VERSION_4_3 != 0) ? "Available" : "Unavailable") << std::endl;
std::cout << "OpenGL 4.3: " << ((GLEW_VERSION_4_3 != 0) ? "Available" : "Unavailable") << std::endl;
std::cout << "OpenGL 4.4: " << ((GLEW_VERSION_4_4 != 0) ? "Available" : "Unavailable") << std::endl;

Be aware that you of course need to have created an OpenGL Context and called glewInit() before you can do the above.

I'm not sure about the following, but also if you do something like the above and it outputs "Unavailable", when you for sure know it's Available, then it might have something to do with the OpenGL version the current OpenGL Context is using.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top