diff --git a/src/libs/tgfclient/gui.cpp b/src/libs/tgfclient/gui.cpp index bab2e55b3..efda7fa77 100644 --- a/src/libs/tgfclient/gui.cpp +++ b/src/libs/tgfclient/gui.cpp @@ -203,6 +203,7 @@ GfuiDisplay(void) glColor3f(GfuiColor[GFUI_BASECOLORBGIMAGE][0],GfuiColor[GFUI_BASECOLORBGIMAGE][1],GfuiColor[GFUI_BASECOLORBGIMAGE][2]); glBindTexture(GL_TEXTURE_2D, GfuiScreen->bgImage); glBegin(GL_QUADS); + glTexCoord2f(tx1, ty1); glVertex3f(0.0, 0.0, 0.0); glTexCoord2f(tx1, ty2); glVertex3f(0.0, GfuiScreen->height, 0.0); glTexCoord2f(tx2, ty2); glVertex3f(GfuiScreen->width, GfuiScreen->height, 0.0); @@ -997,12 +998,10 @@ GfuiScreenAddBgImg(void *scr, const char *filename) return; } - glGenTextures(1, &screen->bgImage); - glBindTexture(GL_TEXTURE_2D, screen->bgImage); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)(tex)); + //Force Background image to power of 2 + GfScaleImagePowerof2((unsigned char*)tex,w,h,GL_RGBA,screen->bgImage); free(tex); + GfParmReleaseHandle(handle); } diff --git a/src/libs/tgfclient/img.cpp b/src/libs/tgfclient/img.cpp index 1d2bc14d4..4d6df8668 100644 --- a/src/libs/tgfclient/img.cpp +++ b/src/libs/tgfclient/img.cpp @@ -36,11 +36,81 @@ #ifdef WIN32 #include #endif +#include "GL/Glu.h" static char buf[1024]; #define PNG_BYTES_TO_CHECK 4 + + +unsigned char * +GfScaleImage(unsigned char *pSrcImg,int srcW,int srcH,int destW,int destH,GLenum format) +{ + unsigned char *pData = NULL; + gluScaleImage(format, srcW,srcH,GL_BYTE,pSrcImg,destW,destH,GL_BYTE,pData); + + return pData; +} + +int GetClosestPowerof2(int Size) +{ + int sizes[8] = {2,4,16,128,256,512,1024,2048 }; + + for (int i=0;i<8;i++) + { + if (Size<=sizes[i]) + return sizes[i]; + } + + //Do not allow textures larger then this for memory usage reasons + return 2048; +} + +void +GfScaleImagePowerof2(unsigned char *pSrcImg,int srcW,int srcH,GLenum format,GLuint &texId) +{ + int destH = 128; + int destW = 128; + + destH = GetClosestPowerof2(srcH); + destW = GetClosestPowerof2(srcW); + + if ((destH!=srcH)||(destW!=srcW)) + { + + unsigned char *texData = NULL; + if (format == GL_RGB) + { + texData = new unsigned char[destW*destH*3]; + } + else if(format == GL_RGBA) + { + texData = new unsigned char[destW*destH*4]; + + } + + int r = gluScaleImage( format, srcW,srcH,GL_UNSIGNED_BYTE,(void*)pSrcImg,destW,destH,GL_UNSIGNED_BYTE,(void*)texData); + if (r!=0) + printf("Error trying to scale image\n"); + + glGenTextures(1, &texId); + glBindTexture(GL_TEXTURE_2D, texId); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, destW, destH, 0, format, GL_UNSIGNED_BYTE, (GLvoid *)(texData)); + delete [] texData; + } + else + { + glGenTextures(1, &texId); + glBindTexture(GL_TEXTURE_2D, texId); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, format, destW, destH, 0, format, GL_UNSIGNED_BYTE, (GLvoid *)(pSrcImg)); + } +} + /** Load an image from disk to a buffer in RGBA mode. @ingroup img @param filename name of the image to load diff --git a/src/libs/tgfclient/tgfclient.h b/src/libs/tgfclient/tgfclient.h index e856c28e8..2760d3fd7 100644 --- a/src/libs/tgfclient/tgfclient.h +++ b/src/libs/tgfclient/tgfclient.h @@ -49,6 +49,7 @@ extern int GfImgWritePng(unsigned char *img, const char *filename, int width, in extern void GfImgFreeTex(GLuint tex); extern GLuint GfImgReadTex(const char *filename); extern GLuint GfImgReadTex(const char *filename,int &height,int &width); +extern void GfScaleImagePowerof2(unsigned char *pSrcImg,int srcW,int srcH,GLenum format,GLuint &texId); extern void GfScrInit(int argc, char *argv[]); extern void GfScrShutdown(void);