00001 #ifndef __GLTEX__
00002 #define __GLTEX__
00003
00004 #ifdef WIN32
00005 #include <windows.h>
00006 #endif
00007
00008 #include <GL/gl.h>
00009 #include <GL/glu.h>
00010 #include "CTexture.h"
00011
00012 namespace gltex
00013 {
00014
00018 inline GLuint gltexIdFromTexture(const CTexture *tex)
00019 {
00020 unsigned int id;
00021
00022 glGenTextures(1,&id);
00023 glBindTexture(GL_TEXTURE_2D,id);
00024 if (tex->isAlpha())
00025 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, tex->getWidth(), tex->getHeight(),
00026 GL_RGBA, GL_UNSIGNED_BYTE, tex->getData());
00027 else
00028 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, tex->getWidth(), tex->getHeight(),
00029 GL_RGB, GL_UNSIGNED_BYTE, tex->getData());
00030 return (id);
00031 }
00032
00036 inline GLuint gltexLoadTexture(const char *n)
00037 {
00038 unsigned int id;
00039 CTexture *tex=CTexture::loadTexture(n);
00040 id=gltexIdFromTexture(tex);
00041 delete (tex);
00042 return (id);
00043 }
00044
00048 inline void gltexUpdateTextureId(GLuint id,const CTexture *tex)
00049 {
00050 glBindTexture(GL_TEXTURE_2D,id);
00051 if (tex->isAlpha())
00052 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, tex->getWidth(), tex->getHeight(),
00053 GL_RGBA, GL_UNSIGNED_BYTE, tex->getData());
00054 else
00055 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, tex->getWidth(), tex->getHeight(),
00056 GL_RGB, GL_UNSIGNED_BYTE, tex->getData());
00057 }
00058
00063 inline CTexture *gltexTextureFromId(GLuint id,GLenum format=GL_RGB)
00064 {
00065 int w,h;
00066 unsigned char *pixels;
00067 CTexture *tex;
00068
00069 glBindTexture(GL_TEXTURE_2D,id);
00070 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&w);
00071 glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&h);
00072 pixels = new unsigned char [w*h*(format==GL_RGBA?4:3)];
00073 glGetTexImage(GL_TEXTURE_2D,0,format,GL_UNSIGNED_BYTE,pixels);
00074 tex=new CTexture("gltexTextureFromId",w,h,(format==GL_RGBA),pixels);
00075 tex->setDataOwner(true);
00076 return (tex);
00077 }
00078
00082 inline CTexture *gltexTextureFromScreen(GLenum format=GL_RGB)
00083 {
00084 GLint v[4];
00085 unsigned char *pixels;
00086 CTexture *tex;
00087
00088 glGetIntegerv(GL_VIEWPORT,v);
00089 pixels = new unsigned char [v[2]*v[3]*(format==GL_RGBA?4:3)];
00090 glReadPixels(v[0],v[1],v[2],v[3],format,GL_UNSIGNED_BYTE,pixels);
00091 tex=new CTexture("gltexTextureFromScreen",v[2],v[3],(format==GL_RGBA),pixels);
00092 tex->setDataOwner(true);
00093 return (tex);
00094 }
00095
00099 inline CTexture *gltexTextureFromDepth()
00100 {
00101 GLint v[4];
00102 int i,j,w,h;
00103 unsigned char *depth,*rgb;
00104 CTexture *tex;
00105
00106 glGetIntegerv(GL_VIEWPORT,v);
00107 depth = new unsigned char [v[2]*v[3]];
00108 rgb=new unsigned char[v[2]*v[3]*3];
00109 glReadPixels(v[0],v[1],v[2],v[3],
00110 GL_DEPTH_COMPONENT,GL_UNSIGNED_BYTE,depth);
00111 w=v[2];
00112 h=v[3];
00113 for (i=0;i<w;i++)
00114 {
00115 for (j=0;j<h;j++)
00116 {
00117 rgb[(i+(h-j-1)*w)*3 ]=depth[i+(h-j-1)*w];
00118 rgb[(i+(h-j-1)*w)*3+1]=depth[i+(h-j-1)*w];
00119 rgb[(i+(h-j-1)*w)*3+2]=depth[i+(h-j-1)*w];
00120 }
00121 }
00122 tex=new CTexture("gltexTextureFromDepth",w,h,false,rgb);
00123 delete (depth);
00124 tex->setDataOwner(true);
00125 return (tex);
00126 }
00127
00131 inline CTexture *gltexTextureFromStencil()
00132 {
00133 GLint v[4];
00134 int i,j,w,h;
00135 unsigned char *stencil,*rgb;
00136 CTexture *tex;
00137
00138 glGetIntegerv(GL_VIEWPORT,v);
00139 stencil = new unsigned char [v[2]*v[3]];
00140 rgb=new unsigned char[v[2]*v[3]*3];
00141 glReadPixels(v[0],v[1],v[2],v[3],
00142 GL_STENCIL_INDEX,GL_UNSIGNED_BYTE,stencil);
00143 w=v[2];
00144 h=v[3];
00145 for (i=0;i<w;i++)
00146 {
00147 for (j=0;j<h;j++)
00148 {
00149 rgb[(i+(h-j-1)*w)*3 ]=stencil[i+(h-j-1)*w];
00150 rgb[(i+(h-j-1)*w)*3+1]=stencil[i+(h-j-1)*w];
00151 rgb[(i+(h-j-1)*w)*3+2]=stencil[i+(h-j-1)*w];
00152 }
00153 }
00154 tex=new CTexture("gltexTextureFromStencil",w,h,false,rgb);
00155 delete (stencil);
00156 tex->setDataOwner(true);
00157 return (tex);
00158 }
00159
00160
00164 inline GLuint gltexConvertToNormal(GLuint id,double scale,double norme=1.0)
00165 {
00166 CTexture *tex=gltexTextureFromId(id,GL_RGB);
00167 tex->convertToNormal(scale,norme);
00168 GLuint glid=gltex::gltexIdFromTexture(tex);
00169 delete (tex);
00170 return (glid);
00171 }
00172 };
00173
00174 #endif