代码之家  ›  专栏  ›  技术社区  ›  Kevin

opengl不能在核心配置文件中使用glvertexattrib绘制

  •  0
  • Kevin  · 技术社区  · 8 年前

    我想使用opengl 3.1。 我使用的MacBook Pro有两个图形卡:NVIDIA GeForce GT 650M 1024 MB和Intel HD Graphics 4000 1536 MB。它们都支持OpenGL4.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;
    }
    

    我试着先用 glGenBuffers 我是说, glBindBuffer glBufferData 但我没能成功。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Axalo David    8 年前

    这个 Fixed Function Pipeline 已从核心OpenGL 3.1及更高版本中删除。
    你必须使用 shaders 相反。 This site 有一个很好的例子说明如何使用它们。