1
votes

I've created a multi-texture fragment shader in which I want to flip one of the textures but when doing this my texture gets all jaggy. Is there a solution to this problem?

Jaggy borders

This is my fragment shader code:

void main(void)
{
    lowp vec4 camera = texture2D(texture0, destinationTexCoord);
    lowp vec4 viewfinder = texture2D(texture1, vec2(destinationTexCoord.x, 1.0 - destinationTexCoord.y));

    lowp vec4 result = mix(camera, viewfinder, viewfinder.a);
    gl_FragColor = result;
}

Texture filtering I'm using:

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

Some background information: I'm blending these textures in my fragment shader (blending code stripped out) so I'm not looking for a solution where I rotate a second plane and merge them down on the framebuffer. I'm wondering if it is a good idea to flip/rotate around the texture coordinates in my fragment shader.

In the end I'll pass in a uniform saying: rotate or don't, resulting in that one texture getting rotated or not.

1
Did you forget to enable texture filtering for that texture?Nicol Bolas
Edited the initial post: I've added the texture filtering I'm usingpolyclick
Could you post a picture of what this looks like?Nicol Bolas
Wouldn't flipping the texture in vertex shader be faster, actually? Fragment shader is of course more powerful, but since you only need per-vertex flip of one coordinate, non some nonlinear manipulations...Bartek Banachewicz
@BartekBanachewicz does this also work if I want to rotate only 1 texture in my multi-texture shader?polyclick

1 Answers

1
votes

Using mediump instead of lowp for destinationTexCoord will improve the sampling rate for the vec2(destinationTexCoord.x, 1.0 - destinationTexCoord.y) calculation. However, my recommendation is to use two varying vec2s instead of using a dependent texture read. i.e. cameraTexCoord and viewfinderTexCoord. You can avoid a mediump calculation in the fragment shader, and take advantage of the hardware's (hopefully) optimized vert-frag interpolation.

There's really no reason to use lowp for texture coordinates, unless they are dependent, in which case lowp may be better for the fragment shader calculations. Otherwise, lowp has not resulted in any gains, in my experience (I only know PowerVR hardware on iOS though).