简体   繁体   中英

How to retreive the 6 faces from cube texure

I parse TGA texture file which should be GL_TEXTURE_CUBE_MAP

char* pixelsArray = LoadTGA(getPath(), &width, &height, &bpp);

and I can't figure how to retrive the 6 faces of the cube.

I tried to get some relative indexes for pixelsArray and the faces. Something like:

newBuffer[rowsIndex * rowSize + columnsIndex] = pixelsArray[rowsIndex*rowSize + imageOffset + rowsIndex*rowSize + columnsIndex]

char * LoadTGA( const char * szFileName, int * width, int * height, int * bpp )
{

    FILE * f;

    if (fopen_s(&f, szFileName, "rb" ) != 0)
        return NULL;

    TGA_HEADER header;
    fread( &header, sizeof(header), 1, f );

    fseek( f, 0, SEEK_END );
    int fileLen = ftell( f );
    fseek( f, sizeof( header ) + header.identsize, SEEK_SET );

    if ( header.imagetype != IT_COMPRESSED && header.imagetype != IT_UNCOMPRESSED )
    {
        fclose( f );
        return NULL;
    }

    if ( header.bits != 24 && header.bits != 32 )
    {
        fclose( f );
        return NULL;
    }

    int bufferSize = fileLen - sizeof( header ) - header.identsize;
    char * pBuffer = new char[bufferSize];
    fread( pBuffer, 1, bufferSize, f );
    fclose( f );

    *width = header.width;
    *height = header.height;
    *bpp = header.bits;
    char * pOutBuffer = new char[ header.width * header.height * header.bits / 8 ];

    switch( header.imagetype )
    {
    case IT_UNCOMPRESSED:
        LoadUncompressedImage( pOutBuffer, pBuffer, &header );
        break;
    case IT_COMPRESSED:
        LoadCompressedImage( pOutBuffer, pBuffer, &header );
        break;
    }

    delete[] pBuffer;

    return pOutBuffer;
}
//...buffering texture

    GLint width, height, bpp;
    GLuint type = GL_TEXTURE_2D, bppType = GL_RGB;
    idBuffer = arrayCopy(idBuffer, lastPoint);
    glGenTextures(1, idBuffer+lastPoint);
    if (!is2D())
        type = GL_TEXTURE_CUBE_MAP;

    glBindTexture(type, idBuffer[lastPoint]);

    char* pixelsArray = LoadTGA(getPath(), &width, &height, &bpp);

    if (bpp == 32)
        bppType = GL_RGBA;

    if (is2D())
        glTexImage2D(type, 0, bppType, width, height, 0, bppType, GL_UNSIGNED_BYTE, pixelsArray);
    else
        for (int face = 0, imageWidth = width / 4, imageHeigth = height / 3; face < 6; face++)
            //glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, 0, 0, width, height, bppType, GL_UNSIGNED_BYTE, pixelsArray + face * imageWidth * imageHeigth);
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, imageWidth, imageHeigth, bppType, GL_UNSIGNED_BYTE, getBufferForFace(face));


    glBindTexture(type, 0);

Texture create isn't the one i want. ( It draws some weird pixels )

To answer your question, your loader will need to copy out the 6 sub-faces into a separate array, and upload each extracted sub-image to glTexImage2D() .

A better answer is "why are you doing it this way"? Your TGA contains a huge amount of wasted space, and TGA is an uncompressed format. These means you have a large install size and high memory bandwidth. Split out the 6 faces off-line, compress them and mipmap them to eg ASTC or ETC format. You can store these in the KTX wrapper format if you want to avoid handling lots of files on disk.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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