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

myst3/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     const float sLeft = screenRect.left;
00172     const float sTop = screenRect.top;
00173     const float sWidth = screenRect.width();
00174     const float sHeight = screenRect.height();
00175 
00176     if (transparency >= 0.0) {
00177         if (additiveBlending) {
00178             glBlendFunc(GL_SRC_ALPHA, GL_ONE);
00179         } else {
00180             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00181         }
00182 
00183         glEnable(GL_BLEND);
00184     } else {
00185         transparency = 1.0;
00186     }
00187 
00188     glEnable(GL_TEXTURE_2D);
00189     glColor4f(1.0f, 1.0f, 1.0f, transparency);
00190     glDepthMask(GL_FALSE);
00191 
00192     glBindTexture(GL_TEXTURE_2D, glTexture->id);
00193     glBegin(GL_TRIANGLE_STRIP);
00194         glTexCoord2f(tLeft, tTop + tHeight);
00195         glVertex3f(sLeft + 0, sTop + sHeight, 1.0f);
00196 
00197         glTexCoord2f(tLeft + tWidth, tTop + tHeight);
00198         glVertex3f(sLeft + sWidth, sTop + sHeight, 1.0f);
00199 
00200         glTexCoord2f(tLeft, tTop);
00201         glVertex3f(sLeft + 0, sTop + 0, 1.0f);
00202 
00203         glTexCoord2f(tLeft + tWidth, tTop);
00204         glVertex3f(sLeft + sWidth, sTop + 0, 1.0f);
00205     glEnd();
00206 
00207     glDisable(GL_BLEND);
00208     glDepthMask(GL_TRUE);
00209 }
00210 
00211 void OpenGLRenderer::draw2DText(const Common::String &text, const Common::Point &position) {
00212     OpenGLTexture *glFont = static_cast<OpenGLTexture *>(_font);
00213 
00214     // The font only has uppercase letters
00215     Common::String textToDraw = text;
00216     textToDraw.toUppercase();
00217 
00218     glEnable(GL_BLEND);
00219     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00220 
00221     glEnable(GL_TEXTURE_2D);
00222     glDepthMask(GL_FALSE);
00223 
00224     glColor3f(1.0f, 1.0f, 1.0f);
00225     glBindTexture(GL_TEXTURE_2D, glFont->id);
00226 
00227     int x = position.x;
00228     int y = position.y;
00229 
00230     for (uint i = 0; i < textToDraw.size(); i++) {
00231         Common::Rect textureRect = getFontCharacterRect(textToDraw[i]);
00232         int w = textureRect.width();
00233         int h = textureRect.height();
00234 
00235         float cw = textureRect.width() / (float)glFont->internalWidth;
00236         float ch = textureRect.height() / (float)glFont->internalHeight;
00237         float cx = textureRect.left / (float)glFont->internalWidth;
00238         float cy = textureRect.top / (float)glFont->internalHeight;
00239 
00240         glBegin(GL_QUADS);
00241         glTexCoord2f(cx, cy + ch);
00242         glVertex3f(x, y, 1.0f);
00243         glTexCoord2f(cx + cw, cy + ch);
00244         glVertex3f(x + w, y, 1.0f);
00245         glTexCoord2f(cx + cw, cy);
00246         glVertex3f(x + w, y + h, 1.0f);
00247         glTexCoord2f(cx, cy);
00248         glVertex3f(x, y + h, 1.0f);
00249         glEnd();
00250 
00251         x += textureRect.width() - 3;
00252     }
00253 
00254     glDisable(GL_TEXTURE_2D);
00255     glDisable(GL_BLEND);
00256     glDepthMask(GL_TRUE);
00257 }
00258 
00259 void OpenGLRenderer::drawFace(uint face, Texture *texture) {
00260     OpenGLTexture *glTexture = static_cast<OpenGLTexture *>(texture);
00261 
00262     // Used fragment of the texture
00263     const float w = glTexture->width  / (float) glTexture->internalWidth;
00264     const float h = glTexture->height / (float) glTexture->internalHeight;
00265 
00266     glBindTexture(GL_TEXTURE_2D, glTexture->id);
00267     glBegin(GL_TRIANGLE_STRIP);
00268     for (uint i = 0; i < 4; i++) {
00269         glTexCoord2f(w * cubeVertices[5 * (4 * face + i) + 0], h * cubeVertices[5 * (4 * face + i) + 1]);
00270         glVertex3f(cubeVertices[5 * (4 * face + i) + 2], cubeVertices[5 * (4 * face + i) + 3], cubeVertices[5 * (4 * face + i) + 4]);
00271     }
00272     glEnd();
00273 }
00274 
00275 void OpenGLRenderer::drawCube(Texture **textures) {
00276     glEnable(GL_TEXTURE_2D);
00277     glDepthMask(GL_FALSE);
00278 
00279     for (uint i = 0; i < 6; i++) {
00280         drawFace(i, textures[i]);
00281     }
00282 
00283     glDepthMask(GL_TRUE);
00284 }
00285 
00286 void OpenGLRenderer::drawTexturedRect3D(const Math::Vector3d &topLeft, const Math::Vector3d &bottomLeft,
00287         const Math::Vector3d &topRight, const Math::Vector3d &bottomRight, Texture *texture) {
00288 
00289     OpenGLTexture *glTexture = static_cast<OpenGLTexture *>(texture);
00290 
00291     const float w = glTexture->width / (float)glTexture->internalWidth;
00292     const float h = glTexture->height / (float)glTexture->internalHeight;
00293 
00294     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00295     glEnable(GL_BLEND);
00296     glDepthMask(GL_FALSE);
00297 
00298     glBindTexture(GL_TEXTURE_2D, glTexture->id);
00299 
00300     glBegin(GL_TRIANGLE_STRIP);
00301         glTexCoord2f(0, 0);
00302         glVertex3f(-topLeft.x(), topLeft.y(), topLeft.z());
00303 
00304         glTexCoord2f(0, h);
00305         glVertex3f(-bottomLeft.x(), bottomLeft.y(), bottomLeft.z());
00306 
00307         glTexCoord2f(w, 0);
00308         glVertex3f(-topRight.x(), topRight.y(), topRight.z());
00309 
00310         glTexCoord2f(w, h);
00311         glVertex3f(-bottomRight.x(), bottomRight.y(), bottomRight.z());
00312     glEnd();
00313 
00314     glDisable(GL_BLEND);
00315     glDepthMask(GL_TRUE);
00316 }
00317 
00318 Graphics::Surface *OpenGLRenderer::getScreenshot() {
00319     Common::Rect screen = viewport();
00320 
00321     Graphics::Surface *s = new Graphics::Surface();
00322     s->create(screen.width(), screen.height(), Texture::getRGBAPixelFormat());
00323 
00324     glReadPixels(screen.left, screen.top, screen.width(), screen.height(), GL_RGBA, GL_UNSIGNED_BYTE, s->getPixels());
00325 
00326     flipVertical(s);
00327 
00328     return s;
00329 }
00330 
00331 } // End of namespace Myst3
00332 
00333 #endif


Generated on Sat Mar 23 2019 05:01:40 for ResidualVM by doxygen 1.7.1
curved edge   curved edge