如何使用回调更新 GLFWWindow 中的 Mujoco 可视化?

问题描述 投票:0回答:0

我想将 mujoco 与 GLFW 的可视化结合使用。 在 mujoco 上我找到了可视化示例。 在此示例中,您构建了一个 mujoco 模型并在 glfw 中显示它。要在模拟中移动相机,您需要回调函数,例如此处的 mouse_button。但这一切只有在这个回调函数是静态的并且可以直接访问全局 mujoco 变量的情况下才有效。

我想要的是创建一个自己的 mujoco 类,如果需要可视化,则使用 glfw 内容调用可视化类。但在这种情况下,回调函数需要访问类成员对象,而这在我的情况下不起作用。有人知道如何处理这些回调函数问题吗?

#include <mujoco/mujoco.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

// MuJoCo data structures
mjModel* m = NULL; // MuJoCo model
mjData* d = NULL; // MuJoCo data
mjvCamera cam; // abstract camera
mjvOption opt; // visualization options
mjvScene scn; // abstract scene
mjrContext con; // custom GPU context

// mouse button callback
void
mouse_button(GLFWwindow* window, int button, int act, int mods)
{
    // update button state
    button_left = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS);
    button_middle = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS);
    button_right = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS);

    // update mouse position
    glfwGetCursorPos(window, &lastx, &lasty);
}

int
main(int argc, const char* argv[])
{

char error[1000];
    std::string const filePath =
        "robot_file.xml";
    m = mj_loadXML(filePath.c_str(), nullptr, error, 1000);
    d = mj_makeData(m);
// init GLFW, create window, make OpenGL context current, request v-sync
    glfwInit();
    GLFWwindow* window = glfwCreateWindow(1200, 900, "Demo", NULL, NULL);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);


    // initialize visualization data structures
    mjv_defaultCamera(&cam);
    //    mjv_defaultPerturb(&pert);
    mjv_defaultOption(&opt);
    mjr_defaultContext(&con);
    // create scene and context
    mjv_makeScene(m, &scn, 1000);
    mjr_makeContext(m, &con, mjFONTSCALE_100);


    // install GLFW mouse and keyboard callbacks
    //glfwSetKeyCallback(window, keyboard);
    //glfwSetCursorPosCallback(window, mouse_move);
    glfwSetMouseButtonCallback(window, mouse_button);
    //glfwSetScrollCallback(window, scroll);


    // run main loop, target real-time simulation and 60 fps rendering
    while (!glfwWindowShouldClose(window))
    {
        // advance interactive simulation for 1/60 sec
        //  Assuming MuJoCo can simulate faster than real-time, which it usually can,
        //  this loop will finish on time for the next frame to be rendered at 60 fps.
        //  Otherwise add a cpu timer and exit this loop when it is time to render.

        mjtNum simstart = d->time;
        while (d->time - simstart < 1.0 / 60.0)
        {
            mj_step(m, d);
        }

        // get framebuffer viewport
        mjrRect viewport = {0, 0, 0, 0};
        glfwGetFramebufferSize(window, &viewport.width, &viewport.height);

        // update scene and render
        mjv_updateScene(m, d, &opt, NULL, &cam, mjCAT_ALL, &scn);
        mjr_render(viewport, &scn, &con);

        // swap OpenGL buffers (blocking call due to v-sync)
        glfwSwapBuffers(window);

        // process pending GUI events, call GLFW callbacks
        glfwPollEvents();
}

我已经尝试直接调用回调函数作为类实例的成员函数,但这不起作用。

c++ callback glfw mujoco
© www.soinside.com 2019 - 2024. All rights reserved.