2011-08-12 13 views
2

現在、C++でタグファイルを読み込み、OpenGLでレンダリングしようとしています。現在、色はすべて混ざっています(赤は青、緑は赤、青は緑になります)。ビデオメモリに入れるための私の現在のコードです。TGAファイルの読み込みとOpenGLでの使用

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

ここで、データはピクセルデータであり、残りは自己説明的です。ファイルは圧縮されずに保存され、Photoshopから24ビットで保存されます。

+0

ポスト問題を示し、完全な、最小限のプログラムをテクスチャパターンを持っていた青でした。 – genpfault

+0

@ジャック:おそらくあなたはあなたが得ているものの写真を投稿することができますか? –

+0

変更: glTextImage2D(GL_TEXTURE_2D、0、GL_RGB、widht、height、0、GL_BRG、GL_UNSIGNED_BYTE、data); 他の色の順序を持​​つTGAを読んだだけです(または間違った順序で読み込んでしまったため)。 2番目のGL_RBGをGL_BRGに変更する必要があります。詳細はこちらをご覧ください:http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml –

答えて

5

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; 
} 
+1

小さなヒント:boolを返す、bot bool * –

+1

コメントを 'Read 13 bytes'に修正してくださいコードのように '9バイト読み込み' –

0
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width,Height, 0, 
     GL_BGRA_EXT, GL_UNSIGNED_BYTE, Pixels); 

TRYこのことは、それだけではない 色

関連する問題