如何在GLUT上加载bmp以将其用作纹理? [英] How to load a bmp on GLUT to use it as a texture?
本文介绍了如何在GLUT上加载bmp以将其用作纹理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在寻找一个简单的解决方案来添加精灵到我的OpenGL GLUT简单月球着陆器游戏在c + +,它似乎我必须使用bmp的,因为他们是最容易加载和使用它们作为矩形纹理。
I've been searching all around for a simple solution to add sprites to my OpenGl GLUT simple moon lander game in c++ and it appears I must use bmp's since they're easiest to load and use them as textures on a rectangle.
如何将bmp加载为纹理?
How exactly can I load the bmp's as textures though?
推荐答案
看看我的简单c实现函数加载纹理。
Look my simple c implementation function to load texture.
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;
}
上述函数返回纹理数据。将纹理数据存储在变量中
Above function returns the texture data. Store the texture data in variable
GLuint texture
texture= LoadTexture( "your_image_name.bmp" );
现在你可以使用glBindTexture绑定texure
Now you can bind the texure using glBindTexture
glBindTexture (GL_TEXTURE_2D, texture);
这篇关于如何在GLUT上加载bmp以将其用作纹理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文