Unable to convert shader from GL2 -> GL3

Hi all,

I’m struggling with understanding how to convert existing shaders from GL2->GL3.

My departure point is chosamuel’s excellent implementation of physarium growth algorithm https://github.com/chosamuel/openframeworks-physarum, which I am trying to integrate into my own project. Unfortunately his project was written for openGL 2, while mine is in 3. Thus the trouble… Currently the shaders compile without complaining, BUT I am getting the error ofFbo: FRAMEBUFFER_INCOMPLETE_ATTACHMENT.

Here’s the original shader.vert:

#version 120

void main(){
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	gl_TexCoord[0] = gl_MultiTexCoord0;
}

and this is the original shader.frag:

#version 120

uniform sampler2DRect tex;
uniform float dR;

void main(){
	vec2 pos = gl_TexCoord[0].xy;
	vec4 col;

	//float decayRate = 0.66;
	float decayRate = dR;

	//DIFFUSE
	col = texture2DRect(tex, pos);
	col += texture2DRect(tex, pos + vec2(-1,-1));
	col += texture2DRect(tex, pos + vec2(0,-1));
	col += texture2DRect(tex, pos + vec2(1,-1));
	col += texture2DRect(tex, pos + vec2(-1,0));
	col += texture2DRect(tex, pos + vec2(1,0));
	col += texture2DRect(tex, pos + vec2(-1,1));
	col += texture2DRect(tex, pos + vec2(0,1));
	col += texture2DRect(tex, pos + vec2(1,1));

	col /= 9.0;
	
	//DECAY
	col.rgb *= decayRate;
	//col *= decayRate;

	gl_FragColor = col;
}

This is my attempt at conversion. First the vertex shader:

#version 150

uniform mat4 modelViewProjectionMatrix;

in vec4 position;
in vec2 texcoord;

out vec2 texCoordVarying;

void main(){
    texCoordVarying = texcoord;
    gl_Position = modelViewProjectionMatrix * position;
}

Secondly my .frag:

#version 150

uniform sampler2DRect tex0;
uniform float dR;

in vec2 texCoordVarying;

out vec4 outputColor;

void main(){
	vec2 pos = texCoordVarying.xy;
	vec4 col;

	//float decayRate = 0.66;
	float decayRate = dR;

	//DIFFUSE
	col = texture2DRect(tex0, pos);
	col += texture2DRect(tex0, pos + vec2(-1,-1));
	col += texture2DRect(tex0, pos + vec2(0,-1));
	col += texture2DRect(tex0, pos + vec2(1,-1));
	col += texture2DRect(tex0, pos + vec2(-1,0));
	col += texture2DRect(tex0, pos + vec2(1,0));
	col += texture2DRect(tex0, pos + vec2(-1,1));
	col += texture2DRect(tex0, pos + vec2(0,1));
	col += texture2DRect(tex0, pos + vec2(1,1));

	col /= 9.0;
	
	//DECAY
	col.rgb *= decayRate;

	outputColor = col;
}

Any help much appreciated!!!

Best,
K

1 Like

Hi,
don’t use texture2DRect use texture instead

Thanks a lot! However, I’m still getting the same error when running the program, so I suspect the problem might lie elsewhere…

The relevant section of the code are (I think)

// header declaration
        ofShader shader;
        ofFbo trailmap;
        ofPixels pixBuffer;
        ofTexture texBuffer;
setup(){
    trailmap.allocate( ofGetWidth(), ofGetHeight(), GL_LUMINANCE);
    pixBuffer.allocate( ofGetWidth(), ofGetHeight(), OF_IMAGE_GRAYSCALE);
    texBuffer.allocate( ofGetWidth(), ofGetHeight(), GL_LUMINANCE);

    shader.load("physarum");
...
}

update(){
    for (int i = 0; i < numParticles; i++) {
        p[i].setMaxSpeed(maxSpeed);
        p[i].setNumDirs(numDirs);
        p[i].setSenseDist(senseDist);
        p[i].setDepositAmt(depositAmt);
        p[i].sense(pixBuffer);
        p[i].move();
        p[i].deposit(pixBuffer);
    }

    texBuffer.loadData(pixBuffer);

    trailmap.begin();
    shader.begin();
    shader.setUniformTexture("tex0", texBuffer, 0);
    shader.setUniform1f("dR", decayRate);
    texBuffer.draw(0, 0);
    shader.end();
    trailmap.end();

    trailmap.readToPixels(pixBuffer);
}

Which works just fine as long as I set my project to use GL2.1 and use the corresponding shaders, but not when setting GL3.2 and the ported shaders…

Aha, so I got it to work by replacing the glFormat of the ofFbo to GL_RGBA. It seems a bit weird though, that I am not able to allocate ofFbo with GL_LUMINANCE. Is this expected behavior?

Hi glad to know you fixed it. I have no idea as why GL_LIMINANCE does not work.

Maybe it’s a Linux specific bug. Will file an issue!

Seems GL_LUMINANCE is actually a very old concept that was about to be deprecated ca. 2014, and kept around in order to support GL2 legacy code.