我已紋理與.bmp格式圖像的球體。問題是,當圖像映射到球體上時,圖像的顏色看起來反轉。就像紅色變成藍色,藍色變成紅色。 我試過用GL_BGR而不是GL_RGB但沒用。 我是否必須更改加載圖像的代碼。因爲它會產生使用功能的警告,我也不認爲它與我所要求的相關。 映射後我得到的圖像是texured sphere with inverted colors紋理加載和映射
這是我試圖加載圖像和應用一些紋理渲染的東西。
GLuint LoadTexture(const char * filename, int width, int height)
{
GLuint texture;
unsigned char * data;
FILE * file;
//The following code will read in our RAW file
file = fopen(filename, "rb");
if (file == NULL) return 0;
data = (unsigned char *)malloc(width * height * 3);
fread(data, width * height * 3, 1, file);
fclose(file);
glGenTextures(1, &texture); //generate the texturewith the loaded data
glBindTexture(GL_TEXTURE_2D, texture); //bind thetexture to it’s array
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE); //set texture environment parameters
// better quality
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
//Here we are setting the parameter to repeat the textureinstead of clamping the texture
//to the edge of our shape.
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);
//Generate the texture with mipmaps
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data);
free(data); //free the texture
return texture; //return whether it was successfull
}
void FreeTexture(GLuint texture)
{
glDeleteTextures(1, &texture);
}
[如何在GLUT上加載bmp以將其用作紋理?](http://stackoverflow.com/questions/12518111/how-to-load-a-bmp-on-glut-使用它作爲一個紋理) – teivaz