More changes to painter

This commit is contained in:
Henrique Santiago
2013-02-22 03:31:13 -03:00
parent 3eeb5ae890
commit 90d2032e9e
18 changed files with 31 additions and 127 deletions

View File

@@ -0,0 +1,317 @@
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
*
* 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 "painterogl.h"
#include <framework/graphics/graphics.h>
#include <framework/platform/platformwindow.h>
PainterOGL::PainterOGL()
{
m_glTextureId = 0;
m_oldStateIndex = 0;
m_color = Color::white;
m_opacity = 1.0f;
m_compositionMode = CompositionMode_Normal;
m_blendEquation = BlendEquation_Add;
m_shaderProgram = nullptr;
m_texture = nullptr;
m_alphaWriting = false;
setResolution(g_window.getSize());
}
void PainterOGL::resetState()
{
resetColor();
resetOpacity();
resetCompositionMode();
resetBlendEquation();
resetClipRect();
resetShaderProgram();
resetTexture();
resetAlphaWriting();
resetTransformMatrix();
}
void PainterOGL::refreshState()
{
updateGlViewport();
updateGlCompositionMode();
updateGlBlendEquation();
updateGlClipRect();
updateGlTexture();
updateGlAlphaWriting();
}
void PainterOGL::saveState()
{
assert(m_oldStateIndex<10);
m_olderStates[m_oldStateIndex].resolution = m_resolution;
m_olderStates[m_oldStateIndex].transformMatrix = m_transformMatrix;
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].blendEquation = m_blendEquation;
m_olderStates[m_oldStateIndex].clipRect = m_clipRect;
m_olderStates[m_oldStateIndex].shaderProgram = m_shaderProgram;
m_olderStates[m_oldStateIndex].texture = m_texture;
m_olderStates[m_oldStateIndex].alphaWriting = m_alphaWriting;
m_oldStateIndex++;
}
void PainterOGL::saveAndResetState()
{
saveState();
resetState();
}
void PainterOGL::restoreSavedState()
{
m_oldStateIndex--;
setResolution(m_olderStates[m_oldStateIndex].resolution);
setTransformMatrix(m_olderStates[m_oldStateIndex].transformMatrix);
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);
setBlendEquation(m_olderStates[m_oldStateIndex].blendEquation);
setClipRect(m_olderStates[m_oldStateIndex].clipRect);
setShaderProgram(m_olderStates[m_oldStateIndex].shaderProgram);
setTexture(m_olderStates[m_oldStateIndex].texture);
setAlphaWriting(m_olderStates[m_oldStateIndex].alphaWriting);
}
void PainterOGL::clear(const Color& color)
{
glClearColor(color.rF(), color.gF(), color.bF(), color.aF());
glClear(GL_COLOR_BUFFER_BIT);
}
void PainterOGL::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);
}
void PainterOGL::setCompositionMode(Painter::CompositionMode compositionMode)
{
if(m_compositionMode == compositionMode)
return;
m_compositionMode = compositionMode;
updateGlCompositionMode();
}
void PainterOGL::setBlendEquation(Painter::BlendEquation blendEquation)
{
if(m_blendEquation == blendEquation)
return;
m_blendEquation = blendEquation;
updateGlBlendEquation();
}
void PainterOGL::setClipRect(const Rect& clipRect)
{
if(m_clipRect == clipRect)
return;
m_clipRect = clipRect;
updateGlClipRect();
}
void PainterOGL::setTexture(Texture* texture)
{
if(m_texture == texture)
return;
m_texture = texture;
uint glTextureId;
if(texture) {
setTextureMatrix(texture->getTransformMatrix());
glTextureId = texture->getId();
} else
glTextureId = 0;
if(m_glTextureId != glTextureId) {
m_glTextureId = glTextureId;
updateGlTexture();
}
}
void PainterOGL::setAlphaWriting(bool enable)
{
if(m_alphaWriting == enable)
return;
m_alphaWriting = enable;
updateGlAlphaWriting();
}
void PainterOGL::setResolution(const Size& resolution)
{
// 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();
}
void PainterOGL::scale(float x, float y)
{
Matrix3 scaleMatrix = {
x, 0.0f, 0.0f,
0.0f, y, 0.0f,
0.0f, 0.0f, 1.0f
};
setTransformMatrix(m_transformMatrix * scaleMatrix.transposed());
}
void PainterOGL::translate(float x, float y)
{
Matrix3 translateMatrix = {
1.0f, 0.0f, x,
0.0f, 1.0f, y,
0.0f, 0.0f, 1.0f
};
setTransformMatrix(m_transformMatrix * translateMatrix.transposed());
}
void PainterOGL::rotate(float angle)
{
Matrix3 rotationMatrix = {
std::cos(angle), -std::sin(angle), 0.0f,
std::sin(angle), std::cos(angle), 0.0f,
0.0f, 0.0f, 1.0f
};
setTransformMatrix(m_transformMatrix * rotationMatrix.transposed());
}
void PainterOGL::rotate(float x, float y, float angle)
{
translate(-x, -y);
rotate(angle);
translate(x, y);
}
void PainterOGL::pushTransformMatrix()
{
m_transformMatrixStack.push_back(m_transformMatrix);
assert(m_transformMatrixStack.size() < 100);
}
void PainterOGL::popTransformMatrix()
{
assert(m_transformMatrixStack.size() > 0);
setTransformMatrix(m_transformMatrixStack.back());
m_transformMatrixStack.pop_back();
}
void PainterOGL::updateGlTexture()
{
if(m_glTextureId != 0)
glBindTexture(GL_TEXTURE_2D, m_glTextureId);
}
void PainterOGL::updateGlCompositionMode()
{
switch(m_compositionMode) {
case CompositionMode_Normal:
if(g_graphics.canUseBlendFuncSeparate())
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
else
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
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;
case CompositionMode_Light:
glBlendFunc(GL_ZERO, GL_SRC_COLOR);
break;
}
}
void PainterOGL::updateGlBlendEquation()
{
if(!g_graphics.canUseBlendEquation())
return;
if(m_blendEquation == BlendEquation_Add)
glBlendEquation(0x8006); // GL_FUNC_ADD
else if(m_blendEquation == BlendEquation_Max)
glBlendEquation(0x8008); // GL_MAX
}
void PainterOGL::updateGlClipRect()
{
if(m_clipRect.isValid()) {
glEnable(GL_SCISSOR_TEST);
glScissor(m_clipRect.left(), m_resolution.height() - m_clipRect.bottom() - 1, m_clipRect.width(), m_clipRect.height());
} else {
glScissor(0, 0, m_resolution.width(), m_resolution.height());
glDisable(GL_SCISSOR_TEST);
}
}
void PainterOGL::updateGlAlphaWriting()
{
if(m_alphaWriting)
glColorMask(1,1,1,1);
else
glColorMask(1,1,1,0);
}
void PainterOGL::updateGlViewport()
{
glViewport(0, 0, m_resolution.width(), m_resolution.height());
}

View File

@@ -0,0 +1,119 @@
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
*
* 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.
*/
#ifndef PAINTEROGL_H
#define PAINTEROGL_H
#include <framework/graphics/painter.h>
class PainterOGL : public Painter
{
public:
struct PainterState {
Size resolution;
Matrix3 transformMatrix;
Matrix3 projectionMatrix;
Matrix3 textureMatrix;
Color color;
float opacity;
Painter::CompositionMode compositionMode;
Painter::BlendEquation blendEquation;
Rect clipRect;
Texture *texture;
PainterShaderProgram *shaderProgram;
bool alphaWriting;
};
PainterOGL();
virtual ~PainterOGL() { }
virtual void bind() { refreshState(); }
virtual void unbind() { }
void resetState();
virtual void refreshState();
void saveState();
void saveAndResetState();
void restoreSavedState();
void clear(const Color& color);
void clearRect(const Color& color, const Rect& rect);
virtual void setTransformMatrix(const Matrix3& transformMatrix) { m_transformMatrix = transformMatrix; }
virtual void setProjectionMatrix(const Matrix3& projectionMatrix) { m_projectionMatrix = projectionMatrix; }
virtual void setTextureMatrix(const Matrix3& textureMatrix) { m_textureMatrix = textureMatrix; }
virtual void setCompositionMode(CompositionMode compositionMode);
virtual void setBlendEquation(BlendEquation blendEquation);
virtual void setClipRect(const Rect& clipRect);
virtual void setShaderProgram(PainterShaderProgram *shaderProgram) { m_shaderProgram = shaderProgram; }
virtual void setTexture(Texture *texture);
virtual void setAlphaWriting(bool enable);
void setTexture(const TexturePtr& texture) { setTexture(texture.get()); }
void setResolution(const Size& resolution);
void scale(float x, float y);
void translate(float x, float y);
void rotate(float angle);
void rotate(float x, float y, float angle);
void pushTransformMatrix();
void popTransformMatrix();
Matrix3 getTransformMatrix() { return m_transformMatrix; }
Matrix3 getProjectionMatrix() { return m_projectionMatrix; }
Matrix3 getTextureMatrix() { return m_textureMatrix; }
BlendEquation getBlendEquation() { return m_blendEquation; }
PainterShaderProgram *getShaderProgram() { return m_shaderProgram; }
bool getAlphaWriting() { return m_alphaWriting; }
void resetBlendEquation() { setBlendEquation(BlendEquation_Add); }
void resetTexture() { setTexture(nullptr); }
void resetAlphaWriting() { setAlphaWriting(false); }
void resetTransformMatrix() { setTransformMatrix(Matrix3()); }
protected:
void updateGlTexture();
void updateGlCompositionMode();
void updateGlBlendEquation();
void updateGlClipRect();
void updateGlAlphaWriting();
void updateGlViewport();
CoordsBuffer m_coordsBuffer;
std::vector<Matrix3> m_transformMatrixStack;
Matrix3 m_transformMatrix;
Matrix3 m_projectionMatrix;
Matrix3 m_textureMatrix;
BlendEquation m_blendEquation;
Texture *m_texture;
bool m_alphaWriting;
PainterState m_olderStates[10];
int m_oldStateIndex;
uint m_glTextureId;
};
#endif

View File

@@ -0,0 +1,328 @@
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
*
* 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.
*/
#if !defined(OPENGL_ES) || OPENGL_ES==1
#include "painterogl1.h"
#include <framework/graphics/graphics.h>
PainterOGL1 *g_painterOGL1 = nullptr;
PainterOGL1::PainterOGL1()
{
m_matrixMode = GL_PROJECTION;
resetState();
}
void PainterOGL1::refreshState()
{
PainterOGL::refreshState();
updateGlColor();
updateGlMatrixMode();
updateGlTransformMatrix();
updateGlProjectionMatrix();
updateGlTextureMatrix();
updateGlTextureState();
}
void PainterOGL1::bind()
{
PainterOGL::bind();
// vertex and texture coord arrays are always enabled
// to avoid massive enable/disables, thus improving frame rate
if(g_graphics.canUseDrawArrays())
glEnableClientState(GL_VERTEX_ARRAY);
}
void PainterOGL1::unbind()
{
if(g_graphics.canUseDrawArrays())
glDisableClientState(GL_VERTEX_ARRAY);
}
void PainterOGL1::drawCoords(CoordsBuffer& coordsBuffer, DrawMode drawMode)
{
int vertexCount = coordsBuffer.getVertexCount();
if(vertexCount == 0)
return;
bool textured = coordsBuffer.getTextureCoordCount() != 0 && m_texture;
// skip drawing of empty textures
if(textured && m_texture->isEmpty())
return;
if(textured != m_textureEnabled) {
m_textureEnabled = textured;
updateGlTextureState();
}
// GDI Generic driver has this bug
if(g_graphics.hasScissorBug())
updateGlClipRect();
// use vertex arrays if possible, much faster
if(g_graphics.canUseDrawArrays()) {
// 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();
glTexCoordPointer(2, GL_FLOAT, 0, nullptr);
} else
glTexCoordPointer(2, GL_FLOAT, 0, coordsBuffer.getTextureCoordArray());
}
// set vertex array
if(hardwareCached) {
coordsBuffer.getHardwareVertexArray()->bind();
glVertexPointer(2, GL_FLOAT, 0, nullptr);
} else
glVertexPointer(2, GL_FLOAT, 0, coordsBuffer.getVertexArray());
if(hardwareCached)
HardwareBuffer::unbind(HardwareBuffer::VertexBuffer);
// draw the element in coords buffers
if(drawMode == Triangles)
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
else if(drawMode == TriangleStrip)
glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexCount);
}
#ifndef OPENGL_ES
else {
int verticesSize = vertexCount*2;
float *vertices = coordsBuffer.getVertexArray();
float *texCoords = coordsBuffer.getTextureCoordArray();
// use glBegin/glEnd, this is not available in OpenGL ES
// and is considered much slower then glDrawArrays,
// but this code is executed in really old graphics cards
if(drawMode == Triangles)
glBegin(GL_TRIANGLES);
else if(drawMode == TriangleStrip)
glBegin(GL_TRIANGLE_STRIP);
for(int i=0;i<verticesSize;i+=2) {
if(textured)
glTexCoord2f(texCoords[i], texCoords[i+1]);
glVertex2f(vertices[i], vertices[i+1]);
}
glEnd();
}
#endif
}
void PainterOGL1::drawTextureCoords(CoordsBuffer& coordsBuffer, const TexturePtr& texture)
{
if(texture->isEmpty())
return;
setTexture(texture.get());
drawCoords(coordsBuffer);
}
void PainterOGL1::drawTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src)
{
if(dest.isEmpty() || src.isEmpty() || texture->isEmpty())
return;
setTexture(texture.get());
m_coordsBuffer.clear();
m_coordsBuffer.addQuad(dest, src);
drawCoords(m_coordsBuffer, TriangleStrip);
}
void PainterOGL1::drawUpsideDownTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src)
{
if(dest.isEmpty() || src.isEmpty() || texture->isEmpty())
return;
setTexture(texture.get());
m_coordsBuffer.clear();
m_coordsBuffer.addUpsideDownQuad(dest, src);
drawCoords(m_coordsBuffer, TriangleStrip);
}
void PainterOGL1::drawRepeatedTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src)
{
if(dest.isEmpty() || src.isEmpty() || texture->isEmpty())
return;
setTexture(texture.get());
m_coordsBuffer.clear();
m_coordsBuffer.addRepeatedRects(dest, src);
drawCoords(m_coordsBuffer);
}
void PainterOGL1::drawFilledRect(const Rect& dest)
{
if(dest.isEmpty())
return;
setTexture(nullptr);
m_coordsBuffer.clear();
m_coordsBuffer.addRect(dest);
drawCoords(m_coordsBuffer);
}
void PainterOGL1::drawFilledTriangle(const Point& a, const Point& b, const Point& c)
{
if(a == b || a == c || b == c)
return;
setTexture(nullptr);
m_coordsBuffer.clear();
m_coordsBuffer.addTriangle(a, b, c);
drawCoords(m_coordsBuffer);
}
void PainterOGL1::drawBoundingRect(const Rect& dest, int innerLineWidth)
{
if(dest.isEmpty() || innerLineWidth == 0)
return;
setTexture(nullptr);
m_coordsBuffer.clear();
m_coordsBuffer.addBoudingRect(dest, innerLineWidth);
drawCoords(m_coordsBuffer);
}
void PainterOGL1::setMatrixMode(PainterOGL1::MatrixMode matrixMode)
{
if(m_matrixMode == matrixMode)
return;
m_matrixMode = matrixMode;
updateGlMatrixMode();
}
void PainterOGL1::setTransformMatrix(const Matrix3& transformMatrix)
{
m_transformMatrix = transformMatrix;
if(g_painter == this)
updateGlTransformMatrix();
}
void PainterOGL1::setProjectionMatrix(const Matrix3& projectionMatrix)
{
m_projectionMatrix = projectionMatrix;
if(g_painter == this)
updateGlProjectionMatrix();
}
void PainterOGL1::setTextureMatrix(const Matrix3& textureMatrix)
{
// avoid re-updating texture matrix
if(m_textureMatrix == textureMatrix)
return;
m_textureMatrix = textureMatrix;
updateGlTextureMatrix();
}
void PainterOGL1::setColor(const Color& color)
{
if(m_color == color)
return;
m_color = color;
updateGlColor();
}
void PainterOGL1::setOpacity(float opacity)
{
if(m_opacity == opacity)
return;
m_opacity = opacity;
updateGlColor();
}
void PainterOGL1::updateGlColor()
{
glColor4f(m_color.rF(), m_color.gF(), m_color.bF(), m_color.aF() * m_opacity);
}
void PainterOGL1::updateGlMatrixMode()
{
glMatrixMode(m_matrixMode);
}
void PainterOGL1::updateGlTransformMatrix()
{
float glTransformMatrix[] = {
m_transformMatrix(1,1), m_transformMatrix(1,2), 0.0f, m_transformMatrix(1,3),
m_transformMatrix(2,1), m_transformMatrix(2,2), 0.0f, m_transformMatrix(2,3),
0.0f, 0.0f, 1.0f, 0.0f,
m_transformMatrix(3,1), m_transformMatrix(3,2), 0.0f, m_transformMatrix(3,3),
};
setMatrixMode(MatrixTransform);
glLoadMatrixf(glTransformMatrix);
}
void PainterOGL1::updateGlProjectionMatrix()
{
float glProjectionMatrix[] = {
m_projectionMatrix(1,1), m_projectionMatrix(1,2), 0.0f, m_projectionMatrix(1,3),
m_projectionMatrix(2,1), m_projectionMatrix(2,2), 0.0f, m_projectionMatrix(2,3),
0.0f, 0.0f, 1.0f, 0.0f,
m_projectionMatrix(3,1), m_projectionMatrix(3,2), 0.0f, m_projectionMatrix(3,3),
};
setMatrixMode(MatrixProjection);
glLoadMatrixf(glProjectionMatrix);
}
void PainterOGL1::updateGlTextureMatrix()
{
float glTextureMatrix[] = {
m_textureMatrix(1,1), m_textureMatrix(1,2), 0.0f, 0.0f,
m_textureMatrix(2,1), m_textureMatrix(2,2), 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
m_textureMatrix(3,1), m_textureMatrix(3,2), 0.0f, m_textureMatrix(3,3),
};
setMatrixMode(MatrixTexture);
glLoadMatrixf(glTextureMatrix);
}
void PainterOGL1::updateGlTextureState()
{
if(m_textureEnabled) {
glEnable(GL_TEXTURE_2D);
if(g_graphics.canUseDrawArrays())
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
} else {
glDisable(GL_TEXTURE_2D);
if(g_graphics.canUseDrawArrays())
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
}
#endif

View File

@@ -0,0 +1,84 @@
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
*
* 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.
*/
#ifndef PAINTEROGL1_H
#define PAINTEROGL1_H
#define PAINTER_OGL1
#include "painterogl.h"
/**
* Painter using OpenGL 1.1 fixed-function rendering pipeline,
* compatible with OpenGL ES 1.0 and intended to be used on
* older graphics cards. Shaders are not available
* for this painter engine.
*/
class PainterOGL1 : public PainterOGL
{
public:
enum MatrixMode {
MatrixProjection = 0x1701, //GL_PROJECTION
MatrixTexture = 0x1702, //GL_TEXTURE
MatrixTransform = 0x1700 // GL_MODELVIEW
};
PainterOGL1();
void bind();
void unbind();
void refreshState();
void drawCoords(CoordsBuffer& coordsBuffer, DrawMode drawMode = Triangles);
void drawTextureCoords(CoordsBuffer& coordsBuffer, const TexturePtr& texture);
void drawTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src);
void drawUpsideDownTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src);
void drawRepeatedTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src);
void drawFilledRect(const Rect& dest);
void drawFilledTriangle(const Point& a, const Point& b, const Point& c);
void drawBoundingRect(const Rect& dest, int innerLineWidth);
void setMatrixMode(MatrixMode matrixMode);
void setTransformMatrix(const Matrix3& projectionMatrix);
void setProjectionMatrix(const Matrix3& projectionMatrix);
void setTextureMatrix(const Matrix3& textureMatrix);
void setColor(const Color& color);
void setOpacity(float opacity);
bool hasShaders() { return false; }
private:
void updateGlColor();
void updateGlMatrixMode();
void updateGlProjectionMatrix();
void updateGlTransformMatrix();
void updateGlTextureMatrix();
void updateGlTextureState();
GLenum m_matrixMode;
stdext::boolean<false> m_textureEnabled;
};
extern PainterOGL1 *g_painterOGL1;
#endif

View File

@@ -0,0 +1,206 @@
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
*
* 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 "painterogl2_shadersources.h"
#include <framework/platform/platformwindow.h>
PainterOGL2 *g_painterOGL2 = nullptr;
PainterOGL2::PainterOGL2()
{
m_drawProgram = nullptr;
resetState();
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();
}
void PainterOGL2::bind()
{
PainterOGL::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;
bool textured = coordsBuffer.getTextureCoordCount() > 0 && m_texture;
// skip drawing of empty textures
if(textured && m_texture->isEmpty())
return;
// update shader with the current painter state
m_drawProgram->bind();
m_drawProgram->setTransformMatrix(m_transformMatrix);
m_drawProgram->setProjectionMatrix(m_projectionMatrix);
if(textured) {
m_drawProgram->setTextureMatrix(m_textureMatrix);
m_drawProgram->bindMultiTextures();
}
m_drawProgram->setOpacity(m_opacity);
m_drawProgram->setColor(m_color);
m_drawProgram->setResolution(m_resolution);
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);
} else
PainterShaderProgram::disableAttributeArray(PainterShaderProgram::TEXCOORD_ATTR);
// 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
if(drawMode == Triangles)
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
else if(drawMode == TriangleStrip)
glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexCount);
if(!textured)
PainterShaderProgram::enableAttributeArray(PainterShaderProgram::TEXCOORD_ATTR);
}
void PainterOGL2::drawTextureCoords(CoordsBuffer& coordsBuffer, const TexturePtr& texture)
{
if(texture && texture->isEmpty())
return;
setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawTexturedProgram.get());
setTexture(texture);
drawCoords(coordsBuffer);
}
void PainterOGL2::drawTexturedRect(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.addQuad(dest, src);
drawCoords(m_coordsBuffer, TriangleStrip);
}
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);
}
void PainterOGL2::drawRepeatedTexturedRect(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.addRepeatedRects(dest, src);
drawCoords(m_coordsBuffer);
}
void PainterOGL2::drawFilledRect(const Rect& dest)
{
if(dest.isEmpty())
return;
setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawSolidColorProgram.get());
m_coordsBuffer.clear();
m_coordsBuffer.addRect(dest);
drawCoords(m_coordsBuffer);
}
void PainterOGL2::drawFilledTriangle(const Point& a, const Point& b, const Point& c)
{
if(a == b || a == c || b == c)
return;
setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawSolidColorProgram.get());
m_coordsBuffer.clear();
m_coordsBuffer.addTriangle(a, b, c);
drawCoords(m_coordsBuffer);
}
void PainterOGL2::drawBoundingRect(const Rect& dest, int innerLineWidth)
{
if(dest.isEmpty() || innerLineWidth == 0)
return;
setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawSolidColorProgram.get());
m_coordsBuffer.clear();
m_coordsBuffer.addBoudingRect(dest, innerLineWidth);
drawCoords(m_coordsBuffer);
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
*
* 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.
*/
#ifndef PAINTEROGL2_H
#define PAINTEROGL2_H
#define PAINTER_OGL2
#include "painterogl.h"
/**
* Painter using OpenGL 2.0 programmable rendering pipeline,
* compatible with OpenGL ES 2.0. Only recent cards support
* this painter engine.
*/
class PainterOGL2 : public PainterOGL
{
public:
PainterOGL2();
void bind();
void unbind();
void drawCoords(CoordsBuffer& coordsBuffer, DrawMode drawMode = Triangles);
void drawTextureCoords(CoordsBuffer& coordsBuffer, const TexturePtr& texture);
void drawTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src);
void drawUpsideDownTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src);
void drawRepeatedTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src);
void drawFilledRect(const Rect& dest);
void drawFilledTriangle(const Point& a, const Point& b, const Point& c);
void drawBoundingRect(const Rect& dest, int innerLineWidth = 1);
void setDrawProgram(PainterShaderProgram *drawProgram) { m_drawProgram = drawProgram; }
bool hasShaders() { return true; }
private:
PainterShaderProgram *m_drawProgram;
PainterShaderProgramPtr m_drawTexturedProgram;
PainterShaderProgramPtr m_drawSolidColorProgram;
};
extern PainterOGL2 *g_painterOGL2;
#endif

View File

@@ -0,0 +1,74 @@
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
*
* 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.
*/
#ifndef PAINTEROGL2_SHADERSOURCES_H
#define PAINTEROGL2_SHADERSOURCES_H
static const std::string glslMainVertexShader = "\n\
highp vec4 calculatePosition();\n\
void main() {\n\
gl_Position = calculatePosition();\n\
}\n";
static const std::string glslMainWithTexCoordsVertexShader = "\n\
attribute highp vec2 a_TexCoord;\n\
uniform highp mat3 u_TextureMatrix;\n\
varying highp vec2 v_TexCoord;\n\
highp vec4 calculatePosition();\n\
void main()\n\
{\n\
gl_Position = calculatePosition();\n\
v_TexCoord = (u_TextureMatrix * vec3(a_TexCoord,1.0)).xy;\n\
}\n";
static std::string glslPositionOnlyVertexShader = "\n\
attribute highp vec2 a_Vertex;\n\
uniform highp mat3 u_TransformMatrix;\n\
uniform highp mat3 u_ProjectionMatrix;\n\
highp vec4 calculatePosition() {\n\
return vec4(u_ProjectionMatrix * u_TransformMatrix * vec3(a_Vertex.xy, 1.0), 1.0);\n\
}\n";
static const std::string glslMainFragmentShader = "\n\
uniform lowp float u_Opacity;\n\
lowp vec4 calculatePixel();\n\
void main()\n\
{\n\
gl_FragColor = calculatePixel();\n\
gl_FragColor.a *= u_Opacity;\n\
}\n";
static const std::string glslTextureSrcFragmentShader = "\n\
varying mediump vec2 v_TexCoord;\n\
uniform lowp vec4 u_Color;\n\
uniform sampler2D u_Tex0;\n\
lowp vec4 calculatePixel() {\n\
return texture2D(u_Tex0, v_TexCoord) * u_Color;\n\
}\n";
static const std::string glslSolidColorFragmentShader = "\n\
uniform lowp vec4 u_Color;\n\
lowp vec4 calculatePixel() {\n\
return u_Color;\n\
}\n";
#endif