OpenGL 无法在核心配置文件中使用 glVertexAttrib 绘制

OpenGL cannot draw with glVertexAttrib in core profile

我想使用 OpenGL 3.1。 我正在使用带有 2 个显卡的 Macbook Pro:NVIDIA GeForce GT 650M 1024 MB 和 Intel HD Graphics 4000 1536 MB。它们都支持 OpenGL 4.1。

以前我可以画一个三角形,但是我的程序使用的是 2.1 版。因此我添加了:SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);。但是,现在三角形不再画了。

#include <string>
#include <iostream>
#include <SDL2/SDL.h>

#define GL3_PROTOTYPES 1
#include "../include/GL3/gl3.h"

int main(int argc, const char *argv[]) {
    // Initialize the SDL
    if(SDL_Init(SDL_INIT_VIDEO) < 0) {
        std::cout << "Failed to initialize the SDL: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return -1;
    }

    // Configure the SDL to use OpenGL 3.1
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);

    // ======= HERE =======
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    // ====================

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    SDL_Window* window = SDL_CreateWindow("Triangle Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);

    if (window == 0) {
        std::cout << "Error when creating the window: " << SDL_GetError() << std::endl;
        SDL_Quit();

        return -1;
    }

    // Create the OpenGL context
    SDL_GLContext contextOpenGL = SDL_GL_CreateContext(window);

    // Initialization may fail
    if (contextOpenGL == 0) {
        std::cout << SDL_GetError() << std::endl;
        SDL_DestroyWindow(window);
        SDL_Quit();
        return -1;
    }

    SDL_Event events;
    bool end = false;

    // Define the vertices of our triangle
    static const GLfloat vertices[] = {0.0,  1.0,  // left point
        -0.5, 0.0,  // right point
        0.5,  0.0}; // upper point
    const int TRIANGLE_IDX = 0;

    while(!end) {
        SDL_WaitEvent(&events);

        if(events.window.event == SDL_WINDOWEVENT_CLOSE) {
            end = true;
        }

        // Clear the screen
        glClear(GL_COLOR_BUFFER_BIT);

        // Send vertices to OpenGL
        glVertexAttribPointer(TRIANGLE_IDX, 2, GL_FLOAT, GL_FALSE, 0, vertices);

        // Activate our vertex array
        glEnableVertexAttribArray(TRIANGLE_IDX);

        // Draw the points passed previously
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glDisableVertexAttribArray(TRIANGLE_IDX);

        // Refresh the screen
        SDL_GL_SwapWindow(window);
    }
    return 0;
}

我尝试先使用 glGenBuffersglBindBufferglBufferData,但无法成功。

Fixed Function Pipeline 已从核心 OpenGL 3.1 及更高版本中删除。
你将不得不使用 shaders instead. This site 有一个很好的例子来说明如何使用它们。