1 /*
2 Copyright (C) 2003 Rice1964
3 
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.
8 
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.
13 
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.
17 */
18 
19 #include <stdlib.h>
20 
21 #include "Config.h"
22 #include "Debugger.h"
23 #include "OGLGraphicsContext.h"
24 #include "OGLTexture.h"
25 #include "TextureManager.h"
26 
COGLTexture(uint32_t dwWidth,uint32_t dwHeight,TextureUsage usage)27 COGLTexture::COGLTexture(uint32_t dwWidth, uint32_t dwHeight, TextureUsage usage) :
28     CTexture(dwWidth,dwHeight,usage),
29     m_glFmt(GL_RGBA)
30 {
31    // FIXME: If usage is AS_RENDER_TARGET, we need to create pbuffer instead of regular texture
32 
33    m_dwTextureFmt = TEXTURE_FMT_A8R8G8B8;  // Always use 32bit to load texture
34    glGenTextures( 1, &m_dwTextureName );
35 
36    // Make the width and height be the power of 2
37    uint32_t w;
38    for (w = 1; w < dwWidth; w <<= 1);
39    m_dwCreatedTextureWidth = w;
40    for (w = 1; w < dwHeight; w <<= 1);
41    m_dwCreatedTextureHeight = w;
42 
43    if (dwWidth*dwHeight > 256*256)
44       TRACE4("Large texture: (%d x %d), created as (%d x %d)",
45             dwWidth, dwHeight,m_dwCreatedTextureWidth,m_dwCreatedTextureHeight);
46 
47    m_fYScale = (float)m_dwCreatedTextureHeight/(float)m_dwHeight;
48    m_fXScale = (float)m_dwCreatedTextureWidth/(float)m_dwWidth;
49 
50    m_pTexture = malloc(m_dwCreatedTextureWidth * m_dwCreatedTextureHeight * GetPixelSize());
51 
52    switch( options.textureQuality )
53    {
54       case TXT_QUALITY_DEFAULT:
55          if( options.colorQuality == TEXTURE_FMT_A4R4G4B4 )
56             m_glFmt = GL_RGBA4;
57          break;
58       case TXT_QUALITY_32BIT:
59          break;
60       case TXT_QUALITY_16BIT:
61          m_glFmt = GL_RGBA4;
62          break;
63    };
64 
65    glBindTexture(GL_TEXTURE_2D, m_dwTextureName);
66    glTexImage2D(GL_TEXTURE_2D, 0, m_glFmt, m_dwCreatedTextureWidth, m_dwCreatedTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_pTexture);
67 }
68 
~COGLTexture()69 COGLTexture::~COGLTexture()
70 {
71     // FIXME: If usage is AS_RENDER_TARGET, we need to destroy the pbuffer
72 
73     glDeleteTextures(1, &m_dwTextureName );
74     free(m_pTexture);
75     m_pTexture = NULL;
76     m_dwWidth = 0;
77     m_dwHeight = 0;
78 }
79 
StartUpdate(DrawInfo * di)80 bool COGLTexture::StartUpdate(DrawInfo *di)
81 {
82     if (m_pTexture == NULL)
83         return false;
84 
85     di->dwHeight        = (uint16_t)m_dwHeight;
86     di->dwWidth         = (uint16_t)m_dwWidth;
87     di->dwCreatedHeight = m_dwCreatedTextureHeight;
88     di->dwCreatedWidth  = m_dwCreatedTextureWidth;
89     di->lpSurface       = m_pTexture;
90     di->lPitch          = GetPixelSize()*m_dwCreatedTextureWidth;
91 
92     return true;
93 }
94 
EndUpdate(DrawInfo * di)95 void COGLTexture::EndUpdate(DrawInfo *di)
96 {
97     COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext); // we need this to check if the GL extension is available
98 
99     glBindTexture(GL_TEXTURE_2D, m_dwTextureName);
100 
101     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
102 
103     // Mipmap support
104     if(options.mipmapping)
105     {
106         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
107         glGenerateMipmap(GL_TEXTURE_2D);
108     }
109     else
110     {
111         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
112     }
113 
114     // Copy the image data from main memory to video card texture memory
115     //GL_BGRA_IMG works on Adreno but not inside profiler.
116     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_dwCreatedTextureWidth, m_dwCreatedTextureHeight, GL_RGBA, GL_UNSIGNED_BYTE, m_pTexture);
117 }
118 
119 
120 // Keep in mind that the real texture is not scaled to fix the created OpenGL texture yet.
121 // when the image is need to be scaled, ScaleImageToSurface in CTexure will be called to
122 // scale the image automatically
123 
124