Varying vec2 surfacePosition

I am pretty new with shaders and gl. I know something close to nothing. I am trying to include some code from glsandbox into an OF project.
I managed to make some example working but I am trying now passing a varying vec2 surfacePosition.
It seems to understand this is a value of the texture coordinates.
I tried this in the vert

# version 120
varying vec2 surfacePosition;

void main()
    	gl_Position = ftransform();
    surfacePosition = gl_TexCoord[0].xy-0.5;

and this in the frag

#ifdef GL_ES
precision mediump float;

uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
varying vec2 surfacePosition;
#define MAX_ITER 10
void main( void ) {
    vec2 p = surfacePosition*3.0- vec2(15.0);
    vec2 i = p;
    float c = 1.0;
    float inten = .05;
    for (int n = 0; n < MAX_ITER; n++)
        float t = time * (1.0 - (3.0 / float(n+1)));
        i = p + vec2(cos(t - i.x) + sin(t + i.y), sin(t - i.y) + cos(t + i.x));
        c += 1.0/length(vec2(p.x / (2.*sin(i.x+t)/inten),p.y / (cos(i.y+t)/inten)));
    c /= float(MAX_ITER);
    c = 1.5-sqrt(pow(c,3.+ 0.5));
    gl_FragColor = vec4(vec3(c*c*c*c*0.3,c*c*c*c*0.3,c*c*c*c*0.3), 1.0);

this is in draw()

void ofApp::draw(){

    ofBackground(100, 0, 0);
    time = ofGetElapsedTimef();
    mouse = ofPoint(ofMap(ofGetMouseX(), 0, ofGetWidth(), 0, 1), ofMap(ofGetMouseY(), 0, ofGetHeight(), 0, 1) );
    if( useShader ){
        shader.setUniform1f("time",time );
        shader.setUniform2f("mouse",mouse.x, mouse.y );
        shader.setUniform2f("resolution", ofGetWidth(), ofGetHeight());
    ofSetColor(0, 0, 0);
    ofRect(0, 0, ofGetWindowWidth(), ofGetWindowHeight()); ///resolution of the frame in which the shader is drawn
    if( useShader ){


It doesn’t work. And I don’t understand how should I work with the varying vec2 surfacePosition.

ANy help would be much appreciated.

thanks :smile:

There seems to be nothing wrong with the use of the varying vec2. Yet besides that I’m not sure about what all that math is going to do (I didn’t analyze it), in which might be a problem. the biggest problem I see is that ofRect doesn’t generate texCoords so I would start by using something that does, like an ofMesh or passing them manually to openGL.

Hi @roymacdonald, thanks for replying.
you can check the code on here
it should be correct but it’s not mine.
I tried to substitute the ofRect with a

in .h

ofPlanePrimitive plane;

in setup()

plane.set(ofGetWidth(), ofGetHeight(), 10, 10);

in draw();

plane.mapTexCoords(0,0, ofGetWidth(), ofGetHeight());

I get a flashy plane but it is not what I should get. from what I understand the varying vec2 surfacePosition communicates the position of each vertex? but How do I communicate that to the main app?


no, the varying passes a value from the vertex shader to its correspondent fragment shader.
Using varyings you can pass whatever you want between the vert and frag shader.
As of the surfacePosition, out of a quick look at it in glslsandbox, it just seems to contains the screen coordinates for each vertex. There might be also some issues whether it is using normalized or not values.
If you want to use glslsandbox shaders right away with OF, you should try @patricio 's superb ofxFX addon.

RIght, so… I am not even sure how I did it but it works. I didn’t use the addon but I will have a look at it.
The problem was that I needed to use gl_MultiTexCoord0.xy; instead of gl_TexCoord[0].xy

so in vert I got:

varying vec2 surfacePosition;

void main()
    gl_Position = ftransform();
    surfacePosition = gl_MultiTexCoord0.xy;

in cpp draw()

plane.mapTexCoords(0,0, 1, 1);
    ofTranslate(ofGetWidth()/2, ofGetHeight()/2);

where the texture size has to be 1 and 1 and not ofGetWidth(), ofGetHeight().

I think I need to buy a book on GLSL soon! :blush:

Thanks for your help!

Hey, I also got this conundrum. Can you share the src and ur data file to me or open sources. That would be really helpful, I also want to use the code from sandbx, but I tried a lot, it still couldn’t run.

this is what I wrote in OF, and I followed the screenshot made by you in this forum, ignore the load ("",""), I comment them since at first it doesn’t work

Hi, I think that you need to load a passthrough vert shader.
Check out the examples, most of these use one.

1 Like

Hi @Yh1, I am not that good with shaders either to be honest. @roymacdonald knows way more than me so, take his advice.
I think my case was a bit different as you’re assigning a shader to an fbo I used a planeprimitive.
I am attaching my code just in case you find it useful.
Mind that you need to replace your vert and frag files and adjust the setUniform functions according to your needs.
I hope this helps


I (2.2 KB)

1 Like