KeosOGLTexture.cpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 #include "KeosOGLTexture.h"
00022 #include "KeosOGLEnum.h"
00023 #include "KeosOGLRenderSystem.h"
00024 #include "KeosPixelUtils.h"
00025 #include "KeosRectangle.h"
00026 
00027 namespace Keos
00028 {
00029   //=======================================================================
00030   // COGLTexture implementation
00031   //=======================================================================
00032 
00033   //-----------------------------------------------------------------------
00034   COGLTexture::COGLTexture(const TVector2I& Size, TPixelFormat Format, bool bHasMipmaps, bool bAutoMipmaps, uint nTexture) :
00035       ITextureBase(Size, Format, bHasMipmaps, bAutoMipmaps),
00036       m_nTexture   (nTexture)
00037   {}
00038 
00039   //-----------------------------------------------------------------------
00040   COGLTexture::~COGLTexture()
00041   {
00042     if (m_nTexture)
00043       glDeleteTextures(1, &m_nTexture);
00044   }
00045 
00046   //-----------------------------------------------------------------------
00047   uint COGLTexture::GetOGLTexture() const
00048   {
00049     return m_nTexture;
00050   }
00051 
00052   //-----------------------------------------------------------------------
00053   void COGLTexture::Update(const CRectangle& Rect)
00054   {
00055     Assert(CRectangle(0, 0, m_Size.x, m_Size.y).Intersects(Rect) == INT_IN);
00056 
00057     // Get the formats of pixels of the texture and the image
00058     COGLEnum::TPixelFmt TexFmt = COGLEnum::Get(m_Format);
00059     COGLEnum::TPixelFmt ImgFmt = COGLEnum::Get(m_Data.GetFormat());
00060 
00061     // Activation of the current texture
00062     glBindTexture(GL_TEXTURE_2D, m_nTexture);
00063 
00064     // Update pixels of the texture
00065     if (FormatCompressed(m_Data.GetFormat()))
00066     {
00067       // Format de pixel compressé - on utilise une extension pour uploader les pixels
00068       ulong DataSize = Rect.Width() * Rect.Height() * GetBytesPerPixel(m_Data.GetFormat());
00069       if (Rect.Width() == m_Size.x && Rect.Height() == m_Size.y)
00070       {
00071         COGLRenderSystem::glCompressedTexSubImage2DARB(GL_TEXTURE_2D, 0, Rect.Left(), Rect.Top(), Rect.Width(), Rect.Height(), ImgFmt.Format, DataSize, m_Data.GetData());
00072       }
00073       else
00074       {
00075         CImage SubData = m_Data.SubImage(Rect);
00076         COGLRenderSystem::glCompressedTexSubImage2DARB(GL_TEXTURE_2D, 0, Rect.Left(), Rect.Top(), Rect.Width(), Rect.Height(), ImgFmt.Format, DataSize, SubData.GetData());
00077       }
00078     }
00079     else
00080     {
00081       if (!m_bHasMipmaps || m_bAutoMipmaps)
00082       {
00083         if ((Rect.Width() == m_Size.x) && (Rect.Height() == m_Size.y))
00084         {
00085           glTexSubImage2D(GL_TEXTURE_2D, 0, Rect.Left(), Rect.Top(), Rect.Width(), Rect.Height(), ImgFmt.Format, ImgFmt.Type, m_Data.GetData());
00086         }
00087         else
00088         {
00089           CImage SubData = m_Data.SubImage(Rect);
00090           glTexSubImage2D(GL_TEXTURE_2D, 0, Rect.Left(), Rect.Top(), Rect.Width(), Rect.Height(), ImgFmt.Format, ImgFmt.Type, SubData.GetData());
00091         }
00092       }
00093       else
00094       {
00095         gluBuild2DMipmaps(GL_TEXTURE_2D, TexFmt.Internal, m_Size.x, m_Size.y, ImgFmt.Format, ImgFmt.Type, m_Data.GetData());
00096       }
00097     }
00098 
00099     // Deactivation of the texture
00100     glBindTexture(GL_TEXTURE_2D, 0);
00101   }
00102 
00103 } // namespace Keos

Generated on Fri Mar 9 14:29:04 2007 for Keos by  doxygen 1.5.1-p1