title: Rendering the mandelbrot set in OpenGl
body: <p>I have to render the mandelbrot set and I was wondering if someone could point out some flaws with my code - at the moment, the output windows just shows a black screen. I think that my mandelbrot mathematics are correct, because I have used the same code to output a .tga of the mandelbrot - is it something to do with the OpenGl method I am using to output the pixels?</p>
  
  <p>Full code:</p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;GL\glew.h&gt;
  #include &lt;GL\freeglut.h&gt;
  #include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;chrono&gt;
  #include &lt;cstdint&gt;
  #include &lt;cstdlib&gt;
  #include &lt;complex&gt;
  #include &lt;fstream&gt;
  #include &lt;thread&gt;
  #include &lt;mutex&gt;
  #include &lt;vector&gt;
  #include &lt;Windows.h&gt;
  
  // Import things we need from the standard library
  using std::chrono::duration_cast;
  using std::chrono::milliseconds;
  using std::complex;
  using std::cout;
  using std::endl;
  using std::ofstream;
  // ...other useful includes
  using std::cout;
  using std::endl;
  using std::thread;
  using std::mutex;
  using std::lock;
  using std::unique_lock;
  using std::vector;
  
  const int width = 600, height = 600; // window size
  int windowID;
  
  // The number of times to iterate before we assume that a point isn't in the
  // Mandelbrot set.
  // (You may need to turn this up if you zoom further into the set.)
  const int MAX_ITERATIONS = 500;
  
  bool fullScreen = false;
  bool need_to_draw = true;
  
  //****************************************
  
  // Render the Mandelbrot set into the image array.
  // The parameters specify the region on the complex plane to plot.
  void compute_mandelbrot(double left, double right, double top, double bottom)
  {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the screen buffer
      glBegin(GL_POINTS); // start drawing in single pixel mode
      for (int y = 0; y &lt; height; ++y)
      {
          for (int x = 0; x &lt; width; ++x)
          {
              // Work out the point in the complex plane that
              // corresponds to this pixel in the output image.
              complex&lt;double&gt; c(left + (x * (right - left) / width),
                  top + (y * (bottom - top) / height));
  
              // Start off z at (0, 0).
              complex&lt;double&gt; z(0.0, 0.0);
  
              // Iterate z = z^2 + c until z moves more than 2 units
              // away from (0, 0), or we've iterated too many times.
              int iterations = 0;
              while (abs(z) &lt; 2.0 &amp;&amp; iterations &lt; MAX_ITERATIONS)
              {
                  z = (z * z) + c;
  
                  ++iterations;
              }
  
              if (iterations == MAX_ITERATIONS)
              {
                  glColor3f(1.0, 0.0, 0.0); // Set color to draw mandelbrot
                  // z didn't escape from the circle.
                  // This point is in the Mandelbrot set.
                  glVertex2i(x, y);
              }
              else
              {
                  glColor3f(0.0, 0.0, 0.0); //Set pixel to black
                  // z escaped within less than MAX_ITERATIONS
                  // iterations. This point isn't in the set.
                  glVertex2i(x, y);
              }
          }
      }
      glEnd();
      glutSwapBuffers();
      need_to_draw = false;
  }
  
  int main(int argc, char** argv) 
  {
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
      glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      GLsizei windowX = (glutGet(GLUT_SCREEN_WIDTH) - width) / 2;
      GLsizei windowY = (glutGet(GLUT_SCREEN_HEIGHT) - height) / 2;
      glutInitWindowPosition(windowX, windowY);
      glutInitWindowSize(width, height);
      windowID = glutCreateWindow("Mandelbrot");
  
      if (need_to_draw)
      {
          compute_mandelbrot(-2.0, 1.0, 1.125, -1.125);
      }
  
      glShadeModel(GL_SMOOTH);
      glEnable(GL_DEPTH_TEST);
      glViewport(0, 0, (GLsizei)width, (GLsizei)height);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glutMainLoop();
  
      return 0;
  }
  </code></pre>