title: C++ OpenGL rendering two rectangles with different colors
body: <p>I have tried to create two rectangle objects with two different colors and moving in opposite direction. However, I am stuck in rendering the two rectangle with different colors (currently they are the same color).</p>
  
  <p>What is the way to do it?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #include &lt;math.h&gt;
  #include &lt;GL/glew.h&gt;
  #include &lt;GL/freeglut.h&gt;
  
  #include "ogldev_util.h"
  #include "ogldev_math_3d.h"
  
  GLuint VBO1;
  GLuint VBO2;
  GLuint gScaleLocation1;
  GLuint gScaleLocation2;
  
  const char* pVSFileName = "shader.vs";
  const char* pFSFileName = "shader.fs";
  
  
  static void RenderSceneCB()
  {
      glClear(GL_COLOR_BUFFER_BIT);
  
  
      static float Scale1 = 0.0f;
  
      Scale1 += 0.01f;
  
      glUniform1f(gScaleLocation1, sinf(Scale1));
  
      glEnableVertexAttribArray(0);
      glBindBuffer(GL_ARRAY_BUFFER, VBO1);
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
      glDrawArrays(GL_TRIANGLES, 0, 6);
  
      static float Scale2 = 0.0f;
  
      Scale2 -= 0.01f;
  
      glUniform1f(gScaleLocation2, sinf(Scale2));
  
      glEnableVertexAttribArray(0);
      glBindBuffer(GL_ARRAY_BUFFER, VBO2);
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
      glDrawArrays(GL_TRIANGLES, 0, 6);
  
      glDisableVertexAttribArray(0);
  
      glutSwapBuffers();
  }
  
  
  static void InitializeGlutCallbacks()
  {
      glutDisplayFunc(RenderSceneCB);
      glutIdleFunc(RenderSceneCB);
  }
  
  static void CreateVertexBuffer()
  {
      Vector3f Vertices[6];
      Vertices[0] = Vector3f(1.0f, -0.5f, 0.0f);
      Vertices[1] = Vector3f(1.0f, 0.5f, 0.0f);
      Vertices[2] = Vector3f(3.0f, -0.5f, 0.0f);
  
      Vertices[3] = Vector3f(1.0f, 0.5f, 0.0f);
      Vertices[4] = Vector3f(3.0f, 0.5f, 0.0f);
      Vertices[5] = Vector3f(3.0f, -0.5f, 0.0f);
  
      glGenBuffers(1, &amp;VBO1);
      glBindBuffer(GL_ARRAY_BUFFER, VBO1);
      glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
  
      Vector3f Vertices2[6];
      Vertices2[0] = Vector3f(-3.0f, -0.5f, 0.0f);
      Vertices2[1] = Vector3f(-3.0f, 0.5f, 0.0f);
      Vertices2[2] = Vector3f(-1.0f, -0.5f, 0.0f);
  
      Vertices2[3] = Vector3f(-3.0f, 0.5f, 0.0f);
      Vertices2[4] = Vector3f(-1.0f, 0.5f, 0.0f);
      Vertices2[5] = Vector3f(-1.0f, -0.5f, 0.0f);
  
      glGenBuffers(1, &amp;VBO2);
      glBindBuffer(GL_ARRAY_BUFFER, VBO2);
      glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices2), Vertices2, GL_STATIC_DRAW);
  }
  
  static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType)
  {
      GLuint ShaderObj = glCreateShader(ShaderType);
  
      if (ShaderObj == 0) {
          fprintf(stderr, "Error creating shader type %d\n", ShaderType);
          exit(1);
      }
  
      const GLchar* p[1];
      p[0] = pShaderText;
      GLint Lengths[1];
      Lengths[0]= strlen(pShaderText);
      glShaderSource(ShaderObj, 1, p, Lengths);
      glCompileShader(ShaderObj);
      GLint success;
      glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &amp;success);
      if (!success) {
          GLchar InfoLog[1024];
          glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog);
          fprintf(stderr, "Error compiling shader type %d: '%s'\n", ShaderType, InfoLog);
          exit(1);
      }
  
      glAttachShader(ShaderProgram, ShaderObj);
  }
  
  static void CompileShaders()
  {
      GLuint ShaderProgram = glCreateProgram();
  
      if (ShaderProgram == 0) {
          fprintf(stderr, "Error creating shader program\n");
          exit(1);
      }
  
      string vs, fs;
  
      if (!ReadFile(pVSFileName, vs)) {
          exit(1);
      };
  
      if (!ReadFile(pFSFileName, fs)) {
          exit(1);
      };
  
      AddShader(ShaderProgram, vs.c_str(), GL_VERTEX_SHADER);
      AddShader(ShaderProgram, fs.c_str(), GL_FRAGMENT_SHADER);
  
      GLint Success = 0;
      GLchar ErrorLog[1024] = { 0 };
  
      glLinkProgram(ShaderProgram);
      glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &amp;Success);
      if (Success == 0) {
          glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
          fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog);
          exit(1);
      }
  
      glValidateProgram(ShaderProgram);
      glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &amp;Success);
      if (!Success) {
          glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
          fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog);
          exit(1);
      }
  
      glUseProgram(ShaderProgram);
  
      gScaleLocation1 = glGetUniformLocation(ShaderProgram, "gScale");
      assert(gScaleLocation1 != 0xFFFFFFFF);
  }
  
  
  int main(int argc, char** argv)
  {
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
      glutInitWindowSize(1024, 768);
      glutInitWindowPosition(100, 100);
      glutCreateWindow("Tutorial 05");
  
      InitializeGlutCallbacks();
  
      // Must be done after glut is initialized!
      GLenum res = glewInit();
      if (res != GLEW_OK) {
        fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
        return 1;
      }
  
      printf("GL version: %s\n", glGetString(GL_VERSION));
  
      glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  
      CreateVertexBuffer();
  
      CompileShaders();
  
      glutMainLoop();
  
      return 0;
  }
  </code></pre>
  
  <p>Fragment Shader</p>
  
  <pre><code>#version 330
  
  out vec4 FragColor;
  
  void main()
  {
      FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  }
  </code></pre>