Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
11 changes: 6 additions & 5 deletions CPP/Chapter1-make/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.20)
project(MUJOCO_T)
set(CMAKE_CXX_STANDARD 17)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/simulate)

#编译安装,从cmake安装位置opt使用
Expand Down Expand Up @@ -30,8 +31,8 @@ target_link_libraries(basic mujoco::mujoco glut GL GLU glfw)


#simulate编译
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/simulate)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/simulate)
file(GLOB SIMULATE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/simulate/*.cc)
add_executable(simulate ${SIMULATE_SRC})
target_link_libraries(simulate mujoco::mujoco glut GL GLU glfw lodepng)
# include_directories(${CMAKE_CURRENT_SOURCE_DIR}/simulate)
# link_directories(${CMAKE_CURRENT_SOURCE_DIR}/simulate)
# file(GLOB SIMULATE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/simulate/*.cc)
# add_executable(simulate ${SIMULATE_SRC})
# target_link_libraries(simulate mujoco::mujoco glut GL GLU glfw lodepng)
67 changes: 34 additions & 33 deletions CPP/Chapter1-make/basic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,46 @@
#include <mujoco/mujoco.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
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 interaction
bool button_left = false;
bool button_middle = false;
bool button_right = false;
bool button_right = false;
double lastx = 0;
double lasty = 0;


// keyboard callback
void keyboard(GLFWwindow* window, int key, int scancdataode, int act, int mods) {
void keyboard(GLFWwindow *window, int key, int scancdataode, int act,
int mods) {
// backspace: reset simulation
if (act==GLFW_PRESS && key==GLFW_KEY_BACKSPACE) {
if (act == GLFW_PRESS && key == GLFW_KEY_BACKSPACE) {
mj_resetData(m, d);
mj_forward(m, d);
}
}


// mouse button callback
void mouse_button(GLFWwindow* window, int button, int act, int mods) {
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);
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);
}


// mouse move callback
void mouse_move(GLFWwindow* window, double xpos, double ypos) {
void mouse_move(GLFWwindow *window, double xpos, double ypos) {
// no buttons down: nothing to do
if (!button_left && !button_middle && !button_right) {
return;
Expand All @@ -74,8 +75,8 @@ void mouse_move(GLFWwindow* window, double xpos, double ypos) {
glfwGetWindowSize(window, &width, &height);

// get shift key state
bool mod_shift = (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT)==GLFW_PRESS ||
glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT)==GLFW_PRESS);
bool mod_shift = (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS ||
glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS);

// determine action based on mouse button
mjtMouse action;
Expand All @@ -88,28 +89,27 @@ void mouse_move(GLFWwindow* window, double xpos, double ypos) {
}

// move camera
mjv_moveCamera(m, action, dx/height, dy/height, &scn, &cam);
mjv_moveCamera(m, action, dx / height, dy / height, &scn, &cam);
}


// scroll callback
void scroll(GLFWwindow* window, double xoffset, double yoffset) {
void scroll(GLFWwindow *window, double xoffset, double yoffset) {
// emulate vertical mouse motion = 5% of window height
mjv_moveCamera(m, mjMOUSE_ZOOM, 0, -0.05*yoffset, &scn, &cam);
mjv_moveCamera(m, mjMOUSE_ZOOM, 0, -0.05 * yoffset, &scn, &cam);
}


// main function
int main(int argc, const char** argv) {
int main(int argc, const char **argv) {
// check command-line arguments
if (argc!=2) {
if (argc != 2) {
std::printf(" USAGE: basic modelfile\n");
return 0;
}

// load and compile model
char error[1000] = "Could not load binary model";
if (std::strlen(argv[1])>4 && !std::strcmp(argv[1]+std::strlen(argv[1])-4, ".mjb")) {
if (std::strlen(argv[1]) > 4 &&
!std::strcmp(argv[1] + std::strlen(argv[1]) - 4, ".mjb")) {
m = mj_loadModel(argv[1], 0);
} else {
m = mj_loadXML(argv[1], 0, error, 1000);
Expand All @@ -127,7 +127,7 @@ int main(int argc, const char** argv) {
}

// create window, make OpenGL context current, request v-sync
GLFWwindow* window = glfwCreateWindow(1200, 900, "Demo", NULL, NULL);
GLFWwindow *window = glfwCreateWindow(1200, 900, "Demo", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);

Expand All @@ -150,11 +150,12 @@ int main(int argc, const char** argv) {
// 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.
// 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) {
while (d->time - simstart < 1.0 / 60.0) {
mj_step(m, d);
}

Expand All @@ -173,7 +174,7 @@ int main(int argc, const char** argv) {
glfwPollEvents();
}

//free visualization storage
// free visualization storage
mjv_freeScene(&scn);
mjr_freeContext(&con);

Expand Down
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions CPP/Chapter2-view&step/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.20)
project(MUJOCO_T)
set(CMAKE_CXX_STANDARD 17)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/simulate)

#编译安装,从cmake安装位置opt使用
Expand Down

This file was deleted.

Loading