Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# 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
    00041     //-----------------------------------------------------------------------
    00042     COGLTexture::~COGLTexture()
    00043     {
    00044         if (m_nTexture)
    00045             glDeleteTextures(1, &m_nTexture);
    00046     }
    00047
    00048     //-----------------------------------------------------------------------
    00049     uint COGLTexture::GetOGLTexture() const
    00050     {
    00051         return m_nTexture;
    00052     }
    00053
    00054     //-----------------------------------------------------------------------
    00055     void COGLTexture::Update(const CRectangle& Rect)
    00056     {
    00057         Assert(CRectangle(0, 0, m_Size.x, m_Size.y).Intersects(Rect) == INT_IN);
    00058
    00059         // Get the formats of pixels of the texture and the image
    00060         COGLEnum::TPixelFmt TexFmt = COGLEnum::Get(m_Format);
    00061         COGLEnum::TPixelFmt ImgFmt = COGLEnum::Get(m_Data.GetFormat());
    00062
    00063         // Activation of the current texture
    00064         glBindTexture(GL_TEXTURE_2D, m_nTexture);
    00065
    00066         // Update pixels of the texture
    00067         if (FormatCompressed(m_Data.GetFormat()))
    00068         {
    00069             // Format de pixel compressé - on utilise une extension pour uploader les pixels
    00070             ulong DataSize = Rect.Width() * Rect.Height() * GetBytesPerPixel(m_Data.GetFormat());
    00071             if (Rect.Width() == m_Size.x && Rect.Height() == m_Size.y)
    00072             {
    00073                 COGLRenderSystem::glCompressedTexSubImage2DARB(GL_TEXTURE_2D, 0, Rect.Left(), Rect.Top(), Rect.Width(), Rect.Height(), ImgFmt.Format, DataSize, m_Data.GetData());
    00074             }
    00075             else
    00076             {
    00077                 CImage SubData = m_Data.SubImage(Rect);
    00078                 COGLRenderSystem::glCompressedTexSubImage2DARB(GL_TEXTURE_2D, 0, Rect.Left(), Rect.Top(), Rect.Width(), Rect.Height(), ImgFmt.Format, DataSize, SubData.GetData());
    00079             }
    00080         }
    00081         else
    00082         {
    00083             if (!m_bHasMipmaps || m_bAutoMipmaps)
    00084             {
    00085                 if ((Rect.Width() == m_Size.x) && (Rect.Height() == m_Size.y))
    00086                 {
    00087                     glTexSubImage2D(GL_TEXTURE_2D, 0, Rect.Left(), Rect.Top(), Rect.Width(), Rect.Height(), ImgFmt.Format, ImgFmt.Type, m_Data.GetData());
    00088                 }
    00089                 else
    00090                 {
    00091                     CImage SubData = m_Data.SubImage(Rect);
    00092                     glTexSubImage2D(GL_TEXTURE_2D, 0, Rect.Left(), Rect.Top(), Rect.Width(), Rect.Height(), ImgFmt.Format, ImgFmt.Type, SubData.GetData());
    00093                 }
    00094             }
    00095             else
    00096             {
    00097                 gluBuild2DMipmaps(GL_TEXTURE_2D, TexFmt.Internal, m_Size.x, m_Size.y, ImgFmt.Format, ImgFmt.Type, m_Data.GetData());
    00098             }
    00099         }
    00100
    00101         // Deactivation of the texture
    00102         glBindTexture(GL_TEXTURE_2D, 0);
    00103     }
    00104
    00105 } // namespace Keos