简体   繁体   English

在OpenGl中读取TGA文件以创建3D色块

[英]reading TGA files in OpenGl to create a 3d ouse

I have a TGA file and a library that allready has everything that I need to read TGA and use them. 我有一个TGA文件和一个库,已经具备阅读TGA和使用它们所需的一切。

This class has a method called pixels(), that returns a pointer that is pointed to the memory area where pixel are stored as RGBRGBRGB... 此类具有称为pixel()的方法,该方法返回一个指针,该指针指向像素存储为RGBRGBRGB的存储区。

My question is, how can I take the pixel value? 我的问题是,如何计算像素值?

Cause if I make something like this: 原因是如果我做这样的事情:

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

It gives back to me what is proprably the address. 它给了我适当的地址。

I've found this code on this site: 我在此站点上找到了以下代码:

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);
        }
    }
}

I think that It can work great for me, but how can I tell the program to read from my img? 我认为它可以很好地工作,但是如何告诉程序从img中读取内容呢?

Tga supports different pixel depths. Tga支持不同的像素深度。 And we don't know what library you're using. 而且我们不知道您正在使用哪个库。 But generally speaking pixels() should return a pointer to a buffer containing pixels. 但是一般来讲,pixels()应该返回一个指向包含像素的缓冲区的指针。 Say for sake of argument it unpacks the pixels into 8-bit per channel subpixels, then each pixel is represented by 3 bytes. 出于争论的目的,它会将像素解压缩为每通道8位子像素,然后每个像素由3个字节表示。

So to access a pixel at a given offset in the buffer: 因此,要访问缓冲区中给定偏移量的像素:

const u8* pixelBuffer = img.pixels():

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

If you know the width of the image buffer then you can get a pixel by its x and y coordinates: 如果知道图像缓冲区的宽度,则可以通过其x和y坐标获得像素:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM