无尽的夜空,唯有月光撒满窗前

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  14 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks

常用链接

留言簿

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

thisIsInitGL(){
loadTexture();
}
///Now the interesting code:
loadTexture()
{
        FREE_IMAGE_FORMAT formato = FreeImage_GetFileType(textureFile,0);//Automatocally detects the format(from over 20 formats!)
        FIBITMAP* imagen = FreeImage_Load(formato, textureFile);
        
        FIBITMAP* temp = imagen;
        imagen = FreeImage_ConvertTo32Bits(imagen);
        FreeImage_Unload(temp);
        
        int w = FreeImage_GetWidth(imagen);
        int h = FreeImage_GetHeight(imagen);
        cout<<"The size of the image is: "<<textureFile<<" es "<<w<<"*"<<h<<endl; //Some debugging code
        
        GLubyte* textura = new GLubyte[4*w*h];
        char* pixeles = (char*)FreeImage_GetBits(imagen);
        //FreeImage loads in BGR format, so you need to swap some bytes(Or use GL_BGR).
        
        for(int j= 0; j<w*h; j++){
                textura[j*4+0]= pixeles[j*4+2];
                textura[j*4+1]= pixeles[j*4+1];
                textura[j*4+2]= pixeles[j*4+0];
                textura[j*4+3]= pixeles[j*4+3];
                //cout<<j<<": "<<textura[j*4+0]<<"**"<<textura[j*4+1]<<"**"<<textura[j*4+2]<<"**"<<textura[j*4+3]<<endl;
        }
        
        //Now generate the OpenGL texture object 
        
        glGenTextures(1, &texturaID);
        glBindTexture(GL_TEXTURE_2D, texturaID);
        glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, w, h, 0, GL_RGBA,GL_UNSIGNED_BYTE,(GLvoid*)textura );
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        
        GLenum huboError = glGetError();
        if(huboError){
                
                cout<<"There was an error loading the texture"<<endl;
        }
        

}
posted on 2012-01-11 13:35 skyline 阅读(913) 评论(1)  编辑 收藏 引用

Feedback

# re: 用freeimage 生成opengl texture 2013-12-23 13:55 shift0ogg
建议这样效率更高些
int glmLoadTexture(const char* filename,
const unsigned int texID)
{
//image format
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
FIBITMAP *dib = NULL , *dib2 = NULL ;
BYTE ss ;
int pix = 0 ;

BYTE* bits = NULL;
//image width and height
unsigned int w = 0 , h = 0 ;
//OpenGL's image ID to map to
GLuint _texID = 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 0;

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

dib2 = FreeImage_ConvertTo32Bits(dib);
FreeImage_Unload(dib);





//retrieve the image data
bits = FreeImage_GetBits(dib2);
//get the image width and height
w = FreeImage_GetWidth(dib2);
h = FreeImage_GetHeight(dib2);
//if this somehow one of these failed (they shouldn't), return failure
if((bits == 0) || (w == 0) || (h == 0))
return 0;

//if this texture ID is in use, unload the current texture

for( pix=0; pix< w*h ; pix++)
{
ss=bits[pix*4+0];
bits[pix*4+0] = bits[pix*4+2] ;
bits[pix*4+2] = ss ;
}



if(glIsTexture(_texID))
glDeleteTextures(1, &_texID);

//generate an OpenGL texture ID for this texture
//glGenTextures(1, &_texID);

//bind to the new texture ID
glBindTexture(GL_TEXTURE_2D, _texID);


if (((w & (w-1)) == 0) && ((h & (h-1)) == 0))
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, bits);
else
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, w, h, GL_RGBA, GL_UNSIGNED_BYTE, bits);

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

return 1 ;
}
  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理