Need help debugging ofxBezel shader - sampler2d versus sampler2Drect


Have a project that needs some bezel compensation, and I came across this amazingly helpful addon:

Unfortunately I hit two walls, I’m on a mac (example is for pc) and my graphics card only supports glsl 1.2 (this example calls for 1.3).

I ended up ripping out the shader code and dropping it into the OpenGL Shader Builder to see if i could reverse engineer it and get it working. I was successful at getting it working, but only when my texture in the shader builder was set to 2D (instead of Rectangle). if I change the code to be sampler2drect/texture2drect, then the shader still displays but, it doesn’t function correctly anymore, so something weird is happening with the math there.

I understand that Sampler2Drect is for non power of 2 textures (1024x768) and Sampler2D is for power of 2 (512x512) but not sure why

When I tried to bring that into openframeworks I got the shader to function in a way, but it was just doing some weird displacement/wrapping that wasn’t the goal.

Big question: what do I need to switch in OF to make a shader work with sampler2d instead of sampler2d rect? That may not be the best method because my whole comp is 3840x720, so I would need an FBO that is 3840x3840 to feed the shader?

Or, what should I look into for adjusting my shader math to work correctly as a shader2drect?


Fragment shader:

#version 120  
uniform sampler2D tex0;  
uniform float mode;  
varying vec2 texcoord0;  
varying vec2 texdim0;  
uniform float columnSpacer;  
uniform float rowSpacer;  
//uniform float texWidthChunk;  
//uniform float texHeightChunk;  
uniform float numColumns;  
uniform float numRows;  
uniform float position;  
void main(void)  
	vec4 input0 = texture2D(tex0, texcoord0);  
	vec2 translate0 = texcoord0;  
	vec2 translate1 = texcoord0;  
	float texWidthChunk = texdim0.x/numColumns;  
	float horzScaler = 1.0 - columnSpacer * (numColumns-1.0);  
	int column = int(translate0.x/texWidthChunk);  
	translate0.x = translate0.x * horzScaler + column * columnSpacer;  
	float texHeightChunk = texdim0.y/numRows;  
	float vertScaler = 1.0 - rowSpacer * (numRows-1.0);  
	int row = int(translate0.y/texHeightChunk);  
	translate0.y = translate0.y * vertScaler + row * rowSpacer;  
	gl_FragColor = texture2D(tex0, translate0);  


// Vertex shader  
varying vec2 texcoord0;  
varying vec2 texdim0;  
void main(void)  
    gl_Position = ftransform();  
    texcoord0 = vec2 (gl_TextureMatrix[0] * gl_MultiTexCoord0);  
    texdim0 = vec2 (abs(gl_TextureMatrix[0][0][0]),abs(gl_TextureMatrix[0][1][1]));  

Picture of it with sampler2d has the chunks taken out of the middle, with sampler2drect it just shifts everything to the left.

Thanks as usual everyone…planning to push my changes to ofxBezel so there can be a mac compatible version of this shader. If you’ve got other suggestions of how to do fast bezel compensation that won’t bog down, i’m all ears.

![]( Shot 2012-08-17 at 12.07.18 PM.png)

![]( Shot 2012-08-17 at 12.21.42 PM.png)

Ended up ditching the shader and switching to the draw into fbo and texcoord it to shift it around. If anyone has suggestions on my original question they are welcome, but I’ll post what I ended up doing…something like this:

//draw yo stuff yo  
    tex1 = largeFbo.getTextureReference();  //i dont think this is necessary, but it was acting funky before I just copied it to it's own texture.  
    //Bottom Screen  
    //suggested texcoord range 0 and .325 instead of 0 and .33  
    ofTranslate(0, 0);  
    glTexCoord2f(0,0); glVertex3f(0,0,0);  
    glTexCoord2f(.33,0); glVertex3f(1280,0,0);  
    glTexCoord2f(.33,1); glVertex3f(1280,720,0);  
    glTexCoord2f(0,1); glVertex3f(0,720,0);  
    //Middle Screen  
    //suggested range .335 and .665 instead of .33 and .66  
    ofTranslate(1280, 0);  
    glTexCoord2f(.33,0); glVertex3f(0,0,0);  
    glTexCoord2f(.66,0); glVertex3f(1280,0,0);  
    glTexCoord2f(.66,1); glVertex3f(1280,720,0);  
    glTexCoord2f(.33,1); glVertex3f(0,720,0);  
    //Top Screen  
    //suggested range .335 and .665 instead of .33 and .66  
    ofTranslate(2560, 0);  
    glTexCoord2f(.655,0); glVertex3f(0,0,0);  
    glTexCoord2f(1,0); glVertex3f(1280,0,0);  
    glTexCoord2f(1,1); glVertex3f(1280,720,0);  
    glTexCoord2f(.655,1); glVertex3f(0,720,0);  
        ofClear(255, 255, 255,0); //Clear the FBO  

I hope this isn’t something obvious, but as I understand it, oF uses glEnable(GL_TEXTURE_RECTANGLE) which means textures are in x/y coordinates hence use sampler2DRect, vs glEnable(GL_TEXTURE_2D) which would use sampler2D and the tex coords would be in normalized where s = x/ofGetWidth() and t = y/ofGetHeight()

I just had a quick play with ofxBezel and the example app seems to work for me on OSX. All I did was change the shader, first line from 130 to 120 and the last line to gl_FragColor = texture2DRect(tex0 , st); as I think that is the only non v1.2 call. If i hit enter to see the bezelHelper.png i can use the arrow keys to line everything up.

[edit] Attached the demo project …


	fragmentShader <<   
		"#version 120\n \  
		#extension GL_ARB_texture_rectangle : enable\n \  
		uniform sampler2DRect tex0;\  
		uniform float columnSpacer;\  
		uniform float rowSpacer;\  
		uniform float texWidth;\  
		uniform float texHeight;\  
		void main()\  
		vec2 st	= gl_TexCoord[0].st;\  
		float texWidthChunk = texWidth / " << numColumns << ";\  
		float texHeightChunk = texHeight / " << numRows << ";\  
		float horzScalar = 1 - columnSpacer * " << numColumns - 1 << " / texWidth; \  
		int column = int(st[0] / texWidthChunk); \  
		st[0] = st[0] * horzScalar + column * columnSpacer; \  
		int row = int(st[1] / texHeightChunk); \  
		float vertScalar = 1 - rowSpacer * " << numRows - 1 << " / texHeight;\  
		st[1] = st[1] * vertScalar + row * rowSpacer;\  
		gl_FragColor = texture2DRect(tex0 , st);\  

Oh hey guys. Blast from the past here. I stumbled across this thread. I’m the author of that addon. FYI for folks in the future, I updated that addon to use GLSL 120, and there’s a mac example now. If anyone’s so inclined to send a pull request for a linux version, go for it, and we can add it to the repo.