title: OpenGL: creat a colored triangle
body: <p>I am learning OpenGL recently, and I want to draw a triangle with red, green, blue at each point. However, when I work with the following codes, it only displays a blank white window. I can't find where is the error. I follow this <a href="https://open.gl/drawing" rel="nofollow">web site</a> and the red book.</p>
  
  <pre><code>#include &lt;GL/glew.h&gt;
  #include &lt;GL/freeglut.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  const int WIDTH = 300;
  const int HEIGHT = 300;
  
  enum VAO_IDs {Triangles,NumVAOs};
  enum Buffer_IDs {ArrayBuffer,NumBuffers};
  enum Attrib_IDs{vVertex,vColor};
  
  GLuint VAOs[NumVAOs];
  GLuint Buffers[NumBuffers];
  static const GLchar* vertexSource[] = {
      "#version 330\n"
      "in vec3 position;\n"
      "in vec3 color;\n"
  
      "out vec3 Color;\n"
  
      "void main()\n"
      "{\n"
      "   Color = color;\n"
      "   gl_Position = vec4(position, 1.0);\n"
      "}\n" };
  
  static const GLchar* fragmentSource[] = {
      "#version 330\n"
  
      "in vec3 Color;\n"
  
      "out vec4 outColor;\n"
  
      "void main()\n"
      "{"
      "   outColor = vec4(Color, 1.0);\n"
      "}" };
  
  
  
  
  
  void OnInit() {
  
      //Generate vertex objects 
      //Bind vertex objects
      glGenVertexArrays(NumVAOs, VAOs);
      glBindVertexArray(VAOs[Triangles]);
  
      //Input vertices information
      GLfloat vertices[] = {
           0.0f,  0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // Vertex 1: Red
           0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,// Vertex 2: Green
          -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f// Vertex 3: Blue
      };
  
      //Generate Buffer object
      //Bind Buffer object
      //Bind Buffer data
      glGenBuffers(NumBuffers,Buffers);
      glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  
      //Generate vertexshader
      //Bind vertexshader source
      //Compile vertexshader
      GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(vertexShader, 1, vertexSource, NULL);
      glCompileShader(vertexShader);
      //debug
      GLint vertexShader_status;
      glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &amp;vertexShader_status);
      if (!vertexShader_status){
          GLsizei log_length = 0;
          GLchar message[1024];
          glGetShaderInfoLog(vertexShader, 1024, &amp;log_length, message);
          std::cout &lt;&lt; message &lt;&lt; std::endl;
      }
  
      //Generate fragmentshader
      //Bind fragmentshader source
      //Compile fragmentshader
      GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
      glShaderSource(fragmentShader, 1, fragmentSource, NULL);
      GLint fragmentShader_status;
      glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &amp;fragmentShader_status);
      if (!fragmentShader_status){
          GLsizei log_length = 0;
          GLchar message[1024];
          glGetShaderInfoLog(vertexShader, 1024, &amp;log_length, message);
          std::cout &lt;&lt; message &lt;&lt; std::endl;
      }
  
      //Generate program
      //Attach attributes to the program
      GLuint shaderProgram = glCreateProgram();
      glAttachShader(shaderProgram, vertexShader);
      glAttachShader(shaderProgram, fragmentShader);
      glLinkProgram(shaderProgram);
      glUseProgram(shaderProgram);
  
      //Figure out position attributes information
      GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
      cout &lt;&lt; "vPosition: " &lt;&lt; posAttrib &lt;&lt; endl;
      glEnableVertexAttribArray(posAttrib);
      glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE,
          6 * sizeof(GLfloat), 0);
  
      //Figure out color attributes information
      GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
      cout &lt;&lt; "vColor: " &lt;&lt; colAttrib &lt;&lt; endl;
      glEnableVertexAttribArray(colAttrib);
      glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE,
          6 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
  
      cout &lt;&lt; "Initialization successfull" &lt;&lt; endl;
  }
  void display(void)
  {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glBindVertexArray(VAOs[Triangles]);
      glDrawArrays(GL_TRIANGLES, 0, 3);
      glFlush();
      cout &lt;&lt; "Display successfull" &lt;&lt; endl;
  
  }
  void OnShutdown() {
      cout &lt;&lt; "Shutdown successfull" &lt;&lt; endl;
  }
  void OnResize(int nw, int nh) {
  }
  
  int main(int argc, char** argv) {
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE |
          GLUT_RGBA);
      glutInitContextVersion(3, 3);
      glutInitContextFlags(GLUT_CORE_PROFILE | GLUT_DEBUG);
      glutInitContextProfile(GLUT_FORWARD_COMPATIBLE);
      glutInitWindowSize(WIDTH, HEIGHT);
      glutInitContextFlags(GLUT_CORE_PROFILE | GLUT_DEBUG);
      glutInitContextProfile(GLUT_FORWARD_COMPATIBLE);
      glutInitWindowSize(WIDTH, HEIGHT);
      glutCreateWindow("Getting started with OpenGL 3.3");
      glewExperimental = GL_TRUE;
      GLenum err = glewInit();
      if (GLEW_OK != err){
          cerr &lt;&lt; "Error: " &lt;&lt; glewGetErrorString(err) &lt;&lt; endl;
      }
      else {
          if (GLEW_VERSION_3_3)
          {
              cout &lt;&lt; "Driver supports OpenGL 3.3\nDetails:" &lt;&lt; endl;
          }
      }
      cout &lt;&lt; "\tUsing glew " &lt;&lt; glewGetString(GLEW_VERSION) &lt;&lt; endl;
      cout &lt;&lt; "\tVendor: " &lt;&lt; glGetString(GL_VENDOR) &lt;&lt; endl;
      cout &lt;&lt; "\tRenderer: " &lt;&lt; glGetString(GL_RENDERER) &lt;&lt; endl;
      cout &lt;&lt; "\tVersion: " &lt;&lt; glGetString(GL_VERSION) &lt;&lt; endl;
      cout &lt;&lt; "\tGLSL:"&lt;&lt;glGetString(GL_SHADING_LANGUAGE_VERSION)&lt;&lt;endl;
      OnInit();
      glutDisplayFunc(display);
      glutReshapeFunc(OnResize);
      glutMainLoop();
      return 0;
  }
  </code></pre>