Beispiel #1
0
 def _render_on_gl_thread(self, viewport, scene):
     mjlib.mjr_setBuffer(enums.mjtFramebuffer.mjFB_OFFSCREEN,
                         self._mujoco_context.ptr)
     mjlib.mjr_render(viewport.mujoco_rect, scene.ptr,
                      self._mujoco_context.ptr)
     self._render_components(self._mujoco_context, viewport)
     mjlib.mjr_readPixels(self._rgb_buffer, None, viewport.mujoco_rect,
                          self._mujoco_context.ptr)
Beispiel #2
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)
Beispiel #3
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),
        )
Beispiel #4
0
  def __init__(self, physics, height=240, width=320, camera_id=-1):
    """Initializes a new `Camera`.

    Args:
      physics: Instance of `Physics`.
      height: Optional image height. Defaults to 240.
      width: Optional image width. Defaults to 320.
      camera_id: Optional camera name or index. Defaults to -1, the free
        camera, which is always defined. A nonnegative integer or string
        corresponds to a fixed camera, which must be defined in the model XML.
        If `camera_id` is a string then the camera must also be named.

    Raises:
      ValueError: If `camera_id` is outside the valid range, or if `width` or
        `height` exceed the dimensions of MuJoCo's offscreen framebuffer.
    """
    buffer_width = physics.model.vis.global_.offwidth
    buffer_height = physics.model.vis.global_.offheight
    if width > buffer_width:
      raise ValueError('Image width {} > framebuffer width {}. Either reduce '
                       'the image width or specify a larger offscreen '
                       'framebuffer in the model XML using the clause\n'
                       '<visual>\n'
                       '  <global offwidth="my_width"/>\n'
                       '</visual>'.format(width, buffer_width))
    if height > buffer_height:
      raise ValueError('Image height {} > framebuffer height {}. Either reduce '
                       'the image height or specify a larger offscreen '
                       'framebuffer in the model XML using the clause\n'
                       '<visual>\n'
                       '  <global offheight="my_height"/>\n'
                       '</visual>'.format(height, buffer_height))
    if isinstance(camera_id, six.string_types):
      camera_id = physics.model.name2id(camera_id, 'camera')
    if camera_id < -1:
      raise ValueError('camera_id cannot be smaller than -1.')
    if camera_id >= physics.model.ncam:
      raise ValueError('model has {} fixed cameras. camera_id={} is invalid.'.
                       format(physics.model.ncam, camera_id))

    self._width = width
    self._height = height
    self._physics = physics

    # Variables corresponding to structs needed by Mujoco's rendering functions.
    self._scene = wrapper.MjvScene()
    self._scene_option = wrapper.MjvOption()

    self._perturb = wrapper.MjvPerturb()
    self._perturb.active = 0
    self._perturb.select = 0

    self._rect = types.MJRRECT(0, 0, self._width, self._height)

    self._render_camera = wrapper.MjvCamera()
    self._render_camera.fixedcamid = camera_id

    if camera_id == -1:
      self._render_camera.type_ = enums.mjtCamera.mjCAMERA_FREE
    else:
      # As defined in the Mujoco documentation, mjCAMERA_FIXED refers to a
      # camera explicitly defined in the model.
      self._render_camera.type_ = enums.mjtCamera.mjCAMERA_FIXED

    # Internal buffers.
    self._rgb_buffer = np.empty((self._height, self._width, 3), dtype=np.uint8)
    self._depth_buffer = np.empty((self._height, self._width), dtype=np.float32)

    if self._physics.contexts.mujoco is not None:
      with self._physics.contexts.gl.make_current(self._width, self._height):
        mjlib.mjr_setBuffer(enums.mjtFramebuffer.mjFB_OFFSCREEN,
                            self._physics.contexts.mujoco.ptr)