当glReadPixel为空时,如何生成屏幕截图

How can I generate a screenshot when glReadPixels is empty?

本文关键字:何生成 屏幕截图 glReadPixel      更新时间:2023-10-16

我有一个程序,它使用OpenGL(VS2012,freeglut 2.8.1(在窗口中运行。基本上在每个时间步(通过从我的glutIdleFunc钩子调用glutPostRedisplay运行(,我调用自己的draw函数,然后调用glFlush以显示结果。然后我调用我自己的screenShot函数,该函数使用 glReadPixels 函数将像素转储到 tga 文件。

此设置的问题在于,当窗口最小化时,文件为空。也就是说,glReadPixels的输出是空的;我怎样才能避免这种情况?

这是我正在使用的screenShot功能的副本(我不是版权所有者(:

//////////////////////////////////////////////////
// Grab the OpenGL screen and save it as a .tga //
// Copyright (C) Marius Andra 2001              //
// http://cone3d.gz.ee  EMAIL: cone3d@hot.ee    //
//////////////////////////////////////////////////
// (modified by me a little)
int screenShot(int const num)
{
    typedef unsigned char uchar;
    // we will store the image data here
    uchar *pixels;
    // the thingy we use to write files
    FILE * shot;
    // we get the width/height of the screen into this array
    int screenStats[4];
    // get the width/height of the window
    glGetIntegerv(GL_VIEWPORT, screenStats);
    // generate an array large enough to hold the pixel data 
    // (width*height*bytesPerPixel)
    pixels = new unsigned char[screenStats[2]*screenStats[3]*3];
    // read in the pixel data, TGA's pixels are BGR aligned
    glReadPixels(0, 0, screenStats[2], screenStats[3], 0x80E0, 
    GL_UNSIGNED_BYTE, pixels);
    // open the file for writing. If unsucessful, return 1
    std::string filename = kScreenShotFileNamePrefix + Function::Num2Str(num) + ".tga";
    shot=fopen(filename.c_str(), "wb");
    if (shot == NULL)
        return 1;
    // this is the tga header it must be in the beginning of 
    // every (uncompressed) .tga
    uchar TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};
    // the header that is used to get the dimensions of the .tga
    // header[1]*256+header[0] - width
    // header[3]*256+header[2] - height
    // header[4] - bits per pixel
    // header[5] - ?
    uchar header[6]={((int)(screenStats[2]%256)),
    ((int)(screenStats[2]/256)),
    ((int)(screenStats[3]%256)),
    ((int)(screenStats[3]/256)),24,0};
    // write out the TGA header
    fwrite(TGAheader, sizeof(uchar), 12, shot);
    // write out the header
    fwrite(header, sizeof(uchar), 6, shot);
    // write the pixels
    fwrite(pixels, sizeof(uchar), 
    screenStats[2]*screenStats[3]*3, shot);
    // close the file
    fclose(shot);
    // free the memory
    delete [] pixels;
    // return success
    return 0;
}

那么,无论Windows是否决定在显示器上实际显示内容,如何将屏幕截图打印到TGA文件?

注意:由于我尝试保留模拟进度的可视记录,因此我需要打印每一帧,无论它是否正在渲染。我意识到最后一句话有点矛盾,因为我需要渲染框架才能产生屏幕截图。改写;我需要glReadPixels(或一些替代函数(在每一步生成程序的更新状态,以便我可以将其打印到文件中,无论窗口是否选择显示它。

听起来你遇到了像素所有权问题。

渲染到 FBO 并使用 glReadPixels() 将图像从该 FBO 而不是前端缓冲区中移出。

我建议将最后一个渲染的帧存储在内存中,并在调用更新并且新渲染中有实际像素数据时更新此内存的内容。要么这样,要么你可以使用accum,尽管我不太记得它是如何存储旧帧的(它可能最终更新得如此之快,以至于它也没有存储渲染数据。

另一种解决方案可能是使用着色器手动渲染每个帧并将结果写入文件