Legacy OpenGL integration

Hello,

I want to integrate some old OpenGL code (OpenGL 2) into OF.
This code is generated by a code generator so I cannot modify it.
How can I integrate it ?
I’ve tried to display the famous colored triangle

glBegin(GL_TRIANGLES);                               
        glColor3f(1.0f,0.0f,0.0f); 
        glVertex3f( 0.0f, 1.0f, 0.0f);                   
        glColor3f(0.0f,1.0f,0.0f); 
        glVertex3f(-1.0f,-1.0f, 0.0f); 
        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f( 1.0f,-1.0f, 0.0f); 
glEnd();

But I only get the grey background of OF…
Is it possible to integrate such code in OF ?

Thanks

Hi,

Hmm, that should work I would have thought, unless you have switched to the programmable renderer.

And you’re not on an exotic platform like a tiny Linux board right? Then you might be using OpenGL ES behind the scenes and that won’t work.

Your legacy code could be expecting a different camera transform that what you have set up now as well, you could try drawing a triangle next to that using ofTriangle() and see if that shows up to debug.

Hello,

At that time I’m developping on Windows - so no exotic platform… The version of OF is the v20150422 nightly build.
No programmable render (…yet…I anticipate difficulties, I think I would need to setup a secondary context and render to a FBO)
I tested with ofTriangle() as suggested and I managed to have something on the screen…

I finally came back to this very basic OF program
main.cpp is only

int main( ){
    ofSetupOpenGL(1400,768, OF_WINDOW);
    ofRunApp( new ofApp());
}

And the ofApp is

//--------------------------------------------------------------
void ofApp::setup(){
}

//--------------------------------------------------------------
void ofApp::update(){
}

//--------------------------------------------------------------
void ofApp::draw(){
    // Green Triangle
    glLoadIdentity();
    glColor3f(0.0f,1.0f,0.0f);
    glBegin(GL_TRIANGLES);
        glVertex3f( 0.0f, 100.0f, 0.0f);
        glVertex3f(-100.0f,-100.0f, 0.0f);
        glVertex3f( 100.0f,-100.0f, 0.0f);
    glEnd();

    // Red Triangle
    glLoadIdentity();
    glTranslatef(-100,0,-660.0f);
    glColor3f(1.0f,0.0f,0.0f);
    glBegin(GL_TRIANGLES);
        glVertex3f( 0.0f, 100.0f, 0.0f);
        glVertex3f(-100.0f,-100.0f, 0.0f);
        glVertex3f( 100.0f,-100.0f, 0.0f);
    glEnd();


    // Blue Triangle
    glLoadIdentity();
    glTranslatef(100,0,660.0f);
    glColor3f(0.0f,0.0f,1.0f);
    glBegin(GL_TRIANGLES);
        glVertex3f( 0.0f, 100.0f, 0.0f);
        glVertex3f(-100.0f,-100.0f, 0.0f);
        glVertex3f( 100.0f,-100.0f, 0.0f);
    glEnd();

}

It turns out that only the red triangle shows up.
Playing around with the z-translation, I found that only triangles in the (-70, -6600) range were drawn. This range matches with the zNear, zFar setup by oF…

So now I have to find a way to bring my green triangle (the one with 0 translation) in the zNear/zFar range wether by changing the perspective or by a translation…

Hello,

I finally found a solution by changing the projection matrix
Here is the updated code

void ofApp::draw(){

    float halfWidth = ofGetWidth() / 2.0;
    float halfHeight = ofGetHeight() / 2.0;
    float zNear = -660;
    float zFar = 660;
    ofSetMatrixMode(OF_MATRIX_PROJECTION);
    ofLoadMatrix( ofMatrix4x4::newOrthoMatrix(-halfWidth, halfWidth, -halfHeight, halfHeight, zNear, zFar) );

    ofSetMatrixMode(OF_MATRIX_MODELVIEW);
    // Green Triangle
    glLoadIdentity();
    glTranslatef(0,0,0.0f);
    glColor3f(0.0f,1.0f,0.0f);
    glBegin(GL_TRIANGLES);
        glVertex3f( 0.0f, 100.0f, 0.0f);
        glVertex3f(-100.0f,-100.0f, 0.0f);
        glVertex3f( 100.0f,-100.0f, 0.0f);
    glEnd();

    // Red Triangle
    glLoadIdentity();
    glTranslatef(-100,0,-660.0f);
    glColor3f(1.0f,0.0f,0.0f);
    glBegin(GL_TRIANGLES);
        glVertex3f( 0.0f, 100.0f, 0.0f);
        glVertex3f(-100.0f,-100.0f, 0.0f);
        glVertex3f( 100.0f,-100.0f, 0.0f);
    glEnd();

    // Blue Triangle
    glLoadIdentity();
    glTranslatef(100,0,660.0f);
    glColor3f(0.0f,0.0f,1.0f);
    glBegin(GL_TRIANGLES);
        glVertex3f( 0.0f, 100.0f, 0.0f);
        glVertex3f(-100.0f,-100.0f, 0.0f);
        glVertex3f( 100.0f,-100.0f, 0.0f);
    glEnd();
}

I’m pretty sure that we can do the same thing with ofSetupScreen, ofSetupScreenOrtho,…
Help welcome…