Light

Light is a special Context that can be assigned to the world, tags or sprites to illuminate their content (whatever is drawn in their onDraw()). Custom lights are defined by subclassing this class.

<world>.addLight(light)
<tag>.addLight(light)
<sprite>.addLight(light)
  • light ... an object that has been instanced from a Light class

light = ALight(name, pos, dir)
  • light ... an object that has been instanced from a Light class
  • name ... name of the light, a string
  • pos ... (x, y, z, p)
    • x, y, z ... location in space
    • p ... flag, 0 being a directional light and 1 being a point source

Relevant examples: shading.py, ALL

In Context

asphere = Sphere(self, 'sph1')
mywhitelight = LightBlue('l1', (5.0, 0.0, 0.0, 1.0), (-1.0, 0.0, 0.0))
asphere.addLight(mywhitelight)

In the above snippet the light source is moved up the x-axis by 5 units and pointed towards the origin. The fourth value makes it a point source.

Custom light classes can be defined by inheriting from slut.light.Light and overwriting onPreDraw() accordingly. Saving and restoring OpenGL's lighting state is done by the Light class. The default onPostDraw() will restore the lighting state to the state as it was before onPreDraw() was called. For this reason onPostDraw() does not have to be implemented by the inheriting class. The LightBlue from above may be defined as follows:

class LightBlue(Light):
    def onPreDraw(self):
        glLightfv(self.GL_LIGHT, GL_SPECULAR, (0.5, 0.5, 1.0, 1.0))
        glLightfv(self.GL_LIGHT, GL_DIFFUSE, (0.5, 0.5, 1.0, 1.0))
        glLightfv(self.GL_LIGHT, GL_AMBIENT, (0.5, 0.5, 1.0, 1.0))
        glLightfv(self.GL_LIGHT, GL_SPOT_EXPONENT, 0.0)
        glLightfv(self.GL_LIGHT, GL_SPOT_CUTOFF, 90.0)

OpengGL's lighting model depends not only on lights but also on the materials that are assigned to the objects of the scene. Slut's material handling is described in a different section: Material

Relevant OpenGL documentation: glLight , glMaterial , glLightModel

Initiated by Stephan Hechenberger
Thanks to CADRE's 103