我已纹理与.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