From b5cf4ad2c4ad116fdeaacb2b26dab95535bc696a Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Wed, 7 Dec 2011 17:54:28 -0200 Subject: [PATCH] test shader effect --- TODO | 1 + modules/shadertest.frag | 53 +++++++++++++++++++ src/framework/graphics/glbuffer.cpp | 17 ++++++ src/framework/graphics/glbuffer.h | 14 +++++ src/framework/graphics/painter.cpp | 2 + src/framework/graphics/painter.h | 2 + .../graphics/paintershaderprogram.cpp | 2 + src/framework/graphics/paintershaderprogram.h | 3 +- src/framework/graphics/shader.cpp | 4 +- src/otclient/core/map.cpp | 40 ++++++++++++-- 10 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 modules/shadertest.frag diff --git a/TODO b/TODO index 3e235b7e..e4b9fe8f 100644 --- a/TODO +++ b/TODO @@ -33,6 +33,7 @@ restore ctrl+g and keybindings create a class for reading binary files handle corrupt errors in dat/spr +create image class use CoordsBuffer in font cache into framebuffers implement glbuffer for CoordsBuffer diff --git a/modules/shadertest.frag b/modules/shadertest.frag new file mode 100644 index 00000000..1a4f6e3e --- /dev/null +++ b/modules/shadertest.frag @@ -0,0 +1,53 @@ +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; +} +*/ \ No newline at end of file diff --git a/src/framework/graphics/glbuffer.cpp b/src/framework/graphics/glbuffer.cpp index 7d6ddcc4..fd2e1cad 100644 --- a/src/framework/graphics/glbuffer.cpp +++ b/src/framework/graphics/glbuffer.cpp @@ -22,3 +22,20 @@ #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() +{ + +} + diff --git a/src/framework/graphics/glbuffer.h b/src/framework/graphics/glbuffer.h index ba31f943..6977363b 100644 --- a/src/framework/graphics/glbuffer.h +++ b/src/framework/graphics/glbuffer.h @@ -23,8 +23,22 @@ #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 diff --git a/src/framework/graphics/painter.cpp b/src/framework/graphics/painter.cpp index f156bcd7..313c58c9 100644 --- a/src/framework/graphics/painter.cpp +++ b/src/framework/graphics/painter.cpp @@ -47,6 +47,7 @@ void Painter::init() program->bindUniformLocation(PainterShaderProgram::COLOR_UNIFORM, "color"); program->bindUniformLocation(PainterShaderProgram::OPACITY_UNIFORM, "opacity"); program->bindUniformLocation(PainterShaderProgram::TEXTURE_UNIFORM, "texture"); + program->bindUniformLocation(PainterShaderProgram::TICKS_UNIFORM, "ticks"); m_drawTexturedProgram = program; program = PainterShaderProgramPtr(new PainterShaderProgram); @@ -57,6 +58,7 @@ void Painter::init() program->bindUniformLocation(PainterShaderProgram::PROJECTION_MATRIX_UNIFORM, "projectionMatrix"); program->bindUniformLocation(PainterShaderProgram::COLOR_UNIFORM, "color"); program->bindUniformLocation(PainterShaderProgram::OPACITY_UNIFORM, "opacity"); + program->bindUniformLocation(PainterShaderProgram::TICKS_UNIFORM, "ticks"); m_drawSolidColorProgram = program; } diff --git a/src/framework/graphics/painter.h b/src/framework/graphics/painter.h index 10652974..73f75a67 100644 --- a/src/framework/graphics/painter.h +++ b/src/framework/graphics/painter.h @@ -57,6 +57,8 @@ public: void setCompositionMode(CompositionMode compositionMode); + GLfloat *getProjectionMatrix() { return (GLfloat*)m_projectionMatrix; } + private: PainterShaderProgramPtr m_drawTexturedProgram; PainterShaderProgramPtr m_drawSolidColorProgram; diff --git a/src/framework/graphics/paintershaderprogram.cpp b/src/framework/graphics/paintershaderprogram.cpp index 2fd9fa6f..99f50c1c 100644 --- a/src/framework/graphics/paintershaderprogram.cpp +++ b/src/framework/graphics/paintershaderprogram.cpp @@ -24,6 +24,7 @@ #include "painter.h" #include "texture.h" #include "texturemanager.h" +#include void PainterShaderProgram::setProjectionMatrix(float projectionMatrix[3][3]) { @@ -76,6 +77,7 @@ void PainterShaderProgram::setTextureCoords(const GLfloat *textureCoords) void PainterShaderProgram::prepareForDraw() { assert(bind()); + setUniformValue(TICKS_UNIFORM, (GLfloat)g_clock.ticks()); } void PainterShaderProgram::drawTriangleStrip(int numVertices) diff --git a/src/framework/graphics/paintershaderprogram.h b/src/framework/graphics/paintershaderprogram.h index f7d9d409..89393961 100644 --- a/src/framework/graphics/paintershaderprogram.h +++ b/src/framework/graphics/paintershaderprogram.h @@ -35,7 +35,8 @@ public: TEXTURE_TRANSFORM_MATRIX_UNIFORM = 1, COLOR_UNIFORM = 2, OPACITY_UNIFORM = 3, - TEXTURE_UNIFORM = 4 + TEXTURE_UNIFORM = 4, + TICKS_UNIFORM = 5 }; void setProjectionMatrix(GLfloat projectionMatrix[3][3]); diff --git a/src/framework/graphics/shader.cpp b/src/framework/graphics/shader.cpp index 34503202..ca24c03d 100644 --- a/src/framework/graphics/shader.cpp +++ b/src/framework/graphics/shader.cpp @@ -21,6 +21,7 @@ */ #include "shader.h" +#include Shader::Shader(Shader::ShaderType shaderType) { @@ -70,8 +71,7 @@ bool Shader::compileSourceCode(const std::string& sourceCode) bool Shader::compileSourceFile(const std::string& sourceFile) { - std::ifstream fin(sourceFile); - std::string sourceCode((std::istreambuf_iterator(fin)), std::istreambuf_iterator()); + std::string sourceCode = g_resources.loadFile(sourceFile); return compileSourceCode(sourceCode); } diff --git a/src/otclient/core/map.cpp b/src/otclient/core/map.cpp index 72173ef3..7ea70ccb 100644 --- a/src/otclient/core/map.cpp +++ b/src/otclient/core/map.cpp @@ -28,7 +28,9 @@ #include "missile.h" #include #include - +#include +#include +#include Map g_map; Map::Map() @@ -36,11 +38,27 @@ Map::Map() setVisibleSize(Size(MAP_VISIBLE_WIDTH, MAP_VISIBLE_HEIGHT)); } +PainterShaderProgramPtr program; void Map::draw(const Rect& rect) { - if(!m_framebuffer) + if(!m_framebuffer) { m_framebuffer = FrameBufferPtr(new FrameBuffer(m_visibleSize.width() * NUM_TILE_PIXELS, m_visibleSize.height() * NUM_TILE_PIXELS)); + + program = PainterShaderProgramPtr(new PainterShaderProgram); + program->addShaderFromSourceCode(Shader::Vertex, glslMainWithTexCoordsVertexShader + glslPositionOnlyVertexShader); + program->addShaderFromSourceFile(Shader::Fragment, "/shadertest.frag"); + program->bindAttributeLocation(VERTEX_COORDS_ATTR, "vertexCoord"); + program->bindAttributeLocation(TEXTURE_COORDS_ATTR, "textureCoord"); + assert(program->link()); + program->bindUniformLocation(PainterShaderProgram::PROJECTION_MATRIX_UNIFORM, "projectionMatrix"); + program->bindUniformLocation(PainterShaderProgram::TEXTURE_TRANSFORM_MATRIX_UNIFORM, "textureTransformMatrix"); + program->bindUniformLocation(PainterShaderProgram::COLOR_UNIFORM, "color"); + program->bindUniformLocation(PainterShaderProgram::OPACITY_UNIFORM, "opacity"); + program->bindUniformLocation(PainterShaderProgram::TEXTURE_UNIFORM, "texture"); + program->bindUniformLocation(PainterShaderProgram::TICKS_UNIFORM, "ticks"); + } + g_painter.setColor(Fw::white); m_framebuffer->bind(); @@ -82,8 +100,22 @@ void Map::draw(const Rect& rect) m_framebuffer->release(); - g_painter.setColor(Fw::white); - m_framebuffer->draw(rect); + //g_painter.setColor(Fw::white); + //m_framebuffer->draw(rect); + + + CoordsBuffer coordsBuffer; + coordsBuffer.addRect(rect, Rect(0,0,m_framebuffer->getTexture()->getSize())); + coordsBuffer.cacheVertexArrays(); + program->prepareForDraw(); + program->setProjectionMatrix((GLfloat (*)[3])g_painter.getProjectionMatrix()); + program->setOpacity(1.0f); + program->setColor(Fw::white); + program->setTexture(m_framebuffer->getTexture()); + program->setVertexCoords(coordsBuffer.getVertexCoords()); + program->setTextureCoords(coordsBuffer.getTextureCoords()); + program->drawTriangles(coordsBuffer.getVertexCount()); + program->releaseFromDraw(); // calculate stretch factor float horizontalStretchFactor = rect.width() / (float)(m_visibleSize.width() * NUM_TILE_PIXELS);