【问题标题】:Loading a TGA File and Using it with OpenGL加载 TGA 文件并将其与 OpenGL 一起使用
【发布时间】:2011-08-12 21:00:13
【问题描述】:

我目前正在尝试使用 c++ 加载标签文件并使用 OpenGL 渲染它。目前颜色都混合了(红色变成蓝色,绿色变成红色,蓝色变成绿色)。我当前将其放入视频内存的代码是。

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

其中数据是像素数据,其余部分是不言自明的。没有压缩保存的文件是从 Photoshop 保存的 24 位。

【问题讨论】:

  • 发布一个完整的、最小的程序来演示这个问题。
  • @Jjack:也许你可以张贴一张你得到的照片?
  • 更改为:glTextImage2D(GL_TEXTURE_2D, 0,GL_RGB, widht, height, 0, GL_BRG, GL_UNSIGNED_BYTE, data);只是因为您阅读了具有其他颜色顺序的 TGA(或者您以错误的顺序阅读它们)。您应该将第二个 GL_RBG 更改为 GL_BRG。更多信息请看这里:opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml

标签: c++ opengl image-processing tga


【解决方案1】:

你的glTexImage2D是正确的,所以这不是问题。

我确实需要(在加载 TGA 文件时)交换颜色组件。试试这个:

typedef struct
{
    unsigned char imageTypeCode;
    short int imageWidth;
    short int imageHeight;
    unsigned char bitCount;
    unsigned char *imageData;
} TGAFILE;

bool LoadTGAFile(char *filename, TGAFILE *tgaFile)
{
    FILE *filePtr;
    unsigned char ucharBad;
    short int sintBad;
    long imageSize;
    int colorMode;
    unsigned char colorSwap;

    // Open the TGA file.
    filePtr = fopen(filename, "rb");
    if (filePtr == NULL)
    {
        return false;
    }

    // Read the two first bytes we don't need.
    fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
    fread(&ucharBad, sizeof(unsigned char), 1, filePtr);

    // Which type of image gets stored in imageTypeCode.
    fread(&tgaFile->imageTypeCode, sizeof(unsigned char), 1, filePtr);

    // For our purposes, the type code should be 2 (uncompressed RGB image)
    // or 3 (uncompressed black-and-white images).
    if (tgaFile->imageTypeCode != 2 && tgaFile->imageTypeCode != 3)
    {
        fclose(filePtr);
        return false;
    }

    // Read 13 bytes of data we don't need.
    fread(&sintBad, sizeof(short int), 1, filePtr);
    fread(&sintBad, sizeof(short int), 1, filePtr);
    fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
    fread(&sintBad, sizeof(short int), 1, filePtr);
    fread(&sintBad, sizeof(short int), 1, filePtr);

    // Read the image's width and height.
    fread(&tgaFile->imageWidth, sizeof(short int), 1, filePtr);
    fread(&tgaFile->imageHeight, sizeof(short int), 1, filePtr);

    // Read the bit depth.
    fread(&tgaFile->bitCount, sizeof(unsigned char), 1, filePtr);

    // Read one byte of data we don't need.
    fread(&ucharBad, sizeof(unsigned char), 1, filePtr);

    // Color mode -> 3 = BGR, 4 = BGRA.
    colorMode = tgaFile->bitCount / 8;
    imageSize = tgaFile->imageWidth * tgaFile->imageHeight * colorMode;

    // Allocate memory for the image data.
    tgaFile->imageData = (unsigned char*)malloc(sizeof(unsigned char)*imageSize);

    // Read the image data.
    fread(tgaFile->imageData, sizeof(unsigned char), imageSize, filePtr);

    // Change from BGR to RGB so OpenGL can read the image data.
    for (int imageIdx = 0; imageIdx < imageSize; imageIdx += colorMode)
    {
        colorSwap = tgaFile->imageData[imageIdx];
        tgaFile->imageData[imageIdx] = tgaFile->imageData[imageIdx + 2];
        tgaFile->imageData[imageIdx + 2] = colorSwap;
    }

    fclose(filePtr);
    return true;
}

【讨论】:

  • 小提示:它返回一个bool,bot a bool*
  • 请将注释“读取 13 个字节”更正为“读取 9 个字节”,如代码中
【解决方案2】:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width,Height, 0,
         GL_BGRA_EXT, GL_UNSIGNED_BYTE, Pixels);

试试这个 我遇到了同样的问题 每件东西都是蓝色的,它有纹理图案,只是没有 颜色

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 2015-07-24
    • 2011-07-10
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多