OpenGL第六节:加载png图片

OpenGL本身没有文件操作有关的接口,需要使用第三方库。这里使用DevIL库。

下载连接:http://openil.sourceforge.net/download.php

下载DevIL-Windows-SDK,解压后在Visual Studio配置头文件位置、lib库位置、lib库名称,拷贝dll库到当前工程。

 

LTexture.cpp

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 )
    {
      textureLoaded = loadTextureFromPixels32( (GLuint*)ilGetData(), (GLuint)ilGetInteger( IL_IMAGE_WIDTH ), (GLuint)ilGetInteger( IL_IMAGE_HEIGHT ) );//根据图片的像素去创建纹理,ilGetData()方法获取图片的像素
    }

    ilDeleteImages( 1, &imgID );//从内存删除即回收该图片。DevIL和OpenGL有着类似的状态机机制,即先生成并绑定ID,然后进行各做操作,最后解除绑定
  }

  if( !textureLoaded )
  {
    printf( "Unable to load %s\n", path.c_str() );
  }

  return textureLoaded;
}

 

LUtil.cpp

bool initGL()
{

  ...

  ilInit();//初始化devil
  ilClearColour( 255, 255, 255, 000 );//设置devil自己的渲染清屏颜色

  ILenum ilError = ilGetError();
  if( ilError != IL_NO_ERROR )
  {
    printf( "Error initializing DevIL! %s\n", iluErrorString( ilError ) );
    return false;
  }

  return true;

}

bool loadMedia()
{
  if( !gLoadedTexture.loadTextureFromFile( "texture.png" ) )
  {
    printf( "Unable to load file texture!\n" );
    return false;
  }

  return true;
}

 

转载于:https://www.cnblogs.com/yongfengnice/p/7885929.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值