Search code examples
pythonopenglrenderphysicspyopengl

Render only a part of the sphere inside the cube in OpenGL using python?


I want to render only the parts where it is inside the cube to something similar that is shown in 2 figure

my current Code to render a sphere is here

def Sphere(ball):
    glPushMatrix()
    sphere = gluNewQuadric()
    glTranslatef(ball.pos.x,ball.pos.y,ball.pos.z) #Move to the place
    glColor3fv((1,0,0)) #Put color
    gluSphere(sphere, ball.radius, 16, 8) #Draw sphere
    glPopMatrix()

Current

Required


Solution

  • You can define clip panes with glClipPlane. e.g.:

    glEnable(GL_CLIP_PLANE0)
    glClipPlane(GL_CLIP_PLANE0, [1, 0, 0, distance])
    
    glEnable(GL_CLIP_PLANE1)
    glClipPlane(GL_CLIP_PLANE1, [0, 1, 0, distance])
    
    # [...]
    

    See also OpenGL overlapping ugly rendering