纹理渲染的图片的宽度和高度需要是二次幂的,如果不是二次幂的,需要通过填充的方式(就是添加边距),填充到二次幂。
LTexture.h
bool loadTextureFromPixels32( GLuint* pixels, GLuint imgWidth, GLuint imgHeight, GLuint texWidth, GLuint texHeight );//imgWidth图片宽,imgHeight图片高,texWidth纹理宽,texHeight纹理高
private:
GLuint powerOfTwo( GLuint num );//计算对应的幂,比例num为60时返回64 。
GLuint mTextureID;
GLuint mTextureWidth;//纹理宽高
GLuint mTextureHeight;
GLuint mImageWidth;//图片的宽高
GLuint mImageHeight;
LTexture.cpp
LTexture::LTexture()
{
mTextureID = 0;
mImageWidth = 0;
mImageHeight = 0;
mTextureWidth = 0;
mTextureHeight = 0;
}
bool LTexture::loadTextureFromFile( std::string path )
{
bool textureLoaded = false;
ILuint imgID = 0;
ilGenImages( 1, &imgID );//生成ID
ilBindImage( imgID );//绑定
ILboolean success = ilLoadImage( path.c_str() );//加载
if( success == IL_TRUE )
{
success = ilConvertImage( IL_RGBA, IL_UNSIGNED_BYTE );//换为rgba
if( success == IL_TRUE )
{
GLuint imgWidth = (GLuint)ilGetInteger( IL_IMAGE_WIDTH );
GLuint imgHeight = (GLuint)ilGetInteger( IL_IMAGE_HEIGHT );
GLuint texWidth = powerOfTwo( imgWidth );
GLuint texHeight = powerOfTwo( imgHeight );
if( imgWidth != texWidth || imgHeight != texHeight )//图片宽高和纹理的宽高不一样
{
iluImageParameter( ILU_PLACEMENT, ILU_UPPER_LEFT );//设置参数,让它图片位置左上角
iluEnlargeCanvas( (int)texWidth, (int)texHeight, 1 );//拉伸画布到和纹理的宽高一样,这样图片在左上角,右下角就会被ilClearColor的颜色填充
}
textureLoaded = loadTextureFromPixels32( (GLuint*)ilGetData(), imgWidth, imgHeight, texWidth, texHeight );//根据图片像素创建纹理
}
ilDeleteImages( 1, &imgID );//从内存删除图片
}
if( !textureLoaded )
{
printf( "Unable to load %s\n", path.c_str() );
}
return textureLoaded;
}
bool LTexture::loadTextureFromPixels32( GLuint* pixels, GLuint imgWidth, GLuint imgHeight, GLuint texWidth, GLuint texHeight )
{
freeTexture();
mImageWidth = imgWidth;
mImageHeight = imgHeight;
mTextureWidth = texWidth;
mTextureHeight = texHeight;
glGenTextures( 1, &mTextureID );//生成纹理ID
glBindTexture( GL_TEXTURE_2D, mTextureID );//绑定
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, mTextureWidth, mTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels );//创建
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glBindTexture( GL_TEXTURE_2D, NULL );//解除
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
printf( "Error loading texture from %p pixels! %s\n", pixels, gluErrorString( error ) );
return false;
}
return true;
}
void LTexture::freeTexture()
{
if( mTextureID != 0 )
{
glDeleteTextures( 1, &mTextureID );
mTextureID = 0;
}
mImageWidth = 0;
mImageHeight = 0;
mTextureWidth = 0;
mTextureHeight = 0;
}
void LTexture::render( GLfloat x, GLfloat y, LFRect* clip )
{
if( mTextureID != 0 )
{ //下面是纹理坐标处理
glLoadIdentity();//重置,移除之前的变换
GLfloat texTop = 0.f;
GLfloat texBottom = (GLfloat)mImageHeight / (GLfloat)mTextureHeight;
GLfloat texLeft = 0.f;
GLfloat texRight = (GLfloat)mImageWidth / (GLfloat)mTextureWidth;
GLfloat quadWidth = mImageWidth;
GLfloat quadHeight = mImageHeight;
if( clip != NULL )
{
texLeft = clip->x / mTextureWidth;
texRight = ( clip->x + clip->w ) / mTextureWidth;
texTop = clip->y / mTextureHeight;
texBottom = ( clip->y + clip->h ) / mTextureHeight;
quadWidth = clip->w;
quadHeight = clip->h;
}
glTranslatef( x, y, 0.f );
glBindTexture( GL_TEXTURE_2D, mTextureID );//绑定
glBegin( GL_QUADS );//渲染
glTexCoord2f( texLeft, texTop ); glVertex2f( 0.f, 0.f );
glTexCoord2f( texRight, texTop ); glVertex2f( quadWidth, 0.f );
glTexCoord2f( texRight, texBottom ); glVertex2f( quadWidth, quadHeight );
glTexCoord2f( texLeft, texBottom ); glVertex2f( 0.f, quadHeight );
glEnd();
}
}
GLuint LTexture::powerOfTwo( GLuint num )
{
if( num != 0 )
{
num--;
num |= (num >> 1); //Or first 2 bits
num |= (num >> 2); //Or next 2 bits
num |= (num >> 4); //Or next 4 bits
num |= (num >> 8); //Or next 8 bits
num |= (num >> 16); //Or next 16 bits
num++;
}
return num;
}
LUtil.cpp
LTexture gNon2NTexture;
bool initGL()
{
...
ilInit();//初始化DevIL
iluInit();//初始化DevILU工具类
ilClearColour( 255, 255, 255, 000 );
ILenum ilError = ilGetError();
if( ilError != IL_NO_ERROR )
{
printf( "Error initializing DevIL! %s\n", iluErrorString( ilError ) );
return false;
}
return true;
}
bool loadMedia()
{
if( !gNon2NTexture.loadTextureFromFile( "opengl.png" ) )
{
printf( "Unable to load non-power-of-two texture!\n" );
return false;
}
return true;
}
void render()
{
glClear( GL_COLOR_BUFFER_BIT );
gNon2NTexture.render( ( SCREEN_WIDTH - gNon2NTexture.imageWidth() ) / 2.f, ( SCREEN_HEIGHT - gNon2NTexture.imageHeight() ) / 2.f );
glutSwapBuffers();
}