2 Copyright (C) 2003 Rice1964
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "OGLGraphicsContext.h"
25 #include "OGLTexture.h"
26 #include "TextureManager.h"
28 COGLTexture::COGLTexture(uint32 dwWidth, uint32 dwHeight, TextureUsage usage) :
29 CTexture(dwWidth,dwHeight,usage),
32 // FIXME: If usage is AS_RENDER_TARGET, we need to create pbuffer instead of regular texture
34 m_dwTextureFmt = TEXTURE_FMT_A8R8G8B8; // Always use 32bit to load texture
35 glGenTextures( 1, &m_dwTextureName );
38 // Make the width and height be the power of 2
40 for (w = 1; w < dwWidth; w <<= 1);
41 m_dwCreatedTextureWidth = w;
42 for (w = 1; w < dwHeight; w <<= 1);
43 m_dwCreatedTextureHeight = w;
45 if (dwWidth*dwHeight > 256*256)
46 TRACE4("Large texture: (%d x %d), created as (%d x %d)",
47 dwWidth, dwHeight,m_dwCreatedTextureWidth,m_dwCreatedTextureHeight);
49 m_fYScale = (float)m_dwCreatedTextureHeight/(float)m_dwHeight;
50 m_fXScale = (float)m_dwCreatedTextureWidth/(float)m_dwWidth;
52 m_pTexture = malloc(m_dwCreatedTextureWidth * m_dwCreatedTextureHeight * GetPixelSize());
54 switch( options.textureQuality )
56 case TXT_QUALITY_DEFAULT:
57 if( options.colorQuality == TEXTURE_FMT_A4R4G4B4 )
60 case TXT_QUALITY_32BIT:
62 case TXT_QUALITY_16BIT:
66 LOG_TEXTURE(TRACE2("New texture: (%d, %d)", dwWidth, dwHeight));
69 COGLTexture::~COGLTexture()
71 // FIXME: If usage is AS_RENDER_TARGET, we need to destroy the pbuffer
73 glDeleteTextures(1, &m_dwTextureName );
81 bool COGLTexture::StartUpdate(DrawInfo *di)
83 if (m_pTexture == NULL)
86 di->dwHeight = (uint16)m_dwHeight;
87 di->dwWidth = (uint16)m_dwWidth;
88 di->dwCreatedHeight = m_dwCreatedTextureHeight;
89 di->dwCreatedWidth = m_dwCreatedTextureWidth;
90 di->lpSurface = m_pTexture;
91 di->lPitch = GetPixelSize()*m_dwCreatedTextureWidth;
96 void COGLTexture::EndUpdate(DrawInfo *di)
98 COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext); // we need this to check if the GL extension is avaible
100 glBindTexture(GL_TEXTURE_2D, m_dwTextureName);
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
107 if(options.mipmapping)
109 int m_maximumAnistropy = pcontext->getMaxAnisotropicFiltering(); //if getMaxAnisotropicFiltering() return more than 0, so aniso is supported and maxAnisotropicFiltering is set
111 // Set Anisotropic filtering (mipmapping have to be activated, aniso filtering is not effective without)
112 if( m_maximumAnistropy )
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, m_maximumAnistropy);
119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
123 // Tell to hardware to generate mipmap (himself) when glTexImage2D is called
124 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
125 #elif SDL_VIDEO_OPENGL_ES2
126 glGenerateMipmap(GL_TEXTURE_2D);
132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
136 // Copy the image data from main memory to video card texture memory
138 glTexImage2D(GL_TEXTURE_2D, 0, m_glFmt, m_dwCreatedTextureWidth, m_dwCreatedTextureHeight, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, m_pTexture);
139 #elif SDL_VIDEO_OPENGL_ES2
140 //GL_BGRA_IMG works on adreno but not inside profiler.
141 glTexImage2D(GL_TEXTURE_2D, 0, m_glFmt, m_dwCreatedTextureWidth, m_dwCreatedTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_pTexture);
147 // Keep in mind that the real texture is not scaled to fix the created opengl texture yet.
148 // when the image is need to be scaled, ScaleImageToSurface in CTexure will be called to
149 // scale the image automatically