Skip to content

Adding objects to the scene via OpenGL in mjviewer #28

Open
@mjm522

Description

@mjm522

Hi,

I was wondering if I could add primitive objects to the scene dynamically that is not present in the XML file. I don't want the physics of the objects to be simulated by mujoco. My core purpose is to translucent primitive shape in the scene that would show the prediction. For example, I have an box object in the scene that is described by the XML file. I want to overlay this box with an OpenGL cube that could be called from mjviewer so that I could remove this overlayed cube from time and again.

I tried adding it using the following bit of code to mjviewer.py. This draws a cube when independently tested. But I am unable to get it draw in the scene. Is it possible to do this or am I wasting time? Thanks for the time.

def drawCube(self, pos, ori, size):
        #this function expects center of the cube, orientation, and size as (length(x), breadh(y), height(z))
        
       glfw.make_context_current(self.window)
        self.gui_lock.acquire()

        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glLoadIdentity()
       
       gl.glTranslatef(pos[0],pos[1],pos[2])
        gl.glScalef(size[0],size[1],size[0])
 
        gl.glRotatef(ori[0],1.0,0.0,0.0)
        gl.glRotatef(ori[1],0.0,1.0,0.0)
        gl.glRotatef(ori[2],0.0,0.0,1.0)
 
        # Draw Cube (multiple quads)
        gl.glBegin(gl.GL_QUADS)
 
        gl.glColor3f(0.0,1.0,0.0)
        gl.glVertex3f( 1.0, 1.0,-1.0)
        gl.glVertex3f(-1.0, 1.0,-1.0)
        gl.glVertex3f(-1.0, 1.0, 1.0)
        gl.glVertex3f( 1.0, 1.0, 1.0) 
 
        gl.glColor3f(1.0,0.0,0.0)
        gl.glVertex3f( 1.0,-1.0, 1.0)
        gl.glVertex3f(-1.0,-1.0, 1.0)
        gl.glVertex3f(-1.0,-1.0,-1.0)
        gl.glVertex3f( 1.0,-1.0,-1.0) 
 
        gl.glColor3f(0.0,1.0,0.0)
        gl.glVertex3f( 1.0, 1.0, 1.0)
        gl.glVertex3f(-1.0, 1.0, 1.0)
        gl.glVertex3f(-1.0,-1.0, 1.0)
        gl.glVertex3f( 1.0,-1.0, 1.0)
 
        gl.glColor3f(1.0,1.0,0.0)
        gl.glVertex3f( 1.0,-1.0,-1.0)
        gl.glVertex3f(-1.0,-1.0,-1.0)
        gl.glVertex3f(-1.0, 1.0,-1.0)
        gl.glVertex3f( 1.0, 1.0,-1.0)
 
        gl.glColor3f(0.0,0.0,1.0)
        gl.glVertex3f(-1.0, 1.0, 1.0) 
        gl.glVertex3f(-1.0, 1.0,-1.0)
        gl.glVertex3f(-1.0,-1.0,-1.0) 
        gl.glVertex3f(-1.0,-1.0, 1.0) 
 
        gl.glColor3f(1.0,0.0,1.0)
        gl.glVertex3f( 1.0, 1.0,-1.0) 
        gl.glVertex3f( 1.0, 1.0, 1.0)
        gl.glVertex3f( 1.0,-1.0, 1.0)
        gl.glVertex3f( 1.0,-1.0,-1.0)

        gl.glEnd()
        self.gui_lock.release()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions