glutDisplayFunc displays garbage
===

I am trying to incorporate openGL into my c++ code for the first time. As a start up, I made this very primitive code, which defines a class called polygon and should display a polygon with a method polygon.draw(). Right now, everything below resides in a single main.cpp file, though for easy reading I am separating into section here:

The problem is, the below code compiles and runs alright. Only when the window named "simple" is created, displays garbage (mostly collected from my computer background screen :(. Any help is appreciated!

Firstly, the class polygon:

    #include <GL/glut.h>
    #include "utility.hpp"
    #include <vector>

    void init(void);

    class nikPolygon{
    public:
        std::vector<nikPosition> m_vertices;
        nikColor m_color;
        double m_alpha;

        // constructors
        // without alpha (default is 1.0)
        nikPolygon(std::vector<nikPosition> vList, nikColor c):
        m_vertices(vList), m_color(c), m_alpha(1.0){
        
        }
    
        nikPolygon(std::vector<nikPosition> vList, nikColor c, double a):
        m_vertices(vList), m_color(c), m_alpha(a){
    
        }

        // default constructor
        nikPolygon(){
            
        }
    
    
        // member functions
        // add vertex
        void addVertex(nikPosition v)              { m_vertices.push_back(v); }
        // remove vertex
        void removeVertex(nikPosition v);
        // adjust vertex
        void modifyVertex(unsigned int vIndex, nikPosition newPosition);
        // fill color
        void setColor(nikColor col)               { m_color = col; }
        // set alpha
        void setAlpha(double a)                   { m_alpha = a; }
        // display
        void drawPolygon(void){
        
            // color the objet
            glColor4f(m_color.red, 
                      m_color.green, 
                      m_color.blue, 
                      m_alpha);
        
            // construct polygon
            glBegin(GL_POLYGON);    
            for (std::vector<nikPosition>::iterator it = m_vertices.begin();
                 it != m_vertices.end(); it++)
                glVertex2f(it->x, it->y);
            glEnd();
        
            // send to screen
            glFlush();
        
        }
    
        void draw(void);
    };

Then the c/c++ callback interface (trampoline/thunk):


     // for c++/c callback
     nikPolygon* currentPolygon;

     extern "C"
     void drawCallback(void){
         currentPolygon->drawPolygon();
     }

     void nikPolygon::draw(){
         currentPolygon = this;
         glutDisplayFunc(drawCallback);
     }


And then the rest of it:


     // initialize openGL etc
     void init(void){
    
         // set clear color to black
         glClearColor(0.0, 0.0, 0.0, 0.0);
    
         // set fill color to white
         glColor3f(1.0, 1.0, 1.0);
    
         // enable transperancy
         glEnable(GL_BLEND);
         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
         // setup standard orthogonal view with clipping
         // box as cube of side 2 centered at origin
         // this is the default view
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity();
         gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
    
     }


     int main(int argc, char** argv){
    
         nikPolygon poly;
    
         poly.addVertex(nikPosition(-0.5, -0.5));
         poly.addVertex(nikPosition(-0.5, 0.5));
         poly.addVertex(nikPosition(0.5, 0.5));
         poly.addVertex(nikPosition(0.5, -0.5));
    
         poly.setColor(nikColor(0.3, 0.5, 0.1));
         poly.setAlpha(0.4);
    
    
         glutInit(&argc, argv);
         glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
         glutInitWindowSize(500, 500);
         glutInitWindowPosition(0, 0);
    
         glutCreateWindow("simple");
         init();
         poly.draw();
         glutMainLoop();
     }


TIA,
Nikhil
