basic.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2021 DeepMind Technologies Limited
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <cstdio>
  15. #include <cstring>
  16. #include <GLFW/glfw3.h>
  17. #include <mujoco/mujoco.h>
  18. // MuJoCo data structures
  19. mjModel* m = NULL; // MuJoCo model
  20. mjData* d = NULL; // MuJoCo data
  21. mjvCamera cam; // abstract camera
  22. mjvOption opt; // visualization options
  23. mjvScene scn; // abstract scene
  24. mjrContext con; // custom GPU context
  25. // mouse interaction
  26. bool button_left = false;
  27. bool button_middle = false;
  28. bool button_right = false;
  29. double lastx = 0;
  30. double lasty = 0;
  31. // keyboard callback
  32. void keyboard(GLFWwindow* window, int key, int scancode, int act, int mods) {
  33. // backspace: reset simulation
  34. if (act==GLFW_PRESS && key==GLFW_KEY_BACKSPACE) {
  35. mj_resetData(m, d);
  36. mj_forward(m, d);
  37. }
  38. }
  39. // mouse button callback
  40. void mouse_button(GLFWwindow* window, int button, int act, int mods) {
  41. // update button state
  42. button_left = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT)==GLFW_PRESS);
  43. button_middle = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE)==GLFW_PRESS);
  44. button_right = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT)==GLFW_PRESS);
  45. // update mouse position
  46. glfwGetCursorPos(window, &lastx, &lasty);
  47. }
  48. // mouse move callback
  49. void mouse_move(GLFWwindow* window, double xpos, double ypos) {
  50. // no buttons down: nothing to do
  51. if (!button_left && !button_middle && !button_right) {
  52. return;
  53. }
  54. // compute mouse displacement, save
  55. double dx = xpos - lastx;
  56. double dy = ypos - lasty;
  57. lastx = xpos;
  58. lasty = ypos;
  59. // get current window size
  60. int width, height;
  61. glfwGetWindowSize(window, &width, &height);
  62. // get shift key state
  63. bool mod_shift = (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT)==GLFW_PRESS ||
  64. glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT)==GLFW_PRESS);
  65. // determine action based on mouse button
  66. mjtMouse action;
  67. if (button_right) {
  68. action = mod_shift ? mjMOUSE_MOVE_H : mjMOUSE_MOVE_V;
  69. } else if (button_left) {
  70. action = mod_shift ? mjMOUSE_ROTATE_H : mjMOUSE_ROTATE_V;
  71. } else {
  72. action = mjMOUSE_ZOOM;
  73. }
  74. // move camera
  75. mjv_moveCamera(m, action, dx/height, dy/height, &scn, &cam);
  76. }
  77. // scroll callback
  78. void scroll(GLFWwindow* window, double xoffset, double yoffset) {
  79. // emulate vertical mouse motion = 5% of window height
  80. mjv_moveCamera(m, mjMOUSE_ZOOM, 0, -0.05*yoffset, &scn, &cam);
  81. }
  82. // main function
  83. int main(int argc, const char** argv) {
  84. // check command-line arguments
  85. if (argc!=2) {
  86. std::printf(" USAGE: basic modelfile\n");
  87. return 0;
  88. }
  89. // load and compile model
  90. char error[1000] = "Could not load binary model";
  91. if (std::strlen(argv[1])>4 && !std::strcmp(argv[1]+std::strlen(argv[1])-4, ".mjb")) {
  92. m = mj_loadModel(argv[1], 0);
  93. } else {
  94. m = mj_loadXML(argv[1], 0, error, 1000);
  95. }
  96. if (!m) {
  97. mju_error("Load model error: %s", error);
  98. }
  99. // make data
  100. d = mj_makeData(m);
  101. // init GLFW
  102. if (!glfwInit()) {
  103. mju_error("Could not initialize GLFW");
  104. }
  105. // create window, make OpenGL context current, request v-sync
  106. GLFWwindow* window = glfwCreateWindow(1200, 900, "Demo", NULL, NULL);
  107. glfwMakeContextCurrent(window);
  108. glfwSwapInterval(1);
  109. // initialize visualization data structures
  110. mjv_defaultCamera(&cam);
  111. mjv_defaultOption(&opt);
  112. mjv_defaultScene(&scn);
  113. mjr_defaultContext(&con);
  114. // create scene and context
  115. mjv_makeScene(m, &scn, 2000);
  116. mjr_makeContext(m, &con, mjFONTSCALE_150);
  117. // install GLFW mouse and keyboard callbacks
  118. glfwSetKeyCallback(window, keyboard);
  119. glfwSetCursorPosCallback(window, mouse_move);
  120. glfwSetMouseButtonCallback(window, mouse_button);
  121. glfwSetScrollCallback(window, scroll);
  122. // run main loop, target real-time simulation and 60 fps rendering
  123. while (!glfwWindowShouldClose(window)) {
  124. // advance interactive simulation for 1/60 sec
  125. // Assuming MuJoCo can simulate faster than real-time, which it usually can,
  126. // this loop will finish on time for the next frame to be rendered at 60 fps.
  127. // Otherwise add a cpu timer and exit this loop when it is time to render.
  128. mjtNum simstart = d->time;
  129. while (d->time - simstart < 1.0/60.0) {
  130. mj_step(m, d);
  131. }
  132. // get framebuffer viewport
  133. mjrRect viewport = {0, 0, 0, 0};
  134. glfwGetFramebufferSize(window, &viewport.width, &viewport.height);
  135. // update scene and render
  136. mjv_updateScene(m, d, &opt, NULL, &cam, mjCAT_ALL, &scn);
  137. mjr_render(viewport, &scn, &con);
  138. // swap OpenGL buffers (blocking call due to v-sync)
  139. glfwSwapBuffers(window);
  140. // process pending GUI events, call GLFW callbacks
  141. glfwPollEvents();
  142. }
  143. //free visualization storage
  144. mjv_freeScene(&scn);
  145. mjr_freeContext(&con);
  146. // free MuJoCo model and data
  147. mj_deleteData(d);
  148. mj_deleteModel(m);
  149. // terminate GLFW (crashes with Linux NVidia drivers)
  150. #if defined(__APPLE__) || defined(_WIN32)
  151. glfwTerminate();
  152. #endif
  153. return 1;
  154. }