tibia-client/src/framework/graphics/paintershaderprogram.cpp

179 lines
5.3 KiB
C++
Raw Normal View History

2011-12-07 01:31:55 +01:00
/*
2014-04-01 07:36:42 +02:00
* Copyright (c) 2010-2014 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 "paintershaderprogram.h"
#include "painter.h"
#include "texture.h"
#include "texturemanager.h"
2012-06-14 20:26:55 +02:00
#include "graphics.h"
2011-12-07 20:54:28 +01:00
#include <framework/core/clock.h>
2012-06-14 20:26:55 +02:00
#include <framework/platform/platformwindow.h>
2011-12-07 01:31:55 +01:00
2012-01-30 07:27:21 +01:00
PainterShaderProgram::PainterShaderProgram()
{
2012-06-02 16:43:27 +02:00
m_startTime = g_clock.seconds();
2012-04-08 21:28:03 +02:00
m_opacity = 1;
m_color = Color::white;
m_time = 0;
2012-01-30 07:27:21 +01:00
}
2012-06-14 20:26:55 +02:00
void PainterShaderProgram::setupUniforms()
{
bindUniformLocation(TRANSFORM_MATRIX_UNIFORM, "u_TransformMatrix");
2012-06-14 20:26:55 +02:00
bindUniformLocation(PROJECTION_MATRIX_UNIFORM, "u_ProjectionMatrix");
bindUniformLocation(TEXTURE_MATRIX_UNIFORM, "u_TextureMatrix");
bindUniformLocation(COLOR_UNIFORM, "u_Color");
bindUniformLocation(OPACITY_UNIFORM, "u_Opacity");
bindUniformLocation(TIME_UNIFORM, "u_Time");
bindUniformLocation(TEX0_UNIFORM, "u_Tex0");
bindUniformLocation(TEX1_UNIFORM, "u_Tex1");
bindUniformLocation(TEX2_UNIFORM, "u_Tex2");
bindUniformLocation(TEX3_UNIFORM, "u_Tex3");
2012-06-14 20:26:55 +02:00
bindUniformLocation(RESOLUTION_UNIFORM, "u_Resolution");
setUniformValue(TRANSFORM_MATRIX_UNIFORM, m_transformMatrix);
2012-06-14 20:26:55 +02:00
setUniformValue(PROJECTION_MATRIX_UNIFORM, m_projectionMatrix);
setUniformValue(TEXTURE_MATRIX_UNIFORM, m_textureMatrix);
setUniformValue(COLOR_UNIFORM, m_color);
setUniformValue(OPACITY_UNIFORM, m_opacity);
setUniformValue(TIME_UNIFORM, m_time);
setUniformValue(TEX0_UNIFORM, 0);
setUniformValue(TEX1_UNIFORM, 1);
setUniformValue(TEX2_UNIFORM, 2);
setUniformValue(TEX3_UNIFORM, 3);
2012-06-14 20:26:55 +02:00
setUniformValue(RESOLUTION_UNIFORM, (float)m_resolution.width(), (float)m_resolution.height());
}
2011-12-08 00:43:12 +01:00
bool PainterShaderProgram::link()
{
2012-06-02 16:43:27 +02:00
m_startTime = g_clock.seconds();
2012-06-14 20:26:55 +02:00
bindAttributeLocation(VERTEX_ATTR, "a_Vertex");
bindAttributeLocation(TEXCOORD_ATTR, "a_TexCoord");
2011-12-08 00:43:12 +01:00
if(ShaderProgram::link()) {
2012-04-08 21:28:03 +02:00
bind();
2012-06-14 20:26:55 +02:00
setupUniforms();
release();
2011-12-08 00:43:12 +01:00
return true;
}
return false;
}
void PainterShaderProgram::setTransformMatrix(const Matrix3& transformMatrix)
{
if(transformMatrix == m_transformMatrix)
return;
bind();
setUniformValue(TRANSFORM_MATRIX_UNIFORM, transformMatrix);
m_transformMatrix = transformMatrix;
}
2011-12-25 00:14:12 +01:00
void PainterShaderProgram::setProjectionMatrix(const Matrix3& projectionMatrix)
2011-12-07 01:31:55 +01:00
{
2012-04-08 21:28:03 +02:00
if(projectionMatrix == m_projectionMatrix)
return;
2011-12-08 00:43:12 +01:00
bind();
2011-12-25 00:14:12 +01:00
setUniformValue(PROJECTION_MATRIX_UNIFORM, projectionMatrix);
2012-04-08 21:28:03 +02:00
m_projectionMatrix = projectionMatrix;
2011-12-07 01:31:55 +01:00
}
2012-06-02 16:43:27 +02:00
void PainterShaderProgram::setTextureMatrix(const Matrix3& textureMatrix)
{
if(textureMatrix == m_textureMatrix)
return;
bind();
setUniformValue(TEXTURE_MATRIX_UNIFORM, textureMatrix);
m_textureMatrix = textureMatrix;
}
2011-12-07 01:31:55 +01:00
void PainterShaderProgram::setColor(const Color& color)
{
2012-04-08 21:28:03 +02:00
if(color == m_color)
return;
2011-12-08 00:43:12 +01:00
bind();
2011-12-08 18:28:29 +01:00
setUniformValue(COLOR_UNIFORM, color);
2012-04-08 21:28:03 +02:00
m_color = color;
2011-12-07 01:31:55 +01:00
}
2011-12-25 00:14:12 +01:00
void PainterShaderProgram::setOpacity(float opacity)
2011-12-07 01:31:55 +01:00
{
2012-04-08 21:28:03 +02:00
if(m_opacity == opacity)
return;
2011-12-08 00:43:12 +01:00
bind();
2011-12-07 01:31:55 +01:00
setUniformValue(OPACITY_UNIFORM, opacity);
2012-04-08 21:28:03 +02:00
m_opacity = opacity;
2011-12-07 01:31:55 +01:00
}
2012-06-14 20:26:55 +02:00
void PainterShaderProgram::setResolution(const Size& resolution)
{
if(m_resolution == resolution)
return;
bind();
setUniformValue(RESOLUTION_UNIFORM, (float)resolution.width(), (float)resolution.height());
m_resolution = resolution;
}
void PainterShaderProgram::updateTime()
2011-12-08 18:28:29 +01:00
{
2012-06-02 16:43:27 +02:00
float time = g_clock.seconds() - m_startTime;
if(m_time == time)
2011-12-08 00:43:12 +01:00
return;
2011-12-07 01:31:55 +01:00
bind();
setUniformValue(TIME_UNIFORM, time);
m_time = time;
2011-12-07 01:31:55 +01:00
}
2012-06-14 20:26:55 +02:00
void PainterShaderProgram::addMultiTexture(const std::string& file)
{
if(m_multiTextures.size() > 3)
g_logger.error("cannot add more multi textures to shader, the max is 3");
TexturePtr texture = g_textures.getTexture(file);
if(!texture)
return;
texture->setSmooth(true);
texture->setRepeat(true);
m_multiTextures.push_back(texture);
}
void PainterShaderProgram::bindMultiTextures()
{
if(m_multiTextures.size() == 0)
return;
int i=1;
for(const TexturePtr& tex : m_multiTextures) {
glActiveTexture(GL_TEXTURE0 + i++);
2012-06-14 20:26:55 +02:00
glBindTexture(GL_TEXTURE_2D, tex->getId());
}
glActiveTexture(GL_TEXTURE0);
}