我一直在寻找一个简单的解决方案来添加精灵到我的OpenGl GLUT简单的月球着陆器游戏在c++中,似乎我必须使用bmp的,因为他们是最容易加载和使用他们作为一个矩形的纹理。
我怎么才能加载bmp的纹理呢?
发布于 2012-09-21 11:54:17
看看我的加载纹理的简单c实现函数。
GLuint LoadTexture( const char * filename )
{
GLuint texture;
int width, height;
unsigned char * data;
FILE * file;
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
width = 1024;
height = 512;
data = (unsigned char *)malloc( width * height * 3 );
//int size = fseek(file,);
fread( data, width * height * 3, 1, file );
fclose( file );
for(int i = 0; i < width * height ; ++i)
{
int index = i*3;
unsigned char B,R;
B = data[index];
R = data[index+2];
data[index] = R;
data[index+2] = B;
}
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
free( data );
return texture;
}
上面的函数返回纹理数据。将纹理数据存储在变量中
GLuint texture;
texture= LoadTexture( "your_image_name.bmp" );
现在可以使用glBindTexture绑定纹理了
glBindTexture (GL_TEXTURE_2D, texture);
发布于 2012-09-21 01:59:14
从OpenGL_3_2_Utils查看我的TextureLoader (TextureLoader.h + TextureLoader.cpp):
https://github.com/mortennobel/OpenGL_3_2_Utils
这两个文件不依赖于任何其他文件,也应该可以在任何版本的OpenGL (以及任何平台)上无缝工作。示例用法可以在文件注释中找到。
发布于 2012-09-21 10:23:01
https://stackoverflow.com/questions/12518111
复制相似问题