Use mjResourceProvider instead of custom LoadAsset callbacks.

PiperOrigin-RevId: 853172824
Change-Id: I60ef1207611c4da119cc3f0a7819088cffca6e4f
This commit is contained in:
Haroon Qureshi
2026-01-07 03:02:06 -08:00
committed by Copybara-Service
parent 4830f5a79e
commit 9743c2d385
12 changed files with 142 additions and 167 deletions
+60 -15
View File
@@ -15,43 +15,88 @@
// Main entry point for the Filament-based MuJoCo renderer.
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <ios>
#include <iosfwd>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include <absl/flags/flag.h>
#include <mujoco/mujoco.h>
#include "experimental/studio/app.h"
ABSL_FLAG(int, window_width, 1400, "Window width");
ABSL_FLAG(int, window_height, 720, "Window height");
ABSL_FLAG(std::string, model_file, "", "MuJoCo model file.");
static std::vector<std::byte> LoadAsset(std::string_view path) {
std::string fullpath = std::string("assets/") + std::string(path);
std::ifstream file(fullpath, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
return {};
}
std::streampos file_size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::byte> buffer(file_size);
if (!file.read(reinterpret_cast<char*>(buffer.data()), file_size)) {
return {};
}
return buffer;
std::string Resolve(std::string_view path) {
return std::string("assets/") + std::string(path);
}
class FileResource {
public:
explicit FileResource(const std::string& path)
: file_(path, std::ios::binary | std::ios::ate) {
if (!file_.is_open()) {
mju_warning("Cannot open file %s", path.c_str());
return;
}
size_ = file_.tellg();
file_.seekg(0, std::ios::beg);
}
int Read(const void** buffer) {
buffer_.resize(size_);
if (!file_.read(reinterpret_cast<char*>(buffer_.data()), size_)) {
return 0;
}
*buffer = buffer_.data();
return size_;
}
int Size() const { return size_; }
FileResource(const FileResource&) = delete;
FileResource& operator=(const FileResource&) = delete;
private:
std::ifstream file_;
std::vector<char> buffer_;
int size_ = 0;
};
int main(int argc, char** argv, char** envp) {
const char* home = getenv("HOME");
const std::string ini_path = std::string(home ? home : ".") + "/.mujoco.ini";
mjpResourceProvider resource_provider;
resource_provider.open = [](mjResource* resource) {
const std::string resolved_path = Resolve(resource->name);
FileResource* f = new FileResource(resolved_path);
resource->data = f;
return f->Size();
};
resource_provider.read = [](mjResource* resource, const void** buffer) {
FileResource* f = static_cast<FileResource*>(resource->data);
return f->Read(buffer);
};
resource_provider.close = [](mjResource* resource) {
delete static_cast<FileResource*>(resource->data);
};
resource_provider.prefix = "font";
mjp_registerResourceProvider(&resource_provider);
resource_provider.prefix = "filament";
mjp_registerResourceProvider(&resource_provider);
const int width = absl::GetFlag(FLAGS_window_width);
const int height = absl::GetFlag(FLAGS_window_height);
mujoco::studio::App app(width, height, ini_path, LoadAsset);
mujoco::studio::App app(width, height, ini_path);
// If the model file is not specified, try to load it from the first argument
std::string model_file = absl::GetFlag(FLAGS_model_file);