Simple zoom shader

Hello to everyone!

It’s been a long time that i code in OF, and i decide today, to try the shaders and their code…
…So i’m trying to re-use an old code, i find it on internet, to make a simple zoom effect.

I create a class with it and some image… but i think i missed something, because it is not really working…

i show you the code: can you tell me what i missed please? First time with it… and i need to know if i’m in the rigth way or not :frowning:

#include “bluetag.h”

bluetag::bluetag()
{

shader.load("shaders/zoom.vert","shaders/zoom.frag");
zoomRadius = 40.0;
zoomMinVal = 0.1;
zoomMaxVal = 1.0;

fbo.allocate(ofGetWidth(), ofGetHeight(), GL_RGBA);

map1.loadImage(“bluetag/map1.jpg”);
map2.loadImage(“bluetag/map2.jpg”);
map3.loadImage(“bluetag/map3.jpg”);
map4.loadImage(“bluetag/map4.jpg”);

}

void bluetag::draw(int handX, int handY)
{

fbo.begin();
map1.draw(0,0,ofGetWidth()/4,ofGetHeight());
map2.draw(ofGetWidth()/4,0,ofGetWidth()/4,ofGetHeight());
fbo.end();
shader.begin();
fbo.draw(0,0);
pos.set(handX,handY );
shader.setUniform2i("circlePos", pos.x, fbo.getHeight() - pos.y);
shader.setUniform1f( "circleRadius", zoomRadius );
shader.setUniform1f( "minZoom", zoomMinVal );
shader.setUniform1f( "maxZoom", zoomMaxVal );
shader.end();

}

and the zoom.frag :

#extension GL_ARB_texture_rectangle : enable
uniform sampler2DRect src_tex_unit0;
uniform vec2 src_tex_offset0;

uniform vec2 circlePos;
uniform float circleRadius;
uniform float minZoom;
uniform float maxZoom;

vec4 color;

void main( void )
{

vec2 st        = gl_TexCoord[0].st;
//check if our pixels fit in the bounding rect - so we avoid expensive operations like sqrt. 
float maxX = circlePos.x + circleRadius;
float minX = circlePos.x - circleRadius;
float maxY = circlePos.y + circleRadius;
float minY = circlePos.y - circleRadius;
if( st.s > minX && st.s < maxX && st.t > minY && st.t < maxY){
    float relX = st.s - circlePos.x;
    float relY = st.t - circlePos.y;
    float ang =  atan(relY, relX);
    float dist = sqrt(relX*relX + relY*relY);
    if( dist <= circleRadius ){
        //figure a zoom value for the pixel based on its distance from the center of the circle
        float newRad = dist * ( (maxZoom * dist/circleRadius) + minZoom );
        float newX = circlePos.x + cos( ang ) * newRad;
        float newY = circlePos.y + sin( ang ) * newRad;        
        gl_FragColor = texture2DRect(src_tex_unit0, vec2(newX, newY) );
    }else{
        //just draw the pixels as normal
        gl_FragColor = texture2DRect(src_tex_unit0, st );
    }
}else{
        //just draw the pixels as normal
        gl_FragColor = texture2DRect(src_tex_unit0, st );
}

}#extension GL_ARB_texture_rectangle : enable
uniform sampler2DRect src_tex_unit0;
uniform vec2 src_tex_offset0;

uniform vec2 circlePos;
uniform float circleRadius;
uniform float minZoom;
uniform float maxZoom;

vec4 color;

void main( void )
{

vec2 st        = gl_TexCoord[0].st;
//check if our pixels fit in the bounding rect - so we avoid expensive operations like sqrt. 
float maxX = circlePos.x + circleRadius;
float minX = circlePos.x - circleRadius;
float maxY = circlePos.y + circleRadius;
float minY = circlePos.y - circleRadius;
if( st.s > minX && st.s < maxX && st.t > minY && st.t < maxY){
    float relX = st.s - circlePos.x;
    float relY = st.t - circlePos.y;
    float ang =  atan(relY, relX);
    float dist = sqrt(relX*relX + relY*relY);
    if( dist <= circleRadius ){
        //figure a zoom value for the pixel based on its distance from the center of the circle
        float newRad = dist * ( (maxZoom * dist/circleRadius) + minZoom );
        float newX = circlePos.x + cos( ang ) * newRad;
        float newY = circlePos.y + sin( ang ) * newRad;        
        gl_FragColor = texture2DRect(src_tex_unit0, vec2(newX, newY) );
    }else{
        //just draw the pixels as normal
        gl_FragColor = texture2DRect(src_tex_unit0, st );
    }
}else{
        //just draw the pixels as normal
        gl_FragColor = texture2DRect(src_tex_unit0, st );
}

}

and the .vert:

void main() {
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}

What is the output you’re getting? Any error message from the ofShader linking? Try narrowing the question a bit :wink:

Actually, there is no error, it is just not working when i start the debugging.
And yes, sorry: to make the “mouse”, i use a leap-motion, maybe it is the problem… ?ux
by the way, i use also code::blocks on Linux :slight_smile: