1 /******************************************************************************
2 * Arachnoid Graphics Plugin for Mupen64Plus
3 * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/
5 * Copyright (C) 2009 Jon Ring
6 * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 *****************************************************************************/
23 #include "OpenGLManager.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
28 OpenGLManager::OpenGLManager()
30 m_forceDisableCulling = false;
33 //-----------------------------------------------------------------------------
35 //! Initializes OpenGL.
37 //! @param fullscreen will render scene in fullscreen if true
38 //! @param width width of window or width of screen resolution
39 //! @param height height of window or height of screen resolution
40 //! @param bitDepth bitDepth to use
41 //! @param refreshRate refresh frequency to use
42 //! @param vSync limits frame rate to the monitor's refresh frequency
43 //! @param hideCursor hides mouse coursor if true
44 //-----------------------------------------------------------------------------
45 bool OpenGLManager::initialize(bool fullscreen, int width, int height, int bitDepth, int refreshRate, bool vSync, bool hideCursor)
49 m_bitDepth = bitDepth;
50 m_refreshRate = refreshRate;
51 m_fullscreen = fullscreen;
52 m_renderingCallback = NULL;
54 setClearColor(0.0f, 0.0f, 0.0f);
55 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
56 glEnable(GL_DEPTH_TEST);
57 glEnable(GL_CULL_FACE);
60 setCullMode(false, true);
61 setTextureing2D(false);
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
70 void OpenGLManager::setViewport( int x, int y, int width, int height, float zNear, float zFar )
72 glViewport(x, y, width, height);
74 //glViewport( gSP.viewport.x * OGL.scaleX,
75 // (VI.height - (gSP.viewport.y + gSP.viewport.height)) * OGL.scaleY + OGL.heightOffset,
76 // gSP.viewport.width * OGL.scaleX,
77 // gSP.viewport.height * OGL.scaleY
80 //glDepthRange( 0.0f, 1.0f );//gSP.viewport.nearz, gSP.viewport.farz );
81 glDepthRange( zNear, zFar );
84 //-----------------------------------------------------------------------------
86 //! glScissor defines a rectangle, called the scissor box, in window coordinates.
87 //! Only pixels inside the box are allowed to be modified.
88 //! glScissor(0,0,1,1) allows modification of only the lower left pixel in the window
89 //! glScissor(0,0,0,0) doesn't allow modification of any pixels in the window.
91 //! @param x,y Specify the lower left corner of the box. Defualt (0, 0).
92 //! @param width,height Specify the width and height of the box.
93 //-----------------------------------------------------------------------------
94 void OpenGLManager::setScissor(int x, int y, int width, int height)
96 glScissor(x,y, width, height);
100 //-----------------------------------------------------------------------------
102 //-----------------------------------------------------------------------------
103 void OpenGLManager::resize(int width, int height, int bitDepth, int refreshRate)
107 initialize(m_fullscreen, width, height, bitDepth, refreshRate, true, false);
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
114 bool OpenGLManager::toggleFullscreen()
118 return initialize(!m_fullscreen, m_width, m_height, m_bitDepth, m_refreshRate, true, !m_fullscreen);
123 //-----------------------------------------------------------------------------
125 //! Should be called before you render everything with OpenGL
126 //-----------------------------------------------------------------------------
127 void OpenGLManager::beginRendering()
130 //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
133 //-----------------------------------------------------------------------------
135 //! Should be called after you have rendered everything with OpenGL
136 //-----------------------------------------------------------------------------
137 void OpenGLManager::endRendering()
140 if (m_renderingCallback)
141 m_renderingCallback(m_drawFlag);
147 CoreVideo_GL_SwapBuffers();
153 //-----------------------------------------------------------------------------
155 //-----------------------------------------------------------------------------
156 void OpenGLManager::setWireFrame(bool wireframe)
158 m_wireframe = wireframe;
162 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
166 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
171 //-----------------------------------------------------------------------------
172 // Set ZBuffer Enabled
173 //-----------------------------------------------------------------------------
174 void OpenGLManager::setZBufferEnabled(bool enable)
178 glEnable( GL_DEPTH_TEST );
182 glDisable( GL_DEPTH_TEST );
186 //-----------------------------------------------------------------------------
187 // Get ZBuffer Enabled
188 //-----------------------------------------------------------------------------
189 bool OpenGLManager::getZBufferEnabled()
191 return (glIsEnabled(GL_DEPTH_TEST) == GL_TRUE);
194 //-----------------------------------------------------------------------------
196 //-----------------------------------------------------------------------------
197 void OpenGLManager::setLighting(bool lighting)
199 m_lighting = lighting;
201 //glEnable(GL_LIGHTING); We dont use this type of lighting (Nintendo 64 specific)
204 glDisable(GL_LIGHTING);
208 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
211 bool OpenGLManager::getLightingEnabled()
214 //return (glIsEnabled(GL_LIGHTING) == GL_TRUE); We dont use this type of lighting (Nintendo 64 specific)
217 //-----------------------------------------------------------------------------
219 //-----------------------------------------------------------------------------
220 void OpenGLManager::setFogEnabled(bool fog)
228 //-----------------------------------------------------------------------------
230 //-----------------------------------------------------------------------------
231 bool OpenGLManager::getFogEnabled()
233 return (glIsEnabled(GL_FOG) == GL_TRUE);
236 //-----------------------------------------------------------------------------
238 //-----------------------------------------------------------------------------
239 void OpenGLManager::setTextureing2D(bool textureing)
242 glEnable(GL_TEXTURE_2D);
244 glDisable(GL_TEXTURE_2D);
247 //-----------------------------------------------------------------------------
249 //-----------------------------------------------------------------------------
250 bool getTextureing2DEnabled()
252 return (glIsEnabled(GL_TEXTURE_2D) == GL_TRUE);
255 //-----------------------------------------------------------------------------
256 // Set Alpha Test Enabled
257 //-----------------------------------------------------------------------------
258 void OpenGLManager::setAlphaTest(bool alphaTestEnable)
260 if ( alphaTestEnable )
261 glEnable(GL_ALPHA_TEST);
263 glDisable(GL_ALPHA_TEST);
266 //-----------------------------------------------------------------------------
267 // Get Alpha Test Enabled
268 //-----------------------------------------------------------------------------
269 bool getAlphaTestEnabled()
271 return (glIsEnabled(GL_ALPHA_TEST) == GL_TRUE);
274 //-----------------------------------------------------------------------------
276 //-----------------------------------------------------------------------------
277 void OpenGLManager::setScissorEnabled(bool enable)
280 glEnable(GL_SCISSOR_TEST);
282 glDisable(GL_SCISSOR_TEST);
285 bool OpenGLManager::getScissorEnabled()
287 return (glIsEnabled(GL_SCISSOR_TEST) == GL_TRUE);
290 //-----------------------------------------------------------------------------
292 //-----------------------------------------------------------------------------
293 void OpenGLManager::setCullMode(bool cullFront, bool cullBack)
295 if( cullFront && cullBack )
297 glEnable(GL_CULL_FACE);
298 glCullFace(GL_FRONT_AND_BACK);
302 glEnable(GL_CULL_FACE);
303 glCullFace(GL_FRONT);
307 glEnable(GL_CULL_FACE);
312 glDisable(GL_CULL_FACE);
315 //Override Face Culling?
316 if ( m_forceDisableCulling )
318 glDisable(GL_CULL_FACE);
322 //-----------------------------------------------------------------------------
324 //! Restores old display settings and destroys the rendering context
325 //-----------------------------------------------------------------------------
326 void OpenGLManager::dispose()
330 //-----------------------------------------------------------------------------
332 //-----------------------------------------------------------------------------
333 OpenGLManager::~OpenGLManager()
338 //-----------------------------------------------------------------------------
339 //! 2D coordinats are in proportion to N64 viewport (vi), but we use
340 //! a viewport of another size, there for we need to scale the coordinats.
341 //! This function calculates that scale.
342 //! @param viWidth The videointerface width that defines the n64 resolution
343 //! @param viHeight The videointerface height that defines the n64 resolution
344 //-----------------------------------------------------------------------------
345 void OpenGLManager::calcViewScale(int viWidth, int viHeight)
347 m_scaleX = m_width / (float)viWidth;
348 m_scaleY = m_height / (float)viHeight;