c - 整个窗口的 glReadPixels (OpenGL)

标签 c opengl

我想在 OpenGL 上保存窗口的所有 RGB 值。 并想将值检查为“int”(因为我必须使用它) 我尝试使用 for 循环按每个像素保存它,并且它有效。 但是,如果我尝试 glReadpixels 一次,它无法检查。有什么问题吗?

这段代码有效。 (正确保存像素RGB,我可以使用cout检查它)

int width = 50;
int height = 50;
for(int i=0; i<height; i++)
{
    for(int j=0; j<width; j++)
    {
        unsigned char pick_col[3];
        glReadPixels(j , i , 1 , 1 , GL_RGB , GL_UNSIGNED_BYTE , pick_col);
        cout << (int)pick_col[0] << " " << (int)pick_col[1] << " " << (int)pick_col[2] << endl;
    }
}

但是这段代码不起作用。 (像素数组中有奇怪的值。有几个值是正确的)

GLubyte pixelarray[width*height*3]; 
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixelarray);

for(int i=0; i<height; i++)
{
    for(int j=0; j<width; j++)
    {
        cout << (int)pixelarray[i*width*3 + j*3] << " " (int)pixelarray[i*width*3 + j*3 +1] << " " << (int)pixelarray[i*width*3 + j*3+2] << endl;
    }
    cout << endl;
}

最佳答案

我解决了问题。它应该是GL_RGBA和4 channel 数组

GLubyte pixelarray[width*height*4]; 
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixelarray);

for(int i=0; i<height; i++)
{
    for(int j=0; j<width; j++)
    {
        cout << (int)pixelarray[i*width*4 + j*4] << " "     (int)pixelarray[i*width*4 + j*4 +1] << " " << (int)pixelarray[i*width*4 + j*4+2]     << endl;
    }
    cout << endl;
}

关于c - 整个窗口的 glReadPixels (OpenGL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41784493/

相关文章:

c++ - Windows 中的 strptime?

c - 数组索引上的整数溢出

c++ - 使用 C/C++ 作为脚本语言

c - 我应该如何在有效输入和缓冲区溢出的 shellcode 之间进行分隔?

OpenGL - 各个颜色附件有不同的透明颜色?

c++ - 如何修复此错误 : #include <gl/glut. h> "Cannot open source file gl/glut.h"

c++ - 对于 scanf(),为什么否定的扫描集 "%[^\n]"显示 "\n"的正确输出,而不是 "%[aeiou]"的扫描集 "aegis"?

c++ - 如何在 2D opengl 中进行拾取?

c++ - OpenGL(核心配置文件)纹理缺失或黑色

c++ - OpenGL - 纹理仅部分显示且浑浊