I am using OpenGL
to simulate objects. And use stbi_write_png
to save picture, but the size of the picture is only over 200K, and it is not very clear after zooming in. So I want to know if there are other ways to save high-definition pictures in C++ code.
My code shows as below:
int SaveScreenshot(const char *filename)
{
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
int x = viewport[0];
int y = viewport[1];
int width = viewport[2];
int height = viewport[3];
char *data = (char*)malloc((size_t)(width * height * 4)); // 3 components (R, G, B)
if (!data)
return 0;
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_flip_vertically_on_write(1);
int saved = stbi_write_png(filename, width, height, 4, data, 0);
free(data);
return saved;
}