Question

I'm trying to use OpenGL with SDL with the following code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <SDL2/SDL.h>
#include <GL/glew.h>

GLuint* SetupCubeBuffers(void)
{
    GLuint *buffers = NULL;

    GLfloat vertexBuffer[48] = {
        1.0, 0.0, 0.0, -1.0, 1.0, -1.0,
        1.0, 0.0, 1.0, -1.0, -1.0, -1.0,
        1.0, 1.0, 1.0, -1.0, 1.0, 1.0,
        0.0, 0.0, 1.0, -1.0, -1.0, 1.0,
        0.0, 1.0, 0.0, 1.0, 1.0, 1.0,
        0.0, 1.0, 1.0, 1.0, -1.0, 1.0,
        1.0, 1.0, 0.0, 1.0, 1.0, -1.0,
        1.0, 1.0, 1.0, 1.0, -1.0, -1.0
    };

    GLuint indexBuffer[36] = {
        0, 1, 2, 2, 1, 3,
        4, 5, 6, 6, 5, 7,
        3, 1, 5, 5, 1, 7,
        0, 2, 6, 6, 2, 4,
        6, 7, 0, 0, 7, 1,
        2, 3, 4, 4, 3, 5
    };

    glGenBuffers(2, buffers);

    glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexBuffer), vertexBuffer, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexBuffer), indexBuffer, GL_STATIC_DRAW);

    return buffers;
}

void DrawBuffers(GLuint* buffers)
{
    glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
    glVertexPointer(3, GL_FLOAT, 6 * sizeof(float), (float*)NULL + 3);
    glColorPointer(3, GL_FLOAT, 6 * sizeof(float), 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);

    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);
}

int main(int argc, char** argv)
{
    int wndHold = 1;
    SDL_Event e;
    SDL_Window *screen;
    SDL_GLContext con;
    GLenum glewStatus;
    GLuint* buffers;

    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        fprintf(stderr, "Error: %s\n\n", SDL_GetError());
    }

    screen = SDL_CreateWindow("test",
                              SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED,
                              640, 480,
                              SDL_WINDOW_OPENGL);

    con = SDL_GL_CreateContext(screen);
    glewStatus = glewInit();

    if (glewStatus != GLEW_OK)
    {
        printf("Error: %s\n\n", glewGetErrorString(glewStatus));
    }

    SDL_GL_SetSwapInterval(1);

    glClearColor(0.0, 0.0, 0.0, 1.0);

    buffers = SetupCubeBuffers();

    while (wndHold)
    {
        glClear(GL_COLOR_BUFFER_BIT);

        DrawBuffers(buffers);

        SDL_GL_SwapWindow(screen);

        SDL_WaitEvent(&e);

        if (e.type == SDL_QUIT)
        {
            wndHold = 0;
        }
    }

    SDL_GL_DeleteContext(con);
    SDL_DestroyWindow(screen);
    SDL_Quit();

    return 0;
}

But I get a "test.exe has stopped working" error when I execute the program. In debug mode, I can see that there's a segfault when I call glGenBuffers. However, glewInit() is called right after initializing GL context so I don't see what is wrong here?

I'm on Windows 8 and I'm using SDL 2.0

Était-ce utile?

La solution

you glGenBuffers function is trying to write to NULL, hence the segfault. You need to initialize your array first before attempting any write to it:

GLuint *buffers = new GLuint[2];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top