title: how to save a shape in an image file format with OpenGL
body: <p>So, im starting to learn opengl, i drew some shapes now i want to save it, i found this code in a doc, but im dont see how would i get it to work with my code.
  this is my code:</p>
  <pre><code>#include &lt;GL/glut.h&gt;
  #include &lt;stdio.h&gt;
  
  void display(void) {
  
    glClear(GL_COLOR_BUFFER_BIT);
  
    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_TRIANGLES);
  
    glVertex2f(0.1, 0.1);
    glVertex2f(0.3, 0.1);
  
    glVertex2f(0.2, 0.3);
  
    glEnd();
  
    glBegin(GL_POLYGON);
  
    glColor3f(0.0, 1.0, 0.0);
  
    glVertex2f(0.3, -0.1);
    glVertex2f(0.3, 0.1);
    glVertex2f(0.1, 0.1);
    glVertex2f(0.1, -0.1);
  
    glEnd();
  
    glFlush();
  }
  int main(int argc, char *argv[]) {
  
    glutInit(&amp;argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(10, 10);
    glutInitWindowSize(920, 920);
    glutCreateWindow(&quot;First Raster Shape&quot;);
    glutDisplayFunc(display);
    glutMainLoop();
  
  
    return 0;
  }
  </code></pre>
  <p>and the code i found and the docs:</p>
  <pre><code>void savePPM(int start_x,int start_y,int w,int h,char *fname)
  {
          FILE *f=fopen(fname,&quot;wb&quot;);
          if (!f) return;
          std::vector&lt;unsigned char&gt; out(3*w*h);
          glPixelStorei(GL_PACK_ALIGNMENT,1); /* byte aligned output */
          glReadPixels(start_x,start_y,w,h, GL_RGB,GL_UNSIGNED_BYTE,&amp;out[0]);
          fprintf(f,&quot;P6\n%d %d\n255\n&quot;,w,h);
          for (int y=0;y&lt;h;y++) { /* flip image bottom-to-top on output */
                  fwrite(&amp;out[3*(h-1-y)*w],1,3*w,f);
          }
          fclose(f);
  }
  </code></pre>
  <p>the code saves into the ppm format, which im cool with it, but still i want to know how would i make it work with my code. or is there any other way to do it?
  link for the docs:
  <a href="https://www.cs.uaf.edu/2010/spring/cs481/section/2/lecture/03_30_imagefiles.html" rel="nofollow noreferrer">https://www.cs.uaf.edu/2010/spring/cs481/section/2/lecture/03_30_imagefiles.html</a></p>