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"
23 #elif SDL_VIDEO_OPENGL_ES2
24 #include "OGLES2FragmentShaders.h"
27 #include "OGLRender.h"
28 #include "OGLGraphicsContext.h"
29 #include "OGLTexture.h"
30 #include "TextureManager.h"
33 //include "ae_bridge.h"
34 //static int hardwareType = HARDWARE_TYPE_UNKNOWN;
37 // FIXME: Use OGL internal L/T and matrix stack
38 // FIXME: Use OGL lookupAt function
39 // FIXME: Use OGL DisplayList
41 UVFlagMap OGLXUVFlagMaps[] =
43 {TEXTURE_UV_FLAG_WRAP, GL_REPEAT},
44 {TEXTURE_UV_FLAG_MIRROR, GL_MIRRORED_REPEAT_ARB},
45 {TEXTURE_UV_FLAG_CLAMP, GL_CLAMP},
48 #if SDL_VIDEO_OPENGL_ES2
49 static GLuint disabledTextureID;
52 //===================================================================
53 OGLRender::OGLRender()
55 COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext);
56 m_bSupportFogCoordExt = pcontext->m_bSupportFogCoord;
57 m_bMultiTexture = pcontext->m_bSupportMultiTexture;
58 m_bSupportClampToEdge = false;
59 for( int i=0; i<8; i++ )
62 m_texUnitEnabled[i]=FALSE;
66 m_bEnableMultiTexture = false;
68 #elif SDL_VIDEO_OPENGL_ES2
69 m_bEnableMultiTexture = true;
71 //Create a texture as replacement for glEnable/Disable(GL_TEXTURE_2D)
72 unsigned int white[8*8];
73 for (int i=0; i<8*8; i++) {
74 //white[i].r = white[i].g = white[i].b = 0;
78 glGenTextures(1,&disabledTextureID);
80 glBindTexture(GL_TEXTURE_2D, disabledTextureID);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
84 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, white);
89 OGLRender::~OGLRender()
94 bool OGLRender::InitDeviceObjects()
96 // enable Z-buffer by default
101 bool OGLRender::ClearDeviceObjects()
106 void OGLRender::Initialize(void)
108 glMatrixMode(GL_MODELVIEW);
113 glViewportWrapper(0, windowSetting.statusBarHeightToUse, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight);
117 COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext);
118 if( pcontext->IsExtensionSupported("GL_IBM_texture_mirrored_repeat") )
120 OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_MIRRORED_REPEAT_IBM;
122 else if( pcontext->IsExtensionSupported("ARB_texture_mirrored_repeat") )
124 OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_MIRRORED_REPEAT_ARB;
128 OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_REPEAT;
131 if( pcontext->IsExtensionSupported("GL_ARB_texture_border_clamp") || pcontext->IsExtensionSupported("GL_EXT_texture_edge_clamp") )
133 m_bSupportClampToEdge = true;
134 OGLXUVFlagMaps[TEXTURE_UV_FLAG_CLAMP].realFlag = GL_CLAMP_TO_EDGE;
138 m_bSupportClampToEdge = false;
139 OGLXUVFlagMaps[TEXTURE_UV_FLAG_CLAMP].realFlag = GL_CLAMP;
142 glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][0]) );
144 glEnableClientState( GL_VERTEX_ARRAY );
147 if( m_bMultiTexture )
149 pglClientActiveTextureARB( GL_TEXTURE0_ARB );
151 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u) );
153 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
156 pglClientActiveTextureARB( GL_TEXTURE1_ARB );
158 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[1].u) );
160 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
165 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u) );
167 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
171 if (m_bSupportFogCoordExt)
173 pglFogCoordPointerEXT( GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][4]) );
175 glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
177 glFogi( GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT );
179 glFogi(GL_FOG_MODE, GL_LINEAR); // Fog Mode
181 glFogf(GL_FOG_DENSITY, 1.0f); // How Dense Will The Fog Be
183 glHint(GL_FOG_HINT, GL_FASTEST); // Fog Hint Value
185 glFogi( GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT );
187 glFogf( GL_FOG_START, 0.0f );
189 glFogf( GL_FOG_END, 1.0f );
193 //glColorPointer( 1, GL_UNSIGNED_BYTE, sizeof(TLITVERTEX), &g_vtxBuffer[0].r);
194 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
196 glEnableClientState( GL_COLOR_ARRAY );
199 if( pcontext->IsExtensionSupported("GL_NV_depth_clamp") )
201 glEnable(GL_DEPTH_CLAMP_NV);
205 #elif SDL_VIDEO_OPENGL_ES2
206 OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_MIRRORED_REPEAT;
207 m_bSupportClampToEdge = true;
208 OGLXUVFlagMaps[TEXTURE_UV_FLAG_CLAMP].realFlag = GL_CLAMP_TO_EDGE;
212 // hardwareType = Android_JNI_GetHardwareType();
215 //===================================================================
216 TextureFilterMap OglTexFilterMap[2]=
218 {FILTER_POINT, GL_NEAREST},
219 {FILTER_LINEAR, GL_LINEAR},
222 void OGLRender::ApplyTextureFilter()
224 static uint32 minflag=0xFFFF, magflag=0xFFFF;
227 if( m_texUnitEnabled[0] )
229 if( mtex != m_curBoundTex[0] )
231 mtex = m_curBoundTex[0];
232 minflag = m_dwMinFilter;
233 magflag = m_dwMagFilter;
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, OglTexFilterMap[m_dwMinFilter].realFilter);
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, OglTexFilterMap[m_dwMagFilter].realFilter);
241 if( minflag != (unsigned int)m_dwMinFilter )
243 minflag = m_dwMinFilter;
244 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, OglTexFilterMap[m_dwMinFilter].realFilter);
247 if( magflag != (unsigned int)m_dwMagFilter )
249 magflag = m_dwMagFilter;
250 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, OglTexFilterMap[m_dwMagFilter].realFilter);
257 void OGLRender::SetShadeMode(RenderShadeMode mode)
260 if( mode == SHADE_SMOOTH )
261 glShadeModel(GL_SMOOTH);
263 glShadeModel(GL_FLAT);
268 void OGLRender::ZBufferEnable(BOOL bZBuffer)
270 gRSP.bZBufferEnabled = bZBuffer;
271 if( g_curRomInfo.bForceDepthBuffer )
275 glDepthMask(GL_TRUE);
277 //glEnable(GL_DEPTH_TEST);
278 glDepthFunc( GL_LEQUAL );
283 glDepthMask(GL_FALSE);
285 //glDisable(GL_DEPTH_TEST);
286 glDepthFunc( GL_ALWAYS );
291 void OGLRender::ClearBuffer(bool cbuffer, bool zbuffer)
294 if( cbuffer ) flag |= GL_COLOR_BUFFER_BIT;
295 if( zbuffer ) flag |= GL_DEPTH_BUFFER_BIT;
296 float depth = ((gRDP.originalFillColor&0xFFFF)>>2)/(float)0x3FFF;
303 void OGLRender::ClearZBuffer(float depth)
305 uint32 flag=GL_DEPTH_BUFFER_BIT;
312 void OGLRender::SetZCompare(BOOL bZCompare)
314 if( g_curRomInfo.bForceDepthBuffer )
317 gRSP.bZBufferEnabled = bZCompare;
318 if( bZCompare == TRUE )
320 //glEnable(GL_DEPTH_TEST);
321 glDepthFunc( GL_LEQUAL );
326 //glDisable(GL_DEPTH_TEST);
327 glDepthFunc( GL_ALWAYS );
332 void OGLRender::SetZUpdate(BOOL bZUpdate)
334 if( g_curRomInfo.bForceDepthBuffer )
339 //glEnable(GL_DEPTH_TEST);
340 glDepthMask(GL_TRUE);
345 glDepthMask(GL_FALSE);
350 void OGLRender::ApplyZBias(int bias)
352 float f1 = bias > 0 ? -3.0f : 0.0f; // z offset = -3.0 * max(abs(dz/dx),abs(dz/dy)) per pixel delta z slope
353 float f2 = bias > 0 ? -3.0f : 0.0f; // z offset += -3.0 * 1 bit
356 // Android_JNI_GetPolygonOffset(hardwareType, bias, &f1, &f2);
357 // glPolygonOffset(0.2f, 0.2f);
362 glEnable(GL_POLYGON_OFFSET_FILL); // enable z offsets
367 glDisable(GL_POLYGON_OFFSET_FILL); // disable z offsets
370 glPolygonOffset(f1, f2); // set bias functions
374 void OGLRender::SetZBias(int bias)
376 #if defined(DEBUGGER)
377 if( pauseAtNext == true )
378 DebuggerAppendMsg("Set zbias = %d", bias);
380 // set member variable and apply the setting in opengl
385 void OGLRender::SetAlphaRef(uint32 dwAlpha)
387 if (m_dwAlpha != dwAlpha)
391 glAlphaFunc(GL_GEQUAL, (float)dwAlpha);
397 void OGLRender::ForceAlphaRef(uint32 dwAlpha)
400 float ref = dwAlpha/255.0f;
401 glAlphaFunc(GL_GEQUAL, ref);
403 #elif SDL_VIDEO_OPENGL_ES2
408 void OGLRender::SetFillMode(FillMode mode)
411 if( mode == RICE_FILLMODE_WINFRAME )
413 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
418 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
424 void OGLRender::SetCullMode(bool bCullFront, bool bCullBack)
426 CRender::SetCullMode(bCullFront, bCullBack);
427 if( bCullFront && bCullBack )
429 glCullFace(GL_FRONT_AND_BACK);
431 glEnable(GL_CULL_FACE);
434 else if( bCullFront )
436 glCullFace(GL_FRONT);
438 glEnable(GL_CULL_FACE);
445 glEnable(GL_CULL_FACE);
450 glDisable(GL_CULL_FACE);
455 bool OGLRender::SetCurrentTexture(int tile, CTexture *handler,uint32 dwTileWidth, uint32 dwTileHeight, TxtrCacheEntry *pTextureEntry)
457 RenderTexture &texture = g_textures[tile];
458 texture.pTextureEntry = pTextureEntry;
460 if( handler!= NULL && texture.m_lpsTexturePtr != handler->GetTexture() )
462 texture.m_pCTexture = handler;
463 texture.m_lpsTexturePtr = handler->GetTexture();
465 texture.m_dwTileWidth = dwTileWidth;
466 texture.m_dwTileHeight = dwTileHeight;
468 if( handler->m_bIsEnhancedTexture )
470 texture.m_fTexWidth = (float)pTextureEntry->pTexture->m_dwCreatedTextureWidth;
471 texture.m_fTexHeight = (float)pTextureEntry->pTexture->m_dwCreatedTextureHeight;
475 texture.m_fTexWidth = (float)handler->m_dwCreatedTextureWidth;
476 texture.m_fTexHeight = (float)handler->m_dwCreatedTextureHeight;
483 bool OGLRender::SetCurrentTexture(int tile, TxtrCacheEntry *pEntry)
485 if (pEntry != NULL && pEntry->pTexture != NULL)
487 SetCurrentTexture( tile, pEntry->pTexture, pEntry->ti.WidthToCreate, pEntry->ti.HeightToCreate, pEntry);
492 SetCurrentTexture( tile, NULL, 64, 64, NULL );
498 void OGLRender::SetAddressUAllStages(uint32 dwTile, TextureUVFlag dwFlag)
500 SetTextureUFlag(dwFlag, dwTile);
503 void OGLRender::SetAddressVAllStages(uint32 dwTile, TextureUVFlag dwFlag)
505 SetTextureVFlag(dwFlag, dwTile);
508 void OGLRender::SetTexWrapS(int unitno,GLuint flag)
515 DebuggerAppendMsg("Check me, unitno != 0 in base ogl");
518 if( m_curBoundTex[0] != mtex || mflag != flag )
520 mtex = m_curBoundTex[0];
522 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, flag);
526 void OGLRender::SetTexWrapT(int unitno,GLuint flag)
530 if( m_curBoundTex[0] != mtex || mflag != flag )
532 mtex = m_curBoundTex[0];
534 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, flag);
539 void OGLRender::SetTextureUFlag(TextureUVFlag dwFlag, uint32 dwTile)
541 TileUFlags[dwTile] = dwFlag;
542 if( dwTile == gRSP.curTile ) // For basic OGL, only support the 1st texel
544 COGLTexture* pTexture = g_textures[gRSP.curTile].m_pCOGLTexture;
547 EnableTexUnit(0,TRUE);
548 BindTexture(pTexture->m_dwTextureName, 0);
550 SetTexWrapS(0, OGLXUVFlagMaps[dwFlag].realFlag);
553 void OGLRender::SetTextureVFlag(TextureUVFlag dwFlag, uint32 dwTile)
555 TileVFlags[dwTile] = dwFlag;
556 if( dwTile == gRSP.curTile ) // For basic OGL, only support the 1st texel
558 COGLTexture* pTexture = g_textures[gRSP.curTile].m_pCOGLTexture;
561 EnableTexUnit(0,TRUE);
562 BindTexture(pTexture->m_dwTextureName, 0);
564 SetTexWrapT(0, OGLXUVFlagMaps[dwFlag].realFlag);
568 // Basic render drawing functions
570 bool OGLRender::RenderTexRect()
572 glViewportWrapper(0, windowSetting.statusBarHeightToUse, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight);
575 GLboolean cullface = glIsEnabled(GL_CULL_FACE);
576 glDisable(GL_CULL_FACE);
579 float depth = -(g_texRectTVtx[3].z*2-1);
583 glBegin(GL_TRIANGLE_FAN);
585 glColor4f(g_texRectTVtx[3].r, g_texRectTVtx[3].g, g_texRectTVtx[3].b, g_texRectTVtx[3].a);
586 TexCoord(g_texRectTVtx[3]);
587 glVertex3f(g_texRectTVtx[3].x, g_texRectTVtx[3].y, depth);
589 glColor4f(g_texRectTVtx[2].r, g_texRectTVtx[2].g, g_texRectTVtx[2].b, g_texRectTVtx[2].a);
590 TexCoord(g_texRectTVtx[2]);
591 glVertex3f(g_texRectTVtx[2].x, g_texRectTVtx[2].y, depth);
593 glColor4f(g_texRectTVtx[1].r, g_texRectTVtx[1].g, g_texRectTVtx[1].b, g_texRectTVtx[1].a);
594 TexCoord(g_texRectTVtx[1]);
595 glVertex3f(g_texRectTVtx[1].x, g_texRectTVtx[1].y, depth);
597 glColor4f(g_texRectTVtx[0].r, g_texRectTVtx[0].g, g_texRectTVtx[0].b, g_texRectTVtx[0].a);
598 TexCoord(g_texRectTVtx[0]);
599 glVertex3f(g_texRectTVtx[0].x, g_texRectTVtx[0].y, depth);
604 #elif SDL_VIDEO_OPENGL_ES2
607 g_texRectTVtx[3].r, g_texRectTVtx[3].g, g_texRectTVtx[3].b, g_texRectTVtx[3].a,
608 g_texRectTVtx[2].r, g_texRectTVtx[2].g, g_texRectTVtx[2].b, g_texRectTVtx[2].a,
609 g_texRectTVtx[1].r, g_texRectTVtx[1].g, g_texRectTVtx[1].b, g_texRectTVtx[1].a,
610 g_texRectTVtx[0].r, g_texRectTVtx[0].g, g_texRectTVtx[0].b, g_texRectTVtx[0].a
614 g_texRectTVtx[3].tcord[0].u,g_texRectTVtx[3].tcord[0].v,
615 g_texRectTVtx[2].tcord[0].u,g_texRectTVtx[2].tcord[0].v,
616 g_texRectTVtx[1].tcord[0].u,g_texRectTVtx[1].tcord[0].v,
617 g_texRectTVtx[0].tcord[0].u,g_texRectTVtx[0].tcord[0].v
620 float w = windowSetting.uDisplayWidth / 2.0f, h = windowSetting.uDisplayHeight / 2.0f, inv = 1.0f;
622 GLfloat vertices[] = {
623 -inv + g_texRectTVtx[3].x / w, inv - g_texRectTVtx[3].y / h, depth, 1,
624 -inv + g_texRectTVtx[2].x / w, inv - g_texRectTVtx[2].y / h, depth, 1,
625 -inv + g_texRectTVtx[1].x / w, inv - g_texRectTVtx[1].y / h, depth, 1,
626 -inv + g_texRectTVtx[0].x / w, inv - g_texRectTVtx[0].y / h, depth, 1
629 glVertexAttribPointer(VS_COLOR, 4, GL_FLOAT,GL_TRUE, 0, &colour );
630 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,0,&vertices);
631 glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, 0, &tex);
633 glDrawArrays(GL_TRIANGLE_FAN,0,4);
636 //Restore old pointers
637 glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
638 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
639 glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u));
643 if( cullface ) glEnable(GL_CULL_FACE);
649 bool OGLRender::RenderFillRect(uint32 dwColor, float depth)
651 float a = (dwColor>>24)/255.0f;
652 float r = ((dwColor>>16)&0xFF)/255.0f;
653 float g = ((dwColor>>8)&0xFF)/255.0f;
654 float b = (dwColor&0xFF)/255.0f;
655 glViewportWrapper(0, windowSetting.statusBarHeightToUse, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight);
658 GLboolean cullface = glIsEnabled(GL_CULL_FACE);
659 glDisable(GL_CULL_FACE);
664 glBegin(GL_TRIANGLE_FAN);
666 glVertex4f(m_fillRectVtx[0].x, m_fillRectVtx[1].y, depth, 1);
667 glVertex4f(m_fillRectVtx[1].x, m_fillRectVtx[1].y, depth, 1);
668 glVertex4f(m_fillRectVtx[1].x, m_fillRectVtx[0].y, depth, 1);
669 glVertex4f(m_fillRectVtx[0].x, m_fillRectVtx[0].y, depth, 1);
673 #elif SDL_VIDEO_OPENGL_ES2
681 float w = windowSetting.uDisplayWidth / 2.0f, h = windowSetting.uDisplayHeight / 2.0f, inv = 1.0f;
683 GLfloat vertices[] = {
684 -inv + m_fillRectVtx[0].x / w, inv - m_fillRectVtx[1].y / h, depth, 1,
685 -inv + m_fillRectVtx[1].x / w, inv - m_fillRectVtx[1].y / h, depth, 1,
686 -inv + m_fillRectVtx[1].x / w, inv - m_fillRectVtx[0].y / h, depth, 1,
687 -inv + m_fillRectVtx[0].x / w, inv - m_fillRectVtx[0].y / h, depth, 1
690 glVertexAttribPointer(VS_COLOR, 4, GL_FLOAT,GL_FALSE, 0, &colour );
691 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,0,&vertices);
692 glDisableVertexAttribArray(VS_TEXCOORD0);
694 glDrawArrays(GL_TRIANGLE_FAN,0,4);
697 //Restore old pointers
698 glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
699 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
700 glEnableVertexAttribArray(VS_TEXCOORD0);
704 if( cullface ) glEnable(GL_CULL_FACE);
710 bool OGLRender::RenderLine3D()
713 ApplyZBias(0); // disable z offsets
715 glBegin(GL_TRIANGLE_FAN);
717 glColor4f(m_line3DVtx[1].r, m_line3DVtx[1].g, m_line3DVtx[1].b, m_line3DVtx[1].a);
718 glVertex3f(m_line3DVector[3].x, m_line3DVector[3].y, -m_line3DVtx[1].z);
719 glVertex3f(m_line3DVector[2].x, m_line3DVector[2].y, -m_line3DVtx[0].z);
721 glColor4ub(m_line3DVtx[0].r, m_line3DVtx[0].g, m_line3DVtx[0].b, m_line3DVtx[0].a);
722 glVertex3f(m_line3DVector[1].x, m_line3DVector[1].y, -m_line3DVtx[1].z);
723 glVertex3f(m_line3DVector[0].x, m_line3DVector[0].y, -m_line3DVtx[0].z);
728 ApplyZBias(m_dwZBias); // set Z offset back to previous value
734 extern FiddledVtx * g_pVtxBase;
736 // This is so weired that I can not do vertex transform by myself. I have to use
737 // OpenGL internal transform
738 bool OGLRender::RenderFlushTris()
740 if( !m_bSupportFogCoordExt )
741 SetFogFlagForNegativeW();
744 if( !gRDP.bFogEnableInBlender && gRSP.bFogEnabled )
750 ApplyZBias(m_dwZBias); // set the bias factors
752 glViewportWrapper(windowSetting.vpLeftW, windowSetting.uDisplayHeight-windowSetting.vpTopW-windowSetting.vpHeightW+windowSetting.statusBarHeightToUse, windowSetting.vpWidthW, windowSetting.vpHeightW, false);
755 //if options.bOGLVertexClipper == FALSE )
757 glDrawElements( GL_TRIANGLES, gRSP.numVertices, GL_UNSIGNED_SHORT, g_vtxIndex );
762 //ClipVertexesOpenGL();
765 glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5Clipped[0][0]) );
766 glEnableClientState( GL_VERTEX_ARRAY );
768 pglClientActiveTextureARB( GL_TEXTURE0_ARB );
769 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_clippedVtxBuffer[0].tcord[0].u) );
770 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
772 pglClientActiveTextureARB( GL_TEXTURE1_ARB );
773 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_clippedVtxBuffer[0].tcord[1].u) );
774 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
776 glDrawElements( GL_TRIANGLES, gRSP.numVertices, GL_UNSIGNED_INT, g_vtxIndex );
779 pglClientActiveTextureARB( GL_TEXTURE0_ARB );
780 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u) );
781 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
783 pglClientActiveTextureARB( GL_TEXTURE1_ARB );
784 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[1].u) );
785 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
787 glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][0]) );
788 glEnableClientState( GL_VERTEX_ARRAY );
792 if( !m_bSupportFogCoordExt )
796 if( !gRDP.bFogEnableInBlender && gRSP.bFogEnabled )
804 void OGLRender::DrawSimple2DTexture(float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, COLOR dif, COLOR spe, float z, float rhw)
806 if( status.bVIOriginIsUpdated == true && currentRomOptions.screenUpdateSetting==SCREEN_UPDATE_AT_1ST_PRIMITIVE )
808 status.bVIOriginIsUpdated=false;
809 CGraphicsContext::Get()->UpdateFrame();
810 DEBUGGER_PAUSE_AND_DUMP_NO_UPDATE(NEXT_SET_CIMG,{DebuggerAppendMsg("Screen Update at 1st Simple2DTexture");});
813 StartDrawSimple2DTexture(x0, y0, x1, y1, u0, v0, u1, v1, dif, spe, z, rhw);
815 GLboolean cullface = glIsEnabled(GL_CULL_FACE);
816 glDisable(GL_CULL_FACE);
818 glViewportWrapper(0, windowSetting.statusBarHeightToUse, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight);
821 float a = (g_texRectTVtx[0].dcDiffuse >>24)/255.0f;
822 float r = ((g_texRectTVtx[0].dcDiffuse>>16)&0xFF)/255.0f;
823 float g = ((g_texRectTVtx[0].dcDiffuse>>8)&0xFF)/255.0f;
824 float b = (g_texRectTVtx[0].dcDiffuse&0xFF)/255.0f;
828 glBegin(GL_TRIANGLES);
832 OGLRender::TexCoord(g_texRectTVtx[0]);
833 glVertex3f(g_texRectTVtx[0].x, g_texRectTVtx[0].y, -g_texRectTVtx[0].z);
835 OGLRender::TexCoord(g_texRectTVtx[1]);
836 glVertex3f(g_texRectTVtx[1].x, g_texRectTVtx[1].y, -g_texRectTVtx[1].z);
838 OGLRender::TexCoord(g_texRectTVtx[2]);
839 glVertex3f(g_texRectTVtx[2].x, g_texRectTVtx[2].y, -g_texRectTVtx[2].z);
841 OGLRender::TexCoord(g_texRectTVtx[0]);
842 glVertex3f(g_texRectTVtx[0].x, g_texRectTVtx[0].y, -g_texRectTVtx[0].z);
844 OGLRender::TexCoord(g_texRectTVtx[2]);
845 glVertex3f(g_texRectTVtx[2].x, g_texRectTVtx[2].y, -g_texRectTVtx[2].z);
847 OGLRender::TexCoord(g_texRectTVtx[3]);
848 glVertex3f(g_texRectTVtx[3].x, g_texRectTVtx[3].y, -g_texRectTVtx[3].z);
853 #elif SDL_VIDEO_OPENGL_ES2
865 g_texRectTVtx[0].tcord[0].u,g_texRectTVtx[0].tcord[0].v,
866 g_texRectTVtx[1].tcord[0].u,g_texRectTVtx[1].tcord[0].v,
867 g_texRectTVtx[2].tcord[0].u,g_texRectTVtx[2].tcord[0].v,
869 g_texRectTVtx[0].tcord[0].u,g_texRectTVtx[0].tcord[0].v,
870 g_texRectTVtx[2].tcord[0].u,g_texRectTVtx[2].tcord[0].v,
871 g_texRectTVtx[3].tcord[0].u,g_texRectTVtx[3].tcord[0].v,
874 float w = windowSetting.uDisplayWidth / 2.0f, h = windowSetting.uDisplayHeight / 2.0f, inv = 1.0f;
876 GLfloat vertices[] = {
877 -inv + g_texRectTVtx[0].x/ w, inv - g_texRectTVtx[0].y/ h, -g_texRectTVtx[0].z,1,
878 -inv + g_texRectTVtx[1].x/ w, inv - g_texRectTVtx[1].y/ h, -g_texRectTVtx[1].z,1,
879 -inv + g_texRectTVtx[2].x/ w, inv - g_texRectTVtx[2].y/ h, -g_texRectTVtx[2].z,1,
881 -inv + g_texRectTVtx[0].x/ w, inv - g_texRectTVtx[0].y/ h, -g_texRectTVtx[0].z,1,
882 -inv + g_texRectTVtx[2].x/ w, inv - g_texRectTVtx[2].y/ h, -g_texRectTVtx[2].z,1,
883 -inv + g_texRectTVtx[3].x/ w, inv - g_texRectTVtx[3].y/ h, -g_texRectTVtx[3].z,1
886 glVertexAttribPointer(VS_COLOR, 4, GL_FLOAT,GL_FALSE, 0, &colour );
887 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,0,&vertices);
888 glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, 0, &tex);
890 glDrawArrays(GL_TRIANGLES,0,6);
893 //Restore old pointers
894 glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
895 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
896 glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u));
900 if( cullface ) glEnable(GL_CULL_FACE);
904 void OGLRender::DrawSimpleRect(int nX0, int nY0, int nX1, int nY1, uint32 dwColor, float depth, float rhw)
906 StartDrawSimpleRect(nX0, nY0, nX1, nY1, dwColor, depth, rhw);
908 GLboolean cullface = glIsEnabled(GL_CULL_FACE);
909 glDisable(GL_CULL_FACE);
912 float a = (dwColor>>24)/255.0f;
913 float r = ((dwColor>>16)&0xFF)/255.0f;
914 float g = ((dwColor>>8)&0xFF)/255.0f;
915 float b = (dwColor&0xFF)/255.0f;
919 glBegin(GL_TRIANGLE_FAN);
922 glVertex3f(m_simpleRectVtx[1].x, m_simpleRectVtx[0].y, -depth);
923 glVertex3f(m_simpleRectVtx[1].x, m_simpleRectVtx[1].y, -depth);
924 glVertex3f(m_simpleRectVtx[0].x, m_simpleRectVtx[1].y, -depth);
925 glVertex3f(m_simpleRectVtx[0].x, m_simpleRectVtx[0].y, -depth);
930 #elif SDL_VIDEO_OPENGL_ES2
937 float w = windowSetting.uDisplayWidth / 2.0f, h = windowSetting.uDisplayHeight / 2.0f, inv = 1.0f;
939 GLfloat vertices[] = {
940 -inv + m_simpleRectVtx[1].x / w, inv - m_simpleRectVtx[0].y / h, -depth, 1,
941 -inv + m_simpleRectVtx[1].x / w, inv - m_simpleRectVtx[1].y / h, -depth, 1,
942 -inv + m_simpleRectVtx[0].x / w, inv - m_simpleRectVtx[1].y / h, -depth, 1,
943 -inv + m_simpleRectVtx[0].x / w, inv - m_simpleRectVtx[0].y / h, -depth, 1
946 glVertexAttribPointer(VS_COLOR, 4, GL_FLOAT,GL_FALSE, 0, &colour );
947 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,0,&vertices);
948 glDisableVertexAttribArray(VS_TEXCOORD0);
950 glDrawArrays(GL_TRIANGLE_FAN,0,4);
953 //Restore old pointers
954 glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
955 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
956 glEnableVertexAttribArray(VS_TEXCOORD0);
960 if( cullface ) glEnable(GL_CULL_FACE);
964 void OGLRender::InitCombinerBlenderForSimpleRectDraw(uint32 tile)
966 //glEnable(GL_CULL_FACE);
967 EnableTexUnit(0,FALSE);
971 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
973 //glEnable(GL_ALPHA_TEST);
976 COLOR OGLRender::PostProcessDiffuseColor(COLOR curDiffuseColor)
978 uint32 color = curDiffuseColor;
979 uint32 colorflag = m_pColorCombiner->m_pDecodedMux->m_dwShadeColorChannelFlag;
980 uint32 alphaflag = m_pColorCombiner->m_pDecodedMux->m_dwShadeAlphaChannelFlag;
981 if( colorflag+alphaflag != MUX_0 )
983 if( (colorflag & 0xFFFFFF00) == 0 && (alphaflag & 0xFFFFFF00) == 0 )
985 color = (m_pColorCombiner->GetConstFactor(colorflag, alphaflag, curDiffuseColor));
988 color = (CalculateConstFactor(colorflag, alphaflag, curDiffuseColor));
991 //return (color<<8)|(color>>24);
995 COLOR OGLRender::PostProcessSpecularColor()
1000 void OGLRender::SetViewportRender()
1002 glViewportWrapper(windowSetting.vpLeftW, windowSetting.uDisplayHeight-windowSetting.vpTopW-windowSetting.vpHeightW+windowSetting.statusBarHeightToUse, windowSetting.vpWidthW, windowSetting.vpHeightW);
1003 OPENGL_CHECK_ERRORS;
1006 void OGLRender::RenderReset()
1008 CRender::RenderReset();
1010 glMatrixMode(GL_PROJECTION);
1011 OPENGL_CHECK_ERRORS;
1013 OPENGL_CHECK_ERRORS;
1014 glOrtho(0, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight, 0, -1, 1);
1015 OPENGL_CHECK_ERRORS;
1018 glMatrixMode(GL_MODELVIEW);
1019 OPENGL_CHECK_ERRORS;
1021 OPENGL_CHECK_ERRORS;
1024 void OGLRender::SetAlphaTestEnable(BOOL bAlphaTestEnable)
1027 if( bAlphaTestEnable && debuggerEnableAlphaTest )
1029 if( bAlphaTestEnable )
1031 #if SDL_VIDEO_OPENGL
1032 glEnable(GL_ALPHA_TEST);
1034 glDisable(GL_ALPHA_TEST);
1035 #elif SDL_VIDEO_OPENGL_ES2
1037 COGL_FragmentProgramCombiner* frag = (COGL_FragmentProgramCombiner*)m_pColorCombiner;
1038 frag->m_AlphaRef = m_dwAlpha / 255.0f;
1042 COGL_FragmentProgramCombiner* frag = (COGL_FragmentProgramCombiner*)m_pColorCombiner;
1043 frag->m_AlphaRef = 0.0f;
1046 OPENGL_CHECK_ERRORS;
1049 void OGLRender::BindTexture(GLuint texture, int unitno)
1054 DebuggerAppendMsg("Check me, base ogl bind texture, unit no != 0");
1057 if( m_curBoundTex[0] != texture )
1059 glBindTexture(GL_TEXTURE_2D,texture);
1060 OPENGL_CHECK_ERRORS;
1061 m_curBoundTex[0] = texture;
1065 void OGLRender::DisBindTexture(GLuint texture, int unitno)
1067 //EnableTexUnit(0,FALSE);
1068 //glBindTexture(GL_TEXTURE_2D, 0); //Not to bind any texture
1071 void OGLRender::EnableTexUnit(int unitno, BOOL flag)
1076 DebuggerAppendMsg("Check me, in the base ogl render, unitno!=0");
1079 if( m_texUnitEnabled[0] != flag )
1081 m_texUnitEnabled[0] = flag;
1082 #if SDL_VIDEO_OPENGL
1084 glEnable(GL_TEXTURE_2D);
1086 glDisable(GL_TEXTURE_2D);
1087 #elif SDL_VIDEO_OPENGL_ES2
1090 pglActiveTexture(GL_TEXTURE0_ARB + unitno);
1091 OPENGL_CHECK_ERRORS;
1092 glBindTexture(GL_TEXTURE_2D,m_curBoundTex[unitno]);
1096 pglActiveTexture(GL_TEXTURE0_ARB + unitno);
1097 OPENGL_CHECK_ERRORS;
1098 glEnable(GL_BLEND); //Need blend for transparent disabled texture
1099 glBindTexture(GL_TEXTURE_2D,disabledTextureID);
1102 OPENGL_CHECK_ERRORS;
1106 void OGLRender::TexCoord2f(float u, float v)
1111 void OGLRender::TexCoord(TLITVERTEX &vtxInfo)
1113 glTexCoord2f(vtxInfo.tcord[0].u, vtxInfo.tcord[0].v);
1116 void OGLRender::UpdateScissor()
1118 if( options.bEnableHacks && g_CI.dwWidth == 0x200 && gRDP.scissor.right == 0x200 && g_CI.dwWidth>(*g_GraphicsInfo.VI_WIDTH_REG & 0xFFF) )
1121 uint32 width = *g_GraphicsInfo.VI_WIDTH_REG & 0xFFF;
1122 uint32 height = (gRDP.scissor.right*gRDP.scissor.bottom)/width;
1123 glEnable(GL_SCISSOR_TEST);
1124 OPENGL_CHECK_ERRORS;
1125 glScissor(0, int(height*windowSetting.fMultY+windowSetting.statusBarHeightToUse),
1126 int(width*windowSetting.fMultX), int(height*windowSetting.fMultY) );
1127 OPENGL_CHECK_ERRORS;
1131 UpdateScissorWithClipRatio();
1135 void OGLRender::ApplyRDPScissor(bool force)
1137 if( !force && status.curScissor == RDP_SCISSOR ) return;
1139 if( options.bEnableHacks && g_CI.dwWidth == 0x200 && gRDP.scissor.right == 0x200 && g_CI.dwWidth>(*g_GraphicsInfo.VI_WIDTH_REG & 0xFFF) )
1142 uint32 width = *g_GraphicsInfo.VI_WIDTH_REG & 0xFFF;
1143 uint32 height = (gRDP.scissor.right*gRDP.scissor.bottom)/width;
1144 glEnable(GL_SCISSOR_TEST);
1145 OPENGL_CHECK_ERRORS;
1146 glScissor(0, int(height*windowSetting.fMultY+windowSetting.statusBarHeightToUse),
1147 int(width*windowSetting.fMultX), int(height*windowSetting.fMultY) );
1148 OPENGL_CHECK_ERRORS;
1152 glScissor(int(gRDP.scissor.left*windowSetting.fMultX), int((windowSetting.uViHeight-gRDP.scissor.bottom)*windowSetting.fMultY+windowSetting.statusBarHeightToUse),
1153 int((gRDP.scissor.right-gRDP.scissor.left)*windowSetting.fMultX), int((gRDP.scissor.bottom-gRDP.scissor.top)*windowSetting.fMultY ));
1154 OPENGL_CHECK_ERRORS;
1157 status.curScissor = RDP_SCISSOR;
1160 void OGLRender::ApplyScissorWithClipRatio(bool force)
1162 if( !force && status.curScissor == RSP_SCISSOR ) return;
1164 glEnable(GL_SCISSOR_TEST);
1165 OPENGL_CHECK_ERRORS;
1166 glScissor(windowSetting.clipping.left, int((windowSetting.uViHeight-gRSP.real_clip_scissor_bottom)*windowSetting.fMultY)+windowSetting.statusBarHeightToUse,
1167 windowSetting.clipping.width, windowSetting.clipping.height);
1168 OPENGL_CHECK_ERRORS;
1170 status.curScissor = RSP_SCISSOR;
1173 void OGLRender::SetFogMinMax(float fMin, float fMax)
1175 #if SDL_VIDEO_OPENGL
1176 glFogf(GL_FOG_START, gRSPfFogMin); // Fog Start Depth
1177 OPENGL_CHECK_ERRORS;
1178 glFogf(GL_FOG_END, gRSPfFogMax); // Fog End Depth
1179 OPENGL_CHECK_ERRORS;
1180 #elif SDL_VIDEO_OPENGL_ES2
1181 ((COGL_FragmentProgramCombiner*)m_pColorCombiner)->UpdateFog(gRSP.bFogEnabled);
1182 OPENGL_CHECK_ERRORS;
1186 void OGLRender::TurnFogOnOff(bool flag)
1188 #if SDL_VIDEO_OPENGL
1193 OPENGL_CHECK_ERRORS;
1194 #elif SDL_VIDEO_OPENGL_ES2
1195 ((COGL_FragmentProgramCombiner*)m_pColorCombiner)->UpdateFog(flag);
1196 OPENGL_CHECK_ERRORS;
1200 void OGLRender::SetFogEnable(bool bEnable)
1202 DEBUGGER_IF_DUMP( (gRSP.bFogEnabled != (bEnable==TRUE) && logFog ), TRACE1("Set Fog %s", bEnable? "enable":"disable"));
1204 gRSP.bFogEnabled = bEnable&&(options.fogMethod == 1);
1207 if(options.fogMethod == 2)
1209 gRSP.bFogEnabled = true;
1212 #if SDL_VIDEO_OPENGL
1213 if( gRSP.bFogEnabled )
1215 //TRACE2("Enable fog, min=%f, max=%f",gRSPfFogMin,gRSPfFogMax );
1216 glFogfv(GL_FOG_COLOR, gRDP.fvFogColor); // Set Fog Color
1217 OPENGL_CHECK_ERRORS;
1218 glFogf(GL_FOG_START, gRSPfFogMin); // Fog Start Depth
1219 OPENGL_CHECK_ERRORS;
1220 glFogf(GL_FOG_END, gRSPfFogMax); // Fog End Depth
1221 OPENGL_CHECK_ERRORS;
1223 OPENGL_CHECK_ERRORS;
1228 OPENGL_CHECK_ERRORS;
1230 #elif SDL_VIDEO_OPENGL_ES2
1231 ((COGL_FragmentProgramCombiner*)m_pColorCombiner)->UpdateFog(gRSP.bFogEnabled);
1232 OPENGL_CHECK_ERRORS;
1236 void OGLRender::SetFogColor(uint32 r, uint32 g, uint32 b, uint32 a)
1238 gRDP.fogColor = COLOR_RGBA(r, g, b, a);
1239 gRDP.fvFogColor[0] = r/255.0f; //r
1240 gRDP.fvFogColor[1] = g/255.0f; //g
1241 gRDP.fvFogColor[2] = b/255.0f; //b
1242 gRDP.fvFogColor[3] = a/255.0f; //a
1243 #if SDL_VIDEO_OPENGL
1244 glFogfv(GL_FOG_COLOR, gRDP.fvFogColor); // Set Fog Color
1246 OPENGL_CHECK_ERRORS;
1249 void OGLRender::DisableMultiTexture()
1251 pglActiveTexture(GL_TEXTURE1_ARB);
1252 OPENGL_CHECK_ERRORS;
1253 EnableTexUnit(1,FALSE);
1254 pglActiveTexture(GL_TEXTURE0_ARB);
1255 OPENGL_CHECK_ERRORS;
1256 EnableTexUnit(0,FALSE);
1257 pglActiveTexture(GL_TEXTURE0_ARB);
1258 OPENGL_CHECK_ERRORS;
1259 EnableTexUnit(0,TRUE);
1262 void OGLRender::EndRendering(void)
1264 #if SDL_VIDEO_OPENGL
1266 OPENGL_CHECK_ERRORS;
1268 if( CRender::gRenderReferenceCount > 0 )
1269 CRender::gRenderReferenceCount--;
1272 void OGLRender::glViewportWrapper(GLint x, GLint y, GLsizei width, GLsizei height, bool flag)
1274 static GLint mx=0,my=0;
1275 static GLsizei m_width=0, m_height=0;
1276 static bool mflag=true;
1278 if( x!=mx || y!=my || width!=m_width || height!=m_height || mflag!=flag)
1285 glMatrixMode(GL_PROJECTION);
1286 OPENGL_CHECK_ERRORS;
1288 OPENGL_CHECK_ERRORS;
1289 if( flag ) glOrtho(0, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight, 0, -1, 1);
1290 OPENGL_CHECK_ERRORS;
1291 glViewport(x,y,width,height);
1292 OPENGL_CHECK_ERRORS;