0

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;
}
luckintt
  • 63
  • 6
  • 1
    Are you saying the function doesn't write the number of pixels you ask for? Ie `width * height` pixels? – Blindy Jan 20 '21 at 15:44
  • No. The function write the number of pixels I ask for, but I want to save HD images, this function cannot meet my requirements. I don't know how to modify it. – luckintt Jan 21 '21 at 06:03

1 Answers1

0

Couple options:

  1. Render to a larger-than-default-framebuffer FBO & glReadPixels() that
  2. Render to multiple tiles & glReadPixels() those, with a stitch-into-single-larger-image final pass
genpfault
  • 51,148
  • 11
  • 85
  • 139