Use mjResourceProvider instead of custom LoadAsset callbacks.
PiperOrigin-RevId: 853172824 Change-Id: I60ef1207611c4da119cc3f0a7819088cffca6e4f
This commit is contained in:
committed by
Copybara-Service
parent
4830f5a79e
commit
9743c2d385
@@ -26,9 +26,6 @@
|
||||
|
||||
namespace mujoco::platform {
|
||||
|
||||
// Function signature for loading assets from a given path.
|
||||
using LoadAssetFn = std::function<std::vector<std::byte>(std::string_view)>;
|
||||
|
||||
// Save/load for simple ascii files.
|
||||
void SaveText(const std::string& contents, const std::string& filename);
|
||||
std::string LoadText(const std::string& filename);
|
||||
|
||||
@@ -14,17 +14,9 @@
|
||||
|
||||
#include "experimental/platform/renderer.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "experimental/platform/helpers.h"
|
||||
|
||||
#if defined(USE_FILAMENT_OPENGL) || defined(USE_FILAMENT_VULKAN)
|
||||
#include "experimental/filament/render_context_filament.h"
|
||||
@@ -37,8 +29,7 @@
|
||||
|
||||
namespace mujoco::platform {
|
||||
|
||||
Renderer::Renderer(void* native_window, const LoadAssetFn& load_asset_fn)
|
||||
: load_asset_fn_(load_asset_fn), native_window_(native_window) {
|
||||
Renderer::Renderer(void* native_window) : native_window_(native_window) {
|
||||
#ifdef USE_CLASSIC_OPENGL
|
||||
ImGui_ImplOpenGL3_Init();
|
||||
#endif
|
||||
@@ -57,8 +48,6 @@ void Renderer::Init(const mjModel* model) {
|
||||
mjrFilamentConfig render_config;
|
||||
mjr_defaultFilamentConfig(&render_config);
|
||||
render_config.native_window = native_window_;
|
||||
render_config.load_asset = &Renderer::LoadAssetCallback;
|
||||
render_config.load_asset_user_data = this;
|
||||
render_config.enable_gui = true;
|
||||
#if defined(USE_FILAMENT_OPENGL)
|
||||
render_config.graphics_api = mjGFX_OPENGL;
|
||||
@@ -140,23 +129,4 @@ void Renderer::RenderToTexture(const mjModel* model, mjData* data,
|
||||
|
||||
double Renderer::GetFps() { return fps_; }
|
||||
|
||||
int Renderer::LoadAssetCallback(const char* path, void* user_data,
|
||||
unsigned char** out, std::uint64_t* out_size) {
|
||||
Renderer* renderer = static_cast<Renderer*>(user_data);
|
||||
std::vector<std::byte> bytes = (renderer->load_asset_fn_)(path);
|
||||
if (bytes.empty()) {
|
||||
*out_size = 0;
|
||||
return 0; // Empty file
|
||||
}
|
||||
|
||||
*out_size = bytes.size();
|
||||
*out = reinterpret_cast<unsigned char*>(malloc(*out_size));
|
||||
if (*out == nullptr) {
|
||||
mju_error("Failed to allocate memory for file %s", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::memcpy(*out, bytes.data(), *out_size);
|
||||
return 0;
|
||||
}
|
||||
} // namespace mujoco::platform
|
||||
|
||||
@@ -17,11 +17,9 @@
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <ratio>
|
||||
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "experimental/platform/helpers.h"
|
||||
|
||||
namespace mujoco::platform {
|
||||
|
||||
@@ -34,7 +32,7 @@ class Renderer {
|
||||
using Seconds = std::chrono::duration<double>;
|
||||
using Milliseconds = std::chrono::duration<double, std::milli>;
|
||||
|
||||
Renderer(void* native_window, const LoadAssetFn& load_asset_fn);
|
||||
explicit Renderer(void* native_window);
|
||||
~Renderer();
|
||||
|
||||
Renderer(const Renderer&) = delete;
|
||||
@@ -64,10 +62,6 @@ class Renderer {
|
||||
// Resets the renderer; no rendering will occur until Init() is called again.
|
||||
void Deinit();
|
||||
|
||||
static int LoadAssetCallback(const char* path, void* user_data,
|
||||
unsigned char** out, std::uint64_t* out_size);
|
||||
|
||||
LoadAssetFn load_asset_fn_;
|
||||
void* native_window_ = nullptr;
|
||||
mjrContext render_context_;
|
||||
mjvScene scene_;
|
||||
|
||||
@@ -26,8 +26,8 @@
|
||||
#include <SDL_video.h>
|
||||
#include <backends/imgui_impl_sdl2.h>
|
||||
#include <imgui.h>
|
||||
#include "experimental/platform/helpers.h"
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "user/user_resource.h"
|
||||
|
||||
// Because X11/Xlib.h defines Status.
|
||||
#ifdef Status
|
||||
@@ -40,8 +40,7 @@ extern void* GetNativeWindowOsx(void* window);
|
||||
|
||||
namespace mujoco::platform {
|
||||
|
||||
static void InitImGui(SDL_Window* window, const LoadAssetFn& load_asset_fn,
|
||||
bool load_fonts, bool build_fonts) {
|
||||
static void InitImGui(SDL_Window* window, bool load_fonts, bool build_fonts) {
|
||||
ImGui::CreateContext();
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
@@ -52,28 +51,36 @@ static void InitImGui(SDL_Window* window, const LoadAssetFn& load_asset_fn,
|
||||
ImGui_ImplSDL2_InitForOther(window);
|
||||
|
||||
if (load_fonts) {
|
||||
// Note: fonts are stored statically because they aren't actually loaded
|
||||
// until the fonts are built.
|
||||
mjResource* font = nullptr;
|
||||
int size = 0;
|
||||
void* data = nullptr;
|
||||
|
||||
ImFontConfig main_cfg;
|
||||
static auto main_font = load_asset_fn("OpenSans-Regular.ttf");
|
||||
io.Fonts->AddFontFromMemoryTTF(main_font.data(), main_font.size(), 20.f,
|
||||
&main_cfg);
|
||||
main_cfg.FontDataOwnedByAtlas = false;
|
||||
font =
|
||||
mju_openResource("", "font:OpenSans-Regular.ttf", nullptr, nullptr, 0);
|
||||
size = mju_readResource(font, const_cast<const void**>(&data));
|
||||
io.Fonts->AddFontFromMemoryTTF(data, size, 20.f, &main_cfg);
|
||||
|
||||
ImFontConfig icon_cfg;
|
||||
icon_cfg.FontDataOwnedByAtlas = false;
|
||||
icon_cfg.MergeMode = true;
|
||||
static auto icon_font = load_asset_fn("fontawesome-webfont.ttf");
|
||||
font = mju_openResource("", "font:fontawesome-webfont.ttf", nullptr,
|
||||
nullptr, 0);
|
||||
size = mju_readResource(font, const_cast<const void**>(&data));
|
||||
constexpr ImWchar icon_ranges[] = {0xf000, 0xf3ff, 0x000};
|
||||
io.Fonts->AddFontFromMemoryTTF(icon_font.data(), icon_font.size(), 14.f,
|
||||
&icon_cfg, icon_ranges);
|
||||
io.Fonts->AddFontFromMemoryTTF(data, size, 14.f, &icon_cfg, icon_ranges);
|
||||
|
||||
if (build_fonts) {
|
||||
io.Fonts->Build();
|
||||
}
|
||||
|
||||
// Note: we purposefully do not "close" the font resources as ImGui may
|
||||
// need them again to resize fonts.
|
||||
}
|
||||
}
|
||||
|
||||
Window::Window(std::string_view title, int width, int height, Config config,
|
||||
const LoadAssetFn& load_asset_fn)
|
||||
Window::Window(std::string_view title, int width, int height, Config config)
|
||||
: width_(width), height_(height), config_(config) {
|
||||
SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "1");
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||
@@ -113,8 +120,7 @@ Window::Window(std::string_view title, int width, int height, Config config,
|
||||
mju_error("Error creating window: %s", SDL_GetError());
|
||||
}
|
||||
|
||||
InitImGui(sdl_window_, load_asset_fn, config.load_fonts,
|
||||
(render_config != kClassicOpenGL));
|
||||
InitImGui(sdl_window_, config.load_fonts, (render_config != kClassicOpenGL));
|
||||
|
||||
if (render_config == kFilamentWebGL || render_config == kClassicOpenGL) {
|
||||
SDL_GLContext gl_context = SDL_GL_CreateContext(sdl_window_);
|
||||
|
||||
@@ -43,8 +43,7 @@ class Window {
|
||||
bool load_fonts = true;
|
||||
};
|
||||
|
||||
Window(std::string_view title, int width, int height, Config config,
|
||||
const LoadAssetFn& load_asset_fn);
|
||||
Window(std::string_view title, int width, int height, Config config);
|
||||
~Window();
|
||||
|
||||
Window(const Window&) = delete;
|
||||
|
||||
Reference in New Issue
Block a user