-
Notifications
You must be signed in to change notification settings - Fork 40
/
OGLTexture.cpp
148 lines (122 loc) · 4.86 KB
/
OGLTexture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright (C) 2003 Rice1964
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include "Config.h"
#include "Debugger.h"
#include "GraphicsContext.h"
#include "OGLDebug.h"
#include "OGLGraphicsContext.h"
#include "OGLTexture.h"
#include "TextureManager.h"
#include "osal_opengl.h"
COGLTexture::COGLTexture(uint32 dwWidth, uint32 dwHeight, TextureUsage usage) :
CTexture(dwWidth,dwHeight,usage),
m_glInternalFmt(GL_RGBA)
{
// FIXME: If usage is AS_RENDER_TARGET, we need to create pbuffer instead of regular texture
m_dwTextureFmt = TEXTURE_FMT_A8R8G8B8; // Always use 32bit to load texture
glGenTextures( 1, &m_dwTextureName );
OPENGL_CHECK_ERRORS;
// Make the width and height be the power of 2
uint32 w;
for (w = 1; w < dwWidth; w <<= 1);
m_dwCreatedTextureWidth = w;
for (w = 1; w < dwHeight; w <<= 1);
m_dwCreatedTextureHeight = w;
if (dwWidth*dwHeight > 256*256)
TRACE4("Large texture: (%d x %d), created as (%d x %d)",
dwWidth, dwHeight,m_dwCreatedTextureWidth,m_dwCreatedTextureHeight);
m_fYScale = (float)m_dwCreatedTextureHeight/(float)m_dwHeight;
m_fXScale = (float)m_dwCreatedTextureWidth/(float)m_dwWidth;
m_pTexture = malloc(m_dwCreatedTextureWidth * m_dwCreatedTextureHeight * GetPixelSize());
switch( options.textureQuality )
{
case TXT_QUALITY_DEFAULT:
if( options.colorQuality == TEXTURE_FMT_A4R4G4B4 )
m_glInternalFmt = GL_RGBA4;
break;
case TXT_QUALITY_32BIT:
break;
case TXT_QUALITY_16BIT:
m_glInternalFmt = GL_RGBA4;
break;
};
#ifndef USE_GLES
m_glFmt = GL_BGRA;
m_glType = GL_UNSIGNED_INT_8_8_8_8_REV;
#else
m_glInternalFmt = m_glFmt = COGLGraphicsContext::Get()->IsSupportTextureFormatBGRA() ? GL_BGRA_EXT : GL_RGBA;
m_glType = GL_UNSIGNED_BYTE;
#endif
LOG_TEXTURE(TRACE2("New texture: (%d, %d)", dwWidth, dwHeight));
// We create the OGL texture here and will use glTexSubImage2D to increase performance.
glBindTexture(GL_TEXTURE_2D, m_dwTextureName);
OPENGL_CHECK_ERRORS;
glTexImage2D(GL_TEXTURE_2D, 0, m_glInternalFmt, m_dwCreatedTextureWidth, m_dwCreatedTextureHeight, 0, m_glFmt, m_glType, NULL);
OPENGL_CHECK_ERRORS;
}
COGLTexture::~COGLTexture()
{
// FIXME: If usage is AS_RENDER_TARGET, we need to destroy the pbuffer
glDeleteTextures(1, &m_dwTextureName);
OPENGL_CHECK_ERRORS;
free(m_pTexture);
m_pTexture = NULL;
m_dwWidth = 0;
m_dwHeight = 0;
}
bool COGLTexture::StartUpdate(DrawInfo *di)
{
if (m_pTexture == NULL)
return false;
di->dwHeight = (uint16)m_dwHeight;
di->dwWidth = (uint16)m_dwWidth;
di->dwCreatedHeight = m_dwCreatedTextureHeight;
di->dwCreatedWidth = m_dwCreatedTextureWidth;
di->lpSurface = m_pTexture;
di->lPitch = GetPixelSize()*m_dwCreatedTextureWidth;
return true;
}
void COGLTexture::EndUpdate(DrawInfo *di)
{
glBindTexture(GL_TEXTURE_2D, m_dwTextureName);
OPENGL_CHECK_ERRORS;
// mipmap support
if(options.mipmapping)
{
int maximumAnistropy = COGLGraphicsContext::Get()->getMaxAnisotropicFiltering(); //if getMaxAnisotropicFiltering() return more than 0, so aniso is supported and maxAnisotropicFiltering is set
// Set Anisotropic filtering (mipmapping have to be activated, aniso filtering is not effective without)
if( maximumAnistropy )
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maximumAnistropy);
OPENGL_CHECK_ERRORS;
}
#ifndef USE_GLES
// Tell to hardware to generate mipmap (himself) when glTexImage2D is called
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
OPENGL_CHECK_ERRORS;
#endif
}
// Copy the image data from main memory to video card texture memory
// On little-endian systems (x86 and many others), ARGB datas send in BGRA order
// and RGBA datas are send as ABGR (something we try to avoid).
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_dwCreatedTextureWidth, m_dwCreatedTextureHeight, m_glFmt, m_glType, m_pTexture);
OPENGL_CHECK_ERRORS;
#ifdef USE_GLES
if(options.mipmapping)
glGenerateMipmap(GL_TEXTURE_2D);
OPENGL_CHECK_ERRORS;
#endif
}