OpenGL 4.1: Triangle Flashes and Disappears Immediately after Being Drawn
<h2>I want to <strong>render a green triangle.</strong></h2>

<ul>
<li>OpenGL Version: 4.1</li>
<li>Shading Language Version: 4.10</li>
</ul>

<h2>Problem</h2>

<p>The code below, when executed, shows a green triangle that <strong>flashes for an instant and disappears</strong>. </p>

<p>I saw this post: <a href="https://stackoverflow.com/questions/18492396/a-red-rectangle-drawn-on-2d-texture-disappears-right-after-being-drawn">A red rectangle drawn on 2D texture disappears right after being drawn</a>, who has a <em>similar issue of disappearing triangles</em>, but his was because he called <code>Swap Buffer</code> multiple times, but I only have one instance of <code>glutSwapBuffers()</code> in the <code>displayFunc()</code>.</p>

<h2>C++ Code:</h2>

<pre><code>#include &lt;iostream&gt;
#include "OpenGLMatrix.h"
#include "BasicPipelineProgram.h"
using namespace std;

int windowWidth = 1280;
int windowHeight = 720;
char windowTitle[512] = "Simple Green Triangle";

// global variables
OpenGLMatrix *matrix;
GLuint buffer;
BasicPipelineProgram *pipelineProgram;
GLint program;
GLuint vao;

// objects to render
int numVertices = 3;
float positions[9] =
  { -1, -1, -1,
     1, -1, -1,
    -1,  1, -1  };   // 3 vertices of triangle to render
float colors[12] =
  { 0, 1, 0, 1,
    0, 1, 0, 1,
    0, 1, 0, 1 }; // all vertices green with alpha = 1

void bindProgram() {
  // upload model view matrix to shader
  float m[16];
  matrix-&gt;SetMatrixMode(OpenGLMatrix::ModelView);
  matrix-&gt;GetMatrix(m);
  pipelineProgram-&gt;SetModelViewMatrix(m);

  // upload projection matrix to shader
  float p[16];
  matrix-&gt;SetMatrixMode(OpenGLMatrix::Projection);
  matrix-&gt;GetMatrix(p);
  pipelineProgram-&gt;SetProjectionMatrix(p);
}

void displayFunc() {
  // computing modelview matrix
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  matrix-&gt;LoadIdentity();

  // camera at (0,0,1), looking in -z direction, up vector y
  matrix-&gt;LookAt(0, 0, 1, 0, 0, -1, 0, 1, 0); 

  bindProgram();

  // use the VAO
  pipelineProgram-&gt;Bind();
  glBindVertexArray(vao);
  GLint first = 0;
  GLsizei count = numVertices;
  glDrawArrays(GL_TRIANGLES, first, count);
  glBindVertexArray(0);

  glutSwapBuffers();
}

void idleFunc() {
  // make the screen update 
  glutPostRedisplay();
}

void reshapeFunc(int w, int h) {
  GLfloat aspect = (GLfloat) w / (GLfloat) h;
  glViewport(0, 0, w, h);

  // setup perspective matrix
  matrix-&gt;SetMatrixMode(OpenGLMatrix::Projection);
  matrix-&gt;LoadIdentity();
  matrix-&gt;Perspective(60.0, aspect, 0.01, 1000.0);
  matrix-&gt;SetMatrixMode(OpenGLMatrix::ModelView);
}

void initPipelineProgram() {
  // initialize shader pipeline program
  pipelineProgram = new BasicPipelineProgram();
  pipelineProgram-&gt;Init();
  pipelineProgram-&gt;Bind();
  program = pipelineProgram-&gt;GetProgramHandle();

  // VAO (vertex array objects) to contain the VBOs
  glGenVertexArrays(1, &amp;vao);
  glBindVertexArray(vao);
  glBindBuffer(GL_ARRAY_BUFFER, buffer);

  // get location index of the "position" shader variable
  GLuint loc = glGetAttribLocation(program, "position");
  glEnableVertexAttribArray(loc);
  const void *offset = (const void*) 0;
  GLsizei stride = 0;
  GLboolean normalized = GL_FALSE;
  glVertexAttribPointer(loc, 3, GL_FLOAT, normalized, stride, offset);

  // get location index of the "color" shader variable
  loc = glGetAttribLocation(program, "color");
  glEnableVertexAttribArray(loc);
  offset = (const void*) sizeOfPositions;
  stride = 0;
  normalized = GL_FALSE;
  glVertexAttribPointer(loc, 4, GL_FLOAT, normalized, stride, offset);

  glBindVertexArray(0);
}

void initVBO() {
  glGenBuffers(1, &amp;buffer);
  glBindBuffer(GL_ARRAY_BUFFER, buffer);
  glBufferData(GL_ARRAY_BUFFER, sizeOfPositions + sizeOfColors, NULL, GL_STATIC_DRAW);

  // upload position data
  glBufferSubData(GL_ARRAY_BUFFER, 0, sizeOfPositions, positions);

  // upload color data
  glBufferSubData(GL_ARRAY_BUFFER, sizeOfPositions, sizeOfColors, colors);
}

void initScene(int argc, char *argv[])
{
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glEnable(GL_DEPTH_TEST);
  matrix = new OpenGLMatrix();
  initVBO();
  initPipelineProgram();
}

int main(int argc, char *argv[])
{
  cout &lt;&lt; "Initializing GLUT..." &lt;&lt; endl;
  glutInit(&amp;argc,argv);

  cout &lt;&lt; "Initializing OpenGL..." &lt;&lt; endl;

  #ifdef __APPLE__
    glutInitDisplayMode(GLUT_3_2_CORE_PROFILE | GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL);
  #else
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL);
  #endif

  glutInitWindowSize(windowWidth, windowHeight);
  glutInitWindowPosition(0, 0);  
  glutCreateWindow(windowTitle);

  // glut callback functions
  glutDisplayFunc(displayFunc);
  glutIdleFunc(idleFunc);
  glutReshapeFunc(reshapeFunc);

  initScene(argc, argv);
  glutMainLoop();
}
</code></pre>

<p>In the code, <code>OpenGLMatrix *matrix</code> and <code>BasicPipelineProgram *pipelineProgram</code> are classes defined elsewhere, and whose code, unless requested, I don't think we need to worry about. <code>"position"</code> and <code>"color"</code> in <code>initPipelineProgram()</code> refer to variables in the <strong>GLSL shader specification</strong>:</p>

<pre><code>#version 150

in vec3 position;
in vec4 color;
out vec4 col;

uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;

void main()
{
  // compute the transformed and projected vertex position (into gl_Position) 
  // compute the vertex color (into col)

  gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0f);
  col = color;
}
</code></pre>

<h2>I don't understand why the triangle flashes and then immediately disappears??</h2>

<p>I'm a beginner in OpenGL and any help is greatly appreciated. Thank you!</p>