gl_texture.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Kutumov Alexey                                  *
00003  *   ru.pixel@gmail.com                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00033 #include "gl_graphic_core/gl_texture.h"
00034 #include "gl_graphic_core/gl_graphic_core.h"
00035 
00036 #include "gpucalc/log_manager.h"
00037 #include "gpucalc/application.h"
00038 #include "gpucalc/framebuffer.h"
00039 #include "gpucalc/string_util.h"
00040 #include "gpucalc/graphic_core.h"
00041 
00042 
00043 #include <GL/glew.h>
00044 
00045 namespace gpucalc
00046 {
00047  GLTexture::~GLTexture()
00048  {
00049   glDeleteTextures(1, &mGLTextureID);
00050   LogManager::getSingleton().logMessage(dynamic_cast<Bindable *>(this), "Texture ID deleted from OpenGL memory.", LML_Normal);
00051  }
00052 
00053  
00054  void GLTexture::initialize()
00055  {
00056   bind();
00057 
00058   glTexParameteri(mGLTextureTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00059   glTexParameteri(mGLTextureTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00060   glTexParameteri(mGLTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP);
00061   glTexParameteri(mGLTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP);
00062 
00063   glTexImage2D(mGLTextureTarget, 0, mGLTextureInternalFormat, mSizeX, mSizeY, 0, mGLTextureFormat, mGLTextureType, 0);
00064   mDefaultGraphicCore->checkErrors("GLTexture::setData");
00065  }
00066 
00067  
00068  void GLTexture::uploadToGPU()
00069  {
00070   if (!mIsUploadedToGPU)
00071   {
00072    switch (mDefaultGraphicCore->getVendor())
00073    {
00074     case NVIDIA:
00075     case MESA:
00076     case ATI:
00077      setNVData();
00078      break;
00079 
00080     //case ATI:
00081     // setATIData();
00082     // break;
00083 
00084     default:
00085      Except<ERR_NOT_IMPLEMENTED>(dynamic_cast<Bindable *>(this), "Uploading data for non-nvidia and non-ati cards is not implemented!", "GLTexture::setData()", __FILE__, __LINE__);
00086      break;
00087    }
00088    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00089    mIsUploadedToGPU = true;
00090    mDefaultGraphicCore->checkErrors("GLTexture::uploadToGPU()");
00091   }
00092  }
00093 
00094 
00095  void GLTexture::setNVData()
00096  {
00097   bind();
00098 
00099   size_t RequestedLengthGPU = mSizeX * mSizeY;
00100 
00101   if (mActualLengthGPU != RequestedLengthGPU)
00102   {
00103    Except<ERR_NOT_IMPLEMENTED>(dynamic_cast<Bindable *>(this), "Uploading non-rectangle data is not implemented!", "GLTexture::setNVData()", __FILE__, __LINE__);
00104 
00105    Warning<ERR_INVALIDPARAMS>(dynamic_cast<Bindable *>(this), "We use some external bytes to upload non-rectangle data in GPU-side.", "GLTexture::setNVData()", __FILE__, __LINE__);
00106 
00107    size_t NumberOfFullRows = mActualLengthGPU / mSizeX;
00108    size_t LengthOfLastRow = mActualLengthGPU - NumberOfFullRows * mSizeX;
00109 
00111    glTexSubImage2D(mGLTextureTarget, 0, 0, 0, mSizeX, NumberOfFullRows, mGLTextureFormat, mGLTextureType, mGLPointer);
00112 
00114    size_t PointerOffset = NumberOfFullRows * mSizeX * mNumberOfComponents * mTypeSize;
00115    glTexSubImage2D(mGLTextureTarget, 0, 0, NumberOfFullRows, LengthOfLastRow, 1, mGLTextureFormat, mGLTextureType,
00116     static_cast<char *>(mGLPointer) + PointerOffset);
00117   }
00118   else
00119   {
00120    glTexSubImage2D(mGLTextureTarget, 0, 0, 0, mSizeX, mSizeY, mGLTextureFormat, mGLTextureType, mGLPointer);
00121    LogManager::getSingleton().logMessage(dynamic_cast<Bindable *>(this), "OpenGL texture uploaded data using NVIDIA specific method. ID: " + 
00122     auxillary::StringUtil::toString(mGLTextureID), LML_Trivial);
00123   }
00124  }
00125  
00126  
00127  void GLTexture::setATIData()
00128  {
00129   size_t RequestedLengthGPU = mSizeX * mSizeY;
00130 
00131   if (mActualLengthGPU  == RequestedLengthGPU)
00132   {
00133    FrameBuffer * Helper = mDefaultGraphicCore->getActiveFrameBuffer();
00134    Helper->bind();
00135    {
00136     Helper->drawBuffer(this);
00137     glRasterPos2i(0,0);
00138     glDrawPixels(mSizeX, mSizeY, mGLTextureFormat, mGLTextureType, mGLPointer);
00139     Helper->detach(this);
00140    }
00141    Helper->unbind();
00142    LogManager::getSingleton().logMessage(dynamic_cast<Bindable *>(this), "OpenGL texture uploaded data using ATI specific method. ID: " + 
00143     auxillary::StringUtil::toString(mGLTextureID), LML_Trivial);
00144   }
00145   else
00146   {
00147    Except<ERR_NOT_IMPLEMENTED>(dynamic_cast<Bindable *>(this), "Uploading non-rectangle data is not implemented!", "GLTexture::setATIData()", __FILE__, __LINE__);
00148   }
00149 
00150   //bind();
00151  }
00152 }

Generated on Thu Mar 5 22:36:42 2009 for gpucalc by  doxygen 1.5.6