我一直在寻找一个简单的解决方案,将精灵添加到我的 C++ OpenGl GLUT 简单月球着陆器游戏中,看来我必须使用 bmp,因为它们最容易加载并用作矩形上的纹理。
我怎样才能将 bmp 加载为纹理?
看我加载纹理的简单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);
从 OpenGL_3_2_Utils 中检查我的 TextureLoader (TextureLoader.h + TextureLoader.cpp):
https://github.com/mortennobel/OpenGL_3_2_Utils
这两个文件不依赖于任何其他文件,也应该在任何版本的 OpenGL(和任何平台)上无缝工作。示例用法可以在文件注释中找到。
您可以使用库 GLAUX 和 SOIL(简单 OpenGL 图像库)。 还有 OpenGL 的其他图像库。
如何在 GLUT 上加载 bmp 以将其用作纹理?
另一个非常简单的解决方案是使用 STB 库,它可以在 GitHub - nothings/stb.
只需要一个源文件,头文件“stb_image.h”。它不需要链接任何库文件或编译任何额外的源文件。
包含头文件并通过设置预处理器定义启用图像读取
STB_IMAGE IMPLEMENTATION
:
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
图像文件可以通过函数读取
stbi_load
:
const char *filename = .....; // path and filename
int req_channels = 3; // 3 color channels of BMP-file
int width = 0, height = 0, channels = 0;
stbi_uc *image = stbi_load( filename, &width, &height, &channels, 3 );
GL_UNPACK_ALIGNMENT
必须设置为1.GL_UNPACK_ALIGNMENT
为 4,因此假定图像的每一行对齐到 4 个字节。一个 BMP 文件的像素一般都是 3 个字节,并且排列得很紧,这会导致错位。stbi_image_free
释放内存:
GLuint texture_obj = 0;
if ( image != nullptr )
{
glGenTextures(1, &texture_obj);
glBindTexture(GL_TEXTURE_2D, texture_obj);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // default
stbi_image_free( image );
}
GLuint LoadTexture(GLuint tex, const char * filename, int width, int height)
{
//bmp 24 bit
unsigned char * data;
unsigned char R,G,B;
FILE * file;
//open .bmp
file = fopen(filename, "rb");
if(file == NULL)return 0;
//get memory for data
data =(unsigned char *)malloc(width * height * 3);
//data skip offset
fseek(file,128,0);
//read file to data
fread(data, width * height * 3, 1, file);
//close file
fclose(file);
//transpose R,G,B values
int index;
for(int i = 0; i < width * height ; ++i)
{
index = i*3;
B = data[index]; G = data[index+1]; R = data[index+2];
data[index] = R; data[index+1] = G; data[index+2] = B;
}
//create a texture
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data);
//texture filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//free memory
free(data);
return 0;
}
void init(void)
{
//texture loading, bmp 24 bit
LoadTexture(1, "01.bmp", 316, 316);
LoadTexture(2, "02.bmp", 316, 316);
LoadTexture(3, "05.bmp", 316, 316);
LoadTexture(4, "03.bmp", 316, 316);
LoadTexture(5, "06.bmp", 316, 316);
LoadTexture(6, "04.bmp", 316, 316);
. . . . . . . . . . . .
Linux
gcc cube.c -o cube -lglut -lGL -lGLU
视窗
tcc cube.c -o cube.exe -LC: cc\lib -lopengl32 -lglu32 -lglut32 -Wl,-subsystem=windows