2012-04-19 01:03:43 +02:00
|
|
|
/*
|
2013-01-08 19:21:54 +01:00
|
|
|
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
|
2012-04-19 01:03:43 +02: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 "painterogl2.h"
|
|
|
|
#include "texture.h"
|
2012-06-14 20:26:55 +02:00
|
|
|
#include "graphics.h"
|
|
|
|
#include "painterogl2_shadersources.h"
|
|
|
|
#include <framework/platform/platformwindow.h>
|
2012-04-19 01:03:43 +02:00
|
|
|
|
|
|
|
PainterOGL2 *g_painterOGL2 = nullptr;
|
|
|
|
|
|
|
|
PainterOGL2::PainterOGL2()
|
|
|
|
{
|
|
|
|
m_drawProgram = nullptr;
|
|
|
|
resetState();
|
|
|
|
|
2012-06-14 20:26:55 +02:00
|
|
|
m_drawTexturedProgram = PainterShaderProgramPtr(new PainterShaderProgram);
|
|
|
|
assert(m_drawTexturedProgram);
|
|
|
|
m_drawTexturedProgram->addShaderFromSourceCode(Shader::Vertex, glslMainWithTexCoordsVertexShader + glslPositionOnlyVertexShader);
|
|
|
|
m_drawTexturedProgram->addShaderFromSourceCode(Shader::Fragment, glslMainFragmentShader + glslTextureSrcFragmentShader);
|
|
|
|
m_drawTexturedProgram->link();
|
|
|
|
|
|
|
|
m_drawSolidColorProgram = PainterShaderProgramPtr(new PainterShaderProgram);
|
|
|
|
assert(m_drawSolidColorProgram);
|
|
|
|
m_drawSolidColorProgram->addShaderFromSourceCode(Shader::Vertex, glslMainVertexShader + glslPositionOnlyVertexShader);
|
|
|
|
m_drawSolidColorProgram->addShaderFromSourceCode(Shader::Fragment, glslMainFragmentShader + glslSolidColorFragmentShader);
|
|
|
|
m_drawSolidColorProgram->link();
|
|
|
|
|
|
|
|
PainterShaderProgram::release();
|
2012-04-19 01:03:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PainterOGL2::bind()
|
|
|
|
{
|
|
|
|
Painter::bind();
|
|
|
|
|
|
|
|
// vertex and texture coord attributes are always enabled
|
|
|
|
// to avoid massive enable/disables, thus improving frame rate
|
|
|
|
PainterShaderProgram::enableAttributeArray(PainterShaderProgram::VERTEX_ATTR);
|
|
|
|
PainterShaderProgram::enableAttributeArray(PainterShaderProgram::TEXCOORD_ATTR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PainterOGL2::unbind()
|
|
|
|
{
|
|
|
|
PainterShaderProgram::disableAttributeArray(PainterShaderProgram::VERTEX_ATTR);
|
|
|
|
PainterShaderProgram::disableAttributeArray(PainterShaderProgram::TEXCOORD_ATTR);
|
|
|
|
PainterShaderProgram::release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PainterOGL2::drawCoords(CoordsBuffer& coordsBuffer, DrawMode drawMode)
|
|
|
|
{
|
|
|
|
int vertexCount = coordsBuffer.getVertexCount();
|
|
|
|
if(vertexCount == 0)
|
|
|
|
return;
|
|
|
|
|
2012-04-24 23:05:46 +02:00
|
|
|
bool textured = coordsBuffer.getTextureCoordCount() > 0 && m_texture;
|
|
|
|
|
|
|
|
// skip drawing of empty textures
|
|
|
|
if(textured && m_texture->isEmpty())
|
|
|
|
return;
|
2012-04-19 01:03:43 +02:00
|
|
|
|
|
|
|
// update shader with the current painter state
|
|
|
|
m_drawProgram->bind();
|
2013-01-19 03:18:29 +01:00
|
|
|
m_drawProgram->setTransformMatrix(m_transformMatrix);
|
2012-04-19 01:03:43 +02:00
|
|
|
m_drawProgram->setProjectionMatrix(m_projectionMatrix);
|
2012-06-14 20:26:55 +02:00
|
|
|
if(textured) {
|
2012-04-19 01:03:43 +02:00
|
|
|
m_drawProgram->setTextureMatrix(m_textureMatrix);
|
2012-06-14 20:26:55 +02:00
|
|
|
m_drawProgram->bindMultiTextures();
|
|
|
|
}
|
2012-04-19 01:03:43 +02:00
|
|
|
m_drawProgram->setOpacity(m_opacity);
|
|
|
|
m_drawProgram->setColor(m_color);
|
2012-06-14 20:26:55 +02:00
|
|
|
m_drawProgram->setResolution(m_resolution);
|
2012-04-19 01:03:43 +02:00
|
|
|
m_drawProgram->updateTime();
|
|
|
|
|
|
|
|
// update coords buffer hardware caches if enabled
|
|
|
|
coordsBuffer.updateCaches();
|
|
|
|
bool hardwareCached = coordsBuffer.isHardwareCached();
|
|
|
|
|
|
|
|
// only set texture coords arrays when needed
|
|
|
|
if(textured) {
|
|
|
|
if(hardwareCached) {
|
|
|
|
coordsBuffer.getHardwareTextureCoordArray()->bind();
|
|
|
|
m_drawProgram->setAttributeArray(PainterShaderProgram::TEXCOORD_ATTR, nullptr, 2);
|
|
|
|
} else
|
|
|
|
m_drawProgram->setAttributeArray(PainterShaderProgram::TEXCOORD_ATTR, coordsBuffer.getTextureCoordArray(), 2);
|
2012-06-18 16:15:44 +02:00
|
|
|
} else
|
|
|
|
PainterShaderProgram::disableAttributeArray(PainterShaderProgram::TEXCOORD_ATTR);
|
2012-04-19 01:03:43 +02:00
|
|
|
|
|
|
|
// set vertex array
|
|
|
|
if(hardwareCached) {
|
|
|
|
coordsBuffer.getHardwareVertexArray()->bind();
|
|
|
|
m_drawProgram->setAttributeArray(PainterShaderProgram::VERTEX_ATTR, nullptr, 2);
|
|
|
|
HardwareBuffer::unbind(HardwareBuffer::VertexBuffer);
|
|
|
|
} else
|
|
|
|
m_drawProgram->setAttributeArray(PainterShaderProgram::VERTEX_ATTR, coordsBuffer.getVertexArray(), 2);
|
|
|
|
|
|
|
|
// draw the element in coords buffers
|
|
|
|
glDrawArrays(drawMode, 0, vertexCount);
|
2012-06-18 16:15:44 +02:00
|
|
|
|
|
|
|
if(!textured)
|
|
|
|
PainterShaderProgram::enableAttributeArray(PainterShaderProgram::TEXCOORD_ATTR);
|
2012-04-19 01:03:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PainterOGL2::drawTextureCoords(CoordsBuffer& coordsBuffer, const TexturePtr& texture)
|
|
|
|
{
|
2012-06-19 10:46:49 +02:00
|
|
|
if(texture && texture->isEmpty())
|
2012-04-19 01:03:43 +02:00
|
|
|
return;
|
|
|
|
|
2012-06-14 20:26:55 +02:00
|
|
|
setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawTexturedProgram.get());
|
2012-04-19 01:03:43 +02:00
|
|
|
setTexture(texture);
|
|
|
|
drawCoords(coordsBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PainterOGL2::drawTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src)
|
|
|
|
{
|
2012-04-24 23:05:46 +02:00
|
|
|
if(dest.isEmpty() || src.isEmpty() || texture->isEmpty())
|
2012-04-19 01:03:43 +02:00
|
|
|
return;
|
|
|
|
|
2012-06-14 20:26:55 +02:00
|
|
|
setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawTexturedProgram.get());
|
2012-04-19 01:03:43 +02:00
|
|
|
setTexture(texture);
|
2012-04-24 14:06:32 +02:00
|
|
|
|
|
|
|
m_coordsBuffer.clear();
|
|
|
|
m_coordsBuffer.addQuad(dest, src);
|
2012-04-19 01:03:43 +02:00
|
|
|
drawCoords(m_coordsBuffer, TriangleStrip);
|
|
|
|
}
|
|
|
|
|
2013-01-08 22:31:41 +01:00
|
|
|
void PainterOGL2::drawUpsideDownTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src)
|
|
|
|
{
|
|
|
|
if(dest.isEmpty() || src.isEmpty() || texture->isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawTexturedProgram.get());
|
|
|
|
setTexture(texture);
|
|
|
|
|
|
|
|
m_coordsBuffer.clear();
|
|
|
|
m_coordsBuffer.addUpsideDownQuad(dest, src);
|
|
|
|
drawCoords(m_coordsBuffer, TriangleStrip);
|
|
|
|
}
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
void PainterOGL2::drawRepeatedTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src)
|
|
|
|
{
|
2012-04-24 23:05:46 +02:00
|
|
|
if(dest.isEmpty() || src.isEmpty() || texture->isEmpty())
|
2012-04-19 01:03:43 +02:00
|
|
|
return;
|
|
|
|
|
2012-06-14 20:26:55 +02:00
|
|
|
setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawTexturedProgram.get());
|
2012-04-19 01:03:43 +02:00
|
|
|
setTexture(texture);
|
2012-04-24 14:06:32 +02:00
|
|
|
|
|
|
|
m_coordsBuffer.clear();
|
|
|
|
m_coordsBuffer.addRepeatedRects(dest, src);
|
2012-04-19 01:03:43 +02:00
|
|
|
drawCoords(m_coordsBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PainterOGL2::drawFilledRect(const Rect& dest)
|
|
|
|
{
|
|
|
|
if(dest.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2012-06-14 20:26:55 +02:00
|
|
|
setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawSolidColorProgram.get());
|
2012-04-24 14:06:32 +02:00
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
m_coordsBuffer.clear();
|
|
|
|
m_coordsBuffer.addRect(dest);
|
|
|
|
drawCoords(m_coordsBuffer);
|
|
|
|
}
|
|
|
|
|
2012-06-10 08:09:37 +02:00
|
|
|
void PainterOGL2::drawFilledTriangle(const Point& a, const Point& b, const Point& c)
|
|
|
|
{
|
|
|
|
if(a == b || a == c || b == c)
|
|
|
|
return;
|
|
|
|
|
2012-06-14 20:26:55 +02:00
|
|
|
setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawSolidColorProgram.get());
|
2012-06-10 08:09:37 +02:00
|
|
|
|
|
|
|
m_coordsBuffer.clear();
|
|
|
|
m_coordsBuffer.addTriangle(a, b, c);
|
|
|
|
drawCoords(m_coordsBuffer);
|
|
|
|
}
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
void PainterOGL2::drawBoundingRect(const Rect& dest, int innerLineWidth)
|
|
|
|
{
|
|
|
|
if(dest.isEmpty() || innerLineWidth == 0)
|
|
|
|
return;
|
|
|
|
|
2012-06-14 20:26:55 +02:00
|
|
|
setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawSolidColorProgram.get());
|
2012-04-24 14:06:32 +02:00
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
m_coordsBuffer.clear();
|
|
|
|
m_coordsBuffer.addBoudingRect(dest, innerLineWidth);
|
|
|
|
drawCoords(m_coordsBuffer);
|
|
|
|
}
|