2011-04-18 2 views
4

glut DisplayFunction에서 개체를 사용하는 데 문제가 있습니다.개체를 glut 표시 함수에 사용

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);

이것은 작동하지 않습니다. 나는 DisplayFunction을 정적 멤버로 선언 할 수 있다는 것을 알고 있지만, 이것이 Cam 객체를 사용하는 것을 허용하지 않습니다.

Thx !!!

답변

0

C++에서 static 메서드가 사용하는 데이터 멤버 및 메서드도 static으로 선언되어야합니다. 가장 쉬운 방법은 Camstatic으로 선언하는 것입니다.

또한 구현 파일에, 즉, 정적 초기화해야합니다 :

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

이 ( Cam 사용 방법 다른, 당신은 static initialization fiasco에 자신을 열 수에 따라 다음 사항을 유의하십시오.)

+0

미안하지만 헤더 파일에 정적 객체 Cam을 선언하려면 어떻게해야합니까? – Athanase

+0

헤더 파일에 '정적 Camera * Cam;'으로 선언하십시오. – Adrian

+0

죄송합니다. 실수를했습니다. 완벽하게 작동합니다. 나는이 포럼에서 질문 할 때 항상 완벽한 답을 얻습니다. 고맙습니다 !! – Athanase

0

void DisplayFunction();이 정적이 아니며 glutDisplayFunc가 함수 포인터를 필요로하기 때문에 그렇게 할 수 없습니다.

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

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

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

    void StartMainLoop(); 
}; 

그것은 당신이 할하려는 것은 말도 하드의 하나 인 C 콜백과 같은 비 정적 멤버 함수를 사용하는 것이 본질적으로

+0

감사합니다.하지만 내 개체 캠이 정적이 아니기 때문에이 방법이 효과가 없습니다. – Athanase

+1

glutDisplayFunc에 함수 포인터를 전달해야합니다. –

0

작동이에 Modelisation 클래스 변경 C++의 일부.

이것이 C++에서 쉽게 작동하지 않는 좋은 개요는 this StackOverflow question과 예제를 통해 해결됩니다.

관련 문제