2011-12-07 01:31:55 +01:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-12-07 01:31:55 +01:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "painter.h"
|
|
|
|
#include "graphics.h"
|
|
|
|
|
2012-06-19 01:58:56 +02:00
|
|
|
#include <framework/platform/platformwindow.h>
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
Painter *g_painter = nullptr;
|
2011-12-07 01:31:55 +01:00
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
Painter::Painter()
|
2011-12-07 01:31:55 +01:00
|
|
|
{
|
2012-04-19 01:03:43 +02:00
|
|
|
m_glTextureId = 0;
|
|
|
|
m_oldStateIndex = 0;
|
|
|
|
m_color = Color::white;
|
|
|
|
m_opacity = 1.0f;
|
|
|
|
m_compositionMode = CompositionMode_Normal;
|
|
|
|
m_shaderProgram = nullptr;
|
|
|
|
m_texture = nullptr;
|
2012-06-09 02:40:22 +02:00
|
|
|
m_alphaWriting = false;
|
2012-06-19 01:58:56 +02:00
|
|
|
setResolution(g_window.getSize());
|
2011-12-07 01:31:55 +01:00
|
|
|
}
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
void Painter::resetState()
|
2011-12-07 01:31:55 +01:00
|
|
|
{
|
2012-04-19 01:03:43 +02:00
|
|
|
resetColor();
|
|
|
|
resetOpacity();
|
|
|
|
resetCompositionMode();
|
|
|
|
resetClipRect();
|
|
|
|
resetShaderProgram();
|
|
|
|
resetTexture();
|
2012-06-09 02:40:22 +02:00
|
|
|
resetAlphaWriting();
|
2011-12-07 01:31:55 +01:00
|
|
|
}
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
void Painter::refreshState()
|
2011-12-07 01:31:55 +01:00
|
|
|
{
|
2012-06-19 01:58:56 +02:00
|
|
|
updateGlViewport();
|
2012-04-19 01:03:43 +02:00
|
|
|
updateGlCompositionMode();
|
|
|
|
updateGlClipRect();
|
|
|
|
updateGlTexture();
|
2012-06-09 02:40:22 +02:00
|
|
|
updateGlAlphaWriting();
|
2012-04-19 01:03:43 +02:00
|
|
|
}
|
2011-12-07 01:31:55 +01:00
|
|
|
|
2012-06-01 21:39:09 +02:00
|
|
|
void Painter::saveState()
|
2012-04-19 01:03:43 +02:00
|
|
|
{
|
|
|
|
assert(m_oldStateIndex<10);
|
2012-06-16 00:18:30 +02:00
|
|
|
m_olderStates[m_oldStateIndex].resolution = m_resolution;
|
2012-04-19 01:03:43 +02:00
|
|
|
m_olderStates[m_oldStateIndex].projectionMatrix = m_projectionMatrix;
|
|
|
|
m_olderStates[m_oldStateIndex].textureMatrix = m_textureMatrix;
|
|
|
|
m_olderStates[m_oldStateIndex].color = m_color;
|
|
|
|
m_olderStates[m_oldStateIndex].opacity = m_opacity;
|
|
|
|
m_olderStates[m_oldStateIndex].compositionMode = m_compositionMode;
|
|
|
|
m_olderStates[m_oldStateIndex].clipRect = m_clipRect;
|
|
|
|
m_olderStates[m_oldStateIndex].shaderProgram = m_shaderProgram;
|
|
|
|
m_olderStates[m_oldStateIndex].texture = m_texture;
|
2012-06-09 02:40:22 +02:00
|
|
|
m_olderStates[m_oldStateIndex].alphaWriting = m_alphaWriting;
|
2012-04-19 01:03:43 +02:00
|
|
|
m_oldStateIndex++;
|
2012-06-01 21:39:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Painter::saveAndResetState()
|
|
|
|
{
|
|
|
|
saveState();
|
2012-04-19 01:03:43 +02:00
|
|
|
resetState();
|
2011-12-07 19:49:20 +01:00
|
|
|
}
|
2011-12-07 01:31:55 +01:00
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
void Painter::restoreSavedState()
|
2011-12-07 19:49:20 +01:00
|
|
|
{
|
2012-04-19 01:03:43 +02:00
|
|
|
m_oldStateIndex--;
|
2012-06-16 00:18:30 +02:00
|
|
|
setResolution(m_olderStates[m_oldStateIndex].resolution);
|
2012-04-19 01:03:43 +02:00
|
|
|
setProjectionMatrix(m_olderStates[m_oldStateIndex].projectionMatrix);
|
|
|
|
setTextureMatrix(m_olderStates[m_oldStateIndex].textureMatrix);
|
|
|
|
setColor(m_olderStates[m_oldStateIndex].color);
|
|
|
|
setOpacity(m_olderStates[m_oldStateIndex].opacity);
|
|
|
|
setCompositionMode(m_olderStates[m_oldStateIndex].compositionMode);
|
|
|
|
setClipRect(m_olderStates[m_oldStateIndex].clipRect);
|
|
|
|
setShaderProgram(m_olderStates[m_oldStateIndex].shaderProgram);
|
|
|
|
setTexture(m_olderStates[m_oldStateIndex].texture);
|
2012-06-16 00:18:30 +02:00
|
|
|
setAlphaWriting(m_olderStates[m_oldStateIndex].alphaWriting);
|
2011-12-07 01:31:55 +01:00
|
|
|
}
|
|
|
|
|
2012-06-06 16:10:35 +02:00
|
|
|
void Painter::clear(const Color& color)
|
2012-06-03 22:25:12 +02:00
|
|
|
{
|
2012-06-06 16:10:35 +02:00
|
|
|
glClearColor(color.rF(), color.gF(), color.bF(), color.aF());
|
2012-06-03 22:25:12 +02:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
2012-06-06 16:10:35 +02:00
|
|
|
void Painter::clearRect(const Color& color, const Rect& rect)
|
|
|
|
{
|
|
|
|
Rect oldClipRect = m_clipRect;
|
|
|
|
setClipRect(rect);
|
|
|
|
glClearColor(color.rF(), color.gF(), color.bF(), color.aF());
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
setClipRect(oldClipRect);
|
|
|
|
}
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
void Painter::setCompositionMode(Painter::CompositionMode compositionMode)
|
2011-12-07 19:49:20 +01:00
|
|
|
{
|
2012-04-19 01:03:43 +02:00
|
|
|
if(m_compositionMode == compositionMode)
|
2011-12-07 19:49:20 +01:00
|
|
|
return;
|
2012-04-19 01:03:43 +02:00
|
|
|
m_compositionMode = compositionMode;
|
|
|
|
updateGlCompositionMode();
|
2011-12-07 19:49:20 +01:00
|
|
|
}
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
void Painter::setClipRect(const Rect& clipRect)
|
2011-12-07 01:31:55 +01:00
|
|
|
{
|
2012-04-19 01:03:43 +02:00
|
|
|
if(m_clipRect == clipRect)
|
2011-12-07 01:31:55 +01:00
|
|
|
return;
|
2012-04-19 01:03:43 +02:00
|
|
|
m_clipRect = clipRect;
|
|
|
|
updateGlClipRect();
|
2011-12-07 01:31:55 +01:00
|
|
|
}
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
void Painter::setTexture(Texture* texture)
|
2011-12-07 01:31:55 +01:00
|
|
|
{
|
2012-04-19 01:03:43 +02:00
|
|
|
if(m_texture == texture)
|
2011-12-07 01:31:55 +01:00
|
|
|
return;
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
m_texture = texture;
|
|
|
|
|
2012-06-22 05:14:13 +02:00
|
|
|
uint glTextureId;
|
2012-04-19 01:03:43 +02:00
|
|
|
if(texture) {
|
|
|
|
setTextureMatrix(texture->getTransformMatrix());
|
2012-04-24 23:05:46 +02:00
|
|
|
glTextureId = texture->getId();
|
|
|
|
} else
|
|
|
|
glTextureId = 0;
|
2012-04-19 01:03:43 +02:00
|
|
|
|
2012-04-24 23:05:46 +02:00
|
|
|
if(m_glTextureId != glTextureId) {
|
|
|
|
m_glTextureId = glTextureId;
|
|
|
|
updateGlTexture();
|
2012-04-24 18:37:46 +02:00
|
|
|
}
|
2011-12-07 01:31:55 +01:00
|
|
|
}
|
|
|
|
|
2012-06-09 02:40:22 +02:00
|
|
|
void Painter::setAlphaWriting(bool enable)
|
|
|
|
{
|
|
|
|
if(m_alphaWriting == enable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_alphaWriting = enable;
|
|
|
|
updateGlAlphaWriting();
|
|
|
|
}
|
|
|
|
|
2012-06-14 20:26:55 +02:00
|
|
|
void Painter::setResolution(const Size& resolution)
|
|
|
|
{
|
|
|
|
if(m_resolution == resolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// The projection matrix converts from Painter's coordinate system to GL's coordinate system
|
|
|
|
// * GL's viewport is 2x2, Painter's is width x height
|
|
|
|
// * GL has +y -> -y going from bottom -> top, Painter is the other way round
|
|
|
|
// * GL has [0,0] in the center, Painter has it in the top-left
|
|
|
|
//
|
|
|
|
// This results in the Projection matrix below.
|
|
|
|
//
|
|
|
|
// Projection Matrix
|
|
|
|
// Painter Coord ------------------------------------------------ GL Coord
|
|
|
|
// ------------- | 2.0 / width | 0.0 | 0.0 | ---------------
|
|
|
|
// | x y 1 | * | 0.0 | -2.0 / height | 0.0 | = | x' y' 1 |
|
|
|
|
// ------------- | -1.0 | 1.0 | 1.0 | ---------------
|
|
|
|
|
|
|
|
Matrix3 projectionMatrix = { 2.0f/resolution.width(), 0.0f, 0.0f,
|
|
|
|
0.0f, -2.0f/resolution.height(), 0.0f,
|
|
|
|
-1.0f, 1.0f, 1.0f };
|
|
|
|
|
|
|
|
m_resolution = resolution;
|
|
|
|
|
|
|
|
setProjectionMatrix(projectionMatrix);
|
|
|
|
if(g_painter == this)
|
|
|
|
updateGlViewport();
|
|
|
|
}
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
void Painter::updateGlTexture()
|
2011-12-07 01:31:55 +01:00
|
|
|
{
|
2012-04-24 23:05:46 +02:00
|
|
|
if(m_glTextureId != 0)
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_glTextureId);
|
2011-12-07 01:31:55 +01:00
|
|
|
}
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
void Painter::updateGlCompositionMode()
|
2011-12-07 01:31:55 +01:00
|
|
|
{
|
2012-04-19 01:03:43 +02:00
|
|
|
switch(m_compositionMode) {
|
|
|
|
case CompositionMode_Normal:
|
2012-06-02 01:21:45 +02:00
|
|
|
if(g_graphics.canUseBlendFuncSeparate())
|
|
|
|
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
|
2012-06-09 15:00:08 +02:00
|
|
|
else
|
2012-06-02 01:21:45 +02:00
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2012-04-19 01:03:43 +02:00
|
|
|
break;
|
|
|
|
case CompositionMode_Multiply:
|
|
|
|
glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
break;
|
|
|
|
case CompositionMode_Add:
|
|
|
|
glBlendFunc(GL_ONE, GL_ONE);
|
|
|
|
break;
|
|
|
|
case CompositionMode_Replace:
|
|
|
|
glBlendFunc(GL_ONE, GL_ZERO);
|
|
|
|
break;
|
|
|
|
case CompositionMode_DestBlending:
|
|
|
|
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
|
|
|
|
break;
|
2011-12-07 01:31:55 +01:00
|
|
|
}
|
2012-01-30 07:27:21 +01:00
|
|
|
}
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
void Painter::updateGlClipRect()
|
2012-04-05 00:46:49 +02:00
|
|
|
{
|
2012-04-19 01:03:43 +02:00
|
|
|
if(m_clipRect.isValid()) {
|
2012-04-05 00:46:49 +02:00
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2012-06-14 20:26:55 +02:00
|
|
|
glScissor(m_clipRect.left(), m_resolution.height() - m_clipRect.bottom() - 1, m_clipRect.width(), m_clipRect.height());
|
2012-04-05 00:46:49 +02:00
|
|
|
} else {
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
}
|
2011-12-07 01:31:55 +01:00
|
|
|
}
|
2012-06-09 02:40:22 +02:00
|
|
|
|
|
|
|
void Painter::updateGlAlphaWriting()
|
|
|
|
{
|
|
|
|
if(m_alphaWriting)
|
|
|
|
glColorMask(1,1,1,1);
|
|
|
|
else
|
|
|
|
glColorMask(1,1,1,0);
|
|
|
|
}
|
2012-06-14 20:26:55 +02:00
|
|
|
|
|
|
|
void Painter::updateGlViewport()
|
|
|
|
{
|
|
|
|
glViewport(0, 0, m_resolution.width(), m_resolution.height());
|
|
|
|
}
|