我目前正在嘗試使用C++加載標記文件並使用OpenGL進行渲染。目前,所有顏色都混在一起(紅色變成藍色,綠色變成紅色,藍色變成綠色)。我目前的代碼放入視頻內存是。加載TGA文件並將其與OpenGL一起使用
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
其中數據是像素數據,其餘是自我解釋。這些文件是保存的,沒有壓縮是從Photoshop中保存的24位。
我目前正在嘗試使用C++加載標記文件並使用OpenGL進行渲染。目前,所有顏色都混在一起(紅色變成藍色,綠色變成紅色,藍色變成綠色)。我目前的代碼放入視頻內存是。加載TGA文件並將其與OpenGL一起使用
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
其中數據是像素數據,其餘是自我解釋。這些文件是保存的,沒有壓縮是從Photoshop中保存的24位。
您的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;
}
小提示:它返回一個布爾值,一個布爾值* –
請修改'Read 13 bytes'的評論到'讀取9個字節'就像代碼中的int一樣 –
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width,Height, 0,
GL_BGRA_EXT, GL_UNSIGNED_BYTE, Pixels);
試試這個我有同樣的問題每一件事情是藍色它有紋理圖案只是不 顏色
後一個完整的,最小的程序演示該問題。 – genpfault
@Jjack:也許你可以發佈你的圖片嗎? –
更改爲: glTextImage2D(GL_TEXTURE_2D,0,GL_RGB,widht,height,0,GL_BRG,GL_UNSIGNED_BYTE,data); 這僅僅是因爲你閱讀了其他顏色順序的TGA(或者你讀錯了順序)。您應該將第二個GL_RBG更改爲GL_BRG。更多內容請看這裏:http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml –