0

I try to develop an android application that swap the pixels (of the camera preview) bottom <=> top. swap diagram

So I wrote this vertex shader but it displays very strange things (a black screen with digital noise).

attribute vec4 vPosition;
attribute vec2 inputTextureCoordinate;
varying vec2 textureCoordinate;
void main()
{
    if (vPosition.y > 0.0)
    {
        gl_Position = vPosition - vec4(0.0, 1.0, 0.0, 0.0);
    }
    else
    {
        gl_Position = vPosition + vec4(0.0, 1.0, 0.0, 0.0);
    }
    textureCoordinate = inputTextureCoordinate;
}

But this vertex shader below works, but it only moves top pixels to bottom.

attribute vec4 vPosition;
attribute vec2 inputTextureCoordinate;
varying vec2 textureCoordinate;
void main()
{
    if (vPosition.y > 0.0)
    {
        gl_Position = vPosition - vec4(0.0, 1.0, 0.0, 0.0);
    }
    else
    {
        gl_Position = vPosition;
    }
    textureCoordinate = inputTextureCoordinate;
}

Do you know how to swap the pixels as in my diagram (make my first vertex shader work)? Thank you

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
far38
  • 13
  • 5

1 Answers1

1

Avoid if statement in shaders performance may deteriorate.

Simple solution may be to swap texture coordinates in fragment shader. I suppose your v texture coordinates are going from 0 to 1 along whole height of the quad where camera image is rendered. Then following code in fragment shader should do the trick.

textureCoordinate = vec2(textureCoordinate.u, 1 - textureCoordinate.v);

But, the actual texture coordinate swap depends on way you define your texture coordinates at the quad's vertices.

Community
  • 1
  • 1
plastique
  • 1,164
  • 2
  • 9
  • 19
  • I second that. You need to swap the texture coordinates, not the vertex positions. The reason why you get garbage on screen is because you move some of the vertices off the screen. – Romain Guy May 23 '16 at 16:31
  • Thank you! But in the fragment shader? Because I think I have to avoid running the processing in the fragment shader because after the swap I have to zoom in a part of the image ... Do you think it's possible to do all in the vertex shader? – far38 May 23 '16 at 17:34
  • Swap and zoom is possible either in fragment or vertex shader. Use input uniforms to define your zoom (min_u, max_u, min_v, max_v). Then in vertex shader assign left bottom vertex texture coordinates of (min_u, max_v), left upper (min_u, min_v), right upper (max_u, min_v), right bottom (max_u, max_v). Texture coordinates needs to be varying. This way you will accomplish both. Zoom and swap in vertex shader. – plastique May 23 '16 at 17:47
  • Thank you for all your answers, it works ! But is it normal that for me the max is not 1.0 but 0.5 ? (it works perfectly with 0.5 but it's very strange with 1.0). But I have a last question (sorry), imagine that I would like to widen one of the half picture. How can ? Because if I do it with my shader, a part of the wide half will be off the screen ? Or maybe, I can do this and after I can zoom out the surface to view all the wide part ? Thank you – far38 May 23 '16 at 19:28
  • I do not really see what you mean. I suppose you are rendering the image using screen aligned quad (4 vertices). Then the idea of zooming it is that instead of mapping whole image space (0-1 in both u and v coordinate), you will select only part of the image (our max/min u and v coordinates) to be rendered in the quad. So it is perfectly ok that you boundary values are different than 1 and 0. As long as they stay at 0-1 interval. Try to draw it on paper. It will become more clear. – plastique May 23 '16 at 20:47
  • Ok thank you for your answer ! For my second question, I create a new question (which is easy to understand) http://stackoverflow.com/questions/37410154/opengl-es-how-display-off-screen do not hesitate to respond if you have an answer :) Thank you – far38 May 24 '16 at 09:55
  • And I have a last question for the main answer, do you know it works for me with "vec2 tc = vec2(textureCoordinate[1], 0.5 - textureCoordinate[1]);" but not with "textureCoordinate = vec2(textureCoordinate[1], 0.5 - textureCoordinate[1]);" not with "vec2 tc = vec2(textureCoordinate.u, 0.5 - textureCoordinate.v);" and not with "vec2 tc = vec2(textureCoordinate[1], 1.0 - textureCoordinate[1]);". So I can't modify textureCoordinate, I can't use .u et .v and I can't translate with 1.0. Have you any tips? Thank you – far38 May 24 '16 at 12:32