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()
|
|
|
|
{
|
|
|
|
m_textures.fill(std::make_tuple(-1, 0));
|
2012-03-20 16:17:10 +01:00
|
|
|
m_startTime = g_clock.time();
|
2012-01-30 07:27:21 +01:00
|
|
|
}
|
|
|
|
|
2011-12-08 00:43:12 +01:00
|
|
|
bool PainterShaderProgram::link()
|
|
|
|
{
|
|
|
|
bindAttributeLocation(VERTEX_COORDS_ATTR, "vertexCoord");
|
|
|
|
bindAttributeLocation(TEXTURE_COORDS_ATTR, "textureCoord");
|
|
|
|
if(ShaderProgram::link()) {
|
|
|
|
bindUniformLocation(PROJECTION_MATRIX_UNIFORM, "projectionMatrix");
|
|
|
|
bindUniformLocation(TEXTURE_TRANSFORM_MATRIX_UNIFORM, "textureTransformMatrix");
|
|
|
|
bindUniformLocation(COLOR_UNIFORM, "color");
|
|
|
|
bindUniformLocation(OPACITY_UNIFORM, "opacity");
|
|
|
|
bindUniformLocation(TEXTURE_UNIFORM, "texture");
|
2012-01-12 00:00:42 +01:00
|
|
|
bindUniformLocation(TIME_UNIFORM, "time");
|
2011-12-08 00:43:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-20 16:17:10 +01:00
|
|
|
m_startTime = g_clock.time();
|
2011-12-08 00:43:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-25 00:14:12 +01:00
|
|
|
void PainterShaderProgram::setProjectionMatrix(const Matrix3& projectionMatrix)
|
2011-12-07 01:31:55 +01:00
|
|
|
{
|
2011-12-08 00:43:12 +01:00
|
|
|
bind();
|
2011-12-25 00:14:12 +01:00
|
|
|
setUniformValue(PROJECTION_MATRIX_UNIFORM, projectionMatrix);
|
2011-12-07 01:31:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PainterShaderProgram::setColor(const Color& color)
|
|
|
|
{
|
2011-12-08 00:43:12 +01:00
|
|
|
bind();
|
2011-12-08 18:28:29 +01:00
|
|
|
setUniformValue(COLOR_UNIFORM, 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
|
|
|
{
|
2011-12-08 00:43:12 +01:00
|
|
|
bind();
|
2011-12-07 01:31:55 +01:00
|
|
|
setUniformValue(OPACITY_UNIFORM, opacity);
|
|
|
|
}
|
|
|
|
|
2011-12-08 18:28:29 +01:00
|
|
|
void PainterShaderProgram::setUniformTexture(int location, const TexturePtr& texture, int index)
|
|
|
|
{
|
2012-01-30 07:27:21 +01:00
|
|
|
assert(index >= 0 && index <= 1);
|
|
|
|
|
|
|
|
m_textures[index] = std::make_tuple(location, texture ? texture->getId() : 0);
|
2011-12-08 18:28:29 +01:00
|
|
|
}
|
|
|
|
|
2011-12-07 01:31:55 +01:00
|
|
|
void PainterShaderProgram::setTexture(const TexturePtr& texture)
|
|
|
|
{
|
2011-12-08 00:43:12 +01:00
|
|
|
if(!texture)
|
|
|
|
return;
|
|
|
|
|
2011-12-25 00:14:12 +01:00
|
|
|
float w = texture->getWidth();
|
|
|
|
float h = texture->getHeight();
|
2011-12-07 01:31:55 +01:00
|
|
|
|
2011-12-25 00:14:12 +01:00
|
|
|
Matrix2 textureTransformMatrix = { 1.0f/w, 0.0f,
|
|
|
|
0.0f, 1.0f/h };
|
|
|
|
textureTransformMatrix.transpose();
|
2011-12-07 01:31:55 +01:00
|
|
|
|
2011-12-08 00:43:12 +01:00
|
|
|
bind();
|
2011-12-08 18:28:29 +01:00
|
|
|
setUniformTexture(TEXTURE_UNIFORM, texture, 0);
|
2011-12-25 00:14:12 +01:00
|
|
|
setUniformValue(TEXTURE_TRANSFORM_MATRIX_UNIFORM, textureTransformMatrix);
|
2011-12-07 01:31:55 +01:00
|
|
|
}
|
|
|
|
|
2011-12-08 00:43:12 +01:00
|
|
|
void PainterShaderProgram::draw(const CoordsBuffer& coordsBuffer, DrawMode drawMode)
|
2011-12-07 01:31:55 +01:00
|
|
|
{
|
2012-02-20 03:27:08 +01:00
|
|
|
bind();
|
2011-12-08 00:43:12 +01:00
|
|
|
|
2012-03-20 16:17:10 +01:00
|
|
|
setUniformValue(TIME_UNIFORM, g_clock.timeElapsed(m_startTime));
|
2011-12-07 01:31:55 +01:00
|
|
|
|
2011-12-08 00:43:12 +01:00
|
|
|
int numVertices = coordsBuffer.getVertexCount();
|
|
|
|
if(numVertices == 0)
|
|
|
|
return;
|
2011-12-07 01:31:55 +01:00
|
|
|
|
2011-12-08 00:43:12 +01:00
|
|
|
bool mustDisableVertexArray = false;
|
|
|
|
if(coordsBuffer.getVertexCount() > 0) {
|
|
|
|
enableAttributeArray(PainterShaderProgram::VERTEX_COORDS_ATTR);
|
|
|
|
setAttributeArray(PainterShaderProgram::VERTEX_COORDS_ATTR, coordsBuffer.getVertices(), 2);
|
|
|
|
mustDisableVertexArray = true;
|
|
|
|
}
|
2011-12-07 19:49:20 +01:00
|
|
|
|
2011-12-08 00:43:12 +01:00
|
|
|
bool mustDisableTexCoordsArray = false;
|
2011-12-19 18:26:19 +01:00
|
|
|
if(coordsBuffer.getTextureCoordsCount() > 0) {
|
2011-12-08 00:43:12 +01:00
|
|
|
enableAttributeArray(PainterShaderProgram::TEXTURE_COORDS_ATTR);
|
|
|
|
setAttributeArray(PainterShaderProgram::TEXTURE_COORDS_ATTR, coordsBuffer.getTextureCoords(), 2);
|
|
|
|
mustDisableTexCoordsArray = true;
|
2011-12-07 01:31:55 +01:00
|
|
|
}
|
|
|
|
|
2012-01-30 07:27:21 +01:00
|
|
|
for(int i=0;i<(int)m_textures.size();++i) {
|
|
|
|
int location = std::get<0>(m_textures[i]);
|
|
|
|
if(location == -1)
|
|
|
|
break;
|
|
|
|
int id = std::get<1>(m_textures[i]);
|
|
|
|
setUniformValue(location, i);
|
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE0+i);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, id);
|
|
|
|
}
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
|
2011-12-08 00:43:12 +01:00
|
|
|
glDrawArrays(drawMode, 0, numVertices);
|
|
|
|
|
|
|
|
if(mustDisableVertexArray)
|
2011-12-07 01:31:55 +01:00
|
|
|
disableAttributeArray(PainterShaderProgram::VERTEX_COORDS_ATTR);
|
2011-12-08 00:43:12 +01:00
|
|
|
|
|
|
|
if(mustDisableTexCoordsArray)
|
|
|
|
disableAttributeArray(PainterShaderProgram::TEXTURE_COORDS_ATTR);
|
2011-12-07 01:31:55 +01:00
|
|
|
|
|
|
|
//release();
|
|
|
|
}
|
2011-12-08 00:43:12 +01:00
|
|
|
|