glfxil.h

00001 /*-------------------------------------------------------------------------
00002 This source code is part of GLfx library (OpenGL effect library)
00003 For more information and updates see http://glfx.sourceforge.net/
00004 Copyright (C) 2005-6 Tomek Rozen <trozen at users sourceforge net>
00005 
00006 This library is free software; you can redistribute it and/or modify it
00007 under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation; either version 2.1 of the License,
00009 or (at your option) any later version.
00010 
00011 This library is distributed in the hope that it will be useful, but WITHOUT
00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00014 for more details.
00015 -------------------------------------------------------------------------*/
00016 
00017 #ifndef __GLFXIL_H__
00018 #define __GLFXIL_H__
00019 
00020 #include <IL/il.h>
00021 
00022 #ifdef _MSC_VER
00023 #pragma comment(lib, "DevIL")
00024 #endif
00025 
00026 namespace GLFX {
00027 
00030 class EffectManager_DevIL : public EffectManager
00031 {
00032 public:
00033 
00034     EffectManager_DevIL() {
00035         ilInit();
00036         ilSetRead(0, 0, MyEofProc, MyGetcProc, MyReadProc, MySeekProc, MyTellProc);
00037         ilEnable(IL_ORIGIN_SET);
00038         ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
00039         MessageFormat(LOG_NOTICE, "This program uses DevIL (Developers Image Library) version %d, (C) %s.",
00040             ilGetInteger(IL_VERSION_NUM), ilGetString(IL_VENDOR));
00041     }
00042 
00043     ~EffectManager_DevIL() {
00044         ilShutDown();
00045     }
00046 
00051     bool FillTextureData(GLenum target, const char* name) {
00052         if(EffectManager::FillTextureData(target, name)) {
00053             return true;
00054         }
00055         if(target == GL_TEXTURE_1D) {
00056             return false;
00057         } else if(target == GL_TEXTURE_3D) {
00058             return false;
00059         } else if(target == GL_TEXTURE_CUBE_MAP) {
00060             return false;
00061         } else {
00062             FileHandle file;
00063             file.fxMgr = this;
00064             file.file = OpenFile(name, false);
00065             bool ok = false;
00066             if(file.file) {
00067                 ILuint image;
00068                 ilGenImages(1, &image);
00069                 ilBindImage(image);
00070                 if(ilLoadF(IL_TYPE_UNKNOWN, (ILHANDLE)&file))
00071                 {
00072                     ILint width = ilGetInteger(IL_IMAGE_WIDTH);
00073                     ILint height = ilGetInteger(IL_IMAGE_HEIGHT);
00074                     GLint glcomp;
00075                     GLenum glformat;
00076                     ILenum ilformat = ilGetInteger(IL_IMAGE_FORMAT);
00077                     switch(ilformat)
00078                     {
00079                     case IL_LUMINANCE:
00080                         glformat = GL_LUMINANCE;
00081                         glcomp = 1;
00082                         break;
00083                     case IL_LUMINANCE_ALPHA:
00084                         glformat = GL_LUMINANCE_ALPHA;
00085                         glcomp = 2;
00086                         break;
00087                     default:
00088                         glformat = GL_BGRA;
00089                         ilformat = IL_BGRA;
00090                         glcomp = 4;
00091                         break;
00092                     }
00093 
00094                     ilConvertImage(ilformat, IL_UNSIGNED_BYTE);
00095                     GLubyte* data = ilGetData();
00096 
00097                     gluBuild2DMipmaps(target, 4, width, height, GL_BGRA, GL_UNSIGNED_BYTE, data);
00098 
00099                     ok = true;
00100                 }
00101                 // Unbind and delete image
00102                 ilBindImage(0);
00103                 ilDeleteImages(1, &image);
00104                 CloseFile(file.file);
00105             }
00106             if(!ok) {
00107                 return false;
00108             }
00109             return true;
00110         }
00111     }
00112 
00113 private:
00114 
00115     struct FileHandle
00116     {
00117         EffectManager_DevIL* fxMgr;
00118         void* file;
00119     };
00120 
00121     static ILboolean ILAPIENTRY MyEofProc(ILHANDLE handle) {
00122         FileHandle* fh = (FileHandle*)handle;
00123         fh->fxMgr->Eof(fh->file);
00124         return false;
00125     }
00126 
00127     static ILint ILAPIENTRY MyGetcProc(ILHANDLE handle) {
00128         unsigned char c;
00129         FileHandle* fh = (FileHandle*)handle;
00130         if(fh->fxMgr->ReadFile(fh->file, &c, 1) != 1) {
00131             return EOF;
00132         }
00133         return c;
00134     }
00135 
00136     static ILint ILAPIENTRY MyReadProc(void *buffer, ILuint size, ILuint count, ILHANDLE handle) {
00137         FileHandle* fh = (FileHandle*)handle;
00138         return fh->fxMgr->ReadFile(fh->file, buffer, size * count);
00139     }
00140 
00141     static ILint ILAPIENTRY MySeekProc(ILHANDLE handle, ILint offset, ILint origin) {
00142         FileHandle* fh = (FileHandle*)handle;
00143         return fh->fxMgr->SeekFile(fh->file, offset, origin);
00144     }
00145 
00146     static ILint ILAPIENTRY MyTellProc(ILHANDLE handle) {
00147         FileHandle* fh = (FileHandle*)handle;
00148         return fh->fxMgr->TellFile(fh->file);
00149     }
00150 };
00151 
00152 }
00153 
00154 #endif // __GLFXIL_H__

SourceForge.net Logo