Conflicts: src/framework/CMakeLists.txt src/framework/application.cpp src/framework/graphics/graphics.cppmaster
commit
2030a4da29
@ -0,0 +1,5 @@
|
||||
function dumpWidgets()
|
||||
for i=1,UI.root:getChildCount() do
|
||||
print(UI.root:getChildByIndex(i):getId())
|
||||
end
|
||||
end
|
@ -1,43 +1,33 @@
|
||||
local eventId = 0
|
||||
local eventsTable = { }
|
||||
local orig = { scheduleEvent = scheduleEvent,
|
||||
addEvent = addEvent }
|
||||
local eventList = {}
|
||||
|
||||
-- fix original scheduleEvent
|
||||
function scheduleEvent(func, delay)
|
||||
eventId = eventId + 1
|
||||
local id = eventId
|
||||
local function proxyFunc()
|
||||
if eventsTable[id] then
|
||||
func()
|
||||
eventsTable[id] = nil
|
||||
if eventList[id] then
|
||||
if eventList[id].active then
|
||||
func()
|
||||
end
|
||||
eventList[id] = nil
|
||||
end
|
||||
end
|
||||
eventsTable[id] = proxyFunc
|
||||
orig.scheduleEvent(proxyFunc, delay)
|
||||
eventList[id] = { func = proxyFunc, active = true }
|
||||
if delay and delay > 0 then
|
||||
g_dispatcher.scheduleEvent(proxyFunc, delay)
|
||||
else
|
||||
g_dispatcher.addEvent(proxyFunc, false)
|
||||
end
|
||||
return id
|
||||
end
|
||||
|
||||
-- FIXME: the event function can be collected
|
||||
-- and the dispatcher would call an invalid function, generating an warning
|
||||
function addEvent(func)
|
||||
return scheduleEvent(func, 0)
|
||||
end
|
||||
|
||||
function removeEvent(id)
|
||||
if id and eventsTable[id] then
|
||||
eventsTable[id] = nil
|
||||
if id and eventList[id] then
|
||||
eventList[id].active = false
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
-- fix original addEvent
|
||||
function addEvent(func)
|
||||
eventId = eventId + 1
|
||||
local id = eventId
|
||||
local function proxyFunc()
|
||||
if eventsTable[id] then
|
||||
func()
|
||||
eventsTable[id] = nil
|
||||
end
|
||||
end
|
||||
eventsTable[id] = proxyFunc
|
||||
orig.addEvent(proxyFunc)
|
||||
return id
|
||||
end
|
@ -0,0 +1,49 @@
|
||||
uniform float opacity;
|
||||
uniform vec4 color;
|
||||
uniform float ticks;
|
||||
|
||||
uniform sampler2D texture; // outfit texture
|
||||
varying vec2 textureCoords; // outfit texture coords
|
||||
uniform sampler2D maskTexture; // outfit color mask
|
||||
|
||||
uniform vec4 headColor;
|
||||
uniform vec4 bodyColor;
|
||||
uniform vec4 legsColor;
|
||||
uniform vec4 feetColor;
|
||||
|
||||
vec4 calcPixel(vec2 texCoord)
|
||||
{
|
||||
vec4 pixel = texture2D(texture, texCoord);
|
||||
vec4 maskColor = texture2D(maskTexture, texCoord);
|
||||
|
||||
vec4 outColor = vec4(0);
|
||||
if(maskColor.r == 1.0 && maskColor.g == 1.0) {
|
||||
outColor = headColor;
|
||||
} else if(maskColor.r == 1.0) {
|
||||
outColor = bodyColor;
|
||||
} else if(maskColor.g == 1.0) {
|
||||
outColor = legsColor;
|
||||
} else if(maskColor.b == 1.0) {
|
||||
outColor = feetColor;
|
||||
}
|
||||
|
||||
if(outColor.a != 0.0)
|
||||
pixel = pixel * outColor;
|
||||
return pixel;
|
||||
}
|
||||
void main()
|
||||
{
|
||||
vec4 pixel = calcPixel(textureCoords);
|
||||
int num = 16;
|
||||
|
||||
vec4 sum = vec4(0);
|
||||
int i, j;
|
||||
for(i=-num/2;i<num/2;++i) {
|
||||
for(j=-num/2;j<num/2;++j) {
|
||||
sum += calcPixel(textureCoords + vec2(i+1, j+1)*0.005) * 1.0/(num*num);
|
||||
}
|
||||
}
|
||||
sum = sin(ticks/500.0)*sum;
|
||||
|
||||
gl_FragColor = pixel * color * opacity + sum;
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
varying vec2 textureCoords;
|
||||
uniform float opacity;
|
||||
uniform vec4 color;
|
||||
uniform float ticks;
|
||||
uniform sampler2D texture;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = texture2D(texture, textureCoords) * color * opacity;
|
||||
}
|
||||
|
||||
/*
|
||||
varying vec2 textureCoords;
|
||||
uniform vec4 color;
|
||||
uniform float ticks;
|
||||
uniform sampler2D texture;
|
||||
|
||||
void main()
|
||||
{
|
||||
int num = 4;
|
||||
vec4 sum = vec4(0);
|
||||
int i, j;
|
||||
for(i=-num/2;i<num/2;++i) {
|
||||
for(j=-num/2;j<num/2;++j) {
|
||||
sum += texture2D(texture, textureCoords + vec2(i+1, j+1)*0.001) * 1.0/(num*num);
|
||||
}
|
||||
}
|
||||
|
||||
float period = ticks/1000.0;
|
||||
float a = (sin(period)+1.0)/2.0;
|
||||
sum.a = 0;
|
||||
gl_FragColor = vec4(1,1,1,2) - texture2D(texture, textureCoords);
|
||||
}
|
||||
*/
|
||||
/*
|
||||
uniform sampler2D texture;
|
||||
varying vec2 textureCoords;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 sum = vec4(0);
|
||||
vec2 texcoord = textureCoords;
|
||||
int j;
|
||||
int i;
|
||||
|
||||
for( i= -4 ;i < 4; i++)
|
||||
{
|
||||
for (j = -3; j < 3; j++)
|
||||
{
|
||||
sum += texture2D(texture, texcoord + vec2(j, i)*0.004) * 0.25;
|
||||
}
|
||||
}
|
||||
if (texture2D(texture, texcoord).r < 0.3)
|
||||
{
|
||||
gl_FragColor = sum*sum*0.012 + texture2D(texture, texcoord);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (texture2D(texture, texcoord).r < 0.5)
|
||||
{
|
||||
gl_FragColor = sum*sum*0.009 + texture2D(texture, texcoord);
|
||||
}
|
||||
else
|
||||
{
|
||||
gl_FragColor = sum*sum*0.0075 + texture2D(texture, texcoord);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
/*
|
||||
uniform sampler2D texture;
|
||||
varying vec2 textureCoords;
|
||||
uniform vec4 color;
|
||||
uniform float opacity;
|
||||
|
||||
uniform float ticks;
|
||||
uniform float rt_w = 18*32;
|
||||
uniform float rt_h = 14*32;
|
||||
|
||||
uniform float radius = 300.0;
|
||||
uniform float angle = 0.2;
|
||||
uniform vec2 center = vec2(8*32, 5*32);
|
||||
|
||||
vec4 PostFX(sampler2D tex, vec2 uv, float time)
|
||||
{
|
||||
vec2 texSize = vec2(rt_w, rt_h);
|
||||
vec2 tc = uv * texSize;
|
||||
tc -= center;
|
||||
float dist = length(tc);
|
||||
if (dist < radius)
|
||||
{
|
||||
float percent = (radius - dist) / radius;
|
||||
float theta = percent * percent * ((int)ticks % 1000)/1000.0 * 8.0;
|
||||
float s = sin(theta);
|
||||
float c = cos(theta);
|
||||
tc = vec2(dot(tc, vec2(c, -s)), dot(tc, vec2(s, c)));
|
||||
}
|
||||
tc += center;
|
||||
vec3 color = texture2D(texture, tc / texSize).rgb;
|
||||
return vec4(color, 1.0);
|
||||
}
|
||||
|
||||
void main (void)
|
||||
{
|
||||
vec2 uv = textureCoords.st;
|
||||
gl_FragColor = PostFX(texture, uv, ticks) * opacity;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
uniform float opacity;
|
||||
vec4 calculatePixel();
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = calculatePixel() * opacity;
|
||||
}
|
||||
|
||||
varying vec2 textureCoords;
|
||||
uniform vec4 color;
|
||||
uniform sampler2D texture;
|
||||
vec4 calculatePixel() {
|
||||
return texture2D(texture, textureCoords) * color;
|
||||
}
|
||||
*/
|
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 "coordsbuffer.h"
|
||||
|
||||
void CoordsBuffer::clear()
|
||||
{
|
||||
m_destRects.reset();
|
||||
m_srcRects.reset();
|
||||
m_textureCoords.clear();
|
||||
m_vertices.clear();
|
||||
m_updateCache = true;
|
||||
}
|
||||
|
||||
void CoordsBuffer::addRect(const Rect& dest)
|
||||
{
|
||||
m_destRects << dest;
|
||||
m_updateCache = true;
|
||||
}
|
||||
|
||||
void CoordsBuffer::addRect(const Rect& dest, const Rect& src)
|
||||
{
|
||||
m_destRects << dest;
|
||||
m_srcRects << src;
|
||||
m_updateCache = true;
|
||||
}
|
||||
|
||||
void CoordsBuffer::addBoudingRect(const Rect& dest, int innerLineWidth)
|
||||
{
|
||||
int left = dest.left();
|
||||
int right = dest.right();
|
||||
int top = dest.top();
|
||||
int bottom = dest.bottom();
|
||||
int width = dest.width();
|
||||
int height = dest.height();
|
||||
int w = innerLineWidth;
|
||||
|
||||
addRect(Rect(left, top, width - w, w)); // top
|
||||
addRect(Rect(right - w + 1, top, w, height - w)); // right
|
||||
addRect(Rect(left + w, bottom - w + 1, width - w, w)); // bottom
|
||||
addRect(Rect(left, top + w, w, height - w)); // left
|
||||
m_updateCache = true;
|
||||
}
|
||||
|
||||
void CoordsBuffer::addRepeatedRects(const Rect& dest, const Rect& src)
|
||||
{
|
||||
if(dest.isEmpty() || src.isEmpty())
|
||||
return;
|
||||
|
||||
Rect virtualDest(0, 0, dest.size());
|
||||
for(int y = 0; y <= virtualDest.height(); y += src.height()) {
|
||||
for(int x = 0; x <= virtualDest.width(); x += src.width()) {
|
||||
Rect partialDest(x, y, src.size());
|
||||
Rect partialSrc(src);
|
||||
|
||||
// partialCoords to screenCoords bottomRight
|
||||
if(partialDest.bottom() > virtualDest.bottom()) {
|
||||
partialSrc.setBottom(partialSrc.bottom() + (virtualDest.bottom() - partialDest.bottom()));
|
||||
partialDest.setBottom(virtualDest.bottom());
|
||||
}
|
||||
if(partialDest.right() > virtualDest.right()) {
|
||||
partialSrc.setRight(partialSrc.right() + (virtualDest.right() - partialDest.right()));
|
||||
partialDest.setRight(virtualDest.right());
|
||||
}
|
||||
|
||||
partialDest.translate(dest.topLeft());
|
||||
m_destRects << partialDest;
|
||||
m_srcRects << partialSrc;
|
||||
}
|
||||
}
|
||||
m_updateCache = true;
|
||||
}
|
||||
|
||||
void CoordsBuffer::cacheVertexArrays()
|
||||
{
|
||||
if(!m_updateCache)
|
||||
return;
|
||||
|
||||
int numDestRects = m_destRects.size();
|
||||
int numSrcRects = m_srcRects.size();
|
||||
m_vertices.clear();
|
||||
m_textureCoords.clear();
|
||||
|
||||
for(int i=0;i<numDestRects;++i) {
|
||||
m_vertices.addRect(m_destRects[i]);
|
||||
if(numSrcRects == numDestRects)
|
||||
m_textureCoords.addRect(m_srcRects[i]);
|
||||
}
|
||||
|
||||
m_updateCache = false;
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 COORDSBUFFER_H
|
||||
#define COORDSBUFFER_H
|
||||
|
||||
#include "vertexarray.h"
|
||||
|
||||
class CoordsBuffer
|
||||
{
|
||||
public:
|
||||
void clear();
|
||||
|
||||
// no texture
|
||||
void addRect(const Rect& dest);
|
||||
void addBoudingRect(const Rect& dest, int innerLineWidth);
|
||||
|
||||
// textured
|
||||
void addRect(const Rect& dest, const Rect& src);
|
||||
void addRepeatedRects(const Rect& dest, const Rect& src);
|
||||
|
||||
void cacheVertexArrays();
|
||||
|
||||
GLfloat *getVertices() const { return m_vertices.vertices(); }
|
||||
GLfloat *getTextureCoords() const { return m_textureCoords.vertices(); }
|
||||
int getVertexCount() const { return m_vertices.vertexCount(); }
|
||||
int getTextureCoordsCount() const { return m_textureCoords.vertexCount(); }
|
||||
|
||||
private:
|
||||
DataBuffer<Rect> m_destRects;
|
||||
DataBuffer<Rect> m_srcRects;
|
||||
VertexArray m_vertices;
|
||||
VertexArray m_textureCoords;
|
||||
Boolean<true> m_updateCache;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 "glbuffer.h"
|
||||
|
||||
GLBuffer::GLBuffer()
|
||||
{
|
||||
glGenBuffers(1, &m_id);
|
||||
if(!m_id)
|
||||
logFatal("Unable to create a simple GL buffer");
|
||||
}
|
||||
|
||||
void GLBuffer::bind()
|
||||
{
|
||||
gl
|
||||
}
|
||||
|
||||
void GLBuffer::release()
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 GLBUFFER_H
|
||||
#define GLBUFFER_H
|
||||
|
||||
#include "declarations.h"
|
||||
|
||||
class GLBuffer
|
||||
{
|
||||
public:
|
||||
GLBuffer();
|
||||
~GLBuffer();
|
||||
|
||||
void write(const
|
||||
void bind();
|
||||
void release();
|
||||
|
||||
GLuint bufferId();
|
||||
|
||||
private:
|
||||
GLuint m_id;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 "painter.h"
|
||||
#include "texture.h"
|
||||
#include "paintershadersources.h"
|
||||
#include "paintershaderprogram.h"
|
||||
#include "shaderprogram.h"
|
||||
#include "graphics.h"
|
||||
#include "vertexarray.h"
|
||||
|
||||
Painter g_painter;
|
||||
|
||||
void Painter::init()
|
||||
{
|
||||
setColor(Fw::white);
|
||||
setOpacity(255);
|
||||
setCompositionMode(CompositionMode_SourceOver);
|
||||
|
||||
m_drawTexturedProgram = PainterShaderProgramPtr(new PainterShaderProgram);
|
||||
m_drawTexturedProgram->addShaderFromSourceCode(Shader::Vertex, glslMainWithTexCoordsVertexShader + glslPositionOnlyVertexShader);
|
||||
m_drawTexturedProgram->addShaderFromSourceCode(Shader::Fragment, glslMainFragmentShader + glslTextureSrcFragmentShader);
|
||||
assert(m_drawTexturedProgram->link());
|
||||
|
||||
m_drawSolidColorProgram = PainterShaderProgramPtr(new PainterShaderProgram);
|
||||
m_drawSolidColorProgram->addShaderFromSourceCode(Shader::Vertex, glslMainVertexShader + glslPositionOnlyVertexShader);
|
||||
m_drawSolidColorProgram->addShaderFromSourceCode(Shader::Fragment, glslMainFragmentShader + glslSolidColorFragmentShader);
|
||||
assert(m_drawSolidColorProgram->link());
|
||||
}
|
||||
|
||||
void Painter::terminate()
|
||||
{
|
||||
m_drawTexturedProgram.reset();
|
||||
m_drawSolidColorProgram.reset();
|
||||
}
|
||||
|
||||
void Painter::updateProjectionMatrix(const Size& viewportSize, bool inverseYAxis)
|
||||
{
|
||||
// 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, which is multiplied by the painter's
|
||||
// transformation matrix, as shown below:
|
||||
//
|
||||
// Projection Matrix Painter Coord GL Coord
|
||||
// ------------------------------------------------ --------- ---------
|
||||
// | 2.0 / width | 0.0 | -1.0 | | x | | y' |
|
||||
// | 0.0 | -2.0 / height | 1.0 | * | y | = | x' |
|
||||
// | 0.0 | 0.0 | 0.0 | | 1 | | 0 |
|
||||
// ------------------------------------------------ --------- ---------
|
||||
float w = viewportSize.width();
|
||||
float h = viewportSize.height();
|
||||
if(inverseYAxis) {
|
||||
m_projectionMatrix[0][0] = 2.0f/w; m_projectionMatrix[0][1] = 0.0f; m_projectionMatrix[0][2] =-1.0f;
|
||||
m_projectionMatrix[1][0] = 0.0f; m_projectionMatrix[1][1] = 2.0f/h; m_projectionMatrix[1][2] =-1.0f;
|
||||
m_projectionMatrix[2][0] = 0.0f; m_projectionMatrix[2][1] = 0.0f; m_projectionMatrix[2][2] = 0.0f;
|
||||
} else {
|
||||
m_projectionMatrix[0][0] = 2.0f/w; m_projectionMatrix[0][1] = 0.0f; m_projectionMatrix[0][2] =-1.0f;
|
||||
m_projectionMatrix[1][0] = 0.0f; m_projectionMatrix[1][1] =-2.0f/h; m_projectionMatrix[1][2] = 1.0f;
|
||||
m_projectionMatrix[2][0] = 0.0f; m_projectionMatrix[2][1] = 0.0f; m_projectionMatrix[2][2] = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void Painter::drawProgram(const PainterShaderProgramPtr& program, CoordsBuffer& coordsBuffer, PainterShaderProgram::DrawMode drawMode)
|
||||
{
|
||||
coordsBuffer.cacheVertexArrays();
|
||||
if(coordsBuffer.getVertexCount() == 0)
|
||||
return;
|
||||
|
||||
program->setProjectionMatrix(m_projectionMatrix);
|
||||
program->setOpacity(m_currentOpacity);
|
||||
program->setColor(m_currentColor);
|
||||
program->draw(coordsBuffer, drawMode);
|
||||
}
|
||||
|
||||
void Painter::drawTextureCoords(CoordsBuffer& coordsBuffer, const TexturePtr& texture)
|
||||
{
|
||||
PainterShaderProgramPtr program = m_customProgram ? m_customProgram : m_drawTexturedProgram;
|
||||
program->setTexture(texture);
|
||||
drawProgram(program, coordsBuffer);
|
||||
}
|
||||
|
||||
void Painter::drawTexturedRect(const Rect& dest, const TexturePtr& texture)
|
||||
{
|
||||
drawTexturedRect(dest, texture, Rect(Point(0,0), texture->getSize()));
|
||||
}
|
||||
|
||||
void Painter::drawTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src)
|
||||
{
|
||||
if(dest.isEmpty() || src.isEmpty() || !texture->getId())
|
||||
return;
|
||||
|
||||
m_coordsBuffer.clear();
|
||||
m_coordsBuffer.addRect(dest, src);
|
||||
drawTextureCoords(m_coordsBuffer, texture);
|
||||
}
|
||||
|
||||
void Painter::drawRepeatedTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src)
|
||||
{
|
||||
if(dest.isEmpty() || src.isEmpty() || !texture->getId())
|
||||
return;
|
||||
|
||||
m_coordsBuffer.clear();
|
||||
m_coordsBuffer.addRepeatedRects(dest, src);
|
||||
drawTextureCoords(m_coordsBuffer, texture);
|
||||
}
|
||||
|
||||
void Painter::drawFilledRect(const Rect& dest)
|
||||
{
|
||||
if(dest.isEmpty())
|
||||
return;
|
||||
|
||||
m_coordsBuffer.clear();
|
||||
m_coordsBuffer.addRect(dest);
|
||||
drawProgram(m_customProgram ? m_customProgram : m_drawSolidColorProgram, m_coordsBuffer);
|
||||
}
|
||||
|
||||
void Painter::drawBoundingRect(const Rect& dest, int innerLineWidth)
|
||||
{
|
||||
if(dest.isEmpty() || innerLineWidth == 0)
|
||||
return;
|
||||
|
||||
m_coordsBuffer.clear();
|
||||
m_coordsBuffer.addBoudingRect(dest, innerLineWidth);
|
||||
drawProgram(m_customProgram ? m_customProgram : m_drawSolidColorProgram, m_coordsBuffer);
|
||||
}
|
||||
|
||||
void Painter::setCustomProgram(PainterShaderProgramPtr program)
|
||||
{
|
||||
m_customProgram = program;
|
||||
}
|
||||
|
||||
void Painter::setCompositionMode(Painter::CompositionMode compositionMode)
|
||||
{
|
||||
switch(compositionMode) {
|
||||
case CompositionMode_SourceOver:
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
break;
|
||||
case CompositionMode_ColorizeSource:
|
||||
glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);
|
||||
break;
|
||||
case CompositionMode_AdditiveSource:
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
||||
break;
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 PAINTER_H
|
||||
#define PAINTER_H
|
||||
|
||||
#include "declarations.h"
|
||||
#include <framework/util/databuffer.h>
|
||||
#include "coordsbuffer.h"
|
||||
#include "paintershaderprogram.h"
|
||||
|
||||
class Painter
|
||||
{
|
||||
public:
|
||||
enum CompositionMode {
|
||||
CompositionMode_SourceOver,
|
||||
CompositionMode_ColorizeSource,
|
||||
CompositionMode_AdditiveSource
|
||||
};
|
||||
|
||||
void init();
|
||||
void terminate();
|
||||
|
||||
void updateProjectionMatrix(const Size& viewportSize, bool inverseYAxis = false);
|
||||
|
||||
void drawProgram(const PainterShaderProgramPtr& program, CoordsBuffer& coordsBuffer, PainterShaderProgram::DrawMode drawMode = PainterShaderProgram::Triangles);
|
||||
void drawTextureCoords(CoordsBuffer& coordsBuffer, const TexturePtr& texture);
|
||||
void drawTexturedRect(const Rect& dest, const TexturePtr& texture);
|
||||
void drawTexturedRect(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 drawBoundingRect(const Rect& dest, int innerLineWidth = 1);
|
||||
|
||||
void setColor(const Color& color) { m_currentColor = color; }
|
||||
Color getColor() { return m_currentColor; }
|
||||
|
||||
void setOpacity(int opacity) { m_currentOpacity = opacity / 255.0f; }
|
||||
int getOpacity() { return m_currentOpacity * 255.0f; }
|
||||
|
||||
void setCustomProgram(PainterShaderProgramPtr program);
|
||||
void releaseCustomProgram() { m_customProgram = nullptr; }
|
||||
void setCompositionMode(CompositionMode compositionMode);
|
||||
|
||||
GLfloat *getProjectionMatrix() { return (GLfloat*)m_projectionMatrix; }
|
||||
|
||||
private:
|
||||
PainterShaderProgramPtr m_drawTexturedProgram;
|
||||
PainterShaderProgramPtr m_drawSolidColorProgram;
|
||||
PainterShaderProgramPtr m_customProgram;
|
||||
GLfloat m_projectionMatrix[3][3];
|
||||
Color m_currentColor;
|
||||
GLfloat m_currentOpacity;
|
||||
CoordsBuffer m_coordsBuffer;
|
||||
};
|
||||
|
||||
extern Painter g_painter;
|
||||
|
||||
#endif
|
@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 "paintershaderprogram.h"
|
||||
#include "painter.h"
|
||||
#include "texture.h"
|
||||
#include "texturemanager.h"
|
||||
#include <framework/core/clock.h>
|
||||
|
||||
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");
|
||||
bindUniformLocation(TICKS_UNIFORM, "ticks");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void PainterShaderProgram::setProjectionMatrix(float projectionMatrix[3][3])
|
||||
{
|
||||
bind();
|
||||
setUniformValue(PROJECTION_MATRIX_UNIFORM, projectionMatrix, true);
|
||||
}
|
||||
|
||||
void PainterShaderProgram::setColor(const Color& color)
|
||||
{
|
||||
bind();
|
||||
setUniformValue(COLOR_UNIFORM, color);
|
||||
}
|
||||
|
||||
void PainterShaderProgram::setOpacity(GLfloat opacity)
|
||||
{
|
||||
bind();
|
||||
setUniformValue(OPACITY_UNIFORM, opacity);
|
||||
}
|
||||
|
||||
void PainterShaderProgram::setUniformTexture(int location, const TexturePtr& texture, int index)
|
||||
{
|
||||
if(!texture)
|
||||
return;
|
||||
glActiveTexture(GL_TEXTURE0 + index);
|
||||
glBindTexture(GL_TEXTURE_2D, texture->getId());
|
||||
setUniformValue(location, index);
|
||||
}
|
||||
|
||||
void PainterShaderProgram::setTexture(const TexturePtr& texture)
|
||||
{
|
||||
if(!texture)
|
||||
return;
|
||||
|
||||
float w = texture->getGlSize().width();
|
||||
float h = texture->getGlSize().height();
|
||||
|
||||
GLfloat textureTransformMatrix[2][2] = {
|
||||
{ 1.0f/w, 0.0f },
|
||||
{ 0.0f, 1.0f/h }
|
||||
};
|
||||
|
||||
bind();
|
||||
setUniformTexture(TEXTURE_UNIFORM, texture, 0);
|
||||
setUniformValue(TEXTURE_TRANSFORM_MATRIX_UNIFORM, textureTransformMatrix, true);
|
||||
}
|
||||
|
||||
void PainterShaderProgram::draw(const CoordsBuffer& coordsBuffer, DrawMode drawMode)
|
||||
{
|
||||
assert(bind());
|
||||
|
||||
setUniformValue(TICKS_UNIFORM, (GLfloat)g_clock.ticks());
|
||||
|
||||
int numVertices = coordsBuffer.getVertexCount();
|
||||
if(numVertices == 0)
|
||||
return;
|
||||
|
||||
bool mustDisableVertexArray = false;
|
||||
if(coordsBuffer.getVertexCount() > 0) {
|
||||
enableAttributeArray(PainterShaderProgram::VERTEX_COORDS_ATTR);
|
||||
setAttributeArray(PainterShaderProgram::VERTEX_COORDS_ATTR, coordsBuffer.getVertices(), 2);
|
||||
mustDisableVertexArray = true;
|
||||
}
|
||||
|
||||
bool mustDisableTexCoordsArray = false;
|
||||
if(coordsBuffer.getTextureCoords() > 0) {
|
||||
enableAttributeArray(PainterShaderProgram::TEXTURE_COORDS_ATTR);
|
||||
setAttributeArray(PainterShaderProgram::TEXTURE_COORDS_ATTR, coordsBuffer.getTextureCoords(), 2);
|
||||
mustDisableTexCoordsArray = true;
|
||||
}
|
||||
|
||||
glDrawArrays(drawMode, 0, numVertices);
|
||||
|
||||
if(mustDisableVertexArray)
|
||||
disableAttributeArray(PainterShaderProgram::VERTEX_COORDS_ATTR);
|
||||
|
||||
if(mustDisableTexCoordsArray)
|
||||
disableAttributeArray(PainterShaderProgram::TEXTURE_COORDS_ATTR);
|
||||
|
||||
//release();
|
||||
}
|
||||
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 PAINTERSHADER_H
|
||||
#define PAINTERSHADER_H
|
||||
|
||||
#include "shaderprogram.h"
|
||||
#include "coordsbuffer.h"
|
||||
|
||||
class PainterShaderProgram : public ShaderProgram
|
||||
{
|
||||
enum {
|
||||
VERTEX_COORDS_ATTR = 0,
|
||||
TEXTURE_COORDS_ATTR = 1,
|
||||
PROJECTION_MATRIX_UNIFORM = 0,
|
||||
TEXTURE_TRANSFORM_MATRIX_UNIFORM = 1,
|
||||
COLOR_UNIFORM = 2,
|
||||
OPACITY_UNIFORM = 3,
|
||||
TEXTURE_UNIFORM = 4,
|
||||
TICKS_UNIFORM = 5
|
||||
};
|
||||
public:
|
||||
enum DrawMode {
|
||||
Triangles = GL_TRIANGLES,
|
||||
TriangleStrip = GL_TRIANGLE_STRIP
|
||||
};
|
||||
|
||||
bool link();
|
||||
|
||||
void setProjectionMatrix(GLfloat projectionMatrix[3][3]);
|
||||
void setColor(const Color& color);
|
||||
void setOpacity(GLfloat opacity);
|
||||
void setTexture(const TexturePtr& texture);
|
||||
void setUniformTexture(int location, const TexturePtr& texture, int index);
|
||||
void draw(const CoordsBuffer& coordsBuffer, DrawMode drawMode = Triangles);
|
||||
|
||||
private:
|
||||
DrawMode m_drawMode;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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.
|
||||
*/
|
||||
|
||||
static int VERTEX_COORDS_ATTR = 0;
|
||||
static int TEXTURE_COORDS_ATTR = 1;
|
||||
|
||||
static int PROJECTION_MATRIX_UNIFORM = 0;
|
||||
static int TEXTURE_TRANSFORM_MATRIX_UNIFORM = 1;
|
||||
static int COLOR_UNIFORM = 2;
|
||||
static int OPACITY_UNIFORM = 3;
|
||||
static int TEXTURE_UNIFORM = 4;
|
||||
|
||||
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 textureCoord;\n\
|
||||
uniform highp mat2 textureTransformMatrix;\n\
|
||||
varying highp vec2 textureCoords;\n\
|
||||
highp vec4 calculatePosition();\n\
|
||||
void main()\n\
|
||||
{\n\
|
||||
gl_Position = calculatePosition();\n\
|
||||
textureCoords = textureTransformMatrix * textureCoord;\n\
|
||||
}\n";
|
||||
|
||||
static std::string glslPositionOnlyVertexShader = "\n\
|
||||
attribute highp vec2 vertexCoord;\n\
|
||||
uniform highp mat3 projectionMatrix;\n\
|
||||
highp vec4 calculatePosition() {\n\
|
||||
return vec4(projectionMatrix * vec3(vertexCoord.xy, 1), 1);\n\
|
||||
}\n";
|
||||
|
||||
static const std::string glslMainFragmentShader = "\n\
|
||||
uniform lowp float opacity;\n\
|
||||
lowp vec4 calculatePixel();\n\
|
||||
void main()\n\
|
||||
{\n\
|
||||
gl_FragColor = calculatePixel() * opacity;\n\
|
||||
}\n";
|
||||
|
||||
static const std::string glslTextureSrcFragmentShader = "\n\
|
||||
varying mediump vec2 textureCoords;\n\
|
||||
uniform lowp vec4 color;\n\
|
||||
uniform sampler2D texture;\n\
|
||||
lowp vec4 calculatePixel() {\n\
|
||||
return texture2D(texture, textureCoords) * color;\n\
|
||||
}\n";
|
||||
|
||||
static const std::string glslSolidColorFragmentShader = "\n\
|
||||
uniform lowp vec4 color;\n\
|
||||
lowp vec4 calculatePixel() {\n\
|
||||
return color;\n\
|
||||
}\n";
|
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 "shader.h"
|
||||
#include <framework/core/resourcemanager.h>
|
||||
|
||||
Shader::Shader(Shader::ShaderType shaderType)
|
||||
{
|
||||
m_shaderType = shaderType;
|
||||
switch(shaderType) {
|
||||
case Vertex:
|
||||
m_shaderId = glCreateShader(GL_VERTEX_SHADER);
|
||||
break;
|
||||
case Fragment:
|
||||
m_shaderId = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
break;
|
||||
}
|
||||
|
||||
if(!m_shaderId)
|
||||
logFatal("Unable to create GL shader");
|
||||
}
|
||||
|
||||
Shader::~Shader()
|
||||
{
|
||||
glDeleteShader(m_shaderId);
|
||||
}
|
||||
|
||||
bool Shader::compileSourceCode(const std::string& sourceCode)
|
||||
{
|
||||
#ifndef OPENGL_ES2
|
||||
static const char *qualifierDefines =
|
||||
"#define lowp\n"
|
||||
"#define mediump\n"
|
||||
"#define highp\n";
|
||||
#else
|
||||
static const char *qualifierDefines =
|
||||
"#ifndef GL_FRAGMENT_PRECISION_HIGH\n"
|
||||
"#define highp mediump\n"
|
||||
"#endif\n";
|
||||
#endif
|
||||
|
||||
std::string code = qualifierDefines;
|
||||
code.append(sourceCode);
|
||||
const char *c_source = code.c_str();
|
||||
glShaderSource(m_shaderId, 1, &c_source, NULL);
|
||||
glCompileShader(m_shaderId);
|
||||
|
||||
int res;
|
||||
glGetShaderiv(m_shaderId, GL_COMPILE_STATUS, &res);
|
||||
return (res == GL_TRUE);
|
||||
}
|
||||
|
||||
bool Shader::compileSourceFile(const std::string& sourceFile)
|
||||
{
|
||||
std::string sourceCode = g_resources.loadFile(sourceFile);
|
||||
return compileSourceCode(sourceCode);
|
||||
}
|
||||
|
||||
std::string Shader::log()
|
||||
{
|
||||
std::string infoLog;
|
||||
GLint infoLogLength;
|
||||
glGetShaderiv(m_shaderId, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||
if(infoLogLength > 1) {
|
||||
std::vector<char> buf(infoLogLength);
|
||||
glGetShaderInfoLog(m_shaderId, infoLogLength-1, NULL, &buf[0]);
|
||||
infoLog = &buf[0];
|
||||
}
|
||||
return infoLog;
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 SHADER_H
|
||||
#define SHADER_H
|
||||
|
||||
#include "declarations.h"
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
enum ShaderType {
|
||||
Vertex,
|
||||
Fragment
|
||||
};
|
||||
|
||||
Shader(ShaderType shaderType);
|
||||
~Shader();
|
||||
|
||||
bool compileSourceCode(const std::string& sourceCode);
|
||||
bool compileSourceFile(const std::string& sourceFile);
|
||||
std::string log();
|
||||
|
||||
GLuint getShaderId() { return m_shaderId; }
|
||||
ShaderType getShaderType() { return m_shaderType; }
|
||||
|
||||
private:
|
||||
GLuint m_shaderId;
|
||||
ShaderType m_shaderType;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 "shaderprogram.h"
|
||||
|
||||
GLuint ShaderProgram::m_currentProgram = 0;
|
||||
|
||||
ShaderProgram::ShaderProgram()
|
||||
{
|
||||
m_linked = false;
|
||||
m_programId = glCreateProgram();
|
||||
m_uniformLocations.fill(-1);
|
||||
if(!m_programId)
|
||||
logFatal("Unable to create GL shader program");
|
||||
}
|
||||
|
||||
ShaderProgram::~ShaderProgram()
|
||||
{
|
||||
glDeleteProgram(m_programId);
|
||||
}
|
||||
|
||||
bool ShaderProgram::addShader(const ShaderPtr& shader) {
|
||||
glAttachShader(m_programId, shader->getShaderId());
|
||||
m_linked = false;
|
||||
m_shaders.push_back(shader);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderProgram::addShaderFromSourceCode(Shader::ShaderType shaderType, const std::string& sourceCode) {
|
||||
ShaderPtr shader(new Shader(shaderType));
|
||||
if(!shader->compileSourceCode(sourceCode)) {
|
||||
logError("failed to compile shader: ", shader->log());
|
||||
return false;
|
||||
}
|
||||
return addShader(shader);
|
||||
}
|
||||
|
||||
bool ShaderProgram::addShaderFromSourceFile(Shader::ShaderType shaderType, const std::string& sourceFile) {
|
||||
ShaderPtr shader(new Shader(shaderType));
|
||||
if(!shader->compileSourceFile(sourceFile)) {
|
||||
logError("failed to compile shader: ", shader->log());
|
||||
return false;
|
||||
}
|
||||
return addShader(shader);
|
||||
}
|
||||
|
||||
void ShaderProgram::removeShader(const ShaderPtr& shader)
|
||||
{
|
||||
auto it = std::find(m_shaders.begin(), m_shaders.end(), shader);
|
||||
if(it == m_shaders.end())
|
||||
return;
|
||||
|
||||
glDetachShader(m_programId, shader->getShaderId());
|
||||
m_shaders.erase(it);
|
||||
m_linked = false;
|
||||
}
|
||||
|
||||
void ShaderProgram::removeAllShaders()
|
||||
{
|
||||
while(!m_shaders.empty())
|
||||
removeShader(m_shaders.front());
|
||||
}
|
||||
|
||||
bool ShaderProgram::link()
|
||||
{
|
||||
if(m_linked)
|
||||
return true;
|
||||
|
||||
glLinkProgram(m_programId);
|
||||
|
||||
GLint value;
|
||||
glGetProgramiv(m_programId, GL_LINK_STATUS, &value);
|
||||
m_linked = (value != GL_FALSE);
|
||||
|
||||
if(!m_linked)
|
||||
logTraceWarning(log());
|
||||
return m_linked;
|
||||
}
|
||||
|
||||
bool ShaderProgram::bind()
|
||||
{
|
||||
if(m_currentProgram != m_programId) {
|
||||
if(!m_linked && !link())
|
||||
return false;
|
||||
glUseProgram(m_programId);
|
||||
m_currentProgram = m_programId;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void ShaderProgram::release()
|
||||
{
|
||||
m_currentProgram = 0;
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
std::string ShaderProgram::log()
|
||||
{
|
||||
std::string infoLog;
|
||||
GLint infoLogLength;
|
||||
glGetProgramiv(m_programId, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||
if(infoLogLength > 1) {
|
||||
std::vector<char> buf(infoLogLength);
|
||||
glGetShaderInfoLog(m_programId, infoLogLength-1, NULL, &buf[0]);
|
||||
infoLog = &buf[0];
|
||||
}
|
||||
return infoLog;
|
||||
}
|
||||
|
||||
int ShaderProgram::getAttributeLocation(const char* name)
|
||||
{
|
||||
return glGetAttribLocation(m_programId, name);
|
||||
}
|
||||
|
||||
void ShaderProgram::bindAttributeLocation(int location, const char* name)
|
||||
{
|
||||
return glBindAttribLocation(m_programId, location, name);
|
||||
}
|
||||
|
||||
void ShaderProgram::bindUniformLocation(int location, const char* name)
|
||||
{
|
||||
assert(m_linked);
|
||||
assert(location >= 0 && location < MAX_UNIFORM_LOCATIONS);
|
||||
m_uniformLocations[location] = glGetUniformLocation(m_programId, name);
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 SHADERPROGRAM_H
|
||||
#define SHADERPROGRAM_H
|
||||
|
||||
#include "shader.h"
|
||||
|
||||
class ShaderProgram
|
||||
{
|
||||
enum {
|
||||
MAX_UNIFORM_LOCATIONS = 30
|
||||
};
|
||||
public:
|
||||
ShaderProgram();
|
||||
~ShaderProgram();
|
||||
|
||||
bool addShader(const ShaderPtr& shader);
|
||||
bool addShaderFromSourceCode(Shader::ShaderType shaderType, const std::string& sourceCode);
|
||||
bool addShaderFromSourceFile(Shader::ShaderType shaderType, const std::string& sourceFile);
|
||||
void removeShader(const ShaderPtr& shader);
|
||||
void removeAllShaders();
|
||||
virtual bool link();
|
||||
bool bind();
|
||||
void release();
|
||||
std::string log();
|
||||
|
||||
void disableAttributeArray(int location) { glDisableVertexAttribArray(location); }
|
||||
void enableAttributeArray(int location) { glEnableVertexAttribArray(location); }
|
||||
void disableAttributeArray(const char *name) { glDisableVertexAttribArray(getAttributeLocation(name)); }
|
||||
void enableAttributeArray(const char *name) { glEnableVertexAttribArray(getAttributeLocation(name)); }
|
||||
|
||||
int getAttributeLocation(const char *name);
|
||||
void bindAttributeLocation(int location, const char *name);
|
||||
void bindUniformLocation(int location, const char *name);
|
||||
|
||||
void setAttributeArray(int location, const GLfloat *values, int size, int stride = 0) { glVertexAttribPointer(location, size, GL_FLOAT, GL_FALSE, stride, values); }
|
||||
void setAttributeValue(int location, GLfloat value) { glVertexAttrib1f(location, value); }
|
||||
void setAttributeValue(int location, GLfloat x, GLfloat y) { glVertexAttrib2f(location, x, y); }
|
||||
void setAttributeValue(int location, GLfloat x, GLfloat y, GLfloat z) { glVertexAttrib3f(location, x, y, z); }
|
||||
void setAttributeArray(const char *name, const GLfloat *values, int size, int stride = 0) { glVertexAttribPointer(getAttributeLocation(name), size, GL_FLOAT, GL_FALSE, stride, values); }
|
||||
void setAttributeValue(const char *name, GLfloat value) { glVertexAttrib1f(getAttributeLocation(name), value); }
|
||||
void setAttributeValue(const char *name, GLfloat x, GLfloat y) { glVertexAttrib2f(getAttributeLocation(name), x, y); }
|
||||
void setAttributeValue(const char *name, GLfloat x, GLfloat y, GLfloat z) { glVertexAttrib3f(getAttributeLocation(name), x, y, z); }
|
||||
|
||||
void setUniformValue(int location, const Color& color) { glUniform4f(m_uniformLocations[location], color.r() / 255.0f, color.g() / 255.0f, color.b() / 255.0f, color.a() / 255.0f); }
|
||||
void setUniformValue(int location, GLint value) { glUniform1i(m_uniformLocations[location], value); }
|
||||
void setUniformValue(int location, GLfloat value) { glUniform1f(m_uniformLocations[location], value); }
|
||||
void setUniformValue(int location, GLfloat x, GLfloat y) { glUniform2f(m_uniformLocations[location], x, y); }
|
||||
void setUniformValue(int location, GLfloat x, GLfloat y, GLfloat z) { glUniform3f(m_uniformLocations[location], x, y, z); }
|
||||
void setUniformValue(int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w) { glUniform4f(m_uniformLocations[location], x, y, z, w); }
|
||||
void setUniformValue(int location, GLfloat mat2[2][2], bool transpose) { glUniformMatrix2fv(m_uniformLocations[location], 1, transpose ? GL_TRUE : GL_FALSE, (GLfloat *)mat2); }
|
||||
void setUniformValue(int location, GLfloat mat3[3][3], bool transpose) { glUniformMatrix3fv(m_uniformLocations[location], 1, transpose ? GL_TRUE : GL_FALSE, (GLfloat *)mat3); }
|
||||
void setUniformValue(const char *name, const Color& color) { glUniform4f(glGetUniformLocation(m_programId, name), color.r() / 255.0f, color.g() / 255.0f, color.b() / 255.0f, color.a() / 255.0f); }
|
||||
void setUniformValue(const char *name, GLint value) { glUniform1i(glGetUniformLocation(m_programId, name), value); }
|
||||
void setUniformValue(const char *name, GLfloat value) { glUniform1f(glGetUniformLocation(m_programId, name), value); }
|
||||
void setUniformValue(const char *name, GLfloat x, GLfloat y) { glUniform2f(glGetUniformLocation(m_programId, name), x, y); }
|
||||
void setUniformValue(const char *name, GLfloat x, GLfloat y, GLfloat z) { glUniform3f(glGetUniformLocation(m_programId, name), x, y, z); }
|
||||
void setUniformValue(const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w) { glUniform4f(glGetUniformLocation(m_programId, name), x, y, z, w); }
|
||||
void setUniformValue(const char *name, GLfloat mat2[2][2], bool transpose = false) { glUniformMatrix2fv(glGetUniformLocation(m_programId, name), 1, transpose ? GL_TRUE : GL_FALSE, (GLfloat *)mat2); }
|
||||
void setUniformValue(const char *name, GLfloat mat3[3][3], bool transpose = false) { glUniformMatrix3fv(glGetUniformLocation(m_programId, name), 1, transpose ? GL_TRUE : GL_FALSE, (GLfloat *)mat3); }
|
||||
|
||||
// Point, PointF, Color, Size, SizeF,
|
||||
bool isLinked() { return m_linked; }
|
||||
GLuint getProgramId() { return m_programId; }
|
||||
ShaderList getShaders() { return m_shaders; }
|
||||
|
||||
private:
|
||||
bool m_linked;
|
||||
GLuint m_programId;
|
||||
static GLuint m_currentProgram;
|
||||
ShaderList m_shaders;
|
||||
std::array<GLint, MAX_UNIFORM_LOCATIONS> m_uniformLocations;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 "textureglyphcache.h"
|
||||
|
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 TEXTUREGLYPHCACHE_H
|
||||
#define TEXTUREGLYPHCACHE_H
|
||||
|
||||
class TextureGlyphCache
|
||||
{
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 VERTEXARRAY_H
|
||||
#define VERTEXARRAY_H
|
||||
|
||||
#include "declarations.h"
|
||||
#include <framework/util/databuffer.h>
|
||||
|
||||
class VertexArray
|
||||
{
|
||||
public:
|
||||
inline void addVertex(GLfloat x, GLfloat y) { m_buffer << x << y; }
|
||||
inline void addRect(const Rect& rect) {
|
||||
GLfloat top = rect.top();
|
||||
GLfloat right = rect.right()+1;
|
||||
GLfloat bottom = rect.bottom()+1;
|
||||
GLfloat left = rect.left();
|
||||
|
||||
addVertex(left, top);
|
||||
addVertex(right, top);
|
||||
addVertex(left, bottom);
|
||||
addVertex(left, bottom);
|
||||
addVertex(right, top);
|
||||
addVertex(right, bottom);
|
||||
}
|
||||
|
||||
inline void addQuad(const Rect& rect) {
|
||||
GLfloat top = rect.top();
|
||||
GLfloat right = rect.right()+1;
|
||||
GLfloat bottom = rect.bottom()+1;
|
||||
GLfloat left = rect.left();
|
||||
|
||||
addVertex(left, top);
|
||||
addVertex(right, top);
|
||||
addVertex(left, bottom);
|
||||
addVertex(right, bottom);
|
||||
}
|
||||
|
||||
void clear() { m_buffer.reset(); }
|
||||
GLfloat *vertices() const { return m_buffer.data(); }
|
||||
int vertexCount() const { return m_buffer.size() / 2; }
|
||||
|
||||
private:
|
||||
DataBuffer<GLfloat> m_buffer;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 DATABUFFER_H
|
||||
#define DATABUFFER_H
|
||||
|
||||
template<class T>
|
||||
class DataBuffer
|
||||
{
|
||||
public:
|
||||
DataBuffer(int res = 64) {
|
||||
m_capacity = res;
|
||||
m_buffer = new T[m_capacity];
|
||||
m_size = 0;
|
||||
}
|
||||
~DataBuffer() { delete[] m_buffer; }
|
||||
|
||||
inline void reset() { m_size = 0; }
|
||||
inline bool isEmpty() const { return m_size == 0; }
|
||||
|
||||
inline int size() const { return m_size; }
|
||||
inline T *data() const { return m_buffer; }
|
||||
|
||||
inline const T& at(int i) const { return m_buffer[i]; }
|
||||
inline const T& last() const { return m_buffer[m_size-1]; }
|
||||
inline const T& first() const { return m_buffer[0]; }
|
||||
inline const T& operator[](int i) const { return m_buffer[i]; }
|
||||
inline T& operator[](int i) { return m_buffer[i]; }
|
||||
|
||||
inline void add(const T &t) {
|
||||
if(m_size >= m_capacity) {
|
||||
m_capacity *= 2;
|
||||
T *buffer = new T[m_capacity];
|
||||
for(int i=0;i<m_size;++i)
|
||||
buffer[i] = m_buffer[i];
|
||||
delete[] m_buffer;
|
||||
m_buffer = buffer;
|
||||
}
|
||||
m_buffer[m_size++] = t;
|
||||
}
|
||||
|
||||
inline DataBuffer &operator<<(const T &t) { add(t); return *this; }
|
||||
|
||||
private:
|
||||
int m_size;
|
||||
int m_capacity;
|
||||
T *m_buffer;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in new issue