0

I am using SDL2 and my intention is to draw a simple triangle using modern OpenGL. Using modern OpenGL means not to use deprecated functions such as glBegin/glEnd etc. My tutorial source is http://www.opengl-tutorial.org.

My code:

int main(int ac, char** av) {
    SDL_Init(SDL_INIT_EVERYTHING);

    // Create SDL window with OpenGL support
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 
    SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
    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_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    SDL_Window *pWindow = SDL_CreateWindow("SDL2 OpenGL", 
                                       SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                                       600,
                                       400, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);

    // I do not know if required, but I create a GL context before calling 'gl3wInit'
    glcontext = SDL_GL_CreateContext(pWindow);
    gl3wInit();

    SDL_GL_SetSwapInterval(1);

    // I do not know if required, but I generate an initial array as done in many tutorials?
    GLuint vertexArrId;
    glGenVertexArrays(1,&vertexArrId);
    glBindVertexArray(vertexArrId);


    // My actual triangle consisting of 3 vertices
    GLfloat arrPos[] = {
        -1.0,-1.0,0.0,
        1.0,-1.0,0.0,
        0.0,1.0,0.0
    };

    // Generate buffer and store buffer id for my triangle data
    GLuint myBufferId;
    glGenBuffers(1, &myBufferId);
    glBindBuffer(GL_ARRAY_BUFFER, myBufferId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(arrPos), arrPos, GL_STATIC_DRAW);

    // Tell open GL to use this buffer
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, myBufferId);
    glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 0, (void*)0);

    bool green = false; // Toggle background clearing color between green and blue
    while(true)
    {
        green ? glClearColor(0.0,0.5,0.1,1.0) : glClearColor(0.0,0.,0.5,1.0);        
        green ^= true;            

        glClear(GL_COLOR_BUFFER_BIT);

        glDrawArrays(GL_TRIANGLES, 0, 3); // 3: Three vertices. Should draw the triangle?

        SDL_GL_SwapWindow(pWindow);
    }
    return 0;
}

I tried to keep the code as short as possible.

Problem: The triangle is not visible. The output of my program is a green or blue background since the background color is toggled each frame, so I assume OpenGL works at least.

Anonymous
  • 4,617
  • 9
  • 48
  • 61
  • 4
    The example in the link you provided installs fragment and vertex shader. I missed this in your sample code. That's probably the reason why you cannot see your triangle. – Scheff's Cat Mar 26 '18 at 05:40
  • I read it like the triangle will be white if you are not using any shaders. But it should still be visible? – Anonymous Mar 26 '18 at 06:06
  • I'm not sure about this as I never tried to render geometry without any shader... ;-) – Scheff's Cat Mar 26 '18 at 06:12
  • 2
    ...but I found this on Khronos: [OpenGL default shaders?](https://www.opengl.org/discussion_boards/showthread.php/174807-OpenGL-default-shaders). – Scheff's Cat Mar 26 '18 at 06:13
  • I will try to add basic shader stuff and edit my question afterwards – Anonymous Mar 26 '18 at 06:25
  • 2
    It worked after adding the example fragment and vertex shaders from the tutorial. Tutorial description was a bit unclear to me... Thanks for helping me, Scheff – Anonymous Mar 26 '18 at 06:51

0 Answers0