GL question : image upside down in Render In Image

benoitlahoz's picture

Hi !

I'm working on a plugin that will be used this next week end for a performance.

This plugin is rendering GL lines and triangles, and I have this trouble : my render is always upside down in render in image.

Is there a good practice to manage this render in image / GL issue ? I'm looking at the source code of many of the generous people posting their codes, but I can't find how !

I don't know GL at all for the moment. it'll be my next step :-)

Here is the (simple) code I'm using, I know there's something to do with the glOrtho and the width and height, but what... :

    double width = context.bounds.size.width;
    double height = context.bounds.size.height;
 
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(-(width/2.) , (width/2.), 
            -(height/2.) , (height/2.), 
            -1, 1);
 
 
    if(self.inputBlendMode == 0) {
        glDisable(GL_BLEND);
    } else if(self.inputBlendMode == 1) {
        glEnable(GL_BLEND);
        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    } else if(self.inputBlendMode == 2) {
        glEnable(GL_BLEND);
        glBlendFunc(GL_ONE, GL_ONE);
    } else if(self.inputBlendMode == 3) {
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    }
 
    glEnable(GL_LINE_SMOOTH);   
    glEnable(GL_POINT_SMOOTH);
 
 
    // RENDER
    for (id triangulation in trianglesArray) {
 
        for (id triangle in triangulation) {
 
            if (self.inputMode == 0 || self.inputMode == 2) {
 
                glColor4f(1., 1., 1., 0.5);
 
                glBegin(GL_TRIANGLE_FAN);
                {
                    for (id edge in triangle) {
 
                        glVertex2d([[edge objectAtIndex:0] doubleValue], [[edge objectAtIndex:1] doubleValue]);
 
                    }
 
                }
                glEnd();
            }
 
            if (self.inputMode == 1 || self.inputMode == 2) {
 
                glLineWidth((GLfloat)1.);
                glColor4f(1., 0., 0., 1.);
 
                glBegin(GL_LINE_LOOP);
                {
 
                    for (id edge in triangle) {
 
                        glVertex2d([[edge objectAtIndex:0] doubleValue], [[edge objectAtIndex:1] doubleValue]);
 
                    }
 
                }
                glEnd();
            }
 
        }
 
    }
 
 
    glDisable(GL_BLEND);
    glDisable(GL_LINE_SMOOTH);   
    glDisable(GL_POINT_SMOOTH);
    glPopMatrix();
 

Thank you !

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

benoitlahoz's picture
Re: GL question : image upside down in Render In Image

I'm sorry that I didn't remember I had already asked this question one year ago : http://kineme.net/forum/Programming/RenderinImageflipped

This "edit" makes me post you the right code (the previous one was a WIP one, sorry again) :

    glPushAttrib(GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);
 
    GLint viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);
    GLfloat w = viewport[2];
    GLfloat h = viewport[3];
 
    GLfloat lWidth[2];
    glGetFloatv(GL_LINE_WIDTH_RANGE, lWidth);
 
 
 
 
 
 
 
    if(self.inputBlendMode == 0) {
        glDisable(GL_BLEND);
    } else if(self.inputBlendMode == 1) {
        glEnable(GL_BLEND);
        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    } else if(self.inputBlendMode == 2) {
        glEnable(GL_BLEND);
        glBlendFunc(GL_ONE, GL_ONE);
    } else if(self.inputBlendMode == 3) {
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    }
 
    glEnable(GL_POLYGON_SMOOTH);
    glHint(GL_POLYGON_SMOOTH_HINT,GL_NICEST); 
    glEnable(GL_LINE_SMOOTH);   
    // glHint(GL_LINE_SMOOTH,GL_NICEST); -> doesn't work here
 
    glClearColor(0.f, 0.f, 0.f, 0.f);
 
 
    // ViewPort
    glViewport(0, 0, w, h);
 
 
    // Sauvegarde l'état et configure la matrice de projection
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
 
    glOrtho(-(width/2.) , (width/2.), 
            -(height/2.) , (height/2.),    // -> HERE IS THE PROBLEM
            -1, 1);            
 
 
 
 
 
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
 
    glLoadIdentity();
 
 
 
    for (id triangulation in trianglesArray) {
 
        for (id triangle in triangulation) {
 
            if (self.inputMode == 0 || self.inputMode == 2) {
 
 
 
                glColor4f(1., 1., 1., 0.5);
 
                glBegin(GL_TRIANGLE_FAN);
                {
                    for (id edge in triangle) {
 
                        glVertex2d([[edge objectAtIndex:0] doubleValue], [[edge objectAtIndex:1] doubleValue]);
 
                    }
 
                }
                glEnd();
 
 
            }
 
            if (self.inputMode == 1 || self.inputMode == 2) {
 
                glLineWidth((GLfloat)1.);
                glColor4f(1., 0., 0., 1.);
 
                glBegin(GL_LINE_LOOP);
                {
 
                    for (id edge in triangle) {
 
                        glVertex2d([[edge objectAtIndex:0] doubleValue], [[edge objectAtIndex:1] doubleValue]);
 
                    }
 
                }
                glEnd();
 
 
            }
 
        }
 
    }
 
 
 
 
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
 
 
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
 
    glDisable(GL_LINE_SMOOTH);
    glDisable(GL_POLYGON_SMOOTH);
 
    glDisable(GL_BLEND);
 
    glPopAttrib();

Thanks again.

benoitlahoz's picture
Re: GL question : image upside down in Render In Image

Well... I managed to do it : I simply don't call the GL_PROJECTION ang glOrtho... I don't know if it is a good way to do it, but well... it works !

vade's picture
Re: GL question : image upside down in Render In Image

You should not need to set the viewport or the ortho coordinates, really you should just blast out vertices assuming you are a consumer patch that just draws geometry. QC will handle the GL State setup for your (projection, ortho, viewport according to the docs (this is spelled out for you), and dump you into GL_MODELVIEW matrix mode when it hits your consumer. Additionally, you don't really need to load identity on those transforms.

Something like the v002 Model loader does none of that setup,

essentially all we do is:

CGLContextObj cgl_ctx = [context CGLContextObj];

glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);

// setup the state defaults we need, capturing any changes the above attribs don't capture

// Draw our verts

glPopClientAttrib();
glPopAttrib();

benoitlahoz's picture
Re: GL question : image upside down in Render In Image

A big big thank you vade !

voxdeserti's picture
Re: GL question : image upside down in Render In Image

Just have met the same problem. A shader comp from Shadertoy collection put into Render In Image patch flips vertically while simple comps don't...