Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

why we draw on screen fine but framebufer is flipped y, webgl?

  1. User

    why we draw on screen fine but framebufer is flipped y, webgl?

  2. Assistant

    In WebGL, the coordinate system's origin is at the bottom-left corner, while in most 2D screen or drawing contexts, the origin is at the top-left corner. This difference in coordinate systems causes the y-axis to be flipped when you draw on a framebuffer in WebGL. The rendered image appears vertically flipped compared to what you'd expect from a typical 2D drawing context.

    To fix this issue, you can do one of the following:

    1. Flip your texture coordinates: Modify the texture coordinates (y-component) of your vertices in the vertex buffer to be (1 - y) instead of y. This way, you can flip the y-axis when the image is being drawn on the WebGL framebuffer.

    2. Flip the Y value in the vertex shader: In your vertex shader, you can multiply the model-space y-coordinate with -1 before performing your transformations. This will flip the coordinates and hence, the framebuffer won't appear flipped. For example:

    vec4 position = vec4(a_position.x, -a_position.y, a_position.z, 1.0);
    gl_Position = u_projectionMatrix * u_viewMatrix * u_modelMatrix * position;
    
    1. Invert the framebuffer readout: When reading the framebuffer's content to an HTML Image, Canvas or using glReadPixels, you can invert the image to get the correct orientation.

    Please note that the above solutions are suggestions, and you can use any of them according to your need and situation in your WebGL application.

  3. User

    how to invert the framebuffer with gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);

  4. Assistant

    If you want to flip the WebGL texture while uploading it from an HTML Image or Canvas, you can use the gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1) option, not 0. This will flip the image vertically during the pixel transfer operation. Here's an example:

    const texture = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, texture);
    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // Set the flag to flip the y-axis
    
    // Upload the image to the texture
    const level = 0;
    const internalFormat = gl.RGBA;
    const format = gl.RGBA;
    const type = gl.UNSIGNED_BYTE;
    gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, format, type, image);
    
    // Set other texture parameters like filtering and wrapping
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
    

    Note that, in the code above, we've set gl.UNPACK_FLIP_Y_WEBGL to 1. This option is useful when you want to flip the texture source (like an image, video or canvas) on the y-axis during the pixel transfer operation from the source to the WebGL texture.

    If you want to revert back to not flipping the image, you can set the flag back to 0 like so:

    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
    

    Keep in mind, this only affects the texture uploads (pixel transfer operations) occurring after setting the flag. It does not modify the content of the framebuffer itself. However, if you render the aforementioned texture into the framebuffer, the rendered image will appear correctly (not flipped) as it takes care of the flipped y-axis during the uploading process to the texture.