pub struct MjRenderer { /* private fields */ }Expand description
A renderer for rendering 3D scenes. By default, RGB rendering is enabled and depth rendering is disabled.
Implementations§
Source§impl MjRenderer
impl MjRenderer
Sourcepub fn new<M: Deref<Target = MjModel>>(
model: M,
width: usize,
height: usize,
max_user_geom: usize,
) -> Result<Self, RendererError>
pub fn new<M: Deref<Target = MjModel>>( model: M, width: usize, height: usize, max_user_geom: usize, ) -> Result<Self, RendererError>
Construct a new renderer.
The max_user_geom parameter
defines how much space will be allocated for additional, user-defined visual-only geoms.
It can thus be set to 0 if no additional geoms will be drawn by the user.
§Scene allocation
The renderer uses two scenes:
- the internal scene: used by the renderer to draw the model’s state.
- the user scene: used by the user to add additional geoms to the internal scene
The internal scene allocates the amount of space needed to fit every pre-existing
model geom + user visual-only geoms + additional visual-only geoms that aren’t from the user (e.g., tendons).
By default, the renderer reserves 100 extra geom slots for drawing the additional visual-only geoms.
If that is not enough or it is too much, you can construct MjRenderer via its builder
(MjRenderer::builder), which allows more configuration.
Parameters width and height must be less or equal to the offscreen buffer size,
which can be configured at the top of the model’s XML like so:
<visual>
<global offwidth="1920" offheight="1080"/>
</visual>Sourcepub fn builder() -> MjRendererBuilder
pub fn builder() -> MjRendererBuilder
Create a MjRendererBuilder to configure MjRenderer.
Sourcepub fn user_scene(&self) -> &MjvScene
pub fn user_scene(&self) -> &MjvScene
Return an immutable reference to a user scene for drawing custom visual-only geoms.
Sourcepub fn user_scene_mut(&mut self) -> &mut MjvScene
pub fn user_scene_mut(&mut self) -> &mut MjvScene
Return a mutable reference to a user scene for drawing custom visual-only geoms.
Sourcepub fn opts_mut(&mut self) -> &mut MjvOption
pub fn opts_mut(&mut self) -> &mut MjvOption
Return a mutable reference to visualization options.
Sourcepub fn camera_mut(&mut self) -> &mut MjvCamera
pub fn camera_mut(&mut self) -> &mut MjvCamera
Return a mutable reference to the camera.
Sourcepub fn rgb_enabled(&self) -> bool
pub fn rgb_enabled(&self) -> bool
Check if RGB rendering is enabled.
Sourcepub fn depth_enabled(&self) -> bool
pub fn depth_enabled(&self) -> bool
Check if depth rendering is enabled.
Sourcepub fn set_font_scale(
&mut self,
font_scale: MjtFontScale,
) -> Result<(), RendererError>
pub fn set_font_scale( &mut self, font_scale: MjtFontScale, ) -> Result<(), RendererError>
Sets the font scale.
§Errors
Returns RendererError::GlutinError if the OpenGL context cannot be made current.
Sourcepub fn update_texture_from(
&self,
model: &MjModel,
texture_id: usize,
) -> Result<(), RendererError>
pub fn update_texture_from( &self, model: &MjModel, texture_id: usize, ) -> Result<(), RendererError>
Re-uploads the texture with texture_id from model to the GPU immediately.
§Errors
RendererError::SignatureMismatchifmodel’s signature does not match the renderer’s scene.RendererError::ContextErroriftexture_id >= model.ntex().RendererError::GlutinErrorif the OpenGL context cannot be made current.
Sourcepub fn update_textures_from(&self, model: &MjModel) -> Result<(), RendererError>
pub fn update_textures_from(&self, model: &MjModel) -> Result<(), RendererError>
Re-uploads all textures from model to the GPU immediately.
§Errors
RendererError::SignatureMismatchifmodel’s signature does not match the renderer’s scene.RendererError::GlutinErrorif the OpenGL context cannot be made current.
Sourcepub fn update_mesh_from(
&self,
model: &MjModel,
mesh_id: usize,
) -> Result<(), RendererError>
pub fn update_mesh_from( &self, model: &MjModel, mesh_id: usize, ) -> Result<(), RendererError>
Re-uploads the mesh with mesh_id from model to the GPU immediately.
All data arrays read by mjr_uploadMesh are copied: vertex positions
(mesh_vert), per-vertex normals (mesh_normal), UV texture coordinates
(mesh_texcoord), face–vertex indices (mesh_face), face–normal indices
(mesh_facenormal), face–texcoord indices (mesh_facetexcoord), and convex
hull graph data (mesh_graph). Layout fields (address and count arrays) are
not copied because they are fixed by the model signature.
§Errors
RendererError::SignatureMismatchifmodel’s signature does not match the renderer’s scene.RendererError::ContextErrorifmesh_id >= model.nmesh().RendererError::GlutinErrorif the OpenGL context cannot be made current.
Sourcepub fn update_meshes_from(&self, model: &MjModel) -> Result<(), RendererError>
pub fn update_meshes_from(&self, model: &MjModel) -> Result<(), RendererError>
Re-uploads all meshes from model to the GPU immediately.
All data arrays read by mjr_uploadMesh are bulk-uploaded: vertex positions
(mesh_vert), per-vertex normals (mesh_normal), UV texture coordinates
(mesh_texcoord), face–vertex indices (mesh_face), face–normal indices
(mesh_facenormal), face–texcoord indices (mesh_facetexcoord), and convex
hull graph data (mesh_graph). Layout fields (address and count arrays) are
not copied because they are fixed by the model signature.
§Errors
RendererError::SignatureMismatchifmodel’s signature does not match the renderer’s scene.RendererError::GlutinErrorif the OpenGL context cannot be made current.
Sourcepub fn update_hfield_from(
&self,
model: &MjModel,
hfield_id: usize,
) -> Result<(), RendererError>
pub fn update_hfield_from( &self, model: &MjModel, hfield_id: usize, ) -> Result<(), RendererError>
Re-uploads the heightfield with hfield_id from model to the GPU immediately.
§Errors
RendererError::SignatureMismatchifmodel’s signature does not match the renderer’s scene.RendererError::ContextErrorifhfield_id >= model.nhfield().RendererError::GlutinErrorif the OpenGL context cannot be made current.
Sourcepub fn update_hfields_from(&self, model: &MjModel) -> Result<(), RendererError>
pub fn update_hfields_from(&self, model: &MjModel) -> Result<(), RendererError>
Re-uploads all heightfields from model to the GPU immediately.
§Errors
RendererError::SignatureMismatchifmodel’s signature does not match the renderer’s scene.RendererError::GlutinErrorif the OpenGL context cannot be made current.
Sourcepub fn set_camera(&mut self, camera: MjvCamera)
pub fn set_camera(&mut self, camera: MjvCamera)
Set the camera used for rendering.
Sourcepub fn set_rgb_rendering(&mut self, enable: bool)
pub fn set_rgb_rendering(&mut self, enable: bool)
Enables/disables RGB rendering.
Sourcepub fn set_depth_rendering(&mut self, enable: bool)
pub fn set_depth_rendering(&mut self, enable: bool)
Enables/disables depth rendering.
Sourcepub fn with_font_scale(
self,
font_scale: MjtFontScale,
) -> Result<Self, RendererError>
pub fn with_font_scale( self, font_scale: MjtFontScale, ) -> Result<Self, RendererError>
Sets the font scale. To be used on construction.
§Errors
Returns RendererError::GlutinError if the OpenGL context cannot be made current.
Sourcepub fn with_opts(self, options: MjvOption) -> Self
pub fn with_opts(self, options: MjvOption) -> Self
Update the visualization options and return self. To be used on construction.
Sourcepub fn with_camera(self, camera: MjvCamera) -> Self
pub fn with_camera(self, camera: MjvCamera) -> Self
Set the camera used for rendering. To be used on construction.
Sourcepub fn with_rgb_rendering(self, enable: bool) -> Self
pub fn with_rgb_rendering(self, enable: bool) -> Self
Enables/disables RGB rendering. To be used on construction.
Sourcepub fn with_depth_rendering(self, enable: bool) -> Self
pub fn with_depth_rendering(self, enable: bool) -> Self
Enables/disables depth rendering. To be used on construction.
Sourcepub fn set_png_compression(&mut self, compression: Compression)
pub fn set_png_compression(&mut self, compression: Compression)
Set the PNG compression level used by MjRenderer::save_rgb and MjRenderer::save_depth.
Sourcepub fn with_png_compression(self, compression: Compression) -> Self
pub fn with_png_compression(self, compression: Compression) -> Self
Set the PNG compression level. To be used on construction.
Sourcepub fn sync_data<M: Deref<Target = MjModel>>(
&mut self,
data: &mut MjData<M>,
) -> Result<(), RendererError>
pub fn sync_data<M: Deref<Target = MjModel>>( &mut self, data: &mut MjData<M>, ) -> Result<(), RendererError>
Update the scene with new data from data.
When data was created from a different model than the renderer,
the internal scene is automatically recreated for the new model.
§Errors
RendererError::GlutinErrorif the OpenGL context could not be made current (only when theMjModelindatadiffers from the model that created the internalMjvScene).
Sourcepub fn sync<M: Deref<Target = MjModel>>(&mut self, data: &mut MjData<M>)
👎Deprecated since 3.0.0: replaced with sync_data + render
pub fn sync<M: Deref<Target = MjModel>>(&mut self, data: &mut MjData<M>)
replaced with sync_data + render
Update the scene with new data and render it.
§Panics
Panics if syncing the data or performing the render step fails (e.g. the OpenGL
context cannot be made current). Use MjRenderer::sync_data + MjRenderer::render
instead.
Sourcepub fn rgb<const WIDTH: usize, const HEIGHT: usize>(
&self,
) -> &[[[u8; 3]; WIDTH]; HEIGHT]
pub fn rgb<const WIDTH: usize, const HEIGHT: usize>( &self, ) -> &[[[u8; 3]; WIDTH]; HEIGHT]
Return an RGB image of the scene. This method accepts two generic parameters <WIDTH, HEIGHT> that define the shape of the output slice.
§Panics
- If the image size doesn’t match the required dimensions.
- If RGB rendering is disabled.
Use MjRenderer::try_rgb for a fallible alternative.
Sourcepub fn try_rgb<const WIDTH: usize, const HEIGHT: usize>(
&self,
) -> Result<&[[[u8; 3]; WIDTH]; HEIGHT], RendererError>
pub fn try_rgb<const WIDTH: usize, const HEIGHT: usize>( &self, ) -> Result<&[[[u8; 3]; WIDTH]; HEIGHT], RendererError>
Fallible version of MjRenderer::rgb.
§Returns
On success, returns Ok variant containing the rendered RGB image.
§Errors
RendererError::DimensionMismatchif the image size doesn’t match the required dimensions.RendererError::RgbDisabledif RGB rendering is disabled.
Sourcepub fn depth_flat(&self) -> Option<&[f32]>
pub fn depth_flat(&self) -> Option<&[f32]>
Return a flattened depth image of the scene.
Sourcepub fn depth<const WIDTH: usize, const HEIGHT: usize>(
&self,
) -> &[[f32; WIDTH]; HEIGHT]
pub fn depth<const WIDTH: usize, const HEIGHT: usize>( &self, ) -> &[[f32; WIDTH]; HEIGHT]
Return a depth image of the scene. This method accepts two generic parameters <WIDTH, HEIGHT> that define the shape of the output slice.
§Panics
- If the image size doesn’t match the required dimensions.
- If depth rendering is disabled.
Use MjRenderer::try_depth for a fallible alternative.
Sourcepub fn try_depth<const WIDTH: usize, const HEIGHT: usize>(
&self,
) -> Result<&[[f32; WIDTH]; HEIGHT], RendererError>
pub fn try_depth<const WIDTH: usize, const HEIGHT: usize>( &self, ) -> Result<&[[f32; WIDTH]; HEIGHT], RendererError>
Fallible version of MjRenderer::depth.
§Returns
On success, returns Ok variant containing the rendered depth image.
§Errors
RendererError::DimensionMismatchif the image size doesn’t match the required dimensions.RendererError::DepthDisabledif depth rendering is disabled.
Sourcepub fn save_rgb<T: AsRef<Path>>(&self, path: T) -> Result<(), RendererError>
pub fn save_rgb<T: AsRef<Path>>(&self, path: T) -> Result<(), RendererError>
Save an RGB image of the scene to a path.
§Returns
Ok(()) on success.
§Errors
RendererError::RgbDisabledwhen RGB rendering is disabled.RendererError::IoErrorif a file I/O operation fails.
Sourcepub fn save_depth<T: AsRef<Path>>(
&self,
path: T,
normalize: bool,
) -> Result<(f32, f32), RendererError>
pub fn save_depth<T: AsRef<Path>>( &self, path: T, normalize: bool, ) -> Result<(f32, f32), RendererError>
Save a depth image of the scene to a path. The image is 16-bit PNG, which
can be converted into depth (distance) data by dividing the grayscale values by
65535.0 and applying inverse normalization: depth = min + (grayscale / 65535.0) * (max - min).
If normalize is true, then the data is normalized with per-frame min-max normalization.
When normalize is false, the depth values are mapped using the model’s camera
near/far clip planes as the range, providing a fixed (frame-independent) mapping.
Use of MjRenderer::save_depth_raw is recommended if performance is critical, as
it skips PNG encoding and also saves the true depth values directly.
§Returns
An Ok((min, max)) is returned, where min and max represent the normalization parameters.
§Errors
RendererError::DepthDisabledwhen depth rendering is disabled.RendererError::IoErrorif a file I/O operation fails.
Sourcepub fn save_depth_raw<T: AsRef<Path>>(
&self,
path: T,
) -> Result<(), RendererError>
pub fn save_depth_raw<T: AsRef<Path>>( &self, path: T, ) -> Result<(), RendererError>
Save the raw depth data to the path. The data is encoded
as a sequence of bytes, where groups of four represent a single f32 value
in native byte order.
§Returns
Ok(()) on success.
§Errors
RendererError::DepthDisabledwhen depth rendering is disabled.RendererError::IoErrorif a file I/O operation fails.
Sourcepub fn render(&mut self) -> Result<(), RendererError>
pub fn render(&mut self) -> Result<(), RendererError>
Draws the scene to internal arrays.
Use MjRenderer::rgb or MjRenderer::depth to obtain the rendered image.
§Errors
RendererError::GlutinErrorif the OpenGL context could not be made current.RendererError::SceneErrorif the user-scene sync overflows the geom buffer.RendererError::ContextErrorif reading pixels from the framebuffer fails.
Trait Implementations§
Source§impl Debug for MjRenderer
impl Debug for MjRenderer
Source§impl Drop for MjRenderer
Ensures the OpenGL context is current before GPU resources are freed.
impl Drop for MjRenderer
Ensures the OpenGL context is current before GPU resources are freed.
Auto Trait Implementations§
impl !RefUnwindSafe for MjRenderer
impl !Send for MjRenderer
impl !Sync for MjRenderer
impl !UnwindSafe for MjRenderer
impl Freeze for MjRenderer
impl Unpin for MjRenderer
impl UnsafeUnpin for MjRenderer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.