gpt4 book ai didi

c++ - 在 OpenGl 中读取 TGA 文件以创建 3d use

转载 作者:行者123 更新时间:2023-11-28 07:54:29 25 4
gpt4 key购买 nike

我有一个 TGA 文件和一个库,里面已经有了我阅读和使用 TGA 所需的一切。

这个类有一个叫做pixels()的方法,它返回一个指针指向存储像素为RGBRGBRGB的内存区域...

我的问题是,如何获取像素值?

因为如果我做这样的事情:

img.load("foo.tga");
printf ("%i", img.pixels());

它会返回给我大概的地址。

我在这个网站上找到了这段代码:

struct Pixel2d
{
static const int SIZE = 50;
unsigned char& operator()( int nCol, int nRow, int RGB)
{
return pixels[ ( nCol* SIZE + nRow) * 3 + RGB];
}

unsigned char pixels[SIZE * SIZE * 3 ];
};

int main()
{

Pixel2d p2darray;
glReadPixels(50,50, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &p.pixels);

for( int i = 0; i < Pixel2d::SIZE ; ++i )
{
for( int j = 0; j < Pixel2d::SIZE ; ++j )
{
unsigned char rpixel = p2darray(i , j , 0);
unsigned char gpixel = p2darray(i , j , 1);
unsigned char bpixel = p2darray(i , j , 2);
}
}
}

我认为它对我很有用,但我如何告诉程序从我的 img 中读取?

最佳答案

Tga 支持不同的像素深度。而且我们不知道您使用的是什么库。但一般来说 pixels() 应该返回一个指向包含像素的缓冲区的指针。为了争论起见,它将像素解压缩为每 channel 8 位子像素,然后每个像素由 3 个字节表示。

因此要访问缓冲区中给定偏移量处的像素:

const u8* pixelBuffer = img.pixels():

u8 red = pixelBuffer[(offset*3)+0];
u8 green = pixelBuffer[(offset*3)+1];
u8 blue = pixelBuffer[(offset*3)+2];

如果你知道图像缓冲区的宽度,那么你可以通过它的 x 和 y 坐标得到一个像素:

u8 red = pixelBuffer[((x+(y*width))*3)+0];

关于c++ - 在 OpenGl 中读取 TGA 文件以创建 3d use,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13033388/

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