Search code examples
c++glutfreeglut

Using Objects into a Glut Display Function


i'm having trouble using an object in a glut DisplayFunction.

class Modelisation
{
private:
    int hauteur, largeur, x, y;
    Camera *Cam;

    void DisplayFunction ();
    static void RedisplayFunction (int, int);

public:
    Modelisation (int argc, char **argv, char[]);
    ~Modelisation ();

    void StartMainLoop();
};

Modelisation.cpp

Modelisation::Modelisation (int argc, char **argv, char windowName [])
{
    Cam = new Camera;
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE);
    glutCreateWindow (windowName);
};
void Modelisation::StartMainLoop()
{
    glutDisplayFunc(DisplayFunction);
    glutIdleFunc(DisplayFunction);
    glutReshapeFunc(RedisplayFunction);
    glutMainLoop(); 
}
void Modelisation::DisplayFunction()
{
    glClearDepth (1);
    glClearColor (0.0f, 0.0f, 0.0f, 0.0f); 
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
    glLoadIdentity ();
    Cam->Render ();
    glFlush ();
    glutSwapBuffers ();
}

glutDisplayFunc(DisplayFunction); glutIdleFunc(DisplayFunction);

This doesn't work. I know that i can declare DisplayFunction as a static member, but this won't allow me to use the Cam Object, any idea ?

Thx !!!


Solution

  • In C++, data members and methods that a static method uses must also be declared static. The easiest way out of this is to declare Cam to be static.

    You'll also have to initialize it statically, that is, in your implementation file:

    Modelisation::Camera* Cam = new Camera();
    

    (Note that, depending on how else Cam is used, you might open yourself up to the static initialization fiasco.)