在提出问题之前,我将介绍部分代码以更好地解释它。
我正在使用OpenGL 3.3
和GLFW
用于鼠标事件。
我有我的OpenGL class
:
class OpenGL
{
public:
OpenGL();
~OpenGL();
private:
//(...)
void registerCallBacks();
static void mouseMove(GLFWwindow* window, double xpos, double ypos);
static void mouseClick(GLFWwindow* window, int button, int action, int mods);
GLFWwindow* m_window;
};
我在哪里注册callbacks
了鼠标事件。
void OpenGL::registerCallBacks()
{
glfwSetWindowUserPointer(m_window, this);
glfwSetCursorPosCallback(m_window, mouseMove);
glfwSetMouseButtonCallback(m_window, mouseClick);
}
从回调中调用的方法是这些(static
在头文件中):
void OpenGL::mouseMove(GLFWwindow* window, double xpos, double ypos)
{
void* userPointer = glfwGetWindowUserPointer(window);
Mouse* mousePointer = static_cast<Mouse*>(userPointer);
mousePointer->move(xpos,ypos); //EXECUTE MOVE AND CRASH on glfwSwapBuffers()
}
void OpenGL::mouseClick(GLFWwindow* window, int button, int action, int mods)
{
void* userPointer = glfwGetWindowUserPointer(window);
Mouse* mousePointer = static_cast<Mouse*>(userPointer);
mousePointer->click(button,action); //EXECUTE CLICK AND IT'S OK!!
}
如您所见,我有一个处理鼠标事件的Mouse类:
class Mouse
{
public:
Mouse();
~Mouse();
void click(const int button, const int action); //called from the mouseClick() in the OpenGL class
void move(const double x, const double y); //called from the mouseMove() in the OpenGL class
private:
double m_x;
double m_y;
};
凡move
方法只是这样的:
void Mouse::move(const double x, const double y)
{
m_x = x;
m_y = y;
}
而click
方法仅仅是这样:
void Mouse::click(const int button, const int action)
{
printf("button:%d, action:%d\n",button, action);
}
我的问题是:
我的openGL主循环glfwSwapBuffers(m_window);
在循环的末尾有::如果我使用Mouse::move()
上面显示的方法,它将在此行崩溃。如果我评论move()
方法的内容,那完全没有问题。
我什printf's
至可以click()
正确地看到。
我发现move()和click()方法之间没有区别...
这是怎么回事 为什么glfwSwapBuffers(m_window);
只有在使用时才会出现崩溃move()
?又为什么不click()
使用它们各自使用相同的方式构造它们callbacks
呢?
注意:我确实需要使用该move()
方法来“保存”鼠标坐标,以便稍后在该click()
方法上使用。
错误:
Unhandled exception at 0x001F2F14 in TheGame.exe: 0xC0000005: Access violation reading location 0x4072822C.
您正在将GLFW的用户指针设置this
为class对象的指针OpenGL
,但是在回调中,将其强制转换为class Mouse
。这些类之间也没有继承关系,因此通过该指针访问任何成员变量或方法都将导致未定义的行为,这在您的情况下表示为崩溃。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句