Search code examples
pythonopenglfliptexturing

how can i flip a cairo surface before saving with python?


I am creating textures using cairo and saving, the issue is if i use the surface directly with opengl its flipped vertically.

if i save and load i can use pil and im.tostring() to flip it.

is there a good way to flip it the cairo surface, perhaps saving my png then flipping for use or saving the images upside down.

alternatively can i create a PIL Image and share the buffer with ciaro so i can then use PIL tostring method to orient the texture.

open to suggestions, not much info on mixing cairo and opengl with python.


Solution

  • One solution is to adjust your texture coordinate or apply a scaling matrix (1,-1) to the texture matrix if old matrix stack is in use.

    Another way is to invert pixel (texcoord.y = 1.0 -texcoord.y) in a shader yourself rendering to a new texture via a FBO. (that's a good option)

    uniform sampler2D diffuseTexture;
    
    void main (void)  
    {
        vec2 t = gl_TexCoord[0].st;
        t.y = 1.0 - t.y;
        vec4 c = texture2D(diffuseTexture, t);          
        gl_FragColor = c;                         
    }