| 1 | /****************************************************************************** |
| 2 | * Arachnoid Graphics Plugin for Mupen64Plus |
| 3 | * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/ |
| 4 | * |
| 5 | * Copyright (C) 2009 Jon Ring |
| 6 | * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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 | *****************************************************************************/ |
| 22 | |
| 23 | #include "OpenGLManager.h" |
| 24 | |
| 25 | //----------------------------------------------------------------------------- |
| 26 | //! Constructor |
| 27 | //----------------------------------------------------------------------------- |
| 28 | OpenGLManager::OpenGLManager() |
| 29 | { |
| 30 | m_forceDisableCulling = false; |
| 31 | } |
| 32 | |
| 33 | //----------------------------------------------------------------------------- |
| 34 | //* Initialize |
| 35 | //! Initializes OpenGL. |
| 36 | // |
| 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) |
| 46 | { |
| 47 | m_width = width; |
| 48 | m_height = height; |
| 49 | m_bitDepth = bitDepth; |
| 50 | m_refreshRate = refreshRate; |
| 51 | m_fullscreen = fullscreen; |
| 52 | m_renderingCallback = NULL; |
| 53 | //Set OpenGL Settings |
| 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); |
| 58 | |
| 59 | //Set render states |
| 60 | setCullMode(false, true); |
| 61 | setTextureing2D(false); |
| 62 | setLighting(false); |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | //----------------------------------------------------------------------------- |
| 68 | // Set Viewport |
| 69 | //----------------------------------------------------------------------------- |
| 70 | void OpenGLManager::setViewport( int x, int y, int width, int height, float zNear, float zFar ) |
| 71 | { |
| 72 | glViewport(x, y, width, height); |
| 73 | |
| 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 |
| 78 | // ); |
| 79 | |
| 80 | //glDepthRange( 0.0f, 1.0f );//gSP.viewport.nearz, gSP.viewport.farz ); |
| 81 | glDepthRange( zNear, zFar ); |
| 82 | } |
| 83 | |
| 84 | //----------------------------------------------------------------------------- |
| 85 | //* Set Scissor |
| 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. |
| 90 | //! |
| 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) |
| 95 | { |
| 96 | glScissor(x,y, width, height); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | //----------------------------------------------------------------------------- |
| 101 | // Resize |
| 102 | //----------------------------------------------------------------------------- |
| 103 | void OpenGLManager::resize(int width, int height, int bitDepth, int refreshRate) |
| 104 | { |
| 105 | #if 0 |
| 106 | dispose(); |
| 107 | initialize(m_fullscreen, width, height, bitDepth, refreshRate, true, false); |
| 108 | #endif |
| 109 | } |
| 110 | |
| 111 | //----------------------------------------------------------------------------- |
| 112 | // Toggle Fullscreen |
| 113 | //----------------------------------------------------------------------------- |
| 114 | bool OpenGLManager::toggleFullscreen() |
| 115 | { |
| 116 | #if 0 |
| 117 | dispose(); |
| 118 | return initialize(!m_fullscreen, m_width, m_height, m_bitDepth, m_refreshRate, true, !m_fullscreen); |
| 119 | #endif |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | //----------------------------------------------------------------------------- |
| 124 | //* Start Rendering |
| 125 | //! Should be called before you render everything with OpenGL |
| 126 | //----------------------------------------------------------------------------- |
| 127 | void OpenGLManager::beginRendering() |
| 128 | { |
| 129 | glDepthMask( true ); |
| 130 | //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
| 131 | } |
| 132 | |
| 133 | //----------------------------------------------------------------------------- |
| 134 | //* End Rendering |
| 135 | //! Should be called after you have rendered everything with OpenGL |
| 136 | //----------------------------------------------------------------------------- |
| 137 | void OpenGLManager::endRendering() |
| 138 | { |
| 139 | glFinish(); |
| 140 | if (m_renderingCallback) |
| 141 | m_renderingCallback(m_drawFlag); |
| 142 | if (m_drawFlag) { |
| 143 | m_drawFlag = 0; |
| 144 | #ifdef HAVE_GLES |
| 145 | EGL_SwapBuffers(); |
| 146 | #else |
| 147 | CoreVideo_GL_SwapBuffers(); |
| 148 | #endif |
| 149 | } |
| 150 | //glFlush(); |
| 151 | } |
| 152 | |
| 153 | //----------------------------------------------------------------------------- |
| 154 | // Set Wireframe |
| 155 | //----------------------------------------------------------------------------- |
| 156 | void OpenGLManager::setWireFrame(bool wireframe) |
| 157 | { |
| 158 | m_wireframe = wireframe; |
| 159 | #ifndef HAVE_GLES |
| 160 | if ( wireframe ) |
| 161 | { |
| 162 | glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); |
| 167 | } |
| 168 | #endif |
| 169 | } |
| 170 | |
| 171 | //----------------------------------------------------------------------------- |
| 172 | // Set ZBuffer Enabled |
| 173 | //----------------------------------------------------------------------------- |
| 174 | void OpenGLManager::setZBufferEnabled(bool enable) |
| 175 | { |
| 176 | if ( enable ) |
| 177 | { |
| 178 | glEnable( GL_DEPTH_TEST ); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | glDisable( GL_DEPTH_TEST ); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | //----------------------------------------------------------------------------- |
| 187 | // Get ZBuffer Enabled |
| 188 | //----------------------------------------------------------------------------- |
| 189 | bool OpenGLManager::getZBufferEnabled() |
| 190 | { |
| 191 | return (glIsEnabled(GL_DEPTH_TEST) == GL_TRUE); |
| 192 | } |
| 193 | |
| 194 | //----------------------------------------------------------------------------- |
| 195 | // Set Lighting |
| 196 | //----------------------------------------------------------------------------- |
| 197 | void OpenGLManager::setLighting(bool lighting) |
| 198 | { |
| 199 | m_lighting = lighting; |
| 200 | if ( lighting ) { |
| 201 | //glEnable(GL_LIGHTING); We dont use this type of lighting (Nintendo 64 specific) |
| 202 | } |
| 203 | else { |
| 204 | glDisable(GL_LIGHTING); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | //----------------------------------------------------------------------------- |
| 209 | // Get Lighting |
| 210 | //----------------------------------------------------------------------------- |
| 211 | bool OpenGLManager::getLightingEnabled() |
| 212 | { |
| 213 | return m_lighting; |
| 214 | //return (glIsEnabled(GL_LIGHTING) == GL_TRUE); We dont use this type of lighting (Nintendo 64 specific) |
| 215 | } |
| 216 | |
| 217 | //----------------------------------------------------------------------------- |
| 218 | // Set Fog |
| 219 | //----------------------------------------------------------------------------- |
| 220 | void OpenGLManager::setFogEnabled(bool fog) |
| 221 | { |
| 222 | if ( fog ) |
| 223 | glEnable(GL_FOG); |
| 224 | else |
| 225 | glDisable(GL_FOG); |
| 226 | } |
| 227 | |
| 228 | //----------------------------------------------------------------------------- |
| 229 | // Get Fog |
| 230 | //----------------------------------------------------------------------------- |
| 231 | bool OpenGLManager::getFogEnabled() |
| 232 | { |
| 233 | return (glIsEnabled(GL_FOG) == GL_TRUE); |
| 234 | } |
| 235 | |
| 236 | //----------------------------------------------------------------------------- |
| 237 | // Set Texturing |
| 238 | //----------------------------------------------------------------------------- |
| 239 | void OpenGLManager::setTextureing2D(bool textureing) |
| 240 | { |
| 241 | if ( textureing ) |
| 242 | glEnable(GL_TEXTURE_2D); |
| 243 | else |
| 244 | glDisable(GL_TEXTURE_2D); |
| 245 | } |
| 246 | |
| 247 | //----------------------------------------------------------------------------- |
| 248 | // Get Texturing |
| 249 | //----------------------------------------------------------------------------- |
| 250 | bool getTextureing2DEnabled() |
| 251 | { |
| 252 | return (glIsEnabled(GL_TEXTURE_2D) == GL_TRUE); |
| 253 | } |
| 254 | |
| 255 | //----------------------------------------------------------------------------- |
| 256 | // Set Alpha Test Enabled |
| 257 | //----------------------------------------------------------------------------- |
| 258 | void OpenGLManager::setAlphaTest(bool alphaTestEnable) |
| 259 | { |
| 260 | if ( alphaTestEnable ) |
| 261 | glEnable(GL_ALPHA_TEST); |
| 262 | else |
| 263 | glDisable(GL_ALPHA_TEST); |
| 264 | } |
| 265 | |
| 266 | //----------------------------------------------------------------------------- |
| 267 | // Get Alpha Test Enabled |
| 268 | //----------------------------------------------------------------------------- |
| 269 | bool getAlphaTestEnabled() |
| 270 | { |
| 271 | return (glIsEnabled(GL_ALPHA_TEST) == GL_TRUE); |
| 272 | } |
| 273 | |
| 274 | //----------------------------------------------------------------------------- |
| 275 | // Scissor |
| 276 | //----------------------------------------------------------------------------- |
| 277 | void OpenGLManager::setScissorEnabled(bool enable) |
| 278 | { |
| 279 | if ( enable ) |
| 280 | glEnable(GL_SCISSOR_TEST); |
| 281 | else |
| 282 | glDisable(GL_SCISSOR_TEST); |
| 283 | } |
| 284 | |
| 285 | bool OpenGLManager::getScissorEnabled() |
| 286 | { |
| 287 | return (glIsEnabled(GL_SCISSOR_TEST) == GL_TRUE); |
| 288 | } |
| 289 | |
| 290 | //----------------------------------------------------------------------------- |
| 291 | // Set Cull Mode |
| 292 | //----------------------------------------------------------------------------- |
| 293 | void OpenGLManager::setCullMode(bool cullFront, bool cullBack) |
| 294 | { |
| 295 | if( cullFront && cullBack ) |
| 296 | { |
| 297 | glEnable(GL_CULL_FACE); |
| 298 | glCullFace(GL_FRONT_AND_BACK); |
| 299 | } |
| 300 | else if( cullFront ) |
| 301 | { |
| 302 | glEnable(GL_CULL_FACE); |
| 303 | glCullFace(GL_FRONT); |
| 304 | } |
| 305 | else if( cullBack ) |
| 306 | { |
| 307 | glEnable(GL_CULL_FACE); |
| 308 | glCullFace(GL_BACK); |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | glDisable(GL_CULL_FACE); |
| 313 | } |
| 314 | |
| 315 | //Override Face Culling? |
| 316 | if ( m_forceDisableCulling ) |
| 317 | { |
| 318 | glDisable(GL_CULL_FACE); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | //----------------------------------------------------------------------------- |
| 323 | //* Dispose |
| 324 | //! Restores old display settings and destroys the rendering context |
| 325 | //----------------------------------------------------------------------------- |
| 326 | void OpenGLManager::dispose() |
| 327 | { |
| 328 | } |
| 329 | |
| 330 | //----------------------------------------------------------------------------- |
| 331 | //! Destructor |
| 332 | //----------------------------------------------------------------------------- |
| 333 | OpenGLManager::~OpenGLManager() |
| 334 | { |
| 335 | dispose(); |
| 336 | } |
| 337 | |
| 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) |
| 346 | { |
| 347 | m_scaleX = m_width / (float)viWidth; |
| 348 | m_scaleY = m_height / (float)viHeight; |
| 349 | } |