8ced90f3829f996ee246680fb41510e3664ab758
[mupen64plus-pandora.git] / source / rice_gles / src / OGLRender.cpp
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 "osal_opengl.h"
20
21 #if SDL_VIDEO_OPENGL
22 #include "OGLExtensions.h"
23 #elif SDL_VIDEO_OPENGL_ES2
24 #include "OGLES2FragmentShaders.h"
25 #endif
26 #include "OGLDebug.h"
27 #include "OGLRender.h"
28 #include "OGLGraphicsContext.h"
29 #include "OGLTexture.h"
30 #include "TextureManager.h"
31
32 #ifdef PAULSCODE
33 //include "ae_bridge.h"
34 //static int hardwareType = HARDWARE_TYPE_UNKNOWN;
35 #endif
36
37 // FIXME: Use OGL internal L/T and matrix stack
38 // FIXME: Use OGL lookupAt function
39 // FIXME: Use OGL DisplayList
40
41 UVFlagMap OGLXUVFlagMaps[] =
42 {
43     {TEXTURE_UV_FLAG_WRAP, GL_REPEAT},
44     {TEXTURE_UV_FLAG_MIRROR, GL_MIRRORED_REPEAT_ARB},
45     {TEXTURE_UV_FLAG_CLAMP, GL_CLAMP},
46 };
47
48 #if SDL_VIDEO_OPENGL_ES2
49   static GLuint disabledTextureID;
50 #endif
51
52 //===================================================================
53 OGLRender::OGLRender()
54 {
55     COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext);
56     m_bSupportFogCoordExt = pcontext->m_bSupportFogCoord;
57     m_bMultiTexture = pcontext->m_bSupportMultiTexture;
58 #ifdef HAVE_GLES
59     m_bSupportClampToEdge = true;
60 #else
61     m_bSupportClampToEdge = false;
62 #endif
63     for( int i=0; i<8; i++ )
64     {
65         m_curBoundTex[i]=0;
66         m_texUnitEnabled[i]=FALSE;
67     }
68
69 #if SDL_VIDEO_OPENGL
70 /*#ifdef HAVE_GLES
71     m_bEnableMultiTexture = true;
72 #else*/
73     m_bEnableMultiTexture = false;
74 //#endif
75
76 #elif SDL_VIDEO_OPENGL_ES2
77     m_bEnableMultiTexture = true;
78
79     //Create a texture as replacement for glEnable/Disable(GL_TEXTURE_2D)
80     unsigned int white[8*8];
81         for (int i=0; i<8*8; i++) {
82                 //white[i].r = white[i].g = white[i].b = 0;
83                 //white[i].a = 0;
84                 white[i] = 0;
85         }
86     glGenTextures(1,&disabledTextureID);
87     OPENGL_CHECK_ERRORS;
88     glBindTexture(GL_TEXTURE_2D, disabledTextureID);
89     OPENGL_CHECK_ERRORS;
90     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
91     OPENGL_CHECK_ERRORS;
92     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, white);
93     OPENGL_CHECK_ERRORS;
94 #endif
95 }
96
97 OGLRender::~OGLRender()
98 {
99     ClearDeviceObjects();
100 }
101
102 bool OGLRender::InitDeviceObjects()
103 {
104     // enable Z-buffer by default
105     ZBufferEnable(true);
106     return true;
107 }
108
109 bool OGLRender::ClearDeviceObjects()
110 {
111     return true;
112 }
113
114 void OGLRender::Initialize(void)
115 {
116     glMatrixMode(GL_MODELVIEW);
117     OPENGL_CHECK_ERRORS;
118     glLoadIdentity();
119     OPENGL_CHECK_ERRORS;
120
121     glViewportWrapper(0, windowSetting.statusBarHeightToUse, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight);
122     OPENGL_CHECK_ERRORS;
123
124 #if SDL_VIDEO_OPENGL
125     COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext);
126 #ifndef HAVE_GLES
127     if( pcontext->IsExtensionSupported("GL_IBM_texture_mirrored_repeat") )
128     {
129         OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_MIRRORED_REPEAT_IBM;
130     }
131     else if( pcontext->IsExtensionSupported("ARB_texture_mirrored_repeat") )
132 #endif
133     {
134         OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_MIRRORED_REPEAT_ARB;
135     }
136 #ifndef HAVE_GLES
137     else
138     {
139         OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_REPEAT;
140     }
141     if( pcontext->IsExtensionSupported("GL_ARB_texture_border_clamp") || pcontext->IsExtensionSupported("GL_EXT_texture_edge_clamp") )
142 #endif
143     {
144         m_bSupportClampToEdge = true;
145         OGLXUVFlagMaps[TEXTURE_UV_FLAG_CLAMP].realFlag = GL_CLAMP_TO_EDGE;
146     }
147 #ifndef HAVE_GLES
148     else
149     {
150         m_bSupportClampToEdge = false;
151         OGLXUVFlagMaps[TEXTURE_UV_FLAG_CLAMP].realFlag = GL_CLAMP;
152     }
153 #endif
154     glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][0]) );
155     OPENGL_CHECK_ERRORS;
156     glEnableClientState( GL_VERTEX_ARRAY );
157     OPENGL_CHECK_ERRORS;
158
159     if( m_bMultiTexture )
160     {
161         pglClientActiveTextureARB( GL_TEXTURE0_ARB );
162         OPENGL_CHECK_ERRORS;
163         glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u) );
164         OPENGL_CHECK_ERRORS;
165         glEnableClientState( GL_TEXTURE_COORD_ARRAY );
166         OPENGL_CHECK_ERRORS;
167
168         pglClientActiveTextureARB( GL_TEXTURE1_ARB );
169         OPENGL_CHECK_ERRORS;
170         glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[1].u) );
171         OPENGL_CHECK_ERRORS;
172         glEnableClientState( GL_TEXTURE_COORD_ARRAY );
173         OPENGL_CHECK_ERRORS;
174         }
175     else
176     {
177         glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u) );
178         OPENGL_CHECK_ERRORS;
179         glEnableClientState( GL_TEXTURE_COORD_ARRAY );
180         OPENGL_CHECK_ERRORS;
181     }
182 #ifndef HAVE_GLES
183     if (m_bSupportFogCoordExt)
184     {
185         pglFogCoordPointerEXT( GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][4]) );
186         OPENGL_CHECK_ERRORS;
187         glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
188         OPENGL_CHECK_ERRORS;
189         glFogi( GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT );
190         OPENGL_CHECK_ERRORS;
191         glFogi(GL_FOG_MODE, GL_LINEAR); // Fog Mode
192         OPENGL_CHECK_ERRORS;
193         glFogf(GL_FOG_DENSITY, 1.0f); // How Dense Will The Fog Be
194         OPENGL_CHECK_ERRORS;
195         glHint(GL_FOG_HINT, GL_FASTEST); // Fog Hint Value
196         OPENGL_CHECK_ERRORS;
197         glFogi( GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT );
198         OPENGL_CHECK_ERRORS;
199         glFogf( GL_FOG_START, 0.0f );
200         OPENGL_CHECK_ERRORS;
201         glFogf( GL_FOG_END, 1.0f );
202         OPENGL_CHECK_ERRORS;
203     }
204 #endif
205     //glColorPointer( 1, GL_UNSIGNED_BYTE, sizeof(TLITVERTEX), &g_vtxBuffer[0].r);
206     glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
207     OPENGL_CHECK_ERRORS;
208     glEnableClientState( GL_COLOR_ARRAY );
209     OPENGL_CHECK_ERRORS;
210 #ifndef HAVE_GLES
211     if( pcontext->IsExtensionSupported("GL_NV_depth_clamp") )
212     {
213         glEnable(GL_DEPTH_CLAMP_NV);
214         OPENGL_CHECK_ERRORS;
215     }
216 #endif
217 #elif SDL_VIDEO_OPENGL_ES2
218     OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_MIRRORED_REPEAT;
219     m_bSupportClampToEdge = true;
220     OGLXUVFlagMaps[TEXTURE_UV_FLAG_CLAMP].realFlag = GL_CLAMP_TO_EDGE;
221 #endif
222
223 #ifdef PAULSCODE
224 //    hardwareType = Android_JNI_GetHardwareType();
225 #endif
226 }
227 //===================================================================
228 TextureFilterMap OglTexFilterMap[2]=
229 {
230     {FILTER_POINT, GL_NEAREST},
231     {FILTER_LINEAR, GL_LINEAR},
232 };
233
234 void OGLRender::ApplyTextureFilter()
235 {
236     static uint32 minflag=0xFFFF, magflag=0xFFFF;
237     static uint32 mtex;
238
239     if( m_texUnitEnabled[0] )
240     {
241         if( mtex != m_curBoundTex[0] )
242         {
243             mtex = m_curBoundTex[0];
244             minflag = m_dwMinFilter;
245             magflag = m_dwMagFilter;
246             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, OglTexFilterMap[m_dwMinFilter].realFilter);
247             OPENGL_CHECK_ERRORS;
248             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, OglTexFilterMap[m_dwMagFilter].realFilter);
249             OPENGL_CHECK_ERRORS;
250         }
251         else
252         {
253             if( minflag != (unsigned int)m_dwMinFilter )
254             {
255                 minflag = m_dwMinFilter;
256                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, OglTexFilterMap[m_dwMinFilter].realFilter);
257                 OPENGL_CHECK_ERRORS;
258             }
259             if( magflag != (unsigned int)m_dwMagFilter )
260             {
261                 magflag = m_dwMagFilter;
262                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, OglTexFilterMap[m_dwMagFilter].realFilter);
263                 OPENGL_CHECK_ERRORS;
264             }   
265         }
266     }
267 }
268
269 void OGLRender::SetShadeMode(RenderShadeMode mode)
270 {
271 #if SDL_VIDEO_OPENGL
272     if( mode == SHADE_SMOOTH )
273         glShadeModel(GL_SMOOTH);
274     else
275         glShadeModel(GL_FLAT);
276     OPENGL_CHECK_ERRORS;
277 #endif
278 }
279
280 void OGLRender::ZBufferEnable(BOOL bZBuffer)
281 {
282     gRSP.bZBufferEnabled = bZBuffer;
283     if( g_curRomInfo.bForceDepthBuffer )
284         bZBuffer = TRUE;
285     if( bZBuffer )
286     {
287         glDepthMask(GL_TRUE);
288         OPENGL_CHECK_ERRORS;
289         //glEnable(GL_DEPTH_TEST);
290         glDepthFunc( GL_LEQUAL );
291         OPENGL_CHECK_ERRORS;
292     }
293     else
294     {
295         glDepthMask(GL_FALSE);
296         OPENGL_CHECK_ERRORS;
297         //glDisable(GL_DEPTH_TEST);
298         glDepthFunc( GL_ALWAYS );
299         OPENGL_CHECK_ERRORS;
300     }
301 }
302
303 void OGLRender::ClearBuffer(bool cbuffer, bool zbuffer)
304 {
305     uint32 flag=0;
306     if( cbuffer )   flag |= GL_COLOR_BUFFER_BIT;
307     if( zbuffer )   flag |= GL_DEPTH_BUFFER_BIT;
308     float depth = ((gRDP.originalFillColor&0xFFFF)>>2)/(float)0x3FFF;
309     glClearDepth(depth);
310     OPENGL_CHECK_ERRORS;
311     glClear(flag);
312     OPENGL_CHECK_ERRORS;
313 }
314
315 void OGLRender::ClearZBuffer(float depth)
316 {
317     uint32 flag=GL_DEPTH_BUFFER_BIT;
318     glClearDepth(depth);
319     OPENGL_CHECK_ERRORS;
320     glClear(flag);
321     OPENGL_CHECK_ERRORS;
322 }
323
324 void OGLRender::SetZCompare(BOOL bZCompare)
325 {
326     if( g_curRomInfo.bForceDepthBuffer )
327         bZCompare = TRUE;
328
329     gRSP.bZBufferEnabled = bZCompare;
330     if( bZCompare == TRUE )
331     {
332         //glEnable(GL_DEPTH_TEST);
333         glDepthFunc( GL_LEQUAL );
334         OPENGL_CHECK_ERRORS;
335     }
336     else
337     {
338         //glDisable(GL_DEPTH_TEST);
339         glDepthFunc( GL_ALWAYS );
340         OPENGL_CHECK_ERRORS;
341     }
342 }
343
344 void OGLRender::SetZUpdate(BOOL bZUpdate)
345 {
346     if( g_curRomInfo.bForceDepthBuffer )
347         bZUpdate = TRUE;
348
349     if( bZUpdate )
350     {
351         //glEnable(GL_DEPTH_TEST);
352         glDepthMask(GL_TRUE);
353         OPENGL_CHECK_ERRORS;
354     }
355     else
356     {
357         glDepthMask(GL_FALSE);
358         OPENGL_CHECK_ERRORS;
359     }
360 }
361
362 void OGLRender::ApplyZBias(int bias)
363 {
364     float f1 = bias > 0 ? -3.0f : 0.0f;  // z offset = -3.0 * max(abs(dz/dx),abs(dz/dy)) per pixel delta z slope
365     float f2 = bias > 0 ? -3.0f : 0.0f;  // z offset += -3.0 * 1 bit
366
367 #ifdef PAULSCODE
368 //    Android_JNI_GetPolygonOffset(hardwareType, bias, &f1, &f2);
369 //      glPolygonOffset(0.2f, 0.2f);
370 #endif
371
372     if (bias > 0)
373     {
374         glEnable(GL_POLYGON_OFFSET_FILL);  // enable z offsets
375         OPENGL_CHECK_ERRORS;
376     }
377     else
378     {
379         glDisable(GL_POLYGON_OFFSET_FILL);  // disable z offsets
380         OPENGL_CHECK_ERRORS;
381     }
382     glPolygonOffset(f1, f2);  // set bias functions
383     OPENGL_CHECK_ERRORS;
384 }
385
386 void OGLRender::SetZBias(int bias)
387 {
388 #if defined(DEBUGGER)
389     if( pauseAtNext == true )
390       DebuggerAppendMsg("Set zbias = %d", bias);
391 #endif
392     // set member variable and apply the setting in opengl
393     m_dwZBias = bias;
394     ApplyZBias(bias);
395 }
396
397 void OGLRender::SetAlphaRef(uint32 dwAlpha)
398 {
399     if (m_dwAlpha != dwAlpha)
400     {
401         m_dwAlpha = dwAlpha;
402 #if SDL_VIDEO_OPENGL
403         glAlphaFunc(GL_GEQUAL, (float)dwAlpha);
404         OPENGL_CHECK_ERRORS;
405 #endif
406     }
407 }
408
409 void OGLRender::ForceAlphaRef(uint32 dwAlpha)
410 {
411 #if SDL_VIDEO_OPENGL
412     float ref = dwAlpha/255.0f;
413     glAlphaFunc(GL_GEQUAL, ref);
414     OPENGL_CHECK_ERRORS;
415 #elif SDL_VIDEO_OPENGL_ES2
416     m_dwAlpha = dwAlpha;
417 #endif
418 }
419
420 void OGLRender::SetFillMode(FillMode mode)
421 {
422 #if SDL_VIDEO_OPENGL
423 #ifndef HAVE_GLES
424     if( mode == RICE_FILLMODE_WINFRAME )
425     {
426         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
427         OPENGL_CHECK_ERRORS;
428     }
429     else
430     {
431         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
432         OPENGL_CHECK_ERRORS;
433     }
434 #endif
435 #endif
436 }
437
438 void OGLRender::SetCullMode(bool bCullFront, bool bCullBack)
439 {
440     CRender::SetCullMode(bCullFront, bCullBack);
441     if( bCullFront && bCullBack )
442     {
443         glCullFace(GL_FRONT_AND_BACK);
444         OPENGL_CHECK_ERRORS;
445         glEnable(GL_CULL_FACE);
446         OPENGL_CHECK_ERRORS;
447     }
448     else if( bCullFront )
449     {
450         glCullFace(GL_FRONT);
451         OPENGL_CHECK_ERRORS;
452         glEnable(GL_CULL_FACE);
453         OPENGL_CHECK_ERRORS;
454     }
455     else if( bCullBack )
456     {
457         glCullFace(GL_BACK);
458         OPENGL_CHECK_ERRORS;
459         glEnable(GL_CULL_FACE);
460         OPENGL_CHECK_ERRORS;
461     }
462     else
463     {
464         glDisable(GL_CULL_FACE);
465         OPENGL_CHECK_ERRORS;
466     }
467 }
468
469 bool OGLRender::SetCurrentTexture(int tile, CTexture *handler,uint32 dwTileWidth, uint32 dwTileHeight, TxtrCacheEntry *pTextureEntry)
470 {
471     RenderTexture &texture = g_textures[tile];
472     texture.pTextureEntry = pTextureEntry;
473
474     if( handler!= NULL  && texture.m_lpsTexturePtr != handler->GetTexture() )
475     {
476         texture.m_pCTexture = handler;
477         texture.m_lpsTexturePtr = handler->GetTexture();
478
479         texture.m_dwTileWidth = dwTileWidth;
480         texture.m_dwTileHeight = dwTileHeight;
481
482         if( handler->m_bIsEnhancedTexture )
483         {
484             texture.m_fTexWidth = (float)pTextureEntry->pTexture->m_dwCreatedTextureWidth;
485             texture.m_fTexHeight = (float)pTextureEntry->pTexture->m_dwCreatedTextureHeight;
486         }
487         else
488         {
489             texture.m_fTexWidth = (float)handler->m_dwCreatedTextureWidth;
490             texture.m_fTexHeight = (float)handler->m_dwCreatedTextureHeight;
491         }
492     }
493     
494     return true;
495 }
496
497 bool OGLRender::SetCurrentTexture(int tile, TxtrCacheEntry *pEntry)
498 {
499     if (pEntry != NULL && pEntry->pTexture != NULL)
500     {   
501         SetCurrentTexture( tile, pEntry->pTexture,  pEntry->ti.WidthToCreate, pEntry->ti.HeightToCreate, pEntry);
502         return true;
503     }
504     else
505     {
506         SetCurrentTexture( tile, NULL, 64, 64, NULL );
507         return false;
508     }
509     return true;
510 }
511
512 void OGLRender::SetAddressUAllStages(uint32 dwTile, TextureUVFlag dwFlag)
513 {
514     SetTextureUFlag(dwFlag, dwTile);
515 }
516
517 void OGLRender::SetAddressVAllStages(uint32 dwTile, TextureUVFlag dwFlag)
518 {
519     SetTextureVFlag(dwFlag, dwTile);
520 }
521
522 void OGLRender::SetTexWrapS(int unitno,GLuint flag)
523 {
524     static GLuint mflag;
525     static GLuint mtex;
526 #ifdef DEBUGGER
527     if( unitno != 0 )
528     {
529         DebuggerAppendMsg("Check me, unitno != 0 in base ogl");
530     }
531 #endif
532     if( m_curBoundTex[0] != mtex || mflag != flag )
533     {
534         mtex = m_curBoundTex[0];
535         mflag = flag;
536         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, flag);
537         OPENGL_CHECK_ERRORS;
538     }
539 }
540 void OGLRender::SetTexWrapT(int unitno,GLuint flag)
541 {
542     static GLuint mflag;
543     static GLuint mtex;
544     if( m_curBoundTex[0] != mtex || mflag != flag )
545     {
546         mtex = m_curBoundTex[0];
547         mflag = flag;
548         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, flag);
549         OPENGL_CHECK_ERRORS;
550     }
551 }
552
553 void OGLRender::SetTextureUFlag(TextureUVFlag dwFlag, uint32 dwTile)
554 {
555     TileUFlags[dwTile] = dwFlag;
556     if( dwTile == gRSP.curTile )    // For basic OGL, only support the 1st texel
557     {
558         COGLTexture* pTexture = g_textures[gRSP.curTile].m_pCOGLTexture;
559         if( pTexture )
560         {
561             EnableTexUnit(0,TRUE);
562             BindTexture(pTexture->m_dwTextureName, 0);
563         }
564         SetTexWrapS(0, OGLXUVFlagMaps[dwFlag].realFlag);
565     }
566 }
567 void OGLRender::SetTextureVFlag(TextureUVFlag dwFlag, uint32 dwTile)
568 {
569     TileVFlags[dwTile] = dwFlag;
570     if( dwTile == gRSP.curTile )    // For basic OGL, only support the 1st texel
571     {
572         COGLTexture* pTexture = g_textures[gRSP.curTile].m_pCOGLTexture;
573         if( pTexture ) 
574         {
575             EnableTexUnit(0,TRUE);
576             BindTexture(pTexture->m_dwTextureName, 0);
577         }
578         SetTexWrapT(0, OGLXUVFlagMaps[dwFlag].realFlag);
579     }
580 }
581
582 // Basic render drawing functions
583
584 bool OGLRender::RenderTexRect()
585 {
586     glViewportWrapper(0, windowSetting.statusBarHeightToUse, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight);
587     OPENGL_CHECK_ERRORS;
588
589     GLboolean cullface = glIsEnabled(GL_CULL_FACE);
590     glDisable(GL_CULL_FACE);
591     OPENGL_CHECK_ERRORS;
592
593     float depth = -(g_texRectTVtx[3].z*2-1);
594
595 #if SDL_VIDEO_OPENGL
596 #ifdef HAVE_GLES
597     GLfloat colour[] = {
598             g_texRectTVtx[3].r, g_texRectTVtx[3].g, g_texRectTVtx[3].b, g_texRectTVtx[3].a,
599             g_texRectTVtx[2].r, g_texRectTVtx[2].g, g_texRectTVtx[2].b, g_texRectTVtx[2].a,
600             g_texRectTVtx[1].r, g_texRectTVtx[1].g, g_texRectTVtx[1].b, g_texRectTVtx[1].a,
601             g_texRectTVtx[0].r, g_texRectTVtx[0].g, g_texRectTVtx[0].b, g_texRectTVtx[0].a
602     };
603
604     GLfloat tex[] = {
605             g_texRectTVtx[3].tcord[0].u,g_texRectTVtx[3].tcord[0].v,
606             g_texRectTVtx[2].tcord[0].u,g_texRectTVtx[2].tcord[0].v,
607             g_texRectTVtx[1].tcord[0].u,g_texRectTVtx[1].tcord[0].v,
608             g_texRectTVtx[0].tcord[0].u,g_texRectTVtx[0].tcord[0].v
609     };
610     GLfloat tex2[] = {
611             g_texRectTVtx[3].tcord[1].u,g_texRectTVtx[3].tcord[1].v,
612             g_texRectTVtx[2].tcord[1].u,g_texRectTVtx[2].tcord[1].v,
613             g_texRectTVtx[1].tcord[1].u,g_texRectTVtx[1].tcord[1].v,
614             g_texRectTVtx[0].tcord[1].u,g_texRectTVtx[0].tcord[1].v
615     };
616
617     GLfloat vertices[] = {
618             g_texRectTVtx[3].x, g_texRectTVtx[3].y, depth, 1,
619             g_texRectTVtx[2].x, g_texRectTVtx[2].y, depth, 1,
620             g_texRectTVtx[1].x, g_texRectTVtx[1].y, depth, 1,
621             g_texRectTVtx[0].x, g_texRectTVtx[0].y, depth, 1
622     };
623
624     if( m_bMultiTexture )
625     {
626                 glClientActiveTexture( GL_TEXTURE1 );
627                 //if (m_texUnitEnabled[1])
628                         glTexCoordPointer(2, GL_FLOAT, 0, &tex2);
629                 /*else
630                         glDisableClientState(GL_TEXTURE_COORD_ARRAY);*/
631                 glClientActiveTexture( GL_TEXTURE0 );
632     }
633         //if (m_texUnitEnabled[0])
634                 glTexCoordPointer(2, GL_FLOAT, 0, &tex);
635         /*else
636                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);*/
637
638     glColorPointer(4, GL_FLOAT,0, &colour );
639     glVertexPointer(4,GL_FLOAT, 0, &vertices);
640     glTexCoordPointer(2, GL_FLOAT, 0, &tex);
641     OPENGL_CHECK_ERRORS;
642     glDrawArrays(GL_TRIANGLE_FAN,0,4);
643     OPENGL_CHECK_ERRORS;
644
645     //Restore old pointers
646     glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
647
648     glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][0]) );
649 //      if (m_texUnitEnabled[0])
650                 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u) );
651 /*      else
652                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);*/
653     if( m_bMultiTexture )
654     {
655                 glClientActiveTexture( GL_TEXTURE1 );
656 //              if (m_texUnitEnabled[1])
657                         glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[1].u) );
658 /*              else
659                         glEnableClientState( GL_TEXTURE_COORD_ARRAY );*/
660     }
661 #else
662
663     glBegin(GL_TRIANGLE_FAN);
664
665     glColor4f(g_texRectTVtx[3].r, g_texRectTVtx[3].g, g_texRectTVtx[3].b, g_texRectTVtx[3].a);
666     TexCoord(g_texRectTVtx[3]);
667     glVertex3f(g_texRectTVtx[3].x, g_texRectTVtx[3].y, depth);
668     
669     glColor4f(g_texRectTVtx[2].r, g_texRectTVtx[2].g, g_texRectTVtx[2].b, g_texRectTVtx[2].a);
670     TexCoord(g_texRectTVtx[2]);
671     glVertex3f(g_texRectTVtx[2].x, g_texRectTVtx[2].y, depth);
672
673     glColor4f(g_texRectTVtx[1].r, g_texRectTVtx[1].g, g_texRectTVtx[1].b, g_texRectTVtx[1].a);
674     TexCoord(g_texRectTVtx[1]);
675     glVertex3f(g_texRectTVtx[1].x, g_texRectTVtx[1].y, depth);
676
677     glColor4f(g_texRectTVtx[0].r, g_texRectTVtx[0].g, g_texRectTVtx[0].b, g_texRectTVtx[0].a);
678     TexCoord(g_texRectTVtx[0]);
679     glVertex3f(g_texRectTVtx[0].x, g_texRectTVtx[0].y, depth);
680
681     glEnd();
682 #endif
683     OPENGL_CHECK_ERRORS;
684
685 #elif SDL_VIDEO_OPENGL_ES2
686     GLfloat colour[] = {
687             g_texRectTVtx[3].r, g_texRectTVtx[3].g, g_texRectTVtx[3].b, g_texRectTVtx[3].a,
688             g_texRectTVtx[2].r, g_texRectTVtx[2].g, g_texRectTVtx[2].b, g_texRectTVtx[2].a,
689             g_texRectTVtx[1].r, g_texRectTVtx[1].g, g_texRectTVtx[1].b, g_texRectTVtx[1].a,
690             g_texRectTVtx[0].r, g_texRectTVtx[0].g, g_texRectTVtx[0].b, g_texRectTVtx[0].a
691     };
692
693     GLfloat tex[] = {
694             g_texRectTVtx[3].tcord[0].u,g_texRectTVtx[3].tcord[0].v,
695             g_texRectTVtx[2].tcord[0].u,g_texRectTVtx[2].tcord[0].v,
696             g_texRectTVtx[1].tcord[0].u,g_texRectTVtx[1].tcord[0].v,
697             g_texRectTVtx[0].tcord[0].u,g_texRectTVtx[0].tcord[0].v
698     };
699
700     float w = windowSetting.uDisplayWidth / 2.0f, h = windowSetting.uDisplayHeight / 2.0f, inv = 1.0f;
701
702     GLfloat vertices[] = {
703             -inv + g_texRectTVtx[3].x / w, inv - g_texRectTVtx[3].y / h, depth, 1,
704             -inv + g_texRectTVtx[2].x / w, inv - g_texRectTVtx[2].y / h, depth, 1,
705             -inv + g_texRectTVtx[1].x / w, inv - g_texRectTVtx[1].y / h, depth, 1,
706             -inv + g_texRectTVtx[0].x / w, inv - g_texRectTVtx[0].y / h, depth, 1
707     };
708
709     glVertexAttribPointer(VS_COLOR, 4, GL_FLOAT,GL_TRUE, 0, &colour );
710     glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,0,&vertices);
711     glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, 0, &tex);
712     OPENGL_CHECK_ERRORS;
713     glDrawArrays(GL_TRIANGLE_FAN,0,4);
714     OPENGL_CHECK_ERRORS;
715
716     //Restore old pointers
717     glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
718     glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
719     glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u));
720 #endif
721
722     if( cullface ) glEnable(GL_CULL_FACE);
723     OPENGL_CHECK_ERRORS;
724
725     return true;
726 }
727
728 bool OGLRender::RenderFillRect(uint32 dwColor, float depth)
729 {
730     float a = (dwColor>>24)/255.0f;
731     float r = ((dwColor>>16)&0xFF)/255.0f;
732     float g = ((dwColor>>8)&0xFF)/255.0f;
733     float b = (dwColor&0xFF)/255.0f;
734     glViewportWrapper(0, windowSetting.statusBarHeightToUse, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight);
735     OPENGL_CHECK_ERRORS;
736
737     GLboolean cullface = glIsEnabled(GL_CULL_FACE);
738     glDisable(GL_CULL_FACE);
739     OPENGL_CHECK_ERRORS;
740
741 #if SDL_VIDEO_OPENGL
742 #ifdef HAVE_GLES
743     GLfloat colour[] = {
744             r,g,b,a,
745             r,g,b,a,
746             r,g,b,a,
747             r,g,b,a};
748
749     GLfloat vertices[] = {
750             m_fillRectVtx[0].x, m_fillRectVtx[1].y, depth, 1,
751             m_fillRectVtx[1].x, m_fillRectVtx[1].y, depth, 1,
752             m_fillRectVtx[1].x, m_fillRectVtx[0].y, depth, 1,
753             m_fillRectVtx[0].x, m_fillRectVtx[0].y, depth, 1
754     };
755
756     glColorPointer(4, GL_FLOAT, 0, &colour );
757     glVertexPointer(4,GL_FLOAT, 0,&vertices);
758     if( m_bMultiTexture ) {
759                 if (m_texUnitEnabled[1])
760                 {
761                         glClientActiveTexture( GL_TEXTURE1 );
762                         glActiveTexture( GL_TEXTURE1 );
763                         glDisableClientState( GL_TEXTURE_COORD_ARRAY );
764                         glDisable(GL_TEXTURE_2D);
765                 }
766     }
767         if (m_texUnitEnabled[0]) {
768         glClientActiveTexture( GL_TEXTURE0 );
769                 glActiveTexture( GL_TEXTURE0 );
770                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
771                 glDisable(GL_TEXTURE_2D);
772         }
773     OPENGL_CHECK_ERRORS;
774     glColor4f(r,g,b,a);
775     glDrawArrays(GL_TRIANGLE_FAN,0,4);
776     OPENGL_CHECK_ERRORS;
777     //Restore old pointers
778     glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
779     glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][0]) );
780         if (m_texUnitEnabled[0]) {
781                 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
782                 glEnable(GL_TEXTURE_2D);
783         }
784     if( m_bMultiTexture ) {
785                 glClientActiveTexture( GL_TEXTURE1 );
786                 glActiveTexture( GL_TEXTURE1 );
787                 if (m_texUnitEnabled[1])
788                 {
789                         glEnable(GL_TEXTURE_2D);
790                         glEnableClientState( GL_TEXTURE_COORD_ARRAY );
791                 }
792                 glActiveTexture( GL_TEXTURE0 );
793     }
794 #else
795
796     glBegin(GL_TRIANGLE_FAN);
797     glColor4f(r,g,b,a);
798     glVertex4f(m_fillRectVtx[0].x, m_fillRectVtx[1].y, depth, 1);
799     glVertex4f(m_fillRectVtx[1].x, m_fillRectVtx[1].y, depth, 1);
800     glVertex4f(m_fillRectVtx[1].x, m_fillRectVtx[0].y, depth, 1);
801     glVertex4f(m_fillRectVtx[0].x, m_fillRectVtx[0].y, depth, 1);
802     glEnd();
803     OPENGL_CHECK_ERRORS;
804 #endif
805 #elif SDL_VIDEO_OPENGL_ES2
806
807     GLfloat colour[] = {
808             r,g,b,a,
809             r,g,b,a,
810             r,g,b,a,
811             r,g,b,a};
812
813     float w = windowSetting.uDisplayWidth / 2.0f, h = windowSetting.uDisplayHeight / 2.0f, inv = 1.0f;
814
815     GLfloat vertices[] = {
816             -inv + m_fillRectVtx[0].x / w, inv - m_fillRectVtx[1].y / h, depth, 1,
817             -inv + m_fillRectVtx[1].x / w, inv - m_fillRectVtx[1].y / h, depth, 1,
818             -inv + m_fillRectVtx[1].x / w, inv - m_fillRectVtx[0].y / h, depth, 1,
819             -inv + m_fillRectVtx[0].x / w, inv - m_fillRectVtx[0].y / h, depth, 1
820     };
821
822     glVertexAttribPointer(VS_COLOR, 4, GL_FLOAT,GL_FALSE, 0, &colour );
823     glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,0,&vertices);
824     glDisableVertexAttribArray(VS_TEXCOORD0);
825     OPENGL_CHECK_ERRORS;
826     glDrawArrays(GL_TRIANGLE_FAN,0,4);
827     OPENGL_CHECK_ERRORS;
828
829     //Restore old pointers
830     glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
831     glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
832     glEnableVertexAttribArray(VS_TEXCOORD0);
833
834 #endif
835
836     if( cullface ) glEnable(GL_CULL_FACE);
837     OPENGL_CHECK_ERRORS;
838
839     return true;
840 }
841
842 bool OGLRender::RenderLine3D()
843 {
844 #if SDL_VIDEO_OPENGL
845     ApplyZBias(0);  // disable z offsets
846
847 #ifdef HAVE_GLES
848     GLfloat colour[] = {
849             m_line3DVtx[1].r,m_line3DVtx[1].g,m_line3DVtx[1].b,m_line3DVtx[1].a,
850             m_line3DVtx[1].r,m_line3DVtx[1].g,m_line3DVtx[1].b,m_line3DVtx[1].a,
851             m_line3DVtx[0].r,m_line3DVtx[0].g,m_line3DVtx[0].b,m_line3DVtx[0].a,
852             m_line3DVtx[0].r,m_line3DVtx[0].g,m_line3DVtx[0].b,m_line3DVtx[0].a};
853
854     GLfloat vertices[] = {
855             m_line3DVector[3].x, m_line3DVector[3].y, -m_line3DVtx[1].z, 1,
856             m_line3DVector[2].x, m_line3DVector[2].y, -m_line3DVtx[0].z, 1,
857             m_line3DVector[1].x, m_line3DVector[1].y, -m_line3DVtx[1].z, 1,
858             m_line3DVector[0].x, m_line3DVector[0].y, -m_line3DVtx[0].z, 1
859     };
860
861     glColorPointer(4, GL_FLOAT, 0, &colour );
862     glVertexPointer(4,GL_FLOAT, 0,&vertices);
863     if( m_bMultiTexture )
864     {
865                 glClientActiveTexture( GL_TEXTURE1 );
866                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
867         glClientActiveTexture( GL_TEXTURE0 );
868     }
869     glDisableClientState( GL_TEXTURE_COORD_ARRAY );
870     OPENGL_CHECK_ERRORS;
871     glDrawArrays(GL_TRIANGLE_FAN,0,4);
872     OPENGL_CHECK_ERRORS;
873     //Restore old pointers
874     glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
875     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
876     glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][0]) );
877     if( m_bMultiTexture )
878     {
879                 glClientActiveTexture( GL_TEXTURE1 );
880                 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
881     }
882 #else
883     glBegin(GL_TRIANGLE_FAN);
884
885     glColor4f(m_line3DVtx[1].r, m_line3DVtx[1].g, m_line3DVtx[1].b, m_line3DVtx[1].a);
886     glVertex3f(m_line3DVector[3].x, m_line3DVector[3].y, -m_line3DVtx[1].z);
887     glVertex3f(m_line3DVector[2].x, m_line3DVector[2].y, -m_line3DVtx[0].z);
888     
889     glColor4ub(m_line3DVtx[0].r, m_line3DVtx[0].g, m_line3DVtx[0].b, m_line3DVtx[0].a);
890     glVertex3f(m_line3DVector[1].x, m_line3DVector[1].y, -m_line3DVtx[1].z);
891     glVertex3f(m_line3DVector[0].x, m_line3DVector[0].y, -m_line3DVtx[0].z);
892
893     glEnd();
894 #endif
895     OPENGL_CHECK_ERRORS;
896
897     ApplyZBias(m_dwZBias);  // set Z offset back to previous value
898 #endif
899
900     return true;
901 }
902
903 extern FiddledVtx * g_pVtxBase;
904
905 // This is so weired that I can not do vertex transform by myself. I have to use
906 // OpenGL internal transform
907 bool OGLRender::RenderFlushTris()
908 {
909     if( !m_bSupportFogCoordExt )    
910         SetFogFlagForNegativeW();
911     else
912     {
913         if( !gRDP.bFogEnableInBlender && gRSP.bFogEnabled )
914         {
915             TurnFogOnOff(false);
916         }
917     }
918
919     ApplyZBias(m_dwZBias);  // set the bias factors
920
921     glViewportWrapper(windowSetting.vpLeftW, windowSetting.uDisplayHeight-windowSetting.vpTopW-windowSetting.vpHeightW+windowSetting.statusBarHeightToUse, windowSetting.vpWidthW, windowSetting.vpHeightW, false);
922     OPENGL_CHECK_ERRORS;
923
924 //    if (options.bOGLVertexClipper == FALSE )
925     {
926         glDrawElements( GL_TRIANGLES, gRSP.numVertices, GL_UNSIGNED_SHORT, g_vtxIndex );
927         OPENGL_CHECK_ERRORS;
928     }
929 /*    else
930     {
931         //ClipVertexesOpenGL();
932         // Redo the index
933         // Set the array
934         glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5Clipped[0][0]) );
935         glEnableClientState( GL_VERTEX_ARRAY );
936
937         pglClientActiveTextureARB( GL_TEXTURE0_ARB );
938         glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_clippedVtxBuffer[0].tcord[0].u) );
939         glEnableClientState( GL_TEXTURE_COORD_ARRAY );
940
941         pglClientActiveTextureARB( GL_TEXTURE1_ARB );
942         glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_clippedVtxBuffer[0].tcord[1].u) );
943         glEnableClientState( GL_TEXTURE_COORD_ARRAY );
944
945         glDrawElements( GL_TRIANGLES, gRSP.numVertices, GL_UNSIGNED_SHORT, g_vtxIndex );
946
947         // Reset the array
948         pglClientActiveTextureARB( GL_TEXTURE0_ARB );
949         glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u) );
950         glEnableClientState( GL_TEXTURE_COORD_ARRAY );
951
952         pglClientActiveTextureARB( GL_TEXTURE1_ARB );
953         glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[1].u) );
954         glEnableClientState( GL_TEXTURE_COORD_ARRAY );
955
956         glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][0]) );
957         glEnableClientState( GL_VERTEX_ARRAY );
958     }
959 */
960
961     if( !m_bSupportFogCoordExt )    
962         RestoreFogFlag();
963     else
964     {
965         if( !gRDP.bFogEnableInBlender && gRSP.bFogEnabled )
966         {
967             TurnFogOnOff(true);
968         }
969     }
970     return true;
971 }
972
973 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)
974 {
975     if( status.bVIOriginIsUpdated == true && currentRomOptions.screenUpdateSetting==SCREEN_UPDATE_AT_1ST_PRIMITIVE )
976     {
977         status.bVIOriginIsUpdated=false;
978         CGraphicsContext::Get()->UpdateFrame();
979         DEBUGGER_PAUSE_AND_DUMP_NO_UPDATE(NEXT_SET_CIMG,{DebuggerAppendMsg("Screen Update at 1st Simple2DTexture");});
980     }
981
982     StartDrawSimple2DTexture(x0, y0, x1, y1, u0, v0, u1, v1, dif, spe, z, rhw);
983
984     GLboolean cullface = glIsEnabled(GL_CULL_FACE);
985     glDisable(GL_CULL_FACE);
986     OPENGL_CHECK_ERRORS;
987
988     glViewportWrapper(0, windowSetting.statusBarHeightToUse, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight);
989     OPENGL_CHECK_ERRORS;
990
991     float a = (g_texRectTVtx[0].dcDiffuse >>24)/255.0f;
992     float r = ((g_texRectTVtx[0].dcDiffuse>>16)&0xFF)/255.0f;
993     float g = ((g_texRectTVtx[0].dcDiffuse>>8)&0xFF)/255.0f;
994     float b = (g_texRectTVtx[0].dcDiffuse&0xFF)/255.0f;
995
996 #if SDL_VIDEO_OPENGL
997
998 #ifdef HAVE_GLES
999     GLfloat colour[] = {
1000             r,g,b,a,
1001             r,g,b,a,
1002             r,g,b,a,
1003             r,g,b,a,
1004             r,g,b,a,
1005             r,g,b,a};
1006
1007     GLfloat tex[] = {
1008             g_texRectTVtx[0].tcord[0].u,g_texRectTVtx[0].tcord[0].v,
1009             g_texRectTVtx[1].tcord[0].u,g_texRectTVtx[1].tcord[0].v,
1010             g_texRectTVtx[2].tcord[0].u,g_texRectTVtx[2].tcord[0].v,
1011             g_texRectTVtx[0].tcord[0].u,g_texRectTVtx[0].tcord[0].v,
1012             g_texRectTVtx[2].tcord[0].u,g_texRectTVtx[2].tcord[0].v,
1013             g_texRectTVtx[3].tcord[0].u,g_texRectTVtx[3].tcord[0].v
1014     };
1015
1016     GLfloat vertices[] = {
1017             g_texRectTVtx[0].x, g_texRectTVtx[0].y, -g_texRectTVtx[0].z, 1,
1018             g_texRectTVtx[1].x, g_texRectTVtx[1].y, -g_texRectTVtx[1].z, 1,
1019             g_texRectTVtx[2].x, g_texRectTVtx[2].y, -g_texRectTVtx[2].z, 1,
1020             g_texRectTVtx[0].x, g_texRectTVtx[0].y, -g_texRectTVtx[0].z, 1,
1021             g_texRectTVtx[2].x, g_texRectTVtx[2].y, -g_texRectTVtx[2].z, 1,
1022             g_texRectTVtx[3].x, g_texRectTVtx[3].y, -g_texRectTVtx[3].z, 1
1023     };
1024
1025     glColorPointer(4, GL_FLOAT, 0, &colour );
1026     glVertexPointer(4,GL_FLOAT, 0,&vertices);
1027     if( m_bMultiTexture )
1028     {
1029                 glClientActiveTexture( GL_TEXTURE1 );
1030 //              if (m_texUnitEnabled[1])
1031                         glTexCoordPointer(2, GL_FLOAT, 0, &tex);
1032 /*              else
1033                         glDisableClientState( GL_TEXTURE_COORD_ARRAY );*/
1034         glClientActiveTexture( GL_TEXTURE0 );
1035     }
1036 //      if (m_texUnitEnabled[0])
1037                         glTexCoordPointer(2, GL_FLOAT, 0, &tex);
1038 /*              else
1039                         glDisableClientState( GL_TEXTURE_COORD_ARRAY );*/
1040     glDrawArrays(GL_TRIANGLES,0,6);
1041     //Restore old pointers
1042     glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
1043     glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u) );
1044 //      if (m_texUnitEnabled[1])
1045                 glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][0]) );
1046 /*      else
1047                         glEnableClientState( GL_TEXTURE_COORD_ARRAY );*/
1048     if( m_bMultiTexture )
1049     {
1050                 glClientActiveTexture( GL_TEXTURE1 );
1051 //              if (m_texUnitEnabled[1])
1052                         glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[1].u) );
1053 /*              else
1054                         glEnableClientState( GL_TEXTURE_COORD_ARRAY );*/
1055     }
1056 #else
1057     glBegin(GL_TRIANGLES);
1058
1059     glColor4f(r,g,b,a);
1060
1061     OGLRender::TexCoord(g_texRectTVtx[0]);
1062     glVertex3f(g_texRectTVtx[0].x, g_texRectTVtx[0].y, -g_texRectTVtx[0].z);
1063
1064     OGLRender::TexCoord(g_texRectTVtx[1]);
1065     glVertex3f(g_texRectTVtx[1].x, g_texRectTVtx[1].y, -g_texRectTVtx[1].z);
1066
1067     OGLRender::TexCoord(g_texRectTVtx[2]);
1068     glVertex3f(g_texRectTVtx[2].x, g_texRectTVtx[2].y, -g_texRectTVtx[2].z);
1069
1070     OGLRender::TexCoord(g_texRectTVtx[0]);
1071     glVertex3f(g_texRectTVtx[0].x, g_texRectTVtx[0].y, -g_texRectTVtx[0].z);
1072
1073     OGLRender::TexCoord(g_texRectTVtx[2]);
1074     glVertex3f(g_texRectTVtx[2].x, g_texRectTVtx[2].y, -g_texRectTVtx[2].z);
1075
1076     OGLRender::TexCoord(g_texRectTVtx[3]);
1077     glVertex3f(g_texRectTVtx[3].x, g_texRectTVtx[3].y, -g_texRectTVtx[3].z);
1078     glEnd();
1079 #endif    
1080     OPENGL_CHECK_ERRORS;
1081
1082 #elif SDL_VIDEO_OPENGL_ES2
1083
1084     GLfloat colour[] = {
1085             r,g,b,a,
1086             r,g,b,a,
1087             r,g,b,a,
1088             r,g,b,a,
1089             r,g,b,a,
1090             r,g,b,a
1091     };
1092
1093     GLfloat tex[] = {
1094             g_texRectTVtx[0].tcord[0].u,g_texRectTVtx[0].tcord[0].v,
1095             g_texRectTVtx[1].tcord[0].u,g_texRectTVtx[1].tcord[0].v,
1096             g_texRectTVtx[2].tcord[0].u,g_texRectTVtx[2].tcord[0].v,
1097
1098             g_texRectTVtx[0].tcord[0].u,g_texRectTVtx[0].tcord[0].v,
1099             g_texRectTVtx[2].tcord[0].u,g_texRectTVtx[2].tcord[0].v,
1100             g_texRectTVtx[3].tcord[0].u,g_texRectTVtx[3].tcord[0].v,
1101     };
1102
1103      float w = windowSetting.uDisplayWidth / 2.0f, h = windowSetting.uDisplayHeight / 2.0f, inv = 1.0f;
1104
1105     GLfloat vertices[] = {
1106             -inv + g_texRectTVtx[0].x/ w, inv - g_texRectTVtx[0].y/ h, -g_texRectTVtx[0].z,1,
1107             -inv + g_texRectTVtx[1].x/ w, inv - g_texRectTVtx[1].y/ h, -g_texRectTVtx[1].z,1,
1108             -inv + g_texRectTVtx[2].x/ w, inv - g_texRectTVtx[2].y/ h, -g_texRectTVtx[2].z,1,
1109
1110             -inv + g_texRectTVtx[0].x/ w, inv - g_texRectTVtx[0].y/ h, -g_texRectTVtx[0].z,1,
1111             -inv + g_texRectTVtx[2].x/ w, inv - g_texRectTVtx[2].y/ h, -g_texRectTVtx[2].z,1,
1112             -inv + g_texRectTVtx[3].x/ w, inv - g_texRectTVtx[3].y/ h, -g_texRectTVtx[3].z,1
1113     };
1114
1115     glVertexAttribPointer(VS_COLOR, 4, GL_FLOAT,GL_FALSE, 0, &colour );
1116     glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,0,&vertices);
1117     glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, 0, &tex);
1118     OPENGL_CHECK_ERRORS;
1119     glDrawArrays(GL_TRIANGLES,0,6);
1120     OPENGL_CHECK_ERRORS;
1121
1122     //Restore old pointers
1123     glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
1124     glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
1125     glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u));
1126
1127 #endif
1128
1129     if( cullface ) glEnable(GL_CULL_FACE);
1130     OPENGL_CHECK_ERRORS;
1131 }
1132
1133 void OGLRender::DrawSimpleRect(int nX0, int nY0, int nX1, int nY1, uint32 dwColor, float depth, float rhw)
1134 {
1135     StartDrawSimpleRect(nX0, nY0, nX1, nY1, dwColor, depth, rhw);
1136
1137     GLboolean cullface = glIsEnabled(GL_CULL_FACE);
1138     glDisable(GL_CULL_FACE);
1139     OPENGL_CHECK_ERRORS;
1140
1141     float a = (dwColor>>24)/255.0f;
1142     float r = ((dwColor>>16)&0xFF)/255.0f;
1143     float g = ((dwColor>>8)&0xFF)/255.0f;
1144     float b = (dwColor&0xFF)/255.0f;
1145
1146 #if SDL_VIDEO_OPENGL
1147
1148 #ifdef HAVE_GLES
1149     GLfloat colour[] = {
1150             r,g,b,a,
1151             r,g,b,a,
1152             r,g,b,a,
1153             r,g,b,a};
1154
1155     GLfloat vertices[] = {
1156             m_simpleRectVtx[1].x, m_simpleRectVtx[0].y, -depth, 1,
1157             m_simpleRectVtx[1].x, m_simpleRectVtx[1].y, -depth, 1,
1158             m_simpleRectVtx[0].x, m_simpleRectVtx[1].y, -depth, 1,
1159             m_simpleRectVtx[0].x, m_simpleRectVtx[0].y, -depth, 1
1160     };
1161
1162     glColorPointer(4, GL_FLOAT, 0, &colour );
1163     glVertexPointer(4,GL_FLOAT, 0,&vertices);
1164     if( m_bMultiTexture )
1165     {
1166                 glClientActiveTexture( GL_TEXTURE1 );
1167                 if (m_texUnitEnabled[1]) {
1168                         glActiveTexture( GL_TEXTURE1 );
1169                         glDisable(GL_TEXTURE_2D);
1170                 }
1171                 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1172         glClientActiveTexture( GL_TEXTURE0 );
1173     }
1174         if (m_texUnitEnabled[0]) {
1175                 glActiveTexture( GL_TEXTURE0 );
1176                 glDisable(GL_TEXTURE_2D);
1177         }
1178     glDisableClientState( GL_TEXTURE_COORD_ARRAY );
1179     OPENGL_CHECK_ERRORS;
1180     glColor4f(r,g,b,a);
1181     glDrawArrays(GL_TRIANGLE_FAN,0,4);
1182     OPENGL_CHECK_ERRORS;
1183     //Restore old pointers
1184     glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
1185     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1186     glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][0]) );
1187         if (m_texUnitEnabled[0]) {
1188                 glEnable(GL_TEXTURE_2D);
1189         }
1190     if( m_bMultiTexture )
1191     {
1192                 if (m_texUnitEnabled[1]) {
1193                         glActiveTexture( GL_TEXTURE1 );
1194                         glEnable(GL_TEXTURE_2D);
1195                 }
1196                 glClientActiveTexture( GL_TEXTURE1 );
1197                 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
1198     }
1199 #else
1200     glBegin(GL_TRIANGLE_FAN);
1201
1202     glColor4f(r,g,b,a);
1203     glVertex3f(m_simpleRectVtx[1].x, m_simpleRectVtx[0].y, -depth);
1204     glVertex3f(m_simpleRectVtx[1].x, m_simpleRectVtx[1].y, -depth);
1205     glVertex3f(m_simpleRectVtx[0].x, m_simpleRectVtx[1].y, -depth);
1206     glVertex3f(m_simpleRectVtx[0].x, m_simpleRectVtx[0].y, -depth);
1207     
1208     glEnd();
1209 #endif
1210     OPENGL_CHECK_ERRORS;
1211
1212 #elif SDL_VIDEO_OPENGL_ES2
1213
1214     GLfloat colour[] = {
1215             r,g,b,a,
1216             r,g,b,a,
1217             r,g,b,a,
1218             r,g,b,a};
1219     float w = windowSetting.uDisplayWidth / 2.0f, h = windowSetting.uDisplayHeight / 2.0f, inv = 1.0f;
1220
1221     GLfloat vertices[] = {
1222             -inv + m_simpleRectVtx[1].x / w, inv - m_simpleRectVtx[0].y / h, -depth, 1,
1223             -inv + m_simpleRectVtx[1].x / w, inv - m_simpleRectVtx[1].y / h, -depth, 1,
1224             -inv + m_simpleRectVtx[0].x / w, inv - m_simpleRectVtx[1].y / h, -depth, 1,
1225             -inv + m_simpleRectVtx[0].x / w, inv - m_simpleRectVtx[0].y / h, -depth, 1
1226     };
1227
1228     glVertexAttribPointer(VS_COLOR, 4, GL_FLOAT,GL_FALSE, 0, &colour );
1229     glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,0,&vertices);
1230     glDisableVertexAttribArray(VS_TEXCOORD0);
1231     OPENGL_CHECK_ERRORS;
1232     glDrawArrays(GL_TRIANGLE_FAN,0,4);
1233     OPENGL_CHECK_ERRORS;
1234
1235     //Restore old pointers
1236     glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
1237     glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
1238     glEnableVertexAttribArray(VS_TEXCOORD0);
1239
1240 #endif
1241
1242     if( cullface ) glEnable(GL_CULL_FACE);
1243     OPENGL_CHECK_ERRORS;
1244 }
1245
1246 void OGLRender::InitCombinerBlenderForSimpleRectDraw(uint32 tile)
1247 {
1248     //glEnable(GL_CULL_FACE);
1249     EnableTexUnit(0,FALSE);
1250     OPENGL_CHECK_ERRORS;
1251     glEnable(GL_BLEND);
1252     OPENGL_CHECK_ERRORS;
1253     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1254     OPENGL_CHECK_ERRORS;
1255     //glEnable(GL_ALPHA_TEST);
1256 }
1257
1258 COLOR OGLRender::PostProcessDiffuseColor(COLOR curDiffuseColor)
1259 {
1260     uint32 color = curDiffuseColor;
1261     uint32 colorflag = m_pColorCombiner->m_pDecodedMux->m_dwShadeColorChannelFlag;
1262     uint32 alphaflag = m_pColorCombiner->m_pDecodedMux->m_dwShadeAlphaChannelFlag;
1263     if( colorflag+alphaflag != MUX_0 )
1264     {
1265         if( (colorflag & 0xFFFFFF00) == 0 && (alphaflag & 0xFFFFFF00) == 0 )
1266         {
1267             color = (m_pColorCombiner->GetConstFactor(colorflag, alphaflag, curDiffuseColor));
1268         }
1269         else
1270             color = (CalculateConstFactor(colorflag, alphaflag, curDiffuseColor));
1271     }
1272
1273     //return (color<<8)|(color>>24);
1274     return color;
1275 }
1276
1277 COLOR OGLRender::PostProcessSpecularColor()
1278 {
1279     return 0;
1280 }
1281
1282 void OGLRender::SetViewportRender()
1283 {
1284     glViewportWrapper(windowSetting.vpLeftW, windowSetting.uDisplayHeight-windowSetting.vpTopW-windowSetting.vpHeightW+windowSetting.statusBarHeightToUse, windowSetting.vpWidthW, windowSetting.vpHeightW);
1285     OPENGL_CHECK_ERRORS;
1286 }
1287
1288 void OGLRender::RenderReset()
1289 {
1290     CRender::RenderReset();
1291
1292     glMatrixMode(GL_PROJECTION);
1293     OPENGL_CHECK_ERRORS;
1294     glLoadIdentity();
1295     OPENGL_CHECK_ERRORS;
1296     glOrtho(0, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight, 0, -1, 1);
1297     OPENGL_CHECK_ERRORS;
1298
1299     // position viewer 
1300     glMatrixMode(GL_MODELVIEW);
1301     OPENGL_CHECK_ERRORS;
1302     glLoadIdentity();
1303     OPENGL_CHECK_ERRORS;
1304 }
1305
1306 void OGLRender::SetAlphaTestEnable(BOOL bAlphaTestEnable)
1307 {
1308 #ifdef DEBUGGER
1309     if( bAlphaTestEnable && debuggerEnableAlphaTest )
1310 #else
1311     if( bAlphaTestEnable )
1312 #endif
1313 #if SDL_VIDEO_OPENGL
1314         glEnable(GL_ALPHA_TEST);
1315     else
1316         glDisable(GL_ALPHA_TEST);
1317 #elif SDL_VIDEO_OPENGL_ES2
1318     {
1319         COGL_FragmentProgramCombiner* frag = (COGL_FragmentProgramCombiner*)m_pColorCombiner;
1320         frag->m_AlphaRef = m_dwAlpha / 255.0f;
1321     }
1322     else
1323     {
1324         COGL_FragmentProgramCombiner* frag = (COGL_FragmentProgramCombiner*)m_pColorCombiner;
1325         frag->m_AlphaRef = 0.0f;
1326     }
1327 #endif
1328     OPENGL_CHECK_ERRORS;
1329 }
1330
1331 void OGLRender::BindTexture(GLuint texture, int unitno)
1332 {
1333 #ifdef DEBUGGER
1334     if( unitno != 0 )
1335     {
1336         DebuggerAppendMsg("Check me, base ogl bind texture, unit no != 0");
1337     }
1338 #endif
1339     if( m_curBoundTex[0] != texture )
1340     {
1341         glBindTexture(GL_TEXTURE_2D,texture);
1342         OPENGL_CHECK_ERRORS;
1343         m_curBoundTex[0] = texture;
1344     }
1345 }
1346
1347 void OGLRender::DisBindTexture(GLuint texture, int unitno)
1348 {
1349     //EnableTexUnit(0,FALSE);
1350     //glBindTexture(GL_TEXTURE_2D, 0);  //Not to bind any texture
1351 }
1352
1353 void OGLRender::EnableTexUnit(int unitno, BOOL flag)
1354 {
1355 #ifdef DEBUGGER
1356     if( unitno != 0 )
1357     {
1358         DebuggerAppendMsg("Check me, in the base ogl render, unitno!=0");
1359     }
1360 #endif
1361     if( m_texUnitEnabled[0] != flag )           //strange, why 0 and not unitno?
1362     {
1363         m_texUnitEnabled[0] = flag;
1364 #if SDL_VIDEO_OPENGL
1365         if( flag == TRUE ) {
1366 //                      pglActiveTexture(GL_TEXTURE0_ARB + unitno);
1367             glEnable(GL_TEXTURE_2D);
1368         } else {
1369 //                      pglActiveTexture(GL_TEXTURE0_ARB + unitno);
1370             glDisable(GL_TEXTURE_2D);
1371                 }
1372 /*              if (m_bMultiTexture)
1373                         pglActiveTexture(GL_TEXTURE1_ARB);*/
1374 #elif SDL_VIDEO_OPENGL_ES2
1375         if(flag)
1376         {
1377             pglActiveTexture(GL_TEXTURE0_ARB + unitno);
1378             OPENGL_CHECK_ERRORS;
1379             glBindTexture(GL_TEXTURE_2D,m_curBoundTex[unitno]);
1380         }
1381         else
1382         {
1383             pglActiveTexture(GL_TEXTURE0_ARB + unitno);
1384             OPENGL_CHECK_ERRORS;
1385             glEnable(GL_BLEND); //Need blend for transparent disabled texture
1386             glBindTexture(GL_TEXTURE_2D,disabledTextureID);
1387         }
1388 #endif
1389         OPENGL_CHECK_ERRORS;
1390     }
1391 }
1392
1393 void OGLRender::TexCoord2f(float u, float v)
1394 {
1395 #ifdef HAVE_GLES
1396         printf("*SEB* TexCoord2f(%f, %f)\n", u, v);
1397 #else
1398     glTexCoord2f(u, v);
1399 #endif
1400 }
1401
1402 void OGLRender::TexCoord(TLITVERTEX &vtxInfo)
1403 {
1404 #ifdef HAVE_GLES
1405         printf("*SEB* TexCoord(%f, %f)\n", vtxInfo.tcord[0].u, vtxInfo.tcord[0].v);
1406 #else
1407     glTexCoord2f(vtxInfo.tcord[0].u, vtxInfo.tcord[0].v);
1408 #endif
1409 }
1410
1411 void OGLRender::UpdateScissor()
1412 {
1413     if( options.bEnableHacks && g_CI.dwWidth == 0x200 && gRDP.scissor.right == 0x200 && g_CI.dwWidth>(*g_GraphicsInfo.VI_WIDTH_REG & 0xFFF) )
1414     {
1415         // Hack for RE2
1416         uint32 width = *g_GraphicsInfo.VI_WIDTH_REG & 0xFFF;
1417         uint32 height = (gRDP.scissor.right*gRDP.scissor.bottom)/width;
1418         glEnable(GL_SCISSOR_TEST);
1419         OPENGL_CHECK_ERRORS;
1420         glScissor(80, int(height*windowSetting.fMultY+windowSetting.statusBarHeightToUse),
1421             int(width*windowSetting.fMultX), int(height*windowSetting.fMultY) );
1422         OPENGL_CHECK_ERRORS;
1423     }
1424     else
1425     {
1426         UpdateScissorWithClipRatio();
1427     }
1428 }
1429
1430 void OGLRender::ApplyRDPScissor(bool force)
1431 {
1432     if( !force && status.curScissor == RDP_SCISSOR )    return;
1433
1434     if( options.bEnableHacks && g_CI.dwWidth == 0x200 && gRDP.scissor.right == 0x200 && g_CI.dwWidth>(*g_GraphicsInfo.VI_WIDTH_REG & 0xFFF) )
1435     {
1436         // Hack for RE2
1437         uint32 width = *g_GraphicsInfo.VI_WIDTH_REG & 0xFFF;
1438         uint32 height = (gRDP.scissor.right*gRDP.scissor.bottom)/width;
1439         glEnable(GL_SCISSOR_TEST);
1440         OPENGL_CHECK_ERRORS;
1441         glScissor(80, int(height*windowSetting.fMultY+windowSetting.statusBarHeightToUse),
1442             int(width*windowSetting.fMultX), int(height*windowSetting.fMultY) );
1443         OPENGL_CHECK_ERRORS;
1444     }
1445     else
1446     {
1447         glScissor(80+int(gRDP.scissor.left*windowSetting.fMultX), int((windowSetting.uViHeight-gRDP.scissor.bottom)*windowSetting.fMultY+windowSetting.statusBarHeightToUse),
1448             int((gRDP.scissor.right-gRDP.scissor.left)*windowSetting.fMultX), int((gRDP.scissor.bottom-gRDP.scissor.top)*windowSetting.fMultY ));
1449         OPENGL_CHECK_ERRORS;
1450     }
1451
1452     status.curScissor = RDP_SCISSOR;
1453 }
1454
1455 void OGLRender::ApplyScissorWithClipRatio(bool force)
1456 {
1457     if( !force && status.curScissor == RSP_SCISSOR )    return;
1458
1459     glEnable(GL_SCISSOR_TEST);
1460     OPENGL_CHECK_ERRORS;
1461     glScissor(80+windowSetting.clipping.left, int((windowSetting.uViHeight-gRSP.real_clip_scissor_bottom)*windowSetting.fMultY)+windowSetting.statusBarHeightToUse,
1462         windowSetting.clipping.width, windowSetting.clipping.height);
1463     OPENGL_CHECK_ERRORS;
1464
1465     status.curScissor = RSP_SCISSOR;
1466 }
1467
1468 void OGLRender::SetFogMinMax(float fMin, float fMax)
1469 {
1470 #if SDL_VIDEO_OPENGL
1471     glFogf(GL_FOG_START, gRSPfFogMin); // Fog Start Depth
1472     OPENGL_CHECK_ERRORS;
1473     glFogf(GL_FOG_END, gRSPfFogMax); // Fog End Depth
1474     OPENGL_CHECK_ERRORS;
1475 #elif SDL_VIDEO_OPENGL_ES2
1476     ((COGL_FragmentProgramCombiner*)m_pColorCombiner)->UpdateFog(gRSP.bFogEnabled);
1477     OPENGL_CHECK_ERRORS;
1478 #endif
1479 }
1480
1481 void OGLRender::TurnFogOnOff(bool flag)
1482 {
1483 #if SDL_VIDEO_OPENGL
1484     if( flag )
1485         glEnable(GL_FOG);
1486     else
1487         glDisable(GL_FOG);
1488     OPENGL_CHECK_ERRORS;
1489 #elif SDL_VIDEO_OPENGL_ES2
1490     ((COGL_FragmentProgramCombiner*)m_pColorCombiner)->UpdateFog(flag);
1491     OPENGL_CHECK_ERRORS;
1492 #endif
1493 }
1494
1495 void OGLRender::SetFogEnable(bool bEnable)
1496 {
1497     DEBUGGER_IF_DUMP( (gRSP.bFogEnabled != (bEnable==TRUE) && logFog ), TRACE1("Set Fog %s", bEnable? "enable":"disable"));
1498
1499     gRSP.bFogEnabled = bEnable&&(options.fogMethod == 1);
1500     
1501     // If force fog
1502     if(options.fogMethod == 2)
1503     {
1504         gRSP.bFogEnabled = true;
1505     }
1506
1507 #if SDL_VIDEO_OPENGL
1508     if( gRSP.bFogEnabled )
1509     {
1510         //TRACE2("Enable fog, min=%f, max=%f",gRSPfFogMin,gRSPfFogMax );
1511         glFogfv(GL_FOG_COLOR, gRDP.fvFogColor); // Set Fog Color
1512         OPENGL_CHECK_ERRORS;
1513         glFogf(GL_FOG_START, gRSPfFogMin); // Fog Start Depth
1514         OPENGL_CHECK_ERRORS;
1515         glFogf(GL_FOG_END, gRSPfFogMax); // Fog End Depth
1516         OPENGL_CHECK_ERRORS;
1517         glEnable(GL_FOG);
1518         OPENGL_CHECK_ERRORS;
1519     }
1520     else
1521     {
1522         glDisable(GL_FOG);
1523         OPENGL_CHECK_ERRORS;
1524     }
1525 #elif SDL_VIDEO_OPENGL_ES2
1526     ((COGL_FragmentProgramCombiner*)m_pColorCombiner)->UpdateFog(gRSP.bFogEnabled);
1527     OPENGL_CHECK_ERRORS;
1528 #endif
1529 }
1530
1531 void OGLRender::SetFogColor(uint32 r, uint32 g, uint32 b, uint32 a)
1532 {
1533     gRDP.fogColor = COLOR_RGBA(r, g, b, a); 
1534     gRDP.fvFogColor[0] = r/255.0f;      //r
1535     gRDP.fvFogColor[1] = g/255.0f;      //g
1536     gRDP.fvFogColor[2] = b/255.0f;      //b
1537     gRDP.fvFogColor[3] = a/255.0f;      //a
1538 #if SDL_VIDEO_OPENGL
1539     glFogfv(GL_FOG_COLOR, gRDP.fvFogColor); // Set Fog Color
1540 #endif
1541     OPENGL_CHECK_ERRORS;
1542 }
1543
1544 void OGLRender::DisableMultiTexture()
1545 {
1546     pglActiveTexture(GL_TEXTURE1_ARB);
1547     OPENGL_CHECK_ERRORS;
1548     EnableTexUnit(1,FALSE);
1549     pglActiveTexture(GL_TEXTURE0_ARB);
1550     OPENGL_CHECK_ERRORS;
1551     EnableTexUnit(0,FALSE);
1552     pglActiveTexture(GL_TEXTURE0_ARB);
1553     OPENGL_CHECK_ERRORS;
1554     EnableTexUnit(0,TRUE);
1555 }
1556
1557 void OGLRender::EndRendering(void)
1558 {
1559 #if SDL_VIDEO_OPENGL
1560     glFlush();
1561     OPENGL_CHECK_ERRORS;
1562 #endif
1563     if( CRender::gRenderReferenceCount > 0 ) 
1564         CRender::gRenderReferenceCount--;
1565 }
1566
1567 void OGLRender::glViewportWrapper(GLint x, GLint y, GLsizei width, GLsizei height, bool flag)
1568 {
1569     static GLint mx=0,my=0;
1570     static GLsizei m_width=0, m_height=0;
1571     static bool mflag=true;
1572         
1573         x+=80;
1574
1575     if( x!=mx || y!=my || width!=m_width || height!=m_height || mflag!=flag)
1576     {
1577         mx=x;
1578         my=y;
1579         m_width=width;
1580         m_height=height;
1581         mflag=flag;
1582         glMatrixMode(GL_PROJECTION);
1583         OPENGL_CHECK_ERRORS;
1584         glLoadIdentity();
1585         OPENGL_CHECK_ERRORS;
1586         if( flag )  glOrtho(0, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight, 0, -1, 1);
1587         OPENGL_CHECK_ERRORS;
1588         glViewport(x,y,width,height);
1589 //printf("glViewport(%i, %i, %i, %i)\n", x, y, width, height);
1590         OPENGL_CHECK_ERRORS;
1591     }
1592 }