【问题标题】:Texture loading and mapping纹理加载和映射
【发布时间】:2016-05-02 18:16:29
【问题描述】:

我用 .bmp 格式的图像对球体进行了纹理处理。问题是,当图像映射到球体上时,图像的颜色看起来是反转的。就像红色变成蓝色,蓝色变成红色。 我曾尝试使用 GL_BGR 而不是 GL_RGB 但它没有用。 我是否必须更改加载图像的代码。因为它会为使用 fopen() 函数产生警告,而且我认为它与我的要求无关。 我映射后得到的图像是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 );
   }

【问题讨论】:

标签: c++ opengl


【解决方案1】:

BMP 文件以 BITMAPFILEHEADER 结构开始,该结构包含(除其他外)文件中位实际开始的偏移量。

所以你可以做这样的事情来获取 BMP 文件的位。

BITMAPFILEHEADER bmpFileHeader;
fread(&bmpFileHeader,sizeof(bmpFileHeader),1,file);
fsetpos(file,bmpFileHeader->bfOffBits,SEEK_SET);
size_t bufferSize = width * height * 3;
fread(data,bufferSize,1,file);

当然,这很危险,因为您希望得到一个大小合适且格式正确的 BMP 文件。所以你真的需要阅读 BITMAPINFO。

BITMAPFILEHEADER bmpFileHeader;
fread(&bmpFileHeader,sizeof(bmpFileHeader),1,file);
BITMAPINFO bmpInfo;
fread(&bmpInfo,sizeof(bmpInfo),1,file)
fsetpos(file,bmpFileHeader->bfOffBits,SEEK_SET);
int width = bmpInfo->bmiHeader.biWidth;
int height = bmpInfo->bmiHeader.biHeight;
assert(bmpInfo->bmiHeader.biCompression == BI_RGB);
assert(bmpInfo->bmiHeader.biBitCount == 24;
size_t bufferSize = width * height * 3;
fread(data,bufferSize,1,file);

您显然可以在将 bmp 格式映射到允许的 opengl 格式时使其变得越来越复杂。

其他并发症将是:

  • bmp 数据是自下而上的,因此会出现上下颠倒,除非您对此进行更正。
  • bmp 文件中的像素数据被填充以确保每行是 4 字节宽的倍数,如果您的图像不是每像素 32 位或 4 像素宽的倍数(实践是通常是真的)。​​
  • 仅仅因为您可以向 OpenGL 提供一个常量并不意味着该格式实际上是受支持的。有时您只需要自己进入并重新排序字节即可。

【讨论】:

  • 我是这个领域的新手,告诉我 BITMAPFILEHEADER 在 glut.h 中是否可用,或者我必须为此添加一些特定的头文件。
  • 哦,我明白了 - 你不是在 Windows 上这样做吗?如果是,只需#include ,如果不是,您可以在此处找到记录的 BMP 文件结构:msdn.microsoft.com/en-us/library/windows/desktop/…
  • 好的。我已经通过编辑纠正了颠倒的图像。
  • 告诉我,如果我用 adobe Photoshop 软件反转源图像的颜色,它会起作用吗@chris
【解决方案2】:

我终于得到了我需要的东西。我使用了错误的参数 GL_RGB 而是在此函数中将其替换为 GL_BRG_EXT

gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_BGR_EXT, GL_UNSIGNED_BYTE, data ); 

以前我得到这个。textured mapped sphere with inverted colors

现在我在上面所做的更改之后得到了这个。textured mapped sphere with true colors

【讨论】:

    猜你喜欢
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多