gpt4 book ai didi

c++ - glReadPixels 存储 x, y 值

转载 作者:搜寻专家 更新时间:2023-10-31 00:19:08 25 4
gpt4 key购买 nike

我正在尝试使用 glReadPixels 存储像素数据,但到目前为止我一次只能存储一个像素。我不确定这是不是要走的路。我目前有这个:

unsigned char pixels[3];
glReadPixels(50,50, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixels);

什么是将它存储在数组中的好方法,这样我就可以得到这样的值:

pixels[20][50][0]; // x=20 y=50 -> R value
pixels[20][50][1]; // x=20 y=50 -> G value
pixels[20][50][2]; // x=20 y=50 -> B value

我想我可以简单地将它放在一个循环中:

for ( all pixels on Y axis )
{
for ( all pixels in X axis )
{
unsigned char pixels[width][height][3];
glReadPixels(x,y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixels[x][y]);
}
}

但我觉得一定有更好的方法来做到这一点。但是我确实需要我的数组就像我在代码上面描述的那样。那么for循环的思路好不好,有没有更好的办法呢?

最佳答案

glReadPixels 仅按 R, G, B, R, G, B, ... 的顺序返回字节(基于您对 GL_RGB< 的设置) 从屏幕左下角到右上角From the OpenGL documentation :

glReadPixels returns pixel data from the frame buffer, starting with the pixel whose lower left corner is at location (x, y), into client memory starting at location data. Several parameters control the processing of the pixel data before it is placed into client memory. These parameters are set with three commands: glPixelStore, glPixelTransfer, and glPixelMap. This reference page describes the effects on glReadPixels of most, but not all of the parameters specified by these three commands.

数千次调用 glReadPixels 的开销很可能会花费大量时间(取决于窗口大小,如果循环花费 1-2 秒,我不会感到惊讶)。

建议您只调用一次glReadPixels并将其存储在大小为(width - x) * (height - y) * 3的字节数组中。从那里您可以使用 data[(py * width + px) * 3 + component] 引用像素的组件位置,其中 pxpy是您要查找的像素位置,component 是像素的 R、G 或 B 分量。

如果您绝对必须将它放在 3 维数组中,您可以在 glReadPixels 调用之后编写一些代码来重新排列 1d 数组。

关于c++ - glReadPixels 存储 x, y 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8954071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com