Making objects reflect the scene

This recipe will show you how to enable and use cube mapping to make your models and actors dynamically reflect any other game objects and the surrounding environment. This is a very useful effect for emphasizing movement by creating a glossy car paint effect in a racing game, for example.

Getting ready

This recipe requires you to have finished the steps of the recipe Setting up the game structure found in Chapter 1 and will follow up to where this recipe left off.

How to do it...

These are the tasks for this recipe:

  1. In the file Application.py, add the following code:
    from direct.showbase.ShowBase import ShowBase
    from panda3d.core import *
    from direct.interval.IntervalGlobal import *
    class Application(ShowBase):
    def __init__(self):
    ShowBase.__init__(self)
    self.world = loader.loadModel("environment")
    self.world.reparentTo(render)
    self.world.setScale(0.5)
    self.world.setPos(-8, 80, 0)
    self.teapot = loader.loadModel("teapot")
    self.teapot.reparentTo(render)
    self.teapot.setPos(0, 0, 10)
    cubeCams = NodePath("cubeCams")
    Panda3Dcube mapping, enablingcubeBuffer = self.win.makeCubeMap("cubemap", 128, cubeCams)
    cubeCams.reparentTo(self.teapot)
    tex = TextureStage.getDefault()
    self.teapot.setTexGen(tex, TexGenAttrib.MWorldCubeMap)
    self.teapot.setTexture(cubeBuffer.getTexture())
    rotate = self.teapot.hprInterval(10, Vec3(360, 0, 0), startHpr = Vec3(0, 0, 0))
    move = self.teapot.posInterval(10, Vec3(-10, 0, 10), startPos = Vec3(10, 0, 10))
    rotate.loop()
    move.loop()
    self.cam.setPos(0, -30, 10)
    
  2. Hit the F6 key to run the code and see the scene similar to the one shown in the following screenshot. Note how the teapot model reflects the scene:
How to do it...

How it works...

To render the reflections on the teapot model, we are using a technique called cube mapping. This effect renders the scene around a center point (marked by the object that uses the reflection texture) into six different textures, forming a so-called 'texture cube'. Each side of the cube is formed by a texture that stores what can be seen if we look up, down, left, right, forward, or backward from the center point. Finally, the six textures are mapped onto a model, making it appear as if it reflected its surroundings.

In the first few lines of code, we set up the scene that we are going to work with, before we add a new dummy node that will mark the center point from where the cube map will be generated.

Next we call the makeCubeMap() method, which initializes everything needed for rendering the teapot's surroundings into a texture with a size of 128x128 pixels, specified by the second parameter. The dummy node we pass as the third parameter to this method will act as parent node to the six cameras. These six cameras point at each direction from the center point and capture the image data that is then put into our cube map texture. This also means that our scene will be rendered six additional times, so be aware of the performance implications this brings!

Finally, we use TexGenAttrib.MWorldCubeMap to enable automatic and proper generation of texture coordinates on the teapot, before assigning the cube map texture by calling the getTexture() method on our cube map buffer.

In the closing steps, two intervals for moving and rotating the teapot are added to show off that the cube map texture is updated dynamically.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset