Arachnoid GLESv1.1 plugin. Compile and run (a bit glitchy and no frameskip) on the...
[mupen64plus-pandora.git] / source / mupen64plus-video-arachnoid / src / renderer / OpenGL2DRenderer.cpp
1 /******************************************************************************
2  * Arachnoid Graphics Plugin for Mupen64Plus
3  * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/
4  *
5  * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  *****************************************************************************/
21
22 #include "OpenGL2DRenderer.h"
23 #include "VI.h"
24 #include "m64p.h"
25 #include "OpenGL.h"
26
27 //-----------------------------------------------------------------------------
28 //* Initialize
29 //! Saves pointer to video interface
30 //-----------------------------------------------------------------------------
31 bool OpenGL2DRenderer::initialize(VI* vi)
32 {
33     m_vi = vi;
34     return true;
35 }
36
37 //-----------------------------------------------------------------------------
38 //* Render Quad
39 //! Renders a 2D rectangle in HUD.
40 //! @todo Set Viewport
41 //! @todo Reset Viewport
42 //-----------------------------------------------------------------------------
43 void OpenGL2DRenderer::renderQuad( const float color[4], 
44                                    float x0, float y0, 
45                                    float x1, float y1,            
46                                    float depth )
47 {
48     //Get States
49     GLboolean scissor = glIsEnabled(GL_SCISSOR_TEST);
50     GLboolean cull    = glIsEnabled(GL_CULL_FACE);
51
52     //Set States
53     glDisable( GL_SCISSOR_TEST );
54     glDisable( GL_CULL_FACE );
55
56     //Set Othographic Projection Matrix
57     glMatrixMode(GL_PROJECTION);
58     glPushMatrix();
59     glLoadIdentity();
60     glOrtho(0, m_vi->getWidth(), m_vi->getHeight(), 0, 1.0f, -1.0f);
61
62     //TODO Set Viewport
63     //glViewport(0, glheightOffset, glwidth, glheight);
64     //glDepthRange( 0.0f, 1.0f );
65
66     //Render Quad
67     glColor4fv(color);
68 #ifdef HAVE_GLES
69                 GLfloat vtx[] = {
70                         x0, y0, depth,
71                         x1, y0, depth,
72                         x1, y1, depth,
73                         x0, y1, depth
74                 };
75                 
76                 GLboolean glcol = glIsEnabled(GL_COLOR_ARRAY);
77                 if (glcol) glDisableClientState(GL_COLOR_ARRAY);
78                 GLboolean glvtx = glIsEnabled(GL_VERTEX_ARRAY);         
79                 if (!glvtx)
80                         glEnableClientState(GL_VERTEX_ARRAY);
81                 glVertexPointer(3,GL_FLOAT, 0,&vtx);
82                 glActiveTexture( GL_TEXTURE1 );
83                 GLboolean gltex1 = glIsEnabled(GL_TEXTURE_2D);
84                 if (gltex1) glDisable(GL_TEXTURE_2D);
85                 glActiveTexture( GL_TEXTURE0 );
86                 GLboolean gltex = glIsEnabled(GL_TEXTURE_2D);
87                 if (gltex) glDisable(GL_TEXTURE_2D);
88                 // draw
89                 glDrawArrays(GL_TRIANGLE_FAN,0,4);
90                 // restaure
91                 if (glcol) glEnableClientState(GL_COLOR_ARRAY);
92                 glActiveTexture( GL_TEXTURE1 );
93                 if (gltex1) glEnable(GL_TEXTURE_2D);
94                 glActiveTexture( GL_TEXTURE0 );
95                 if (gltex) glEnable(GL_TEXTURE_2D);
96                 if (!glvtx)
97                         glDisableClientState(GL_VERTEX_ARRAY);
98                 else
99                         glVertexPointer(glsav_vtx_size, glsav_vtx_type, glsav_vtx_stride, glsav_vtx_array );
100
101 #else
102     glBegin(GL_QUADS);
103         glVertex3f(x0, y0, depth);
104         glVertex3f(x1, y0, depth);
105         glVertex3f(x1, y1, depth);
106         glVertex3f(x0, y1, depth);
107     glEnd();
108 #endif
109     //Reset Projection Matrix
110     glMatrixMode(GL_PROJECTION);
111     glPopMatrix();
112     glMatrixMode(GL_MODELVIEW);
113
114     //Reset States
115     if ( scissor ) glEnable(GL_SCISSOR_TEST);
116     if ( cull ) glEnable(GL_CULL_FACE);
117     
118     //TODO Reset viewport?    
119 }
120
121
122 //-----------------------------------------------------------------------------
123 //* Render Textured Quad
124 //! Renders a textured 2D rectangle in HUD.
125 //! @todo Set Viewport
126 //! @todo Reset Viewport
127 //-----------------------------------------------------------------------------
128 void OpenGL2DRenderer::renderTexturedQuad( const float color[4], 
129                                            const float secondaryColor[4],
130                                            float x0, float y0,            
131                                            float x1, float y1, 
132                                            float depth,
133                                            float t0s0, float t0t0,  
134                                            float t0s1, float t0t1,
135                                            float t1s0, float t1t0, 
136                                            float t1s1, float t1t1 )
137 {
138     //Get States
139     GLboolean cull = glIsEnabled(GL_CULL_FACE);
140     GLboolean fog  = glIsEnabled(GL_FOG);
141
142     //Set States
143     glDisable(GL_CULL_FACE);
144     glDisable(GL_FOG);
145
146     //Set Orthographic Projection
147     glMatrixMode(GL_PROJECTION);
148     glPushMatrix();
149     glLoadIdentity();
150     glOrtho(0, m_vi->getWidth(), m_vi->getHeight(), 0, 1.0f, -1.0f);
151
152     //TODO Set Viewport
153     //glViewport(0, glheightOffset, glwidth, glheight);
154     //glDepthRange( 0.0f, 1.0f );
155
156     //Set Color
157     glColor4fv( color );
158 #ifdef HAVE_GLES
159                 GLfloat tex[] = {
160                         t0s0, t0t0,
161                         t0s1, t0t0,
162                         t0s1, t0t1,
163                         t0s0, t0t1
164                 };
165                 GLfloat tex1[] = {
166                         t1s0, t1t0,
167                         t1s1, t1t0,
168                         t1s1, t1t1,
169                         t1s0, t1t1
170                 };
171                 GLfloat vtx[] = {
172                         x0, y0, depth,
173                         x1, y0, depth,
174                         x1, y1, depth,
175                         x0, y1, depth
176                 };
177                 
178                 GLboolean glcol = glIsEnabled(GL_COLOR_ARRAY);
179                 if (glcol) glDisableClientState(GL_COLOR_ARRAY);
180                 GLboolean glvtx = glIsEnabled(GL_VERTEX_ARRAY);         
181                 if (!glvtx)
182                         glEnableClientState(GL_VERTEX_ARRAY);
183                 glVertexPointer(3,GL_FLOAT, 0,&vtx);
184 /*              glActiveTexture( GL_TEXTURE1 );
185                 GLboolean gltex1 = glIsEnabled(GL_TEXTURE_2D);
186                 if (gltex1) glDisable(GL_TEXTURE_2D);*/
187                 glClientActiveTexture( GL_TEXTURE1 );
188                 glTexCoordPointer(2, GL_FLOAT, 0, &tex1);
189                 glClientActiveTexture( GL_TEXTURE0 );
190                 glTexCoordPointer(2, GL_FLOAT, 0, &tex);
191                 // draw
192                 glDrawArrays(GL_TRIANGLE_FAN,0,4);
193                 // restaure
194                 if (glcol) glEnableClientState(GL_COLOR_ARRAY);
195 //              if (gltex1) glEnable(GL_TEXTURE_2D);
196                 if (!glvtx)
197                         glDisableClientState(GL_VERTEX_ARRAY);
198                 if (glsav_vtx_type==GL_FLOAT) glVertexPointer(glsav_vtx_size, glsav_vtx_type, glsav_vtx_stride, glsav_vtx_array );
199                 glClientActiveTexture( GL_TEXTURE1 );
200                 if (glsav_tex1_type==GL_FLOAT) glTexCoordPointer( glsav_tex1_size, glsav_tex1_type, glsav_tex1_stride, glsav_tex1_array );
201                 glClientActiveTexture( GL_TEXTURE0 );
202                 if (glsav_tex_type==GL_FLOAT) glTexCoordPointer( glsav_tex_size, glsav_tex_type, glsav_tex_stride, glsav_tex_array );
203 #else
204     //Render Rectangle
205     glBegin(GL_QUADS);
206     {    
207         //Vertex 00
208         glTexCoord2f(t0s0, t0t0);    
209         glVertex3f(x0, y0, depth);   
210
211         //Vertex 10
212         glTexCoord2f(t0s1, t0t0);    
213         glVertex3f(x1, y0, depth);
214
215         //Vertex 11
216         glTexCoord2f(t0s1, t0t1 );   
217         glVertex3f(x1, y1, depth);
218
219         //Vertex 01
220         glTexCoord2f(t0s0, t0t1 );   
221         glVertex3f(x0, y1, depth);
222     }
223     glEnd();
224 #endif
225     //Reset Projection Matrix
226     glMatrixMode(GL_PROJECTION);
227     glPopMatrix();
228     glMatrixMode(GL_MODELVIEW);
229
230     //Reset States
231     if ( cull ) glEnable(GL_CULL_FACE);
232     if ( fog ) glEnable(GL_FOG);
233
234     //TODO Reset viewport?    
235 }
236
237
238 //-----------------------------------------------------------------------------
239 //Render Flipped Textured Quad
240 //! Renders a flipped textured 2D rectangle in HUD.
241 //! @todo Set Viewport
242 //! @todo Reset Viewport
243 //-----------------------------------------------------------------------------
244 void OpenGL2DRenderer::renderFlippedTexturedQuad( const float color[4], 
245                                 const float secondaryColor[4],
246                                 float x0, float y0,            
247                                 float x1, float y1,    
248                                 float depth,
249                                 float t0s0, float t0t0,  
250                                 float t0s1, float t0t1,
251                                 float t1s0, float t1t0, 
252                                 float t1s1, float t1t1 )
253 {
254     //Get States
255     GLboolean cull = glIsEnabled(GL_CULL_FACE);
256     GLboolean fog  = glIsEnabled(GL_FOG);
257
258     //Set States
259     glDisable(GL_CULL_FACE);
260     glDisable(GL_FOG);
261
262     //Set Orthographic Projection
263     glMatrixMode(GL_PROJECTION);
264     glPushMatrix();
265     glLoadIdentity();
266     glOrtho(0, m_vi->getWidth(), m_vi->getHeight(), 0, 1.0f, -1.0f);
267
268     //TODO
269     //glViewport(0, glheightOffset, glwidth, glheight);
270     //glDepthRange( 0.0f, 1.0f );
271
272     //Set Color
273     glColor4fv( color );
274
275 #ifdef HAVE_GLES
276                 GLfloat tex[] = {
277                         t0s0, t0t0,
278                         t0s0, t0t1,
279                         t0s1, t0t1,
280                         t0s1, t0t0
281                 };
282                 GLfloat tex1[] = {
283                         t1s0, t1t0,
284                         t1s0, t1t1,
285                         t1s1, t1t1,
286                         t1s1, t1t0
287                 };
288                 GLfloat vtx[] = {
289                         x0, y0, depth,
290                         x1, y0, depth,
291                         x1, y1, depth,
292                         x0, y1, depth
293                 };
294                 
295                 GLboolean glcol = glIsEnabled(GL_COLOR_ARRAY);
296                 if (glcol) glDisableClientState(GL_COLOR_ARRAY);
297                 GLboolean glvtx = glIsEnabled(GL_VERTEX_ARRAY);         
298                 if (!glvtx)
299                         glEnableClientState(GL_VERTEX_ARRAY);
300                 glVertexPointer(3,GL_FLOAT, 0,&vtx);
301 /*              glActiveTexture( GL_TEXTURE1 );
302                 GLboolean gltex1 = glIsEnabled(GL_TEXTURE_2D);
303                 if (gltex1) glDisable(GL_TEXTURE_2D);*/
304                 glClientActiveTexture( GL_TEXTURE1 );
305                 glTexCoordPointer(2, GL_FLOAT, 0, &tex1);
306                 glClientActiveTexture( GL_TEXTURE0 );
307                 glTexCoordPointer(2, GL_FLOAT, 0, &tex);
308                 // draw
309                 glDrawArrays(GL_TRIANGLE_FAN,0,4);
310                 // restaure
311                 if (glcol) glEnableClientState(GL_COLOR_ARRAY);
312 //              if (gltex1) glEnable(GL_TEXTURE_2D);
313                 glVertexPointer(glsav_vtx_size, glsav_vtx_type, glsav_vtx_stride, glsav_vtx_array );
314                 glClientActiveTexture( GL_TEXTURE1 );
315                 glTexCoordPointer( glsav_tex1_size, glsav_tex1_type, glsav_tex1_stride, glsav_tex1_array );
316                 glClientActiveTexture( GL_TEXTURE0 );
317                 glTexCoordPointer( glsav_tex_size, glsav_tex_type, glsav_tex_stride, glsav_tex_array );
318 #else    //Render Rectangle
319     glBegin(GL_QUADS);
320     {    
321         //Vertex 00
322         glTexCoord2f(t0s0, t0t0);        //00
323         glVertex3f(x0, y0, depth);   
324
325         //Vertex 10
326         glTexCoord2f(t0s0, t0t1);        //01 !
327         glVertex3f(x1, y0, depth);
328
329         //Vertex 11
330         glTexCoord2f(t0s1, t0t1);        //11
331         glVertex3f(x1, y1, depth);
332
333         //Vertex 01
334         glTexCoord2f(t0s1, t0t0);        //10  !
335         glVertex3f(x0, y1, depth);
336     }
337     glEnd();
338 #endif
339     //Reset Projection Matrix
340     glMatrixMode(GL_PROJECTION);
341     glPopMatrix();
342     glMatrixMode(GL_MODELVIEW);
343
344     //Reset States
345     if ( cull ) glEnable(GL_CULL_FACE);
346     if ( fog ) glEnable(GL_FOG);
347
348     //TODO Reset viewport?    
349 }
350