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.
19 #include "osal_opengl.h"
22 #include "OGLExtensions.h"
25 #include "OGLExtRender.h"
26 #include "OGLTexture.h"
28 void COGLExtRender::Initialize(void)
30 OGLRender::Initialize();
32 // Initialize multitexture
33 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB,&m_maxTexUnits);
36 for( int i=0; i<8; i++ )
37 m_textureUnitMap[i] = -1;
38 m_textureUnitMap[0] = 0; // T0 is usually using texture unit 0
39 m_textureUnitMap[1] = 1; // T1 is usually using texture unit 1
43 void COGLExtRender::BindTexture(GLuint texture, int unitno)
45 if( m_bEnableMultiTexture )
47 if( unitno < m_maxTexUnits )
49 if( m_curBoundTex[unitno] != texture )
51 pglActiveTexture(GL_TEXTURE0_ARB+unitno);
53 glBindTexture(GL_TEXTURE_2D,texture);
55 m_curBoundTex[unitno] = texture;
61 OGLRender::BindTexture(texture, unitno);
65 void COGLExtRender::DisBindTexture(GLuint texture, int unitno)
67 if( m_bEnableMultiTexture )
69 pglActiveTexture(GL_TEXTURE0_ARB+unitno);
71 glBindTexture(GL_TEXTURE_2D, 0); //Not to bind any texture
75 OGLRender::DisBindTexture(texture, unitno);
78 void COGLExtRender::TexCoord2f(float u, float v)
81 if( m_bEnableMultiTexture )
83 for( int i=0; i<8; i++ )
85 if( m_textureUnitMap[i] >= 0 )
87 pglMultiTexCoord2f(GL_TEXTURE0_ARB+i, u, v);
93 OGLRender::TexCoord2f(u,v);
98 void COGLExtRender::TexCoord(TLITVERTEX &vtxInfo)
101 if( m_bEnableMultiTexture )
103 for( int i=0; i<8; i++ )
105 if( m_textureUnitMap[i] >= 0 )
107 pglMultiTexCoord2fv(GL_TEXTURE0_ARB+i, &(vtxInfo.tcord[m_textureUnitMap[i]].u));
113 OGLRender::TexCoord(vtxInfo);
119 void COGLExtRender::SetTexWrapS(int unitno,GLuint flag)
121 static GLuint mflag[8];
122 static GLuint mtex[8];
123 if( m_curBoundTex[unitno] != mtex[unitno] || mflag[unitno] != flag )
125 mtex[unitno] = m_curBoundTex[0];
126 mflag[unitno] = flag;
127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, flag);
131 void COGLExtRender::SetTexWrapT(int unitno,GLuint flag)
133 static GLuint mflag[8];
134 static GLuint mtex[8];
135 if( m_curBoundTex[unitno] != mtex[unitno] || mflag[unitno] != flag )
137 mtex[unitno] = m_curBoundTex[0];
138 mflag[unitno] = flag;
139 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, flag);
144 extern UVFlagMap OGLXUVFlagMaps[];
145 void COGLExtRender::SetTextureUFlag(TextureUVFlag dwFlag, uint32 dwTile)
147 TileUFlags[dwTile] = dwFlag;
148 if( !m_bEnableMultiTexture )
150 OGLRender::SetTextureUFlag(dwFlag, dwTile);
155 if( dwTile == gRSP.curTile )
157 else if( dwTile == ((gRSP.curTile+1)&7) )
161 if( dwTile == ((gRSP.curTile+2)&7) )
163 else if( dwTile == ((gRSP.curTile+3)&7) )
167 TRACE2("Incorrect tile number for OGL SetTextureUFlag: cur=%d, tile=%d", gRSP.curTile, dwTile);
172 for( int textureNo=0; textureNo<8; textureNo++)
174 if( m_textureUnitMap[textureNo] == tex )
176 pglActiveTexture(GL_TEXTURE0_ARB+textureNo);
178 COGLTexture* pTexture = g_textures[(gRSP.curTile+tex)&7].m_pCOGLTexture;
181 EnableTexUnit(textureNo,TRUE);
182 BindTexture(pTexture->m_dwTextureName, textureNo);
184 SetTexWrapS(textureNo, OGLXUVFlagMaps[dwFlag].realFlag);
188 void COGLExtRender::SetTextureVFlag(TextureUVFlag dwFlag, uint32 dwTile)
190 TileVFlags[dwTile] = dwFlag;
191 if( !m_bEnableMultiTexture )
193 OGLRender::SetTextureVFlag(dwFlag, dwTile);
198 if( dwTile == gRSP.curTile )
200 else if( dwTile == ((gRSP.curTile+1)&7) )
204 if( dwTile == ((gRSP.curTile+2)&7) )
206 else if( dwTile == ((gRSP.curTile+3)&7) )
210 TRACE2("Incorrect tile number for OGL SetTextureVFlag: cur=%d, tile=%d", gRSP.curTile, dwTile);
215 for( int textureNo=0; textureNo<8; textureNo++)
217 if( m_textureUnitMap[textureNo] == tex )
219 COGLTexture* pTexture = g_textures[(gRSP.curTile+tex)&7].m_pCOGLTexture;
222 EnableTexUnit(textureNo,TRUE);
223 BindTexture(pTexture->m_dwTextureName, textureNo);
225 SetTexWrapT(textureNo, OGLXUVFlagMaps[dwFlag].realFlag);
230 void COGLExtRender::EnableTexUnit(int unitno, BOOL flag)
232 if( m_texUnitEnabled[unitno] != flag )
234 m_texUnitEnabled[unitno] = flag;
235 pglActiveTexture(GL_TEXTURE0_ARB+unitno);
238 glEnable(GL_TEXTURE_2D);
240 glDisable(GL_TEXTURE_2D);
245 void COGLExtRender::ApplyTextureFilter()
247 static uint32 minflag[8], magflag[8];
248 static uint32 mtex[8];
250 int iMinFilter, iMagFilter;
252 for( int i=0; i<m_maxTexUnits; i++ )
254 //Compute iMinFilter and iMagFilter
255 if(m_dwMinFilter == FILTER_LINEAR) //Texture will use filtering
257 iMagFilter = GL_LINEAR;
259 //Texture filtering method user want
260 switch(options.mipmapping)
262 case TEXTURE_BILINEAR_FILTER:
263 iMinFilter = GL_LINEAR_MIPMAP_NEAREST;
265 case TEXTURE_TRILINEAR_FILTER:
266 iMinFilter = GL_LINEAR_MIPMAP_LINEAR;
268 case TEXTURE_NO_FILTER:
269 iMinFilter = GL_NEAREST_MIPMAP_NEAREST;
271 case TEXTURE_NO_MIPMAP:
273 //Bilinear without mipmap
274 iMinFilter = GL_LINEAR;
277 else //dont use filtering, all is nearest
279 iMagFilter = GL_NEAREST;
281 if(options.mipmapping)
283 iMinFilter = GL_NEAREST_MIPMAP_NEAREST;
287 iMinFilter = GL_NEAREST;
291 if( m_texUnitEnabled[i] )
293 if( mtex[i] != m_curBoundTex[i] )
295 mtex[i] = m_curBoundTex[i];
296 pglActiveTexture(GL_TEXTURE0_ARB+i);
298 minflag[i] = m_dwMinFilter;
299 magflag[i] = m_dwMagFilter;
300 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, iMinFilter);
302 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, iMagFilter);
307 if( minflag[i] != (unsigned int)m_dwMinFilter )
309 minflag[i] = m_dwMinFilter;
310 pglActiveTexture(GL_TEXTURE0_ARB+i);
312 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, iMinFilter);
315 if( magflag[i] != (unsigned int)m_dwMagFilter )
317 magflag[i] = m_dwMagFilter;
318 pglActiveTexture(GL_TEXTURE0_ARB+i);
320 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, iMagFilter);
328 void COGLExtRender::SetTextureToTextureUnitMap(int tex, int unit)
330 if( unit < 8 && (tex >= -1 || tex <= 1))
331 m_textureUnitMap[unit] = tex;