Skip to content

Commit d1fd11b

Browse files
haroonqcopybara-github
authored andcommitted
Refactor SaveScreenshot into a more useful RenderToTexture function.
Removes a few unused functions from helpers library. PiperOrigin-RevId: 846661437 Change-Id: I815ffe1990098dedb71f694af088d85294f9c7de
1 parent 522ff1c commit d1fd11b

File tree

5 files changed

+40
-64
lines changed

5 files changed

+40
-64
lines changed

src/experimental/platform/helpers.cc

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
#include <ios>
2424
#include <iterator>
2525
#include <string>
26-
#include <vector>
2726

2827
#include "webp/encode.h"
2928
#include "webp/types.h"
30-
#include <mujoco/mjrender.h>
3129
#include <mujoco/mjxmacro.h>
3230
#include <mujoco/mujoco.h>
3331
#include "engine/engine_vis_visualize.h"
@@ -89,45 +87,17 @@ std::string ResolveFile(const std::string& filename,
8987
return "";
9088
}
9189

92-
void SaveColorToWebp(int width, int height, const unsigned char* data,
93-
const std::string& filename) {
90+
void SaveToWebp(int width, int height, const std::byte* data,
91+
const std::string& filename) {
9492
uint8_t* webp = nullptr;
95-
const size_t size =
96-
WebPEncodeLosslessRGB(data, width, height, width * 3, &webp);
97-
93+
const size_t size = WebPEncodeLosslessRGB(
94+
reinterpret_cast<const uint8_t*>(data), width, height, width * 3, &webp);
9895
std::ofstream file(filename, std::ios::binary);
9996
file.write(reinterpret_cast<const char*>(webp), size);
10097
file.close();
10198
WebPFree(webp);
10299
}
103100

104-
void SaveDepthToWebp(int width, int height, const float* data,
105-
const std::string& filename) {
106-
const int size = width * height;
107-
108-
// Turn the depth buffer into a greyscale color buffer.
109-
std::vector<unsigned char> byte_buffer;
110-
byte_buffer.reserve(size * 3);
111-
for (int i = 0; i < size; ++i) {
112-
auto byte = static_cast<int>(255.0 * data[i]);
113-
byte_buffer.push_back(byte);
114-
byte_buffer.push_back(byte);
115-
byte_buffer.push_back(byte);
116-
}
117-
SaveColorToWebp(width, height, byte_buffer.data(), filename);
118-
}
119-
120-
void SaveScreenshotToWebp(int width, int height, mjrContext* con,
121-
const std::string& filename) {
122-
mjr_setBuffer(mjFB_OFFSCREEN, con);
123-
auto rgb_buffer = std::vector<unsigned char>(3 * width * height);
124-
auto depth_buffer = std::vector<float>(width * height, 1.0f);
125-
mjrRect viewport = {0, 0, width, height};
126-
mjr_readPixels(rgb_buffer.data(), depth_buffer.data(), viewport, con);
127-
mjr_setBuffer(mjFB_WINDOW, con);
128-
SaveColorToWebp(width, height, rgb_buffer.data(), filename);
129-
}
130-
131101
const void* GetValue(const mjModel* model, const mjData* data,
132102
const char* field, int index) {
133103
MJDATA_POINTERS_PREAMBLE(model);

src/experimental/platform/helpers.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <string_view>
2323
#include <vector>
2424

25-
#include <mujoco/mjrender.h>
2625
#include <mujoco/mujoco.h>
2726

2827
namespace mujoco::platform {
@@ -39,17 +38,9 @@ std::string LoadText(const std::string& filename);
3938
std::string ResolveFile(const std::string& filename,
4039
const std::vector<std::string>& search_paths);
4140

42-
// Exports the given color buffer to a webp file.
43-
void SaveColorToWebp(int width, int height, const unsigned char* data,
44-
const std::string& filename);
45-
46-
// Exports the given depth buffer to a webp file.
47-
void SaveDepthToWebp(int width, int height, const float* data,
48-
const std::string& filename);
49-
50-
// Exports the current state of the mjrContext to a webp file.
51-
void SaveScreenshotToWebp(int width, int height, mjrContext* con,
52-
const std::string& filename);
41+
// Exports the given image (assumed to be RGB888) to a webp file.
42+
void SaveToWebp(int width, int height, const std::byte* data,
43+
const std::string& filename);
5344

5445
// Returns a pointer to the value of the given field in the given data.
5546
// Returns nullptr if the field is not found or the index is out of bounds.

src/experimental/platform/renderer.cc

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414

1515
#include "experimental/platform/renderer.h"
1616

17-
#include <chrono>
18-
#include <string>
17+
#include <cstddef>
1918

20-
#include "experimental/platform/helpers.h"
2119
#include <mujoco/mujoco.h>
2220

2321
namespace mujoco::platform {
@@ -50,18 +48,31 @@ void Renderer::Deinit() {
5048
void Renderer::Render(const mjModel* model, mjData* data,
5149
const mjvPerturb* perturb, mjvCamera* camera,
5250
const mjvOption* vis_option, int width, int height) {
53-
if (initialized_) {
54-
mjv_updateScene(model, data, vis_option, perturb, camera, mjCAT_ALL,
55-
&scene_);
51+
if (!initialized_) {
52+
return;
5653
}
5754

58-
mjrRect main_viewport = {0, 0, width, height};
59-
mjr_render(main_viewport, data ? &scene_ : nullptr, &render_context_);
55+
mjv_updateScene(model, data, vis_option, perturb, camera, mjCAT_ALL,
56+
&scene_);
57+
58+
const mjrRect viewport = {0, 0, width, height};
59+
mjr_render(viewport, &scene_, &render_context_);
6060
}
6161

62-
void Renderer::SaveScreenshot(const std::string& filename, int width,
63-
int height) {
64-
SaveScreenshotToWebp(width, height, &render_context_, filename);
62+
void Renderer::RenderToTexture(const mjModel* model, mjData* data,
63+
mjvCamera* camera, int width, int height,
64+
std::byte* output) {
65+
if (!initialized_) {
66+
return;
67+
}
68+
69+
const mjrRect viewport = {0, 0, width, height};
70+
71+
mjr_setBuffer(mjFB_OFFSCREEN, &render_context_);
72+
mjv_updateCamera(model, data, camera, &scene_);
73+
mjr_render(viewport, &scene_, &render_context_);
74+
mjr_readPixels((unsigned char*)output, nullptr, viewport, &render_context_);
75+
mjr_setBuffer(mjFB_WINDOW, &render_context_);
6576
}
6677

6778
} // namespace mujoco::platform

src/experimental/platform/renderer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
#ifndef MUJOCO_SRC_EXPERIMENTAL_PLATFORM_RENDERER_H_
1616
#define MUJOCO_SRC_EXPERIMENTAL_PLATFORM_RENDERER_H_
1717

18-
#include <chrono>
19-
#include <cstdint>
18+
#include <cstddef>
2019
#include <functional>
21-
#include <string>
2220

2321
#include <mujoco/mujoco.h>
2422

@@ -48,8 +46,10 @@ class Renderer {
4846
mjvCamera* camera, const mjvOption* vis_option, int width,
4947
int height);
5048

51-
// Saves a screenshot of the simulation state into the given file.
52-
void SaveScreenshot(const std::string& filename, int width, int height);
49+
// Populates the given output buffer with RGB888 pixel data. The size of the
50+
// output buffer must be at least width * height * 3.
51+
void RenderToTexture(const mjModel* model, mjData* data, mjvCamera* camera,
52+
int width, int height, std::byte* output);
5353

5454
// Rendering flags.
5555
mjtByte* GetRenderFlags() { return scene_.flags; }

src/experimental/studio/app.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,8 +1678,12 @@ void App::FileDialogGui() {
16781678
if (ImGui::BeginPopupModal("SaveWebp", NULL,
16791679
ImGuiWindowFlags_AlwaysAutoResize)) {
16801680
if (platform::ImGui_FileDialog(tmp_.filename, sizeof(tmp_.filename))) {
1681-
renderer_->SaveScreenshot(tmp_.filename, window_->GetWidth(),
1682-
window_->GetHeight());
1681+
const int width = window_->GetWidth();
1682+
const int height = window_->GetHeight();
1683+
std::vector<std::byte> buffer(width * height * 3);
1684+
renderer_->RenderToTexture(model_, data_, &camera_, width, height,
1685+
buffer.data());
1686+
platform::SaveToWebp(width, height, buffer.data(), tmp_.filename);
16831687
tmp_.last_save_screenshot_file = tmp_.filename;
16841688
}
16851689
ImGui::EndPopup();

0 commit comments

Comments
 (0)