所以,我有一个简单的类,初始化GLFW并创建一个窗口:
namespace ALT {
struct InputInformation {
GLfloat lastX;
GLfloat lastY;
bool keyboard[1024];
};
class Window {
public:
Window(GLuint width, GLuint height, std::string title);
~Window();
int run();
private:
void setUp();
void mainLoop();
GLuint _width;
GLuint _height;
GLFWwindow* _window;
std::vector<Shader> _shaders;
std::unique_ptr<ALT::Heightmap> _heightmap;
std::unique_ptr<Model> _nanosuit;
std::unique_ptr<ALT::Camera> _camera;
void executeKeyboardInput();
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
ALT::InputInformation _input;
};
}现在我想从构造函数中设置Mouse和Key回调:
ALT::Window::Window(GLuint width, GLuint height, std::string title)
: _width(width), _height(height)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
_window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
glfwMakeContextCurrent(_window);
glfwSetInputMode(_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetKeyCallback(_window, ALT::Window::key_callback);
glfwSetCursorPosCallback(_window, ALT::Window::mouse_callback);
glewExperimental = GL_TRUE;
glewInit();
}这是我得到的错误:
cannot convert ‘ALT::Window::mouse_callback’ from type ‘void (ALT::Window::)(GLFWwindow*, double, double)’ to type ‘GLFWcursorposfun {aka void (*)(GLFWwindow*, double, double)}’
glfwSetCursorPosCallback(_window, ALT::Window::mouse_callback);这两个回调函数的错误是相同的。有没有可能像我这样做这个回调函数?
我过去常常在main()上这样做,所以我要简单得多。
我也可以在类之外声明回调函数,但是这样我就不能从Window类访问成员了。
发布于 2014-12-10 05:01:10
您可以使用glfwSetWindowUserPointer并将this指针传递给它,然后它只是在外部函数中强制转换:
ALT::Window::Window(GLuint width, GLuint height, std::string title)
: _width(width), _height(height)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
_window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
glfwMakeContextCurrent(_window);
glfwSetWindowUserPointer(_window, reinterpret_cast<void*>(this));//<--- right here
glfwSetInputMode(_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetKeyCallback(_window, ALT::key_callback);
glfwSetCursorPosCallback(_window, ALT::mouse_callback);
glewExperimental = GL_TRUE;
glewInit();
}
void mouse_callback(GLFWwindow *window, double xpos, double ypos){
Window wind = reinterpret_cast<Window*>(glfwGetWindowUserPointer(window));
wind->mouse_callback(xpos, ypos);
}对其他回调重复上述步骤。
https://stackoverflow.com/questions/27387040
复制相似问题