#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

int windowWidth = 640; // VGA size window
int windowHeight = 480; 

int numChannel = 4; // save as RGBA image
int count = 0; // image name indicator

...

if (saveImage)
{
	size_t windowSize = numChannel * windowWidth * windowHeight;
	GLubyte *pixels = new GLubyte[windowSize]; // Create unsigned char array (byte == char)
	
	glReadPixels(0,0,640,480,GL_RGBA,GL_UNSIGNED_BYTE,pixels);
	
	char imgPath[256];
	sprintf_s(imgPath, "Images/image%d.png", count);

	stbi_write_png(imgPath, windowWidth, windowHeight, numChannel,pixels, numChannel * windowWidth);

	delete[] pixels; // clear buffer
	count += 1;

	saveImage = false;
}

mainWindow.swapBufferss(); // Use if you want to render what you've just saved