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

164 lines
5.3 KiB
C++
Raw Normal View History

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 "paintershaderprogram.h"
#include "painter.h"
#include "texture.h"
#include "texturemanager.h"
2011-12-07 20:54:28 +01:00
#include <framework/core/clock.h>
2011-12-07 01:31:55 +01:00
2012-01-30 07:27:21 +01:00
PainterShaderProgram::PainterShaderProgram()
{
2012-03-20 16:17:10 +01:00
m_startTime = g_clock.time();
2012-04-08 21:28:03 +02:00
m_opacity = 1;
m_color = Color::white;
m_time = 0;
2012-04-05 21:08:46 +02:00
m_lastTexture = -1;
2012-04-09 14:35:46 +02:00
m_textures.fill(0);
2012-01-30 07:27:21 +01:00
}
2011-12-08 00:43:12 +01:00
bool PainterShaderProgram::link()
{
2012-04-08 21:28:03 +02:00
m_startTime = g_clock.time();
bindAttributeLocation(VERTEX_ATTR, "a_vertex");
bindAttributeLocation(TEXCOORD_ATTR, "a_texCoord");
2011-12-08 00:43:12 +01:00
if(ShaderProgram::link()) {
bindUniformLocation(PROJECTION_MATRIX_UNIFORM, "projectionMatrix");
2012-04-08 21:28:03 +02:00
bindUniformLocation(TEXTURE_TRANSFORM_MATRIX_UNIFORM, "texTransformMatrix");
2011-12-08 00:43:12 +01:00
bindUniformLocation(COLOR_UNIFORM, "color");
bindUniformLocation(OPACITY_UNIFORM, "opacity");
2012-01-12 00:00:42 +01:00
bindUniformLocation(TIME_UNIFORM, "time");
2012-04-08 21:28:03 +02:00
bindUniformLocation(TEX0_UNIFORM, "tex0");
bindUniformLocation(TEX1_UNIFORM, "tex1");
bind();
setUniformValue(PROJECTION_MATRIX_UNIFORM, m_projectionMatrix);
setUniformValue(TEXTURE_TRANSFORM_MATRIX_UNIFORM, m_textureTransformMatrix);
setUniformValue(COLOR_UNIFORM, m_color);
setUniformValue(OPACITY_UNIFORM, m_opacity);
setUniformValue(TIME_UNIFORM, m_time);
setUniformValue(TEX0_UNIFORM, 0);
setUniformValue(TEX1_UNIFORM, 1);
2012-04-09 14:35:46 +02:00
enableAttributeArray(PainterShaderProgram::VERTEX_ATTR);
enableAttributeArray(PainterShaderProgram::TEXCOORD_ATTR);
2011-12-08 00:43:12 +01:00
return true;
}
return false;
}
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
}
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-04-08 21:28:03 +02:00
void PainterShaderProgram::setTexture(const TexturePtr& texture, int index)
2011-12-08 18:28:29 +01:00
{
2012-01-30 07:27:21 +01:00
assert(index >= 0 && index <= 1);
2012-04-09 14:35:46 +02:00
uint texId = texture ? texture->getId() : 0;
if(m_textures[index] == texId)
2011-12-08 00:43:12 +01:00
return;
2012-04-09 14:35:46 +02:00
m_textures[index] = texId;
2012-04-08 21:28:03 +02:00
m_lastTexture = std::max(m_lastTexture, index);
if(texture && index == 0) {
const Matrix2& textureTransformMatrix = texture->getTransformMatrix();
if(m_textureTransformMatrix != textureTransformMatrix) {
bind();
setUniformValue(TEXTURE_TRANSFORM_MATRIX_UNIFORM, textureTransformMatrix);
m_textureTransformMatrix = textureTransformMatrix;
}
}
2011-12-07 01:31:55 +01:00
}
void PainterShaderProgram::draw(CoordsBuffer& coordsBuffer, DrawMode drawMode)
2011-12-07 01:31:55 +01:00
{
bind();
2011-12-08 00:43:12 +01:00
2012-04-08 21:28:03 +02:00
float time = g_clock.timeElapsed(m_startTime);
if(m_time != time) {
setUniformValue(TIME_UNIFORM, time);
m_time = time;
}
2011-12-07 01:31:55 +01:00
int vertexCount = coordsBuffer.getVertexCount();
if(vertexCount == 0)
2011-12-08 00:43:12 +01:00
return;
2011-12-07 01:31:55 +01:00
coordsBuffer.updateCaches();
bool hardwareCached = coordsBuffer.isHardwareCached();
2011-12-07 19:49:20 +01:00
if(hardwareCached)
coordsBuffer.getHardwareVertexBuffer()->bind();
2012-04-08 21:28:03 +02:00
setAttributeArray(PainterShaderProgram::VERTEX_ATTR, hardwareCached ? 0 : coordsBuffer.getVertexBuffer(), 2);
2012-04-05 21:08:46 +02:00
bool hasTexture = coordsBuffer.getTextureVertexCount() != 0;
if(hasTexture) {
if(hardwareCached)
coordsBuffer.getHardwareTextureVertexBuffer()->bind();
2012-04-08 21:28:03 +02:00
setAttributeArray(PainterShaderProgram::TEXCOORD_ATTR, hardwareCached ? 0 : coordsBuffer.getTextureVertexBuffer(), 2);
2011-12-07 01:31:55 +01:00
}
if(hardwareCached)
HardwareBuffer::unbind(HardwareBuffer::VertexBuffer);
2012-04-08 21:28:03 +02:00
if(m_lastTexture == 0) {
2012-04-09 14:35:46 +02:00
glBindTexture(GL_TEXTURE_2D, m_textures[0]);
2012-04-08 21:28:03 +02:00
} else {
glActiveTexture(GL_TEXTURE1);
2012-04-09 14:35:46 +02:00
glBindTexture(GL_TEXTURE_2D, m_textures[1]);
2012-04-08 21:28:03 +02:00
glActiveTexture(GL_TEXTURE0);
2012-04-09 14:35:46 +02:00
glBindTexture(GL_TEXTURE_2D, m_textures[0]);
2012-01-30 07:27:21 +01:00
}
glDrawArrays(drawMode, 0, vertexCount);
2011-12-07 01:31:55 +01:00
}
2011-12-08 00:43:12 +01:00