青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆 - 5, 文章 - 0, 評論 - 24, 引用 - 0
數據加載中……

OpenGL中用FreeImage

FreeImage is an Open Source library project for developers who would like to support popular graphics image formats like PNG, BMP, JPEG, TIFF and others as needed by today's multimedia applications. FreeImage is easy to use, fast, multithreading safe, compatible with all 32-bit versions of Windows, and cross-platform (works both with Linux and Mac OS X).
測試過png,bmp,jpg,tga。

TextureManager.h
//**********************************************
//Singleton Texture Manager class
//Written by Ben English
//benjamin.english@oit.edu
//
//For use with OpenGL and the FreeImage library
//**********************************************

#ifndef TextureManager_H
#define TextureManager_H

#include 
<gl/glew.h>
#include 
"FreeImage.h"
#include 
<map>

class TextureManager
{
public:
    
static TextureManager* Inst();
    
virtual ~TextureManager();

    
//load a texture an make it the current texture
    
//if texID is already in use, it will be unloaded and replaced with this texture
    bool LoadTexture(const char* filename,    //where to load the file from
        const unsigned int texID,            //arbitrary id you will reference the texture by
                                            
//does not have to be generated with glGenTextures
        GLenum image_format = GL_RGB,        //format the image is in
        GLint internal_format = GL_RGB,        //format to store the image in
        GLint level = 0,                    //mipmapping level
        GLint border = 0);                    //border size

    
//free the memory for a texture
    bool UnloadTexture(const unsigned int texID);

    
//set the current texture
    bool BindTexture(const unsigned int texID);

    
//free all texture memory
    void UnloadAllTextures();

protected:
    TextureManager();
    TextureManager(
const TextureManager& tm);
    TextureManager
& operator=(const TextureManager& tm);

    
static TextureManager* m_inst;
    std::map
<unsigned int, GLuint> m_texID;
}
;

#endif

TextureManager.cpp
//**********************************************
//Singleton Texture Manager class
//Written by Ben English
//benjamin.english@oit.edu
//
//For use with OpenGL and the FreeImage library
//**********************************************

#include 
"TextureManager.h"

TextureManager
* TextureManager::m_inst(0);

TextureManager
* TextureManager::Inst()
{
    
if(!m_inst)
        m_inst 
= new TextureManager();

    
return m_inst;
}


TextureManager::TextureManager()
{
    
// call this ONLY when linking with FreeImage as a static library
    #ifdef FREEIMAGE_LIB
        FreeImage_Initialise();
    
#endif
}


//these should never be called
//TextureManager::TextureManager(const TextureManager& tm){}
//TextureManager& TextureManager::operator=(const TextureManager& tm){}
    
TextureManager::
~TextureManager()
{
    
// call this ONLY when linking with FreeImage as a static library
    #ifdef FREEIMAGE_LIB
        FreeImage_DeInitialise();
    
#endif

    UnloadAllTextures();
    m_inst 
= 0;
}


bool TextureManager::LoadTexture(const char* filename, const unsigned int texID, GLenum image_format, GLint internal_format, GLint level, GLint border)
{
    
//image format
    FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
    
//pointer to the image, once loaded
    FIBITMAP *dib(0);
    
//pointer to the image data
    BYTE* bits(0);
    
//image width and height
    unsigned int width(0), height(0);
    
//OpenGL's image ID to map to
    GLuint gl_texID;
    
    
//check the file signature and deduce its format
    fif = FreeImage_GetFileType(filename, 0);
    
//if still unknown, try to guess the file format from the file extension
    if(fif == FIF_UNKNOWN) 
        fif 
= FreeImage_GetFIFFromFilename(filename);
    
//if still unkown, return failure
    if(fif == FIF_UNKNOWN)
        
return false;

    
//check that the plugin has reading capabilities and load the file
    if(FreeImage_FIFSupportsReading(fif))
        dib 
= FreeImage_Load(fif, filename);
    
//if the image failed to load, return failure
    if(!dib)
        
return false;

    
//retrieve the image data
    bits = FreeImage_GetBits(dib);
    
//get the image width and height
    width = FreeImage_GetWidth(dib);
    height 
= FreeImage_GetHeight(dib);
    
//if this somehow one of these failed (they shouldn't), return failure
    if((bits == 0|| (width == 0|| (height == 0))
        
return false;
    
    
//if this texture ID is in use, unload the current texture
    if(m_texID.find(texID) != m_texID.end())
        glDeleteTextures(
1&(m_texID[texID]));

    
//generate an OpenGL texture ID for this texture
    glGenTextures(1&gl_texID);
    
//store the texture ID mapping
    m_texID[texID] = gl_texID;
    
//bind to the new texture ID
    glBindTexture(GL_TEXTURE_2D, gl_texID);
    
//store the texture data for OpenGL use
    glTexImage2D(GL_TEXTURE_2D, level, internal_format, width, height,
        border, image_format, GL_UNSIGNED_BYTE, bits);

    
//Free FreeImage's copy of the data
    FreeImage_Unload(dib);

    
//return success
    return true;
}


bool TextureManager::UnloadTexture(const unsigned int texID)
{
    
bool result(true);
    
//if this texture ID mapped, unload it's texture, and remove it from the map
    if(m_texID.find(texID) != m_texID.end())
    
{
        glDeleteTextures(
1&(m_texID[texID]));
        m_texID.erase(texID);
    }

    
//otherwise, unload failed
    else
    
{
        result 
= false;
    }


    
return result;
}


bool TextureManager::BindTexture(const unsigned int texID)
{
    
bool result(true);
    
//if this texture ID mapped, bind it's texture as current
    if(m_texID.find(texID) != m_texID.end())
        glBindTexture(GL_TEXTURE_2D, m_texID[texID]);
    
//otherwise, binding failed
    else
        result 
= false;

    
return result;
}


void TextureManager::UnloadAllTextures()
{
    
//start at the begginning of the texture map
    std::map<unsigned int, GLuint>::iterator i = m_texID.begin();

    
//Unload the textures untill the end of the texture map is found
    while(i != m_texID.end())
        UnloadTexture(i
->first);

    
//clear the texture map
    m_texID.clear();
}

Main.cpp
/************************************************************************/
/*Brief:  Test all kinds of Textures in OpenGL                          */
/*Author: tiny                                                          */
/*Date:   09/29/2008                                                    */
/************************************************************************/
#include 
"TextureManager.h"
#include 
<gl/glew.h>
#include 
<gl/glut.h>

//全局貼圖ID
GLuint texture[1];

void init()
{
    
//將2D貼圖狀態打開
    glEnable( GL_TEXTURE_2D );

    
//單件貼圖管理
    
//如果加載帶路徑的文件最好選用.\\這樣的格式
    TextureManager::Inst()->LoadTexture( "OpenGL_his.jpg", texture[0] );

    
//線性過濾一定要放到加載紋理的后面
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);    // 線性濾波
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);    // 線性濾波

    glClearColor( 
0.50.50.50.5 );
}


void display()
{
    glClear( GL_COLOR_BUFFER_BIT 
| GL_DEPTH_BUFFER_BIT );

    
//綁定紋理
    TextureManager::Inst()->BindTexture( texture[0] );

    
//渲染
    glBegin( GL_QUADS );
    glTexCoord2d( 
00 ); glVertex3f( -5.0f-5.0f0.0f );
    glTexCoord2d( 
01 ); glVertex3f( -5.0f5.0f0.0f );
    glTexCoord2d( 
11 ); glVertex3f( 5.0f5.0f0.0f );
    glTexCoord2d( 
10 ); glVertex3f( 5.0f-5.0f0.0f );
    glEnd();
    glFlush();

    glutSwapBuffers();
}


void reshape( int w, int h )
{
    glViewport( 
00, GLsizei( w ), GLsizei( h ) );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 
45, ( GLdouble ) w / ( GLdouble ) h, 1.0f1000.0f );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    gluLookAt( 
0020000010 );
}


void keyboard( unsigned char key, int x, int y )
{
    
if ( key == 27 )
    
{
        
//釋放掉貼圖,防止內存泄露
        TextureManager::Inst()->UnloadTexture( texture[0] );
        exit( 
0 );
    }

        
}



int main( int argc, char *argv[] )
{
    glutInit( 
&argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE 
| GLUT_RGBA | GLUT_DEPTH );
    glutInitWindowPosition( 
300300 );
    glutInitWindowSize( 
400300 );
    glutCreateWindow( 
"OpenGL Texture Test" );
    init();
    glutReshapeFunc( reshape );
    glutKeyboardFunc( keyboard );
    glutDisplayFunc( display );
    glutMainLoop();

    
return 0;
}
bmp效果圖:

tga效果圖:

jpg效果圖:

png效果圖:

有的圖的效果不是想要的,可能和RGB的順序有關。

posted on 2008-09-29 13:14 brilyf 閱讀(4384) 評論(5)  編輯 收藏 引用

評論

# re: OpenGL中用FreeImage  回復  更多評論   

不要意思,png和tga都是帶有alpha通道的,必須指定象素格式類型,如下:
TextureManager::Inst()->LoadTexture( "snowman.png", texture[0], GL_BGRA );
2008-09-29 13:48 | brilyf

# re: OpenGL中用FreeImage  回復  更多評論   

有人在嗎?究竟怎樣弄才能的到你這個效果呢?
2009-05-08 20:44 | hello opengl

# re: OpenGL中用FreeImage  回復  更多評論   

當我載入多個紋理時 只有1個有用...!!!!!!!!!!!
2012-01-27 11:15 | hrlqqq

# re: OpenGL中用FreeImage  回復  更多評論   

載入多個紋理時 只有一個有用...

代碼有問題??
2012-01-27 11:15 | hrlqqq

# re: OpenGL中用FreeImage  回復  更多評論   

是的,我也是驗證了很久,載入多個紋理時,只有最后載入的那個紋理才有用,前面的都無法使用。texture[i]沒有意義,無法通過使用texture[i]來bind指定的那個紋理
2012-05-17 12:27 | LHZorro
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美日韩国产系列| 亚洲一区二区三区三| 亚洲午夜一二三区视频| 欧美日韩在线播放| 亚洲欧美日韩精品久久奇米色影视 | 欧美在线一二三四区| 国产日本亚洲高清| 久热re这里精品视频在线6| 美女日韩在线中文字幕| 9久草视频在线视频精品| 99在线|亚洲一区二区| 国产精品黄视频| 卡一卡二国产精品| 欧美国产精品| 欧美一级视频精品观看| 久久亚洲国产成人| 亚洲桃色在线一区| 久久精品国产一区二区三区| 亚洲精品乱码久久久久久日本蜜臀 | 美日韩精品视频免费看| 欧美日韩三级一区二区| 久久国产精品久久精品国产 | 亚洲另类一区二区| 国产日韩欧美综合| 亚洲日韩欧美视频一区| 国产精品日韩在线观看| 欧美成人性生活| 欧美午夜剧场| 欧美电影免费观看高清| 国产精品美女久久久久久免费| 免费不卡亚洲欧美| 国产精品稀缺呦系列在线| 欧美福利网址| 国产日韩一区二区三区在线| 亚洲人成人77777线观看| 国产日韩免费| 一区二区日韩免费看| 亚洲国产美女精品久久久久∴| 亚洲天天影视| 日韩一级大片在线| 久久久精品一品道一区| 午夜国产精品影院在线观看| 欧美大片一区二区| 美女视频黄免费的久久| 国产欧美在线看| 一本久久a久久精品亚洲| 最新69国产成人精品视频免费 | 欧美大片va欧美在线播放| 国产精品欧美风情| 亚洲免费成人av| 亚洲国产视频一区| 老司机久久99久久精品播放免费 | 欧美xx69| 亚洲二区精品| 久久久久成人精品| 久久久久久网址| 国产一在线精品一区在线观看| 中文成人激情娱乐网| 亚洲午夜av电影| 欧美精品一区二| 亚洲激情成人网| 日韩视频一区二区| 欧美精品在线一区二区| 亚洲精品五月天| 夜夜嗨av一区二区三区四区| 欧美激情综合亚洲一二区| 亚洲福利视频一区二区| 91久久亚洲| 欧美日韩国产a| 9久草视频在线视频精品| 亚洲午夜在线观看视频在线| 欧美日韩一区综合| 亚洲一区日韩在线| 久久久777| 亚洲高清不卡在线| 麻豆国产精品va在线观看不卡| 亚洲高清影视| 在线视频亚洲一区| 国产精品入口夜色视频大尺度 | 亚洲一区三区电影在线观看| 久久精品在线播放| 亚洲国产精品成人va在线观看| 玖玖玖免费嫩草在线影院一区| 亚洲高清视频一区| 亚洲一区二区精品在线观看| 国产精品婷婷午夜在线观看| 欧美夜福利tv在线| 欧美插天视频在线播放| 99精品视频免费| 国产精品一区二区三区观看| 久久久九九九九| 日韩午夜在线视频| 久久免费少妇高潮久久精品99| 亚洲日本欧美在线| 国产精品黄色在线观看| 久久夜色精品国产欧美乱极品| 亚洲精品美女在线观看| 欧美呦呦网站| 在线视频精品一区| 国产在线视频不卡二| 欧美国产免费| 性色av香蕉一区二区| 亚洲国产综合在线| 久久久欧美精品sm网站| 99国产精品久久久| 伊人久久综合97精品| 国产精品第三页| 欧美成人tv| 午夜视频一区二区| 99re视频这里只有精品| 美女精品视频一区| 亚洲综合欧美| 最新日韩在线视频| 韩国精品久久久999| 欧美色欧美亚洲另类二区| 久久最新视频| 性做久久久久久免费观看欧美| 亚洲日本精品国产第一区| 美女精品网站| 久久夜色撩人精品| 欧美诱惑福利视频| 亚洲欧美偷拍卡通变态| 日韩亚洲视频| 亚洲精品少妇| 在线精品国产欧美| 国产在线一区二区三区四区 | 国产精品福利片| 欧美精品免费播放| 欧美激情精品久久久久久黑人| 久久国内精品视频| 香蕉尹人综合在线观看| 在线综合+亚洲+欧美中文字幕| 亚洲国产精品美女| 亚洲电影av| 欧美成人免费va影院高清| 美女图片一区二区| 女仆av观看一区| 欧美高清视频在线观看| 欧美大片一区二区三区| 美女尤物久久精品| 欧美第一黄网免费网站| 欧美a级一区| 欧美ab在线视频| 亚洲高清免费在线| 最新国产の精品合集bt伙计| 亚洲激情六月丁香| 一区二区免费在线播放| 日韩网站在线观看| 亚洲午夜在线观看视频在线| 亚洲午夜视频在线| 亚洲欧美一区在线| 久久狠狠亚洲综合| 牛人盗摄一区二区三区视频| 欧美大片免费看| 欧美肉体xxxx裸体137大胆| 欧美性猛片xxxx免费看久爱| 国产精自产拍久久久久久| 国产日韩久久| 91久久精品网| 亚洲免费视频中文字幕| 久久精品亚洲| 欧美华人在线视频| 一区二区三区视频在线看| 亚洲一区在线观看视频| 久久久久久色| 欧美三级韩国三级日本三斤| 国产私拍一区| 亚洲精选在线观看| 亚洲一区制服诱惑| 久久亚洲春色中文字幕| 亚洲国产精品va在线看黑人动漫| 亚洲片在线资源| 午夜精品免费| 另类激情亚洲| 国产精品久久九九| 亚洲第一天堂无码专区| 在线视频欧美日韩| 久久久久久网址| 日韩一二三在线视频播| 久久xxxx| 欧美日韩裸体免费视频| 韩国一区二区三区美女美女秀| 亚洲精品中文字幕在线| 久久爱www.| 亚洲人成人99网站| 久久久久国产精品厨房| 国产精品卡一卡二| 亚洲人成网站999久久久综合| 香蕉久久夜色精品国产使用方法| 欧美ed2k| 亚洲欧美另类久久久精品2019| 欧美电影免费观看| 激情成人综合网| 欧美自拍偷拍午夜视频| 一本大道久久a久久精品综合| 久久久久久久91| 国产亚洲精品美女| 亚洲免费影视| 亚洲精品一区二区网址| 欧美www在线|