Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

GLUpdaterGLTexturingVisitor.cpp

Go to the documentation of this file.
00001 #include "GLUpdaterGLTexturingVisitor.h"
00002 #include "GLImageTexture.h"
00003 #include "ImageTexture.h"
00004 
00005 #include <math.h>
00006 #include <GL/gl.h>
00007 #include <qimage.h>
00008 #include <qgl.h>
00009 #include <qstring.h>
00010 
00011 #include <iostream>
00012 
00013 using namespace X3DTK;
00014 using namespace std;
00015 
00016 GLUpdaterGLTexturingVisitor::GLUpdaterGLTexturingVisitor()
00017 : GLTexturingVisitor()
00018 {
00019   // Enter functions.
00020   defineNewEnterFunction<GLUpdaterGLTexturingVisitor, GLImageTexture>(&GLUpdaterGLTexturingVisitor::enterGLImageTexture);
00021 
00022 
00023   // GlobalVariables assignation.
00024   globalVariables = GVManager::getInstanceOf<GLUpdaterGlobalVariables>();
00025 }
00026 
00027 GLUpdaterGLTexturingVisitor::~GLUpdaterGLTexturingVisitor()
00028 {
00029 }
00030 
00031 void GLUpdaterGLTexturingVisitor::enterGLImageTexture(GLImageTexture *G) const
00032 {
00033   ImageTexture *I = static_cast<ImageTexture *>(G->getLink());
00034   
00035   MFString url = I->getUrl();
00036   if (url.empty())
00037     return;
00038   
00039   const char *file = SFString(globalVariables->getPath() + "/" + url.front());
00040   
00041   QImage img(file);
00042   
00043   if (img.isNull())
00044   {
00045     cerr << "unable to load the texture from " << file << ", unsupported file format" << endl;
00046     return;
00047   }
00048   // 1E-3 needed. Just try with width=128 and see !
00049   int newWidth  = 1<<(int)(1+log(img.width() -1+1E-3) / log(2.0));
00050   int newHeight = 1<<(int)(1+log(img.height()-1+1E-3) / log(2.0));
00051 
00052   //set the ratios for the next geometry node
00053   SFFloat ratioS = (float)(img.width())/(float)(newWidth);
00054   SFFloat ratioT = (float)(img.height())/(float)(newHeight);
00055   
00056   G->getTextureTransform()[0] = ratioS;
00057   G->getTextureTransform()[12] = -ratioS + 2.0f;
00058   
00059   G->getTextureTransform()[5] = ratioT;
00060   G->getTextureTransform()[13] = 1.0f - ratioT;
00061         
00062   if ((img.width() != newWidth) || (img.height() != newHeight))
00063     img = img.copy(0, 0, newWidth, newHeight);
00064 
00065   QImage glImg = QGLWidget::convertToGLFormat(img);  // flipped 32bit RGBA
00066 
00067   // GL commands
00068   glGenTextures(1, &G->getTexName());
00069   
00070   glBindTexture(GL_TEXTURE_2D, G->getTexName());
00071   
00072   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00073   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00074 
00075   if (I->getRepeatS())
00076     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00077   else
00078     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00079     
00080   if (I->getRepeatT())
00081     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00082   else
00083     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00084     
00085   glTexImage2D(GL_TEXTURE_2D, 0, 3, glImg.width(), glImg.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, glImg.bits());
00086 }
00087 

Generated on Wed May 14 10:03:09 2003 for X3DToolKit by doxygen1.3