title: I can't color my OpenGL circle properly
body: <p>I am supposed to draw 6 different 2D shapes (dot, line, triangle, square, star, circle) and assign them each a different color. I managed to draw all of them and color them, except for my circle. The color i gave (g_colors_circle) the circle however is drawn from the center of the circle but does not reach the edges. Here is the current output:</p>
  
  <p><a href="https://i.stack.imgur.com/wSqFM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wSqFM.png" alt="output"></a>
  <br></p>
  
  <p>Can someone tell me how to fix it?</p>
  
  <p>Here is my code:</p>
  
  <p><code>SimpleTriangle.cpp</code>:</p>
  
  <pre><code>#include &lt;cstdio&gt;       // for C++ i/o
  #include &lt;iostream&gt;
  using namespace std;    // to avoid having to use std::
  
  #define GLEW_STATIC     // include GLEW as a static library
  #include &lt;GLEW/glew.h&gt;  // include GLEW
  #include &lt;GLFW/glfw3.h&gt; // include GLFW (which includes the OpenGL header)
  #include &lt;glm/glm.hpp&gt;  // include GLM (ideally should only use the GLM headers that are actually used)
  using namespace glm;    // to avoid having to use glm::
  
  #include "shader.h"
  
  /*-------------------- Circle Code --------------------*/
  
  //Constants for Circle
  #define PI 3.14159265
  #define MAX_SLICES 32
  #define MIN_SLICES 8
  #define MAX_VERTICES (MAX_SLICES+2)*3   // a triangle fan should have a minimum of 3 vertices
  #define CIRCLE_RADIUS 0.5
  
  GLuint g_VBO_circle[2];             // identifiers
  GLuint g_VAO_circle = 0;
  
  //Vertices for the circle
  GLfloat g_vertices_circle[MAX_VERTICES] = {
      0.0f, 0.0f, 0.0f,
      0.0f, 0.0f, 0.0f,
  };
  
  GLfloat g_colors_circle[] = {
      1.0f, 1.0f, 0.0f,
      1.0f, 1.0f, 0.0f
  };
  
  GLuint g_slices = MIN_SLICES;   // number of circle slices
  
  //raz: i think this generates the vertice values for array of the circle
  void generate_circle()
  {
      float angle = PI * 2 / static_cast&lt;float&gt;(g_slices);    // used to generate x and y coordinates
      float scale_factor = static_cast&lt;float&gt;(768) / 1024;    // scale to make it a circle instead of an elipse
      int index = 0;  // vertex index
  
      g_vertices_circle[3] = CIRCLE_RADIUS * scale_factor;    // set x coordinate of vertex 1
  
                                                      // generate vertex coordinates for triangle fan
      for (int i = 2; i &lt; g_slices + 2; i++)
      {
          // multiply by 3 because a vertex has x, y, z coordinates
          index = i * 3;
  
          g_vertices_circle[index] = CIRCLE_RADIUS * cos(angle) * scale_factor;
          g_vertices_circle[index + 1] = CIRCLE_RADIUS * sin(angle);
          g_vertices_circle[index + 2] = 0.0f;
  
          // update to next angle
          angle += PI * 2 / static_cast&lt;float&gt;(g_slices);
      }
  }
  
  static void init_circle()
  {
      // generate vertices of triangle fan
      generate_circle();
  
      // create VBO (vertice positions) and buffer the data
      glGenBuffers(2, g_VBO_circle);
      glBindBuffer(GL_ARRAY_BUFFER, g_VBO_circle[0]);
      glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * (g_slices + 2), g_vertices_circle, GL_DYNAMIC_DRAW);
      // create VBO (vertice color) and buffer the data
      glBindBuffer(GL_ARRAY_BUFFER, g_VBO_circle[1]);
      glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * (g_slices + 2), g_colors_circle, GL_STATIC_DRAW);
  
      // create VAO and specify VBO data
      glGenVertexArrays(1, &amp;g_VAO_circle);
      glBindVertexArray(g_VAO_circle);
      glBindBuffer(GL_ARRAY_BUFFER, g_VBO_circle[0]);
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);  // specify the form of the data 
      glBindBuffer(GL_ARRAY_BUFFER, g_VBO_circle[1]);
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);  // specify the form of the data
  
      glEnableVertexAttribArray(0);   // enable vertex attributes
      glEnableVertexAttribArray(1);
  }
  
  /*---------------------------------------------------------*/
  
  // global variables
  GLuint g_VBO[2];            // vertex buffer object identifier
  GLuint g_VAO = 0;           // vertex array object identifier
  GLuint g_shaderProgramID = 0;       // shader program identifier
  
  static void init()
  {
      glClearColor(0.0, 0.0, 0.0, 1.0);   // set clear background colour
  
      // create and compile our GLSL program from the shader files
      g_shaderProgramID = loadShaders("SimpleVS.vert", "SimpleFS.frag");
  
      // enable point size
      glEnable(GL_PROGRAM_POINT_SIZE);
      // set line width
      glLineWidth(5.0);
  
      GLfloat verticesPosition[] = {
  
          //Dot
          -0.7f, 0.7f, 0.0f,
          //Line
          -0.5f, 0.7f, 0.0f,
          -0.2f, 0.7f, 0.0f,
          //Triangle  
          0.2f, 0.75f, 0.0f,
          0.0f, 0.4f, 0.0f,
          0.4f, 0.4f, 0.0f,
          //Star 
          0.0f, 0.2f, 0.0f,
          0.1f, 0.1f, 0.0f,
          0.2f, 0.05f, 0.0f,
          0.1f, 0.0f, 0.0f,
          0.2f, -0.1f, 0.0f,
          0.0f, 0.0f, 0.0f,
          -0.2f, -0.1f, 0.0f,
          -0.1f, 0.0f, 0.0f,
          -0.2f, 0.05f, 0.0f,
          -0.1f, 0.1f, 0.0f,
          //Rectangle
          -0.8f, 0.4f, 0.0f,
          -0.8f, 0.0f, 0.0f,
          -0.3f, 0.4f, 0.0f,
          -0.3f, 0.0f, 0.0f
  
      };
  
      GLfloat verticesColor[] = {
          //Dot
          1.0f, 1.0f, 1.0f,
          //Line
          0.5f, 0.0f, 0.0f,
          0.5f, 0.0f, 0.0f,
          //Triangle
          0.0f, 1.0f, 1.0f,
          0.0f, 1.0f, 1.0f,
          0.0f, 1.0f, 1.0f,
          //Star
          0.0f, 1.0f, 0.0f,
          0.0f, 1.0f, 0.0f,
          0.0f, 1.0f, 0.0f,
          0.0f, 1.0f, 0.0f,
          0.0f, 1.0f, 0.0f,
          0.0f, 1.0f, 0.0f,
          0.0f, 1.0f, 0.0f,
          0.0f, 1.0f, 0.0f,
          0.0f, 1.0f, 0.0f,
          0.0f, 1.0f, 0.0f,
          //Rectangle
          0.0f, 1.0f, 0.0f,
          0.0f, 1.0f, 0.0f,
          0.0f, 1.0f, 0.0f,
          0.0f, 1.0f, 0.0f,
      };
  
      // create VBO and buffer the data
      glGenBuffers(2, g_VBO);
      glBindBuffer(GL_ARRAY_BUFFER, g_VBO[0]);        // bind the VBO
      glBufferData(GL_ARRAY_BUFFER, sizeof(verticesPosition), verticesPosition, GL_STATIC_DRAW);      // copy data to buffer
      glBindBuffer(GL_ARRAY_BUFFER, g_VBO[1]);        // bind the VBO
      glBufferData(GL_ARRAY_BUFFER, sizeof(verticesColor), verticesColor, GL_STATIC_DRAW);        // copy data to buffer
  
      // create VAO and specify VBO data
      glGenVertexArrays(1, &amp;g_VAO);
      glBindVertexArray(g_VAO);
      glBindBuffer(GL_ARRAY_BUFFER, g_VBO[0]);        // bind the VBO
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);      // specify the form of the data
      glBindBuffer(GL_ARRAY_BUFFER, g_VBO[1]);        // bind the VBO
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);      // specify the form of the data
  
      glEnableVertexAttribArray(0);   // enable vertex attributes
      glEnableVertexAttribArray(1);
  }
  
  // function used to render the scene
  static void render_scene()
  {
      glClear(GL_COLOR_BUFFER_BIT);   // clear colour buffer
  
      glUseProgram(g_shaderProgramID);    // use the shaders associated with the shader program
  
      glBindVertexArray(g_VAO);           // make VAO active
  
      glDrawArrays(GL_POINTS, 0, 1);          //Draw dot 
      glDrawArrays(GL_LINES, 1, 2);           //Draw line
      glDrawArrays(GL_TRIANGLES, 3, 3);       //Draw triangle
      glDrawArrays(GL_LINE_LOOP, 6, 10);      //Draw star
      glDrawArrays(GL_TRIANGLE_STRIP, 16, 4); //Draw rectangle
  
      glFlush();  // flush the pipeline
  
      //To draw circle
      glBindVertexArray(g_VAO_circle);            // make VAO for circle active
      glDrawArrays(GL_TRIANGLE_FAN, 0, g_slices + 2); // display the vertices based on the primitive type
  
      glFlush();  // flush the pipeline
  }
  
  // key press or release callback function
  static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  {
      // quit if the ESCAPE key was press
      if(key == GLFW_KEY_ESCAPE &amp;&amp; action == GLFW_PRESS) 
      {
          // set flag to close the window
          glfwSetWindowShouldClose(window, GL_TRUE);
          return;
      }
      else if (key == GLFW_KEY_W &amp;&amp; action == GLFW_PRESS)
      {
          // renderer using wireframe
          glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
          return;
      }
      else if (key == GLFW_KEY_S &amp;&amp; action == GLFW_PRESS)
      {
          // renderer using wireframe
          glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
          return;
      }
      else if (key == GLFW_KEY_UP &amp;&amp; action == GLFW_PRESS)
      {
          if (g_slices &lt; MAX_SLICES)
          {
              g_slices++; // increment number of slices
  
                          // generate vertices of triangle fan
              generate_circle();
  
              // bind and copy data to GPU
              glBindBuffer(GL_ARRAY_BUFFER, g_VBO_circle[0]);
              glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * (g_slices + 2), g_vertices_circle, GL_DYNAMIC_DRAW);
          }
          return;
      }
      else if (key == GLFW_KEY_DOWN &amp;&amp; action == GLFW_PRESS)
      {
          if (g_slices &gt; MIN_SLICES)
          {
              g_slices--; // decrement number of slices
  
                          // generate vertices of triangle fan
              generate_circle();
  
              // bind and copy data to GPU
              glBindBuffer(GL_ARRAY_BUFFER, g_VBO_circle[0]);
              glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * (g_slices + 2), g_vertices_circle, GL_DYNAMIC_DRAW);
              return;
          }
      }
  }
  
  // error callback function
  static void error_callback(int error, const char* description)
  {
      cerr &lt;&lt; description &lt;&lt; endl;    // output error description
  }
  
  int main(void)
  {
      GLFWwindow* window = NULL;  // pointer to a GLFW window handle
  
      glfwSetErrorCallback(error_callback);   // set error callback function
  
      // initialise GLFW
      if(!glfwInit()) 
      {
          // if failed to initialise GLFW
          exit(EXIT_FAILURE);
      }
  
      // minimum OpenGL version 3.3
      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  
      // create a window and its OpenGL context
      window = glfwCreateWindow(1024, 768, "Assignment 1", NULL, NULL);
  
      // if failed to create window
      if(window == NULL)
      {
          glfwTerminate();
          exit(EXIT_FAILURE);
      }
  
      glfwMakeContextCurrent(window); // set window context as the current context
      glfwSwapInterval(1);            // swap buffer interval
  
      // initialise GLEW
      if(glewInit() != GLEW_OK) 
      {
          // if failed to initialise GLEW
          cerr &lt;&lt; "GLEW initialisation failed" &lt;&lt; endl;
          exit(EXIT_FAILURE);
      }
  
      // set key callback function
      glfwSetKeyCallback(window, key_callback);
  
      // initialise rendering states
      init();
      init_circle();
  
      // the rendering loop
      while(!glfwWindowShouldClose(window))       
      {
          render_scene();             // render the scene
  
          glfwSwapBuffers(window);    // swap buffers
          glfwPollEvents();           // poll for events
      }
  
      // clean up
      glDeleteProgram(g_shaderProgramID);
      glDeleteBuffers(1, g_VBO);
      glDeleteVertexArrays(1, &amp;g_VAO);
      glDeleteBuffers(1, g_VBO_circle);
      glDeleteVertexArrays(1, &amp;g_VAO_circle);
  
      // close the window and terminate GLFW
      glfwDestroyWindow(window);
      glfwTerminate();
  
      exit(EXIT_SUCCESS);
  }
  </code></pre>
  
  <p><code>SimpleVS.vert</code>:</p>
  
  <pre><code>#version 330 core
  
  // input data (different for all executions of this shader)
  layout(location = 0) in vec3 aPosition;
  layout(location = 1) in vec3 aColor;
  
  // output data (will be interpolated for each fragment)
  out vec3 vColor;        //raz: this output is passed as input to the fragment shader
  
  void main()
  {
      // set point size
      gl_PointSize = 10.0;
  
      // set vertex position
      gl_Position = vec4(aPosition, 1.0);
  
      // the color of each vertex will be interpolated
      // to produce the color of each fragment
      vColor = aColor;
  }
  </code></pre>
  
  <p><code>SimpleFS.frag</code>:</p>
  
  <pre><code>#version 330 core
  
  // interpolated values from the vertex shaders
  in vec3 vColor;
  
  // output data
  out vec3 fColor;
  
  void main()
  {
      // set output color
      fColor = vColor;
  }
  </code></pre>