Path: blob/master/libmupen64plus/mupen64plus-video-rice/src/OGLExtRender.cpp
2 views
/*1Copyright (C) 2003 Rice196423This program is free software; you can redistribute it and/or4modify it under the terms of the GNU General Public License5as published by the Free Software Foundation; either version 26of the License, or (at your option) any later version.78This program is distributed in the hope that it will be useful,9but WITHOUT ANY WARRANTY; without even the implied warranty of10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11GNU General Public License for more details.1213You should have received a copy of the GNU General Public License14along with this program; if not, write to the Free Software15Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.16*/1718#include "osal_opengl.h"1920#if SDL_VIDEO_OPENGL21#include "OGLExtensions.h"22#endif23#include "OGLDebug.h"24#include "OGLExtRender.h"25#include "OGLTexture.h"2627void COGLExtRender::Initialize(void)28{29OGLRender::Initialize();3031// Initialize multitexture32glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB,&m_maxTexUnits);33OPENGL_CHECK_ERRORS;3435for( int i=0; i<8; i++ )36m_textureUnitMap[i] = -1;37m_textureUnitMap[0] = 0; // T0 is usually using texture unit 038m_textureUnitMap[1] = 1; // T1 is usually using texture unit 139}404142void COGLExtRender::BindTexture(GLuint texture, int unitno)43{44if( m_bEnableMultiTexture )45{46if( unitno < m_maxTexUnits )47{48if( m_curBoundTex[unitno] != texture )49{50pglActiveTexture(GL_TEXTURE0_ARB+unitno);51OPENGL_CHECK_ERRORS;52glBindTexture(GL_TEXTURE_2D,texture);53OPENGL_CHECK_ERRORS;54m_curBoundTex[unitno] = texture;55}56}57}58else59{60OGLRender::BindTexture(texture, unitno);61}62}6364void COGLExtRender::DisBindTexture(GLuint texture, int unitno)65{66if( m_bEnableMultiTexture )67{68pglActiveTexture(GL_TEXTURE0_ARB+unitno);69OPENGL_CHECK_ERRORS;70glBindTexture(GL_TEXTURE_2D, 0); //Not to bind any texture71OPENGL_CHECK_ERRORS;72}73else74OGLRender::DisBindTexture(texture, unitno);75}7677void COGLExtRender::TexCoord2f(float u, float v)78{79#if SDL_VIDEO_OPENGL80if( m_bEnableMultiTexture )81{82for( int i=0; i<8; i++ )83{84if( m_textureUnitMap[i] >= 0 )85{86pglMultiTexCoord2f(GL_TEXTURE0_ARB+i, u, v);87}88}89}90else91{92OGLRender::TexCoord2f(u,v);93}94#endif95}9697void COGLExtRender::TexCoord(TLITVERTEX &vtxInfo)98{99#if SDL_VIDEO_OPENGL100if( m_bEnableMultiTexture )101{102for( int i=0; i<8; i++ )103{104if( m_textureUnitMap[i] >= 0 )105{106pglMultiTexCoord2fv(GL_TEXTURE0_ARB+i, &(vtxInfo.tcord[m_textureUnitMap[i]].u));107}108}109}110else111{112OGLRender::TexCoord(vtxInfo);113}114#endif115}116117118void COGLExtRender::SetTexWrapS(int unitno,GLuint flag)119{120static GLuint mflag[8];121static GLuint mtex[8];122if( m_curBoundTex[unitno] != mtex[unitno] || mflag[unitno] != flag )123{124mtex[unitno] = m_curBoundTex[0];125mflag[unitno] = flag;126glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, flag);127OPENGL_CHECK_ERRORS;128}129}130void COGLExtRender::SetTexWrapT(int unitno,GLuint flag)131{132static GLuint mflag[8];133static GLuint mtex[8];134if( m_curBoundTex[unitno] != mtex[unitno] || mflag[unitno] != flag )135{136mtex[unitno] = m_curBoundTex[0];137mflag[unitno] = flag;138glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, flag);139OPENGL_CHECK_ERRORS;140}141}142143extern UVFlagMap OGLXUVFlagMaps[];144void COGLExtRender::SetTextureUFlag(TextureUVFlag dwFlag, uint32 dwTile)145{146TileUFlags[dwTile] = dwFlag;147if( !m_bEnableMultiTexture )148{149OGLRender::SetTextureUFlag(dwFlag, dwTile);150return;151}152153int tex;154if( dwTile == gRSP.curTile )155tex=0;156else if( dwTile == ((gRSP.curTile+1)&7) )157tex=1;158else159{160if( dwTile == ((gRSP.curTile+2)&7) )161tex=2;162else if( dwTile == ((gRSP.curTile+3)&7) )163tex=3;164else165{166TRACE2("Incorrect tile number for OGL SetTextureUFlag: cur=%d, tile=%d", gRSP.curTile, dwTile);167return;168}169}170171for( int textureNo=0; textureNo<8; textureNo++)172{173if( m_textureUnitMap[textureNo] == tex )174{175pglActiveTexture(GL_TEXTURE0_ARB+textureNo);176OPENGL_CHECK_ERRORS;177COGLTexture* pTexture = g_textures[(gRSP.curTile+tex)&7].m_pCOGLTexture;178if( pTexture )179{180EnableTexUnit(textureNo,TRUE);181BindTexture(pTexture->m_dwTextureName, textureNo);182}183SetTexWrapS(textureNo, OGLXUVFlagMaps[dwFlag].realFlag);184}185}186}187void COGLExtRender::SetTextureVFlag(TextureUVFlag dwFlag, uint32 dwTile)188{189TileVFlags[dwTile] = dwFlag;190if( !m_bEnableMultiTexture )191{192OGLRender::SetTextureVFlag(dwFlag, dwTile);193return;194}195196int tex;197if( dwTile == gRSP.curTile )198tex=0;199else if( dwTile == ((gRSP.curTile+1)&7) )200tex=1;201else202{203if( dwTile == ((gRSP.curTile+2)&7) )204tex=2;205else if( dwTile == ((gRSP.curTile+3)&7) )206tex=3;207else208{209TRACE2("Incorrect tile number for OGL SetTextureVFlag: cur=%d, tile=%d", gRSP.curTile, dwTile);210return;211}212}213214for( int textureNo=0; textureNo<8; textureNo++)215{216if( m_textureUnitMap[textureNo] == tex )217{218COGLTexture* pTexture = g_textures[(gRSP.curTile+tex)&7].m_pCOGLTexture;219if( pTexture )220{221EnableTexUnit(textureNo,TRUE);222BindTexture(pTexture->m_dwTextureName, textureNo);223}224SetTexWrapT(textureNo, OGLXUVFlagMaps[dwFlag].realFlag);225}226}227}228229void COGLExtRender::EnableTexUnit(int unitno, BOOL flag)230{231if( m_texUnitEnabled[unitno] != flag )232{233m_texUnitEnabled[unitno] = flag;234pglActiveTexture(GL_TEXTURE0_ARB+unitno);235OPENGL_CHECK_ERRORS;236if( flag == TRUE )237glEnable(GL_TEXTURE_2D);238else239glDisable(GL_TEXTURE_2D);240OPENGL_CHECK_ERRORS;241}242}243244void COGLExtRender::ApplyTextureFilter()245{246static uint32 minflag[8], magflag[8];247static uint32 mtex[8];248249int iMinFilter, iMagFilter;250251for( int i=0; i<m_maxTexUnits; i++ )252{253//Compute iMinFilter and iMagFilter254if(m_dwMinFilter == FILTER_LINEAR) //Texture will use filtering255{256iMagFilter = GL_LINEAR;257258//Texture filtering method user want259switch(options.mipmapping)260{261case TEXTURE_BILINEAR_FILTER:262iMinFilter = GL_LINEAR_MIPMAP_NEAREST;263break;264case TEXTURE_TRILINEAR_FILTER:265iMinFilter = GL_LINEAR_MIPMAP_LINEAR;266break;267case TEXTURE_NO_FILTER:268iMinFilter = GL_NEAREST_MIPMAP_NEAREST;269break;270case TEXTURE_NO_MIPMAP:271default:272//Bilinear without mipmap273iMinFilter = GL_LINEAR;274}275}276else //dont use filtering, all is nearest277{278iMagFilter = GL_NEAREST;279280if(options.mipmapping)281{282iMinFilter = GL_NEAREST_MIPMAP_NEAREST;283}284else285{286iMinFilter = GL_NEAREST;287}288}289290if( m_texUnitEnabled[i] )291{292if( mtex[i] != m_curBoundTex[i] )293{294mtex[i] = m_curBoundTex[i];295pglActiveTexture(GL_TEXTURE0_ARB+i);296OPENGL_CHECK_ERRORS;297minflag[i] = m_dwMinFilter;298magflag[i] = m_dwMagFilter;299glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, iMinFilter);300OPENGL_CHECK_ERRORS;301glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, iMagFilter);302OPENGL_CHECK_ERRORS;303}304else305{306if( minflag[i] != (unsigned int)m_dwMinFilter )307{308minflag[i] = m_dwMinFilter;309pglActiveTexture(GL_TEXTURE0_ARB+i);310OPENGL_CHECK_ERRORS;311glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, iMinFilter);312OPENGL_CHECK_ERRORS;313}314if( magflag[i] != (unsigned int)m_dwMagFilter )315{316magflag[i] = m_dwMagFilter;317pglActiveTexture(GL_TEXTURE0_ARB+i);318OPENGL_CHECK_ERRORS;319glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, iMagFilter);320OPENGL_CHECK_ERRORS;321}322}323}324}325}326327void COGLExtRender::SetTextureToTextureUnitMap(int tex, int unit)328{329if( unit < 8 && (tex >= -1 || tex <= 1))330m_textureUnitMap[unit] = tex;331}332333334335