zoukankan      html  css  js  c++  java
  • 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 ", 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 ", iluErrorString( ilError ) );
        return false;
      }

      return true;

    }

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

      return true;
    }

  • 相关阅读:
    HDU 4325 Flowers(树状数组)
    HDU 1166 敌兵布阵(树状数组)
    linux网络编程之一-----多播(组播)编程
    对 /dev/shm 认识
    使用GDB调试STL容器
    Android中图片优化之webp使用
    Android后台进程与前台线程间的区别使用
    Android如何从外部跳进App
    熟悉Android开发不得不知道的技巧
    Java代码规范文档
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/7885929.html
Copyright © 2011-2022 走看看