根据http://open.gl尝试使用SDL2和SDL2_image尝试使用C ++和OpenGL

一旦到达glGenTextures调用,我就会收到主题错误。我的大部分搜索都提到尚未创建gl上下文。我已经成功使用了多个gl调用。这是我主要的东西。所以

int main(int argc, const char * argv[]) {

    uniforms = std::vector<GLuint>();
    SDL_Init(SDL_INIT_VIDEO);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    SDL_Window* window = SDL_CreateWindow("OpenGL", 100, 100, 800, 600, SDL_WINDOW_OPENGL);
    SDL_GLContext context = SDL_GL_CreateContext(window);

    glewExperimental = GL_TRUE;
    glewInit();

    SDL_Event windowEvent;

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    GLuint vbo;
    glGenBuffers(1, &vbo);

    GLuint ebo;
    glGenBuffers(1, &ebo);

    setupVertices(vbo);
    setupElementBuffer(ebo);

    GLuint fragmentShader;
    GLuint vertexShader;

    GLuint shaderProgram = compileShaders(vertexShader, fragmentShader);
    bindAttributes(shaderProgram);

    GLuint tex;
    glGenTextures(GL_TEXTURE_2D, &tex);

    SDL_Surface *image = IMG_Load("pic.png");

    if (!image) {
        assert(false);
    }


    int mode;
    if (image->format->BytesPerPixel == 3) { // RGB 24bit
        mode = GL_RGB;
    } else if (image->format->BytesPerPixel == 4) { // RGBA 32bit
        mode = GL_RGBA;
    }

    glBindTexture(GL_TEXTURE_2D, tex);
    // ... etc

最佳答案

glGenTextures()将要创建的纹理名称的计数作为第一个参数,其次将指针指向足以容纳所有名称的对象。你在打电话

GLuint tex;
glGenTextures(GL_TEXTURE_2D, &tex);


因此,您使用GL_TEXTURE_2D作为计数,这是一个相当大的数字,并且GL将覆盖堆栈中tex之后的所有内容,从而导致崩溃...

07-24 13:58