在过剩的显示功能中使用对象
我在使用过剩 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();
};
Modelization.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 声明为静态成员,但这不允许我使用 Cam 对象,知道吗?
谢谢 !!!
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 !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C++ 中,
static
方法使用的数据成员和方法也必须声明为static
。最简单的方法是将Cam
声明为static
。您还必须静态初始化它,即在您的实现文件中:(
请注意,根据
Cam
的其他使用方式,您可能会向 静态初始化惨败。)In C++, data members and methods that a
static
method uses must also be declaredstatic
. The easiest way out of this is to declareCam
to bestatic
.You'll also have to initialize it statically, that is, in your implementation file:
(Note that, depending on how else
Cam
is used, you might open yourself up to the static initialization fiasco.)您不能这样做,因为您的
void DisplayFunction ();
不是静态的,并且 glutDisplayFunc 需要一个函数指针。将您的 Modelization 类更改为:它会起作用
You can not do that, because your
void DisplayFunction ();
is not static, and glutDisplayFunc expects a function pointer. Change your Modelisation class to this :and it will work
您想要做的本质上是使用非静态成员函数作为 C 回调,这是 C++ 中最荒谬的困难部分之一。
很好地概述了为什么这在 C++03 中不容易工作这个 StackOverflow 问题和解决示例。
What you are trying to do is essentially to use a non-static member function as a C callback, which is one of the ridiculous hard parts of C++.
A good overview why this doesn't work easily in C++03 is given in this StackOverflow question and examples to work around.