1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/scummsys.h"
#if defined(USE_OPENGL)
#include "graphics/opengl/surfacerenderer.h"
#include "graphics/opengl/context.h"
#include "graphics/opengl/texture.h"
#if defined(USE_OPENGL_SHADERS) || defined(USE_GLES2)
#include "graphics/opengl/shader.h"
#endif
namespace OpenGL {
SurfaceRenderer *createBestSurfaceRenderer() {
#if defined(USE_OPENGL_SHADERS) || defined(USE_GLES2)
if (OpenGLContext.shadersSupported) {
return new ShaderSurfaceRenderer();
}
#endif
#ifndef USE_GLES2
return new FixedSurfaceRenderer();
#else
error("Could not create an appropriate surface renderer for the current OpenGL context");
#endif
}
SurfaceRenderer::SurfaceRenderer() :
_flipY(false),
_alphaBlending(false) {
}
SurfaceRenderer::~SurfaceRenderer() {
}
void SurfaceRenderer::setFlipY(bool flipY) {
_flipY = flipY;
}
void SurfaceRenderer::enableAlphaBlending(bool enable) {
if (_alphaBlending != enable) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
} else {
glDisable(GL_BLEND);
}
_alphaBlending = enable;
}
#ifndef USE_GLES2
FixedSurfaceRenderer::~FixedSurfaceRenderer() {
}
void FixedSurfaceRenderer::prepareState() {
// Save current state
glPushAttrib(GL_TRANSFORM_BIT | GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_SCISSOR_BIT);
// prepare view
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, 1.0, 1.0, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_ALPHA_TEST);
glDepthMask(GL_FALSE);
}
void FixedSurfaceRenderer::render(const Texture *tex, const Math::Rect2d &dest) {
float texcropX = tex->getWidth() / float(tex->getTexWidth());
float texcropY = tex->getHeight() / float(tex->getTexHeight());
float texTop = _flipY ? 0.0f : texcropY;
float texBottom = _flipY ? texcropY : 0.0f;
float offsetX = dest.getTopLeft().getX();
float offsetY = _flipY ? dest.getTopLeft().getY() : 1.0f - dest.getTopLeft().getY() - dest.getHeight();
float sizeX = fabsf(dest.getWidth());
float sizeY = fabsf(dest.getHeight());
glColor4f(1.0, 1.0, 1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, tex->getTextureName());
glBegin(GL_QUADS);
glTexCoord2f(0, texTop);
glVertex2f(offsetX, offsetY);
glTexCoord2f(texcropX, texTop);
glVertex2f(offsetX + sizeX, offsetY);
glTexCoord2f(texcropX, texBottom);
glVertex2f(offsetX + sizeX, offsetY + sizeY);
glTexCoord2f(0.0, texBottom);
glVertex2f(offsetX, offsetY + sizeY);
glEnd();
}
void FixedSurfaceRenderer::restorePreviousState() {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_TEXTURE);
glPopMatrix();
glPopAttrib();
_flipY = false;
_alphaBlending = false;
}
#endif
#if defined(USE_OPENGL_SHADERS) || defined(USE_GLES2)
ShaderSurfaceRenderer::ShaderSurfaceRenderer() {
const GLfloat vertices[] = {
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 1.0,
};
// Setup the box shader used to render the overlay
const char *attributes[] = { "position", "texcoord", nullptr };
_boxShader = Shader::fromStrings("box", BuiltinShaders::boxVertex, BuiltinShaders::boxFragment, attributes);
_boxVerticesVBO = Shader::createBuffer(GL_ARRAY_BUFFER, sizeof(vertices), vertices);
_boxShader->enableVertexAttribute("position", _boxVerticesVBO, 2, GL_FLOAT, GL_TRUE, 2 * sizeof(float), 0);
_boxShader->enableVertexAttribute("texcoord", _boxVerticesVBO, 2, GL_FLOAT, GL_TRUE, 2 * sizeof(float), 0);
}
void ShaderSurfaceRenderer::prepareState() {
_boxShader->use();
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
}
void ShaderSurfaceRenderer::render(const Texture *tex, const Math::Rect2d &dest) {
glBindTexture(GL_TEXTURE_2D, tex->getTextureName());
float texcropX = tex->getWidth() / float(tex->getTexWidth());
float texcropY = tex->getHeight() / float(tex->getTexHeight());
_boxShader->setUniform("texcrop", Math::Vector2d(texcropX, texcropY));
_boxShader->setUniform("flipY", _flipY);
_boxShader->setUniform("offsetXY", dest.getTopLeft());
_boxShader->setUniform("sizeWH", Math::Vector2d(fabsf(dest.getWidth()), fabsf(dest.getHeight())));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindTexture(GL_TEXTURE_2D, 0);
}
void ShaderSurfaceRenderer::restorePreviousState() {
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
_flipY = false;
if (_alphaBlending) {
enableAlphaBlending(false);
}
_boxShader->unbind();
}
ShaderSurfaceRenderer::~ShaderSurfaceRenderer() {
Shader::freeBuffer(_boxVerticesVBO);
delete _boxShader;
}
#endif
} // End of namespace OpenGL
#endif
|