ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

gfx_opengl.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #if defined(WIN32)
00024 #include <windows.h>
00025 // winnt.h defines ARRAYSIZE, but we want our own one...
00026 #undef ARRAYSIZE
00027 #endif
00028 
00029 #include "common/rect.h"
00030 #include "common/textconsole.h"
00031 
00032 #if defined(USE_OPENGL) && !defined(USE_GLES2)
00033 
00034 #include "graphics/colormasks.h"
00035 #include "graphics/opengl/context.h"
00036 #include "graphics/surface.h"
00037 
00038 #include "engines/myst3/gfx.h"
00039 #include "engines/myst3/gfx_opengl.h"
00040 #include "engines/myst3/gfx_opengl_texture.h"
00041 
00042 namespace Myst3 {
00043 
00044 Renderer *CreateGfxOpenGL(OSystem *system) {
00045     return new OpenGLRenderer(system);
00046 }
00047 
00048 OpenGLRenderer::OpenGLRenderer(OSystem *system) :
00049         Renderer(system) {
00050 }
00051 
00052 OpenGLRenderer::~OpenGLRenderer() {
00053 }
00054 
00055 Texture *OpenGLRenderer::createTexture(const Graphics::Surface *surface) {
00056     return new OpenGLTexture(surface);
00057 }
00058 
00059 void OpenGLRenderer::freeTexture(Texture *texture) {
00060     OpenGLTexture *glTexture = static_cast<OpenGLTexture *>(texture);
00061     delete glTexture;
00062 }
00063 
00064 void OpenGLRenderer::init() {
00065     debug("Initializing OpenGL Renderer");
00066 
00067     computeScreenViewport();
00068 
00069     // Check the available OpenGL extensions
00070     if (!OpenGLContext.NPOTSupported) {
00071         warning("GL_ARB_texture_non_power_of_two is not available.");
00072     }
00073 
00074     glMatrixMode(GL_PROJECTION);
00075     glLoadIdentity();
00076 
00077     glMatrixMode(GL_MODELVIEW);
00078     glLoadIdentity();
00079 
00080     glDisable(GL_LIGHTING);
00081     glEnable(GL_TEXTURE_2D);
00082     glEnable(GL_DEPTH_TEST);
00083 }
00084 
00085 void OpenGLRenderer::clear() {
00086     glClearColor(0.f, 0.f, 0.f, 1.f); // Solid black
00087     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00088     glColor3f(1.0f, 1.0f, 1.0f);
00089 }
00090 
00091 void OpenGLRenderer::selectTargetWindow(Window *window, bool is3D, bool scaled) {
00092     if (!window) {
00093         // No window found ...
00094         if (scaled) {
00095             // ... in scaled mode draw in the original game screen area
00096             Common::Rect vp = viewport();
00097             glViewport(vp.left, _system->getHeight() - vp.top - vp.height(), vp.width(), vp.height());
00098         } else {
00099             // ... otherwise, draw on the whole screen
00100             glViewport(0, 0, _system->getWidth(), _system->getHeight());
00101         }
00102     } else {
00103         // Found a window, draw inside it
00104         Common::Rect vp = window->getPosition();
00105         glViewport(vp.left, _system->getHeight() - vp.top - vp.height(), vp.width(), vp.height());
00106     }
00107 
00108     if (is3D) {
00109         glMatrixMode(GL_PROJECTION);
00110         glLoadMatrixf(_projectionMatrix.getData());
00111 
00112         glMatrixMode(GL_MODELVIEW);
00113         glLoadMatrixf(_modelViewMatrix.getData());
00114     } else {
00115         glMatrixMode(GL_PROJECTION);
00116         glLoadIdentity();
00117 
00118         if (!window) {
00119             if (scaled) {
00120                 glOrtho(0.0, kOriginalWidth, kOriginalHeight, 0.0, -1.0, 1.0);
00121             } else {
00122                 glOrtho(0.0, _system->getWidth(), _system->getHeight(), 0.0, -1.0, 1.0);
00123             }
00124         } else {
00125             if (scaled) {
00126                 Common::Rect originalRect = window->getOriginalPosition();
00127                 glOrtho(0.0, originalRect.width(), originalRect.height(), 0.0, -1.0, 1.0);
00128             } else {
00129                 Common::Rect vp = window->getPosition();
00130                 glOrtho(0.0, vp.width(), vp.height(), 0.0, -1.0, 1.0);
00131             }
00132         }
00133 
00134         glMatrixMode(GL_MODELVIEW);
00135         glLoadIdentity();
00136     }
00137 }
00138 
00139 void OpenGLRenderer::drawRect2D(const Common::Rect &rect, uint32 color) {
00140     uint8 a, r, g, b;
00141     Graphics::colorToARGB< Graphics::ColorMasks<8888> >(color, a, r, g, b);
00142 
00143     glDisable(GL_TEXTURE_2D);
00144     glColor4f(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
00145 
00146     if (a != 255) {
00147         glEnable(GL_BLEND);
00148         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00149     }
00150 
00151     glBegin(GL_TRIANGLE_STRIP);
00152         glVertex3f(rect.left, rect.bottom, 0.0f);
00153         glVertex3f(rect.right, rect.bottom, 0.0f);
00154         glVertex3f(rect.left, rect.top, 0.0f);
00155         glVertex3f(rect.right, rect.top, 0.0f);
00156     glEnd();
00157 
00158     glDisable(GL_BLEND);
00159 }
00160 
00161 void OpenGLRenderer::drawTexturedRect2D(const Common::Rect &screenRect, const Common::Rect &textureRect,
00162         Texture *texture, float transparency, bool additiveBlending) {
00163 
00164     OpenGLTexture *glTexture = static_cast<OpenGLTexture *>(texture);
00165 
00166     const float tLeft = textureRect.left / (float)glTexture->internalWidth;
00167     const float tWidth = textureRect.width() / (float)glTexture->internalWidth;
00168     const float tTop = textureRect.top / (float)glTexture->internalHeight;
00169     const float tHeight = textureRect.height() / (float)glTexture->internalHeight;
00170 
00171     float sLeft = screenRect.left;
00172     float sTop = screenRect.top;
00173     float sRight = sLeft + screenRect.width();
00174     float sBottom = sTop + screenRect.height();
00175 
00176     if (glTexture->upsideDown) {
00177         SWAP(sTop, sBottom);
00178     }
00179 
00180     if (transparency >= 0.0) {
00181         if (additiveBlending) {
00182             glBlendFunc(GL_SRC_ALPHA, GL_ONE);
00183         } else {
00184             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00185         }
00186 
00187         glEnable(GL_BLEND);
00188     } else {
00189         transparency = 1.0;
00190     }
00191 
00192     glEnable(GL_TEXTURE_2D);
00193     glColor4f(1.0f, 1.0f, 1.0f, transparency);
00194     glDepthMask(GL_FALSE);
00195 
00196     glBindTexture(GL_TEXTURE_2D, glTexture->id);
00197     glBegin(GL_TRIANGLE_STRIP);
00198         glTexCoord2f(tLeft, tTop + tHeight);
00199         glVertex3f(sLeft + 0, sBottom, 1.0f);
00200 
00201         glTexCoord2f(tLeft + tWidth, tTop + tHeight);
00202         glVertex3f(sRight, sBottom, 1.0f);
00203 
00204         glTexCoord2f(tLeft, tTop);
00205         glVertex3f(sLeft + 0, sTop + 0, 1.0f);
00206 
00207         glTexCoord2f(tLeft + tWidth, tTop);
00208         glVertex3f(sRight, sTop + 0, 1.0f);
00209     glEnd();
00210 
00211     glDisable(GL_BLEND);
00212     glDepthMask(GL_TRUE);
00213 }
00214 
00215 void OpenGLRenderer::draw2DText(const Common::String &text, const Common::Point &position) {
00216     OpenGLTexture *glFont = static_cast<OpenGLTexture *>(_font);
00217 
00218     // The font only has uppercase letters
00219     Common::String textToDraw = text;
00220     textToDraw.toUppercase();
00221 
00222     glEnable(GL_BLEND);
00223     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00224 
00225     glEnable(GL_TEXTURE_2D);
00226     glDepthMask(GL_FALSE);
00227 
00228     glColor3f(1.0f, 1.0f, 1.0f);
00229     glBindTexture(GL_TEXTURE_2D, glFont->id);
00230 
00231     int x = position.x;
00232     int y = position.y;
00233 
00234     for (uint i = 0; i < textToDraw.size(); i++) {
00235         Common::Rect textureRect = getFontCharacterRect(textToDraw[i]);
00236         int w = textureRect.width();
00237         int h = textureRect.height();
00238 
00239         float cw = textureRect.width() / (float)glFont->internalWidth;
00240         float ch = textureRect.height() / (float)glFont->internalHeight;
00241         float cx = textureRect.left / (float)glFont->internalWidth;
00242         float cy = textureRect.top / (float)glFont->internalHeight;
00243 
00244         glBegin(GL_QUADS);
00245         glTexCoord2f(cx, cy + ch);
00246         glVertex3f(x, y, 1.0f);
00247         glTexCoord2f(cx + cw, cy + ch);
00248         glVertex3f(x + w, y, 1.0f);
00249         glTexCoord2f(cx + cw, cy);
00250         glVertex3f(x + w, y + h, 1.0f);
00251         glTexCoord2f(cx, cy);
00252         glVertex3f(x, y + h, 1.0f);
00253         glEnd();
00254 
00255         x += textureRect.width() - 3;
00256     }
00257 
00258     glDisable(GL_TEXTURE_2D);
00259     glDisable(GL_BLEND);
00260     glDepthMask(GL_TRUE);
00261 }
00262 
00263 void OpenGLRenderer::drawFace(uint face, Texture *texture) {
00264     OpenGLTexture *glTexture = static_cast<OpenGLTexture *>(texture);
00265 
00266     // Used fragment of the texture
00267     const float w = glTexture->width  / (float) glTexture->internalWidth;
00268     const float h = glTexture->height / (float) glTexture->internalHeight;
00269 
00270     glBindTexture(GL_TEXTURE_2D, glTexture->id);
00271     glBegin(GL_TRIANGLE_STRIP);
00272     for (uint i = 0; i < 4; i++) {
00273         glTexCoord2f(w * cubeVertices[5 * (4 * face + i) + 0], h * cubeVertices[5 * (4 * face + i) + 1]);
00274         glVertex3f(cubeVertices[5 * (4 * face + i) + 2], cubeVertices[5 * (4 * face + i) + 3], cubeVertices[5 * (4 * face + i) + 4]);
00275     }
00276     glEnd();
00277 }
00278 
00279 void OpenGLRenderer::drawCube(Texture **textures) {
00280     glEnable(GL_TEXTURE_2D);
00281     glDepthMask(GL_FALSE);
00282 
00283     for (uint i = 0; i < 6; i++) {
00284         drawFace(i, textures[i]);
00285     }
00286 
00287     glDepthMask(GL_TRUE);
00288 }
00289 
00290 void OpenGLRenderer::drawTexturedRect3D(const Math::Vector3d &topLeft, const Math::Vector3d &bottomLeft,
00291         const Math::Vector3d &topRight, const Math::Vector3d &bottomRight, Texture *texture) {
00292 
00293     OpenGLTexture *glTexture = static_cast<OpenGLTexture *>(texture);
00294 
00295     const float w = glTexture->width / (float)glTexture->internalWidth;
00296     const float h = glTexture->height / (float)glTexture->internalHeight;
00297 
00298     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00299     glEnable(GL_BLEND);
00300     glDepthMask(GL_FALSE);
00301 
00302     glBindTexture(GL_TEXTURE_2D, glTexture->id);
00303 
00304     glBegin(GL_TRIANGLE_STRIP);
00305         glTexCoord2f(0, 0);
00306         glVertex3f(-topLeft.x(), topLeft.y(), topLeft.z());
00307 
00308         glTexCoord2f(0, h);
00309         glVertex3f(-bottomLeft.x(), bottomLeft.y(), bottomLeft.z());
00310 
00311         glTexCoord2f(w, 0);
00312         glVertex3f(-topRight.x(), topRight.y(), topRight.z());
00313 
00314         glTexCoord2f(w, h);
00315         glVertex3f(-bottomRight.x(), bottomRight.y(), bottomRight.z());
00316     glEnd();
00317 
00318     glDisable(GL_BLEND);
00319     glDepthMask(GL_TRUE);
00320 }
00321 
00322 Graphics::Surface *OpenGLRenderer::getScreenshot() {
00323     Common::Rect screen = viewport();
00324 
00325     Graphics::Surface *s = new Graphics::Surface();
00326     s->create(screen.width(), screen.height(), Texture::getRGBAPixelFormat());
00327 
00328     glReadPixels(screen.left, screen.top, screen.width(), screen.height(), GL_RGBA, GL_UNSIGNED_BYTE, s->getPixels());
00329 
00330     flipVertical(s);
00331 
00332     return s;
00333 }
00334 
00335 Texture *OpenGLRenderer::copyScreenshotToTexture() {
00336     OpenGLTexture *texture = new OpenGLTexture();
00337 
00338     Common::Rect screen = viewport();
00339     texture->copyFromFramebuffer(screen);
00340 
00341     return texture;
00342 }
00343 
00344 } // End of namespace Myst3
00345 
00346 #endif


Generated on Sat Dec 14 2019 05:00:37 for ResidualVM by doxygen 1.7.1
curved edge   curved edge