1 /* ResidualVM - A 3D game interpreter
2  *
3  * ResidualVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the AUTHORS
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "engines/stark/gfx/openglssurface.h"
24 
25 #include "engines/stark/gfx/opengls.h"
26 #include "engines/stark/gfx/texture.h"
27 
28 #include "graphics/opengl/shader.h"
29 
30 namespace Stark {
31 namespace Gfx {
32 
OpenGLSSurfaceRenderer(OpenGLSDriver * gfx)33 OpenGLSSurfaceRenderer::OpenGLSSurfaceRenderer(OpenGLSDriver *gfx) :
34 		SurfaceRenderer(),
35 		_gfx(gfx) {
36 	_shader = _gfx->createSurfaceShaderInstance();
37 }
38 
~OpenGLSSurfaceRenderer()39 OpenGLSSurfaceRenderer::~OpenGLSSurfaceRenderer() {
40 	delete _shader;
41 }
42 
render(const Texture * texture,const Common::Point & dest)43 void OpenGLSSurfaceRenderer::render(const Texture *texture, const Common::Point &dest) {
44 	// Destination rectangle
45 	const float sLeft = dest.x;
46 	const float sTop = dest.y;
47 	const float sWidth = texture->width();
48 	const float sHeight = texture->height();
49 
50 	_gfx->start2DMode();
51 
52 	_shader->use();
53 	_shader->setUniform1f("fadeLevel", _fadeLevel);
54 	_shader->setUniform("verOffsetXY", normalizeOriginalCoordinates(sLeft, sTop));
55 	if (_noScalingOverride) {
56 		_shader->setUniform("verSizeWH", normalizeCurrentCoordinates(sWidth, sHeight));
57 	} else {
58 		_shader->setUniform("verSizeWH", normalizeOriginalCoordinates(sWidth, sHeight));
59 	}
60 
61 	texture->bind();
62 	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
63 
64 	_shader->unbind();
65 	_gfx->end2DMode();
66 }
67 
normalizeOriginalCoordinates(float x,float y) const68 Math::Vector2d OpenGLSSurfaceRenderer::normalizeOriginalCoordinates(float x, float y) const {
69 	Common::Rect viewport = _gfx->getUnscaledViewport();
70 	return Math::Vector2d(x / (float)viewport.width(), y / (float)viewport.height());
71 }
72 
normalizeCurrentCoordinates(float x,float y) const73 Math::Vector2d OpenGLSSurfaceRenderer::normalizeCurrentCoordinates(float x, float y) const {
74 	Common::Rect viewport = _gfx->getViewport();
75 	return Math::Vector2d(x / (float)viewport.width(), y / (float)viewport.height());
76 }
77 
78 } // End of namespace Gfx
79 } // End of namespace Stark
80