Odd issue with GLSL texture coordinates

Hello.

I am developing a plugin for an environment which uses GLSL rendering on a quad, in which a callback function needs to be able to handle whether my input images are flipped or not, so I provide properly oriented images. Sounds simple. I have my FBO and GLSL code working and happily outputting images, however, when I attempt to handle flipping, no matter what I attempt to do, my GLSL shaders output oddly malformed images. For example, a simple convolution shader which does “image space” motion blur works properly when not flipped, however, when it is flipped, only 0, 90, 180, and 360 degrees output the proper expected image. Angles intermediate output what looks to be a box blur. Other shaders retain this behaviour. Zoom blurs only seem to behave when one origin is constrained to the mid point on an axis when flipped. When not flipped, zoom blur works. It seems like there is some odd coordinate transform going on in the texture matrix, but I cannot deduce what… ive attempted to google this issue, and have not come up with anything , but my knowledge of GL is, on the whole, much like swiss cheese, so I am curious what the proper way of flipping a texture is, while retaining proper working coordinates for use within a GLSL shader. Im curious if there is something happening when I create the FBO, which resets some state I may not be handling properly? Ive attempted to reset the GL_TEXTURE_MATRIX by calling glMatrixMode(GL_TEXTURE), glPushMatrix(), glLoadIdentity() etc, to no avail.

Some images to illustrate the issue.

http://abstrakt.vade.info/ILOVETHEGLLIST/flipped1.jpg (45ºshows odd box blur behaviour)
http://abstrakt.vade.info/ILOVETHEGLLIST/flipped2.jpg (almost 0 degrees, approximates correct result)
http://abstrakt.vade.info/ILOVETHEGLLIST/non-flipped1.jpg (45º correct)
http://abstrakt.vade.info/ILOVETHEGLLIST/non-flipped2.jpg (correct)

For reference,

My flipping code: (here, I just alter the texcoords per vertex on a quad - ive attempted some other methods, calling glScalef(1.0, -1.0, 1.0) and the like, all result in the same odd results in my shader) The callback from QC properly informs me of whether BOOL* flipped is true. Note, the flipping works (my image is properly oriented if I flip), its just my odd results within GLSL… Thanks for any hints, and sorry if this is very basic, i believe my shader is correctly set up, and the issue lies somewhere in the depths of the GL_TEXTURE_MATRIX, but these are somewhat murky waters for me… :slight_smile:

if(flipped)
	{
		// flip coords
		glBegin(GL_QUADS);
			glTexCoord2f(0, pixelsHigh);
			glVertex2f(0, 0);
			glTexCoord2f(pixelsWide, pixelsHigh);
			glVertex2f(width, 0);
			glTexCoord2f(pixelsWide, 0);
			glVertex2f(width, height);
			glTexCoord2f(0, 0);
			glVertex2f(0, height);
		glEnd();		
	}
		
	else
	{	
		glBegin(GL_QUADS);
			glTexCoord2f(0, 0);
			glVertex2f(0, 0);
			glTexCoord2f(0, pixelsHigh);
			glVertex2f(0, height);
			glTexCoord2f(pixelsWide, pixelsHigh);
			glVertex2f(width, height);
			glTexCoord2f(pixelsWide, 0);
			glVertex2f(width, 0);
		glEnd();		
	}

vertex shader:

varying vec2 texcoord;
uniform float amount;

void main()
{
// perform standard transform on vertex
gl_Position = ftransform();

// transform texcoords
texcoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);

}

frag:

varying vec2 texcoord;
uniform sampler2DRect tex0;
uniform vec2 texdim0;
uniform float amount;
uniform float angle;

void main (void)
{
vec2 texcoord0 = texcoord;
float theta = radians(angle);

// our offsets, where we sample
vec2 amount1,amount2,amount3,amount4,amount5,amount6,amount7,amount8;

amount1 = vec2(cos(theta) , sin(theta)) * amount;

amount2 = amount1 *2.0;
amount3 = amount1 *3.0;
amount4 = amount1 *4.0;

amount5 = -amount1;
amount6 = amount5 * 2.0;
amount7 = amount5 * 3.0;
amount8 = amount5 * 4.0;

// sample our textures
vec4 sample0 = texture2DRect(tex0, texcoord0);
vec4 sample1 = texture2DRect(tex0, texcoord0 + amount1);
vec4 sample2 = texture2DRect(tex0, texcoord0 + amount2);
vec4 sample3 = texture2DRect(tex0, texcoord0 + amount3);
vec4 sample4 = texture2DRect(tex0, texcoord0 + amount4);
vec4 sample5 = texture2DRect(tex0, texcoord0 + amount5);
vec4 sample6 = texture2DRect(tex0, texcoord0 + amount6);
vec4 sample7 = texture2DRect(tex0, texcoord0 + amount7);
vec4 sample8 = texture2DRect(tex0, texcoord0 + amount8);

//straight averaging
gl_FragColor = (sample0 + sample1 + sample2 + sample3 + sample4 + sample5 + sample6 + sample7 + sample8) / 9.0;
}

Hi. Nevermind. I fixed it :slight_smile:

It’d be nice if you posted the solution, for people searching the archives a year from now.

Sure, thats fair enough. I was a bit embarrassed though when I realized my issue. :slight_smile:

In my plugin I am doing multipass rendering, and the strange box blur effect came from the image being flipped more than once. Since I had an odd number of passes, the final resulting image was flipped.

My solution was to add the following code to react to the flipping by changing the projection matrix appropriately like so:

if(flipped)
glOrtho(0, width, height, 0, -1, 1);
else
glOrtho(0, width, 0, height, -1, 1);

and then only flip the first texture. The other passes are not flipped. The box blur result was due the flipping, blurring, flipping, blurring. Makes sense… god what a waste of time that was :slight_smile:

Hope this helps someone in the future…

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.