7

I want to use a framebuffer to create an image using a shader program. This works well, except that the Y is inverted. It seems that the bottom left corner becomes the top left corner. Why does this happen? Is this normal? Does this happen on all android devices?

Inside my shader program gl_FragCoord will have inverted Y, depending on if i draw to the framebuffer or to the screen directly.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Alexanus
  • 679
  • 4
  • 22
  • 3
    In normalized device space the bottom left is (-1, -1) and the top right is (1, 1). The transformation of the view coordinates to normalized device space depends on the projection (matrix). Do you use orthographic projection? You've to add some code to make this an [Minimal, **Complete, and Verifiable example**](https://stackoverflow.com/help/mcve). – Rabbid76 May 06 '19 at 17:13
  • i am not using any kind of matrix, the shader program gets gl_FragCoord as input for each pixel. But the y will be inverted when drawing to a framebuffer and not to the screen directly. – Alexanus May 06 '19 at 17:15
  • When you use a texture then the texture coordinates (0, 0) are the bottom left and (1, 1) are the top right. See [How do opengl texture coordinates work?](https://stackoverflow.com/questions/5532595/how-do-opengl-texture-coordinates-work) – Rabbid76 May 06 '19 at 17:20
  • *"Inside my shader program gl_FragCoord will have inverted Y, depending on if i draw to the framebuffer or to the screen directly."* how do you know that? Do you render the framebuffer to the screen? – Rabbid76 May 06 '19 at 17:43
  • 1
    i use the framebuffer to create a texture, this texture i render to the screen or i save it as a file, in both cases it is flipped. – Alexanus May 06 '19 at 17:50
  • Ok. Do you consider that `gl_FragCoord.xy` == (0, 0) is the lower left? – Rabbid76 May 06 '19 at 17:54
  • yes normally it is the lower left, but when i draw to a framebuffer it becomes the top left. X stays the same, but Y gets flipped – Alexanus May 06 '19 at 18:06
  • Can you post the code? – Ricardo A. May 14 '19 at 13:12

1 Answers1

0

I found out that drawing to the FrameBuffer does not invert the Y but loading a bitmap to opengl does.
These were my texture vertices:

public final static float textureVertices[] = {
        0.0f, 1.0f, // top left
        0.0f, 0.0f, // bot left
        1.0f, 1.0f, // top right
        1.0f, 0.0f, // bot right
};

But those are already inverted to account for the inverted bitmap, i thought this was normal, but normal for opengl is this:

public final static float textureVertices[] = {
        0.0f, 0.0f, // bot left
        0.0f, 1.0f, // top left
        1.0f, 0.0f, // bot right
        1.0f, 1.0f, // top right
};

So if i create a texture from a bitmap like this:

public static int loadGLTexture(Bitmap bmp) { // also destroys bitmap
    int id[] = new int[1];
    id[0] = -1;

    GLES20.glGenTextures(1, id, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, id[0]);

    // Set Filters
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bmp, 0); // upload the texture to OpenGL

    bmp.recycle();
    return id[0];
}

The texture will have inverted Y, but if i create an empty texture with opengl and draw on it using a FrameBuffer, the Y will not be inverted.

Alexanus
  • 679
  • 4
  • 22