SDL2 / Opengl3 can't display anything

I have some big trouble with the SDL2/Opengl. I just want to see if i can make it work, but this basic code is not running :

text.cpp:

#define GL_GLEXT_PROTOTYPES
#define GL3_PROTOTYPES 1
#include "glcorearb.h"
#include <SDL2/SDL.h>
#include <iostream>

int main(int argc, char **argv)
{
        SDL_Window* fenetre(0);
        SDL_GLContext contexteOpenGL(0);
        SDL_Event evenements;
        bool terminer(false);
        // Init SDL
        if(SDL_Init(SDL_INIT_VIDEO) < 0)
        {
                std::cout << "Erreur lors de l'initialisation de la SDL : " << SDL_GetError() << std::endl;
                SDL_Quit();
                return -1;
        }
        // OpenGL Version
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
        // Double Buffer
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
        // Window create
        fenetre = SDL_CreateWindow("Test SDL 2.0", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);
        if(fenetre == 0)
        {
                std::cout << "Erreur lors de la creation de la fenetre : " << SDL_GetError() << std::endl;
                SDL_Quit();
                return -1;
        }
        // OpenGL context
        contexteOpenGL = SDL_GL_CreateContext(fenetre);
        if(contexteOpenGL == 0)
        {
                std::cout << SDL_GetError() << std::endl;
                SDL_DestroyWindow(fenetre);
                SDL_Quit();
                return -1;
        }
        float vertices[] = {-0.5, -0.5,   0.0, 0.5,   0.5, -0.5};
        // Main loop
        while(!terminer)
        {       
                SDL_WaitEvent(&evenements);
                if(evenements.window.event == SDL_WINDOWEVENT_CLOSE)
                        terminer = true;
                // Clear
                glClear(GL_COLOR_BUFFER_BIT);
                glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
                glEnableVertexAttribArray(0);
                // Trinagle Display
                glDrawArrays(GL_TRIANGLES, 0, 3);
                glDisableVertexAttribArray(0);
                // Update 
                SDL_GL_SwapWindow(fenetre);
        }
        // On quitte la SDL
        SDL_GL_DeleteContext(contexteOpenGL);
        SDL_DestroyWindow(fenetre);
        SDL_Quit();
        return 0;
}

It's a program from a tutorial here but when i run it, i juste have a black screen. Any idea? Thanks.


  • It is incorrect to not specify the profile and create a window, as the SDL's documentation clearly states:
  • SDL_GL_CONTEXT_PROFILE_MASK determines the type of context created, while both SDL_GL_CONTEXT_MAJOR_VERSION and SDL_GL_CONTEXT_MINOR_VERSION determine which version. All three attributes must be set prior to creating the first window

    Set SDL_GL_CONTEXT_PROFILE_MASK to SDL_GL_CONTEXT_PROFILE_CORE since the tutorial you're following is geared towards shader-based OpenGL, you're supposed to create a core profile context.

  • When you've a core profile OpenGL context, there's no concept of default shaders ie atleast the vertex and fragment shaders are to be specified by you, the programmer. For details see How does the default GLSL shaders look like? for version 330.

  • You need to create and bind a VAO before using functions like glVertexAttribPointer . Some OpenGL drivers let you skip this step, nevertheless it's better to not skip this.

  • See this tutorial on how to setup the VAO, vertex and fragment shaders. Since you've only 2D points as input to the vertex shader, while the tutorial has 3D points, change the vertex shader to this:

    #version 140 core
    layout(location = 0) in vec2 vertexPosition_modelspace;
    
    void main() {
        gl_Position.xy = vertexPosition_modelspace;
        gl_Position.z = 0.0;
        gl_Position.w = 1.0;
    }
    
    链接地址: http://www.djcxy.com/p/38496.html

    上一篇: 如何加载VBO并将其呈现在单独的Java线程上?

    下一篇: SDL2 / Opengl3无法显示任何内容