从类引用GLFW的回调函数

时间:2014-12-09 19:17:54

标签: function class opengl glfw

所以,我有这个简单的类,女巫初始化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 Callback:

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类中访问成员。

1 个答案:

答案 0 :(得分:3)

您可以使用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);
}

重复其他回调。