Esempio n. 1
0
    def render(self, viewport, scene):
        """Renders the scene to the specified viewport.

    Args:
      viewport: Instance of Viewport.
      scene: Instance of MjvScene.
    Returns:
      A 3-dimensional array of shape (viewport.width, viewport.height, 3),
      with the contents of the front buffer.
    """
        if not np.array_equal(self._prev_viewport, viewport.dimensions):
            self._prev_viewport = viewport.dimensions
            if self._mujoco_context:
                self._mujoco_context.free()
            self._mujoco_context = None
        if not self._mujoco_context:
            # Ensure that MuJoCo's offscreen framebuffer is large enough to
            # accommodate the viewport.
            new_offwidth = max(self._model.vis.global_.offwidth,
                               viewport.width)
            new_offheight = max(self._model.vis.global_.offheight,
                                viewport.height)
            self._model.vis.global_.offwidth = new_offwidth
            self._model.vis.global_.offheight = new_offheight
            self._mujoco_context = wrapper.MjrContext(
                model=self._model,
                gl_context=self._surface,
                font_scale=enums.mjtFontScale.mjFONTSCALE_100)
            self._rgb_buffer = np.empty((viewport.height, viewport.width, 3),
                                        dtype=np.uint8)

        with self._surface.make_current() as ctx:
            ctx.call(self._render_on_gl_thread, viewport, scene)
        self._pixels = np.flipud(self._rgb_buffer)
Esempio n. 2
0
 def _make_rendering_contexts(self):
     """Creates the OpenGL and MuJoCo rendering contexts."""
     # Get the offscreen framebuffer size, as specified in the model XML.
     max_width = self.model.vis.global_.offwidth
     max_height = self.model.vis.global_.offheight
     # Create the OpenGL context.
     render_context = _render.Renderer(max_width=max_width,
                                       max_height=max_height)
     # Create the MuJoCo context.
     mujoco_context = wrapper.MjrContext(self.model, render_context)
     self._contexts = Contexts(gl=render_context, mujoco=mujoco_context)
Esempio n. 3
0
 def _make_rendering_contexts(self):
   """Creates the OpenGL and MuJoCo rendering contexts."""
   # Forcibly clear the previous GL context to avoid problems with GL
   # implementations which do not support multiple contexts on a given device.
   if self._contexts:
     self._contexts.mujoco.free()
     self._contexts.gl.free()
   # Get the offscreen framebuffer size, as specified in the model XML.
   max_width = self.model.vis.global_.offwidth
   max_height = self.model.vis.global_.offheight
   # Create the OpenGL context.
   render_context = render.Renderer(max_width=max_width, max_height=max_height)
   # Create the MuJoCo context.
   mujoco_context = wrapper.MjrContext(self.model, render_context)
   self._contexts = Contexts(gl=render_context, mujoco=mujoco_context)
Esempio n. 4
0
 def _make_rendering_contexts(self):
   """Creates the OpenGL and MuJoCo rendering contexts."""
   # Forcibly clear the previous GL context to avoid problems with GL
   # implementations which do not support multiple contexts on a given device.
   if self._contexts:
     self._contexts.gl.free()
   # Create the OpenGL context.
   render_context = render.Renderer(_MAX_WIDTH, _MAX_HEIGHT)
   # Create the MuJoCo context.
   mujoco_context = wrapper.MjrContext()
   with render_context.make_current(_MAX_WIDTH, _MAX_HEIGHT):
     mjlib.mjr_makeContext(self.model.ptr, mujoco_context.ptr, _FONT_SCALE)
     mjlib.mjr_setBuffer(
         enums.mjtFramebuffer.mjFB_OFFSCREEN, mujoco_context.ptr)
   self._contexts = Contexts(gl=render_context, mujoco=mujoco_context)
Esempio n. 5
0
    def _reload_from_data(self, data):
        """Initializes a new or existing `Physics` instance from a `wrapper.MjData`.

    Assigns all attributes and sets up rendering contexts and named indexing.

    The default constructor as well as the other `reload_from` methods should
    delegate to this method.

    Args:
      data: Instance of `wrapper.MjData`.
    """
        self._data = data

        # Forcibly clear the previous context to avoid problems with GL
        # implementations which do not support multiple contexts on a given device.
        if hasattr(self, '_contexts'):
            self._contexts.gl.free_context()

        # Set up rendering context. Need to provide at least one rendering api in
        # the BUILD target.
        render_context = render.Renderer(_MAX_WIDTH, _MAX_HEIGHT)
        mujoco_context = wrapper.MjrContext()
        with render_context.make_current(_MAX_WIDTH, _MAX_HEIGHT):
            mjlib.mjr_makeContext(self.model.ptr, mujoco_context.ptr,
                                  _FONT_SCALE)
            mjlib.mjr_setBuffer(enums.mjtFramebuffer.mjFB_OFFSCREEN,
                                mujoco_context.ptr)
        self._contexts = Contexts(gl=render_context, mujoco=mujoco_context)

        # Call kinematics update to enable rendering.
        self.after_reset()

        # Set up named indexing.
        axis_indexers = index.make_axis_indexers(self.model)
        self._named = NamedIndexStructs(
            model=index.struct_indexer(self.model, 'mjmodel', axis_indexers),
            data=index.struct_indexer(self.data, 'mjdata', axis_indexers),
        )
Esempio n. 6
0
 def test_repeatedly_create_and_destroy_context(self):
     renderer = glfw_renderer.GLFWContext(MAX_WIDTH, MAX_HEIGHT)
     wrapper.MjrContext(wrapper.MjModel.from_xml_string('<mujoco/>'),
                        renderer)