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

myst3/gfx_opengl_shaders.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 // Matrix calculations taken from the glm library
00024 // Which is covered by the MIT license
00025 // And has this additional copyright note:
00026 /* Copyright (c) 2005 - 2012 G-Truc Creation
00027  *
00028  * Permission is hereby granted, free of charge, to any person obtaining a copy
00029  * of this software and associated documentation files (the "Software"), to deal
00030  * in the Software without restriction, including without limitation the rights
00031  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00032  * copies of the Software, and to permit persons to whom the Software is
00033  * furnished to do so, subject to the following conditions:
00034  *
00035  * The above copyright notice and this permission notice shall be included in
00036  * all copies or substantial portions of the Software.
00037  */
00038 
00039 #if defined(WIN32)
00040 #include <windows.h>
00041 // winnt.h defines ARRAYSIZE, but we want our own one...
00042 #undef ARRAYSIZE
00043 #endif
00044 
00045 #include "common/rect.h"
00046 #include "common/textconsole.h"
00047 
00048 #if defined(USE_GLES2) || defined(USE_OPENGL_SHADERS)
00049 
00050 #include "graphics/colormasks.h"
00051 #include "graphics/surface.h"
00052 
00053 #include "math/glmath.h"
00054 #include "math/vector2d.h"
00055 #include "math/rect2d.h"
00056 #include "math/quat.h"
00057 
00058 #include "graphics/opengl/shader.h"
00059 
00060 #include "engines/myst3/gfx.h"
00061 #include "engines/myst3/gfx_opengl_texture.h"
00062 #include "engines/myst3/gfx_opengl_shaders.h"
00063 
00064 namespace Myst3 {
00065 
00066 Renderer *CreateGfxOpenGLShader(OSystem *system) {
00067     return new ShaderRenderer(system);
00068 }
00069 
00070 static const GLfloat boxVertices[] = {
00071     // XS   YT
00072     0.0, 0.0,
00073     1.0, 0.0,
00074     0.0, 1.0,
00075     1.0, 1.0,
00076 };
00077 
00078 void ShaderRenderer::setupQuadEBO() {
00079     unsigned short quadIndices[6 * 100];
00080 
00081     unsigned short start = 0;
00082     for (unsigned short *p = quadIndices; p < &quadIndices[6 * 100]; p += 6) {
00083         p[0] = p[3] = start++;
00084         p[1] = start++;
00085         p[2] = p[4] = start++;
00086         p[5] = start++;
00087     }
00088 
00089     _quadEBO = OpenGL::Shader::createBuffer(GL_ELEMENT_ARRAY_BUFFER, sizeof(quadIndices), quadIndices, GL_STATIC_DRAW);
00090 }
00091 
00092 Math::Vector2d ShaderRenderer::scaled(float x, float y) const {
00093     return Math::Vector2d(x / _currentViewport.width(), y / _currentViewport.height());
00094 }
00095 
00096 ShaderRenderer::ShaderRenderer(OSystem *system) :
00097         Renderer(system),
00098         _prevText(""),
00099         _prevTextPosition(0,0),
00100         _currentViewport(kOriginalWidth, kOriginalHeight),
00101         _boxShader(nullptr),
00102         _cubeShader(nullptr),
00103         _rect3dShader(nullptr),
00104         _textShader(nullptr),
00105         _boxVBO(0),
00106         _cubeVBO(0),
00107         _rect3dVBO(0),
00108         _textVBO(0),
00109         _quadEBO(0) {
00110 }
00111 
00112 ShaderRenderer::~ShaderRenderer() {
00113     OpenGL::Shader::freeBuffer(_boxVBO);
00114     OpenGL::Shader::freeBuffer(_cubeVBO);
00115     OpenGL::Shader::freeBuffer(_rect3dVBO);
00116     OpenGL::Shader::freeBuffer(_textVBO);
00117     OpenGL::Shader::freeBuffer(_quadEBO);
00118 
00119     delete _boxShader;
00120     delete _cubeShader;
00121     delete _rect3dShader;
00122     delete _textShader;
00123 }
00124 
00125 Texture *ShaderRenderer::createTexture(const Graphics::Surface *surface) {
00126     return new OpenGLTexture(surface);
00127 }
00128 
00129 void ShaderRenderer::freeTexture(Texture *texture) {
00130     OpenGLTexture *glTexture = static_cast<OpenGLTexture *>(texture);
00131     delete glTexture;
00132 }
00133 
00134 void ShaderRenderer::init() {
00135     debug("Initializing OpenGL Renderer with shaders");
00136 
00137     computeScreenViewport();
00138 
00139     glEnable(GL_DEPTH_TEST);
00140 
00141     static const char* attributes[] = { "position", "texcoord", NULL };
00142     _boxShader = OpenGL::Shader::fromFiles("myst3_box", attributes);
00143     _boxVBO = OpenGL::Shader::createBuffer(GL_ARRAY_BUFFER, sizeof(boxVertices), boxVertices);
00144     _boxShader->enableVertexAttribute("position", _boxVBO, 2, GL_FLOAT, GL_TRUE, 2 * sizeof(float), 0);
00145     _boxShader->enableVertexAttribute("texcoord", _boxVBO, 2, GL_FLOAT, GL_TRUE, 2 * sizeof(float), 0);
00146 
00147     _cubeShader = OpenGL::Shader::fromFiles("myst3_cube", attributes);
00148     _cubeVBO = OpenGL::Shader::createBuffer(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices);
00149     _cubeShader->enableVertexAttribute("texcoord", _cubeVBO, 2, GL_FLOAT, GL_TRUE, 5 * sizeof(float), 0);
00150     _cubeShader->enableVertexAttribute("position", _cubeVBO, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 2 * sizeof(float));
00151 
00152     _rect3dShader = OpenGL::Shader::fromFiles("myst3_cube", attributes);
00153     _rect3dVBO = OpenGL::Shader::createBuffer(GL_ARRAY_BUFFER, 20 * sizeof(float), NULL);
00154     _rect3dShader->enableVertexAttribute("texcoord", _rect3dVBO, 2, GL_FLOAT, GL_TRUE, 5 * sizeof(float), 0);
00155     _rect3dShader->enableVertexAttribute("position", _rect3dVBO, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 2 * sizeof(float));
00156 
00157     _textShader = OpenGL::Shader::fromFiles("myst3_text", attributes);
00158     _textVBO = OpenGL::Shader::createBuffer(GL_ARRAY_BUFFER, 100 * 16 * sizeof(float), NULL, GL_DYNAMIC_DRAW);
00159     _textShader->enableVertexAttribute("texcoord", _textVBO, 2, GL_FLOAT, GL_TRUE, 4 * sizeof(float), 0);
00160     _textShader->enableVertexAttribute("position", _textVBO, 2, GL_FLOAT, GL_TRUE, 4 * sizeof(float), 2 * sizeof(float));
00161 
00162     setupQuadEBO();
00163 }
00164 
00165 void ShaderRenderer::clear() {
00166     glClearColor(0.f, 0.f, 0.f, 1.f); // Solid black
00167     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00168 }
00169 
00170 void ShaderRenderer::selectTargetWindow(Window *window, bool is3D, bool scaled) {
00171     if (!window) {
00172         // No window found ...
00173         if (scaled) {
00174             // ... in scaled mode draw in the original game screen area
00175             Common::Rect vp = viewport();
00176             glViewport(vp.left, _system->getHeight() - vp.top - vp.height(), vp.width(), vp.height());
00177             _currentViewport = Common::Rect(kOriginalWidth, kOriginalHeight);
00178         } else {
00179             // ... otherwise, draw on the whole screen
00180             glViewport(0, 0, _system->getWidth(), _system->getHeight());
00181             _currentViewport = Common::Rect(_system->getWidth(), _system->getHeight());
00182         }
00183     } else {
00184         // Found a window, draw inside it
00185         Common::Rect vp = window->getPosition();
00186         glViewport(vp.left, _system->getHeight() - vp.top - vp.height(), vp.width(), vp.height());
00187 
00188         if (scaled) {
00189             _currentViewport = window->getOriginalPosition();
00190         } else {
00191             _currentViewport = vp;
00192         }
00193     }
00194 }
00195 
00196 void ShaderRenderer::drawRect2D(const Common::Rect &rect, uint32 color) {
00197     uint8 a, r, g, b;
00198     Graphics::colorToARGB< Graphics::ColorMasks<8888> >(color, a, r, g, b);
00199 
00200     _boxShader->use();
00201     _boxShader->setUniform("textured", false);
00202     _boxShader->setUniform("color", Math::Vector4d(r / 255.0, g / 255.0, b / 255.0, a / 255.0));
00203     _boxShader->setUniform("verOffsetXY", scaled(rect.left, rect.top));
00204     _boxShader->setUniform("verSizeWH", scaled(rect.width(), rect.height()));
00205 
00206     glDepthMask(GL_FALSE);
00207 
00208     if (a != 255) {
00209         glEnable(GL_BLEND);
00210         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00211     }
00212 
00213     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
00214 
00215     glDisable(GL_BLEND);
00216     glDepthMask(GL_TRUE);
00217 }
00218 
00219 void ShaderRenderer::drawTexturedRect2D(const Common::Rect &screenRect, const Common::Rect &textureRect,
00220         Texture *texture, float transparency, bool additiveBlending) {
00221 
00222     OpenGLTexture *glTexture = static_cast<OpenGLTexture *>(texture);
00223 
00224     const float tLeft = textureRect.left / (float)glTexture->internalWidth;
00225     const float tWidth = textureRect.width() / (float)glTexture->internalWidth;
00226     const float tTop = textureRect.top / (float)glTexture->internalHeight;
00227     const float tHeight = textureRect.height() / (float)glTexture->internalHeight;
00228 
00229     const float sLeft = screenRect.left;
00230     const float sTop = screenRect.top;
00231     const float sWidth = screenRect.width();
00232     const float sHeight = screenRect.height();
00233 
00234     if (transparency >= 0.0) {
00235         if (additiveBlending) {
00236             glBlendFunc(GL_SRC_ALPHA, GL_ONE);
00237         } else {
00238             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00239         }
00240         glEnable(GL_BLEND);
00241     } else {
00242         transparency = 1.0;
00243     }
00244 
00245     _boxShader->use();
00246     _boxShader->setUniform("textured", true);
00247     _boxShader->setUniform("color", Math::Vector4d(1.0f, 1.0f, 1.0f, transparency));
00248     _boxShader->setUniform("verOffsetXY", scaled(sLeft, sTop));
00249     _boxShader->setUniform("verSizeWH", scaled(sWidth, sHeight));
00250     _boxShader->setUniform("texOffsetXY", Math::Vector2d(tLeft, tTop));
00251     _boxShader->setUniform("texSizeWH", Math::Vector2d(tWidth, tHeight));
00252 
00253     glDepthMask(GL_FALSE);
00254 
00255     glBindTexture(GL_TEXTURE_2D, glTexture->id);
00256 
00257     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
00258 
00259     glDisable(GL_BLEND);
00260     glDepthMask(GL_TRUE);
00261 }
00262 
00263 void ShaderRenderer::draw2DText(const Common::String &text, const Common::Point &position) {
00264     OpenGLTexture *glFont = static_cast<OpenGLTexture *>(_font);
00265 
00266     // The font only has uppercase letters
00267     Common::String textToDraw = text;
00268     textToDraw.toUppercase();
00269 
00270     glEnable(GL_BLEND);
00271     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00272 
00273     glDisable(GL_DEPTH_TEST);
00274     glDepthMask(GL_FALSE);
00275 
00276     if (_prevText != textToDraw || _prevTextPosition != position) {
00277         _prevText = textToDraw;
00278         _prevTextPosition = position;
00279 
00280         float x = position.x / (float) _currentViewport.width();
00281         float y = position.y / (float) _currentViewport.height();
00282 
00283         float *bufData = new float[16 * textToDraw.size()];
00284         float *cur = bufData;
00285 
00286         for (uint i = 0; i < textToDraw.size(); i++) {
00287             Common::Rect textureRect = getFontCharacterRect(textToDraw[i]);
00288             float w = textureRect.width() / (float) _currentViewport.width();
00289             float h = textureRect.height() / (float) _currentViewport.height();
00290 
00291             float cw = textureRect.width() / (float)glFont->internalWidth;
00292             float ch = textureRect.height() / (float)glFont->internalHeight;
00293             float cx = textureRect.left / (float)glFont->internalWidth;
00294             float cy = textureRect.top / (float)glFont->internalHeight;
00295 
00296             const float charData[] = {
00297                 cx,      cy + ch, x,     y,    
00298                 cx + cw, cy + ch, x + w, y,    
00299                 cx + cw, cy,      x + w, y + h,
00300                 cx,      cy,      x,     y + h,
00301             };
00302 
00303             memcpy(cur, charData, 16 * sizeof(float));
00304             cur += 16;
00305 
00306             x += (textureRect.width() - 3) / (float) _currentViewport.width();
00307         }
00308 
00309         glBindBuffer(GL_ARRAY_BUFFER, _textVBO);
00310         glBufferSubData(GL_ARRAY_BUFFER, 0, textToDraw.size() * 16 * sizeof(float), bufData);
00311         delete[] bufData;
00312     }
00313 
00314     _textShader->use();
00315     glBindTexture(GL_TEXTURE_2D, glFont->id);
00316     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _quadEBO);
00317     glDrawElements(GL_TRIANGLES, 6 * textToDraw.size(), GL_UNSIGNED_SHORT, 0);
00318     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
00319 
00320     glDisable(GL_BLEND);
00321     glEnable(GL_DEPTH_TEST);
00322     glDepthMask(GL_TRUE);
00323 }
00324 
00325 void ShaderRenderer::drawCube(Texture **textures) {
00326     OpenGLTexture *texture0 = static_cast<OpenGLTexture *>(textures[0]);
00327 
00328     glDepthMask(GL_FALSE);
00329 
00330     _cubeShader->use();
00331     _cubeShader->setUniform1f("texScale", texture0->width / (float) texture0->internalWidth);
00332     _cubeShader->setUniform("mvpMatrix", _mvpMatrix);
00333 
00334     glBindTexture(GL_TEXTURE_2D, static_cast<OpenGLTexture *>(textures[0])->id);
00335     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
00336 
00337     glBindTexture(GL_TEXTURE_2D, static_cast<OpenGLTexture *>(textures[1])->id);
00338     glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
00339 
00340     glBindTexture(GL_TEXTURE_2D, static_cast<OpenGLTexture *>(textures[2])->id);
00341     glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);
00342 
00343     glBindTexture(GL_TEXTURE_2D, static_cast<OpenGLTexture *>(textures[3])->id);
00344     glDrawArrays(GL_TRIANGLE_STRIP, 12, 4);
00345 
00346     glBindTexture(GL_TEXTURE_2D, static_cast<OpenGLTexture *>(textures[4])->id);
00347     glDrawArrays(GL_TRIANGLE_STRIP, 16, 4);
00348 
00349     glBindTexture(GL_TEXTURE_2D, static_cast<OpenGLTexture *>(textures[5])->id);
00350     glDrawArrays(GL_TRIANGLE_STRIP, 20, 4);
00351 
00352     glDepthMask(GL_TRUE);
00353 }
00354 
00355 void ShaderRenderer::drawTexturedRect3D(const Math::Vector3d &topLeft, const Math::Vector3d &bottomLeft,
00356         const Math::Vector3d &topRight, const Math::Vector3d &bottomRight, Texture *texture) {
00357     OpenGLTexture *glTexture = static_cast<OpenGLTexture *>(texture);
00358 
00359     const float w = glTexture->width / (float)glTexture->internalWidth;
00360     const float h = glTexture->height / (float)glTexture->internalHeight;
00361 
00362     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00363     glEnable(GL_BLEND);
00364     glDepthMask(GL_FALSE);
00365 
00366     glBindTexture(GL_TEXTURE_2D, glTexture->id);
00367 
00368     const GLfloat vertices[] = {
00369         // S   T         X                 Y                 Z
00370            0,  0,  -topLeft.x(),      topLeft.y(),      topLeft.z(),
00371            0,  h,  -bottomLeft.x(),   bottomLeft.y(),   bottomLeft.z(),
00372            w,  0,  -topRight.x(),     topRight.y(),     topRight.z(),
00373            w,  h,  -bottomRight.x(),  bottomRight.y(),  bottomRight.z(),
00374     };
00375 
00376     _rect3dShader->use();
00377     _rect3dShader->setUniform1f("texScale", 1.0f);
00378     _rect3dShader->setUniform("mvpMatrix", _mvpMatrix);
00379     glBindBuffer(GL_ARRAY_BUFFER, _rect3dVBO);
00380     glBufferSubData(GL_ARRAY_BUFFER, 0, 20 * sizeof(float), vertices);
00381 
00382     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
00383 
00384     glDisable(GL_BLEND);
00385     glDepthMask(GL_TRUE);
00386 }
00387 
00388 Graphics::Surface *ShaderRenderer::getScreenshot() {
00389     Common::Rect screen = viewport();
00390 
00391     Graphics::Surface *s = new Graphics::Surface();
00392     s->create(screen.width(), screen.height(), Texture::getRGBAPixelFormat());
00393 
00394     glReadPixels(screen.left, screen.top, screen.width(), screen.height(), GL_RGBA, GL_UNSIGNED_BYTE, s->getPixels());
00395 
00396     flipVertical(s);
00397 
00398     return s;
00399 }
00400 
00401 } // End of namespace Myst3
00402 
00403 #endif


Generated on Sat May 25 2019 05:00:46 for ResidualVM by doxygen 1.7.1
curved edge   curved edge