title: Modern OpenGL with SDL2: How to draw a triangle
body: <p>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 <a href="http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/" rel="nofollow noreferrer">http://www.opengl-tutorial.org</a>. </p>
  
  <p><strong>My code:</strong></p>
  
  <pre><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,&amp;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, &amp;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;
  }
  </code></pre>
  
  <p>I tried to keep the code as short as possible. </p>
  
  <p><strong>Problem:</strong> 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.</p>