Search code examples
c++opengl-3

OpenGL cannot draw with glVertexAttrib in core profile


I want to use OpenGL 3.1. I'm using a Macbook Pro with 2 graphic cards: NVIDIA GeForce GT 650M 1024 MB, and Intel HD Graphics 4000 1536 MB. They both support up to OpenGL 4.1.

Previously i was able to draw a triangle however, my program was using version 2.1. Therefore I added: SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);. However, now the triangle is no longer drawn.

#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;
}

I tried to first use glGenBuffers, glBindBuffer and glBufferData but i could not manage to make it work.


Solution

  • The Fixed Function Pipeline has been removed from core OpenGL 3.1 and above.
    You will have to use shaders instead. This site has a nice example of how to use them.