Changing a model's render attributes

One of the big strengths of Panda3D is its nice and clever programming interface that allows us to quickly put together scenes by loading and placing models and actors. Getting something rendered onto the screen works amazingly fast and easy.

Beyond this simple programming interface lies a very advanced renderer that takes the information from the current scene graph and draws the models and actors found in there to the screen. The way this rendering subsystem draws geometry can be changed by setting some render attributes, which we will explore in this recipe.

Getting ready

This recipe requires a project setup as described in Setting up the game structure found in Chapter 1, Setting Up Panda3D and Configuring Development Tools. Please revisit these instructions before proceeding with this tip.

How to do it...

Let's see how Panda3D allows you to change render attributes:

  1. Open Application.py and replace its content with the following code:
    from direct.showbase.ShowBase import ShowBase
    from direct.actor.Actor import Actor
    from panda3d.core import *
    from pandac.PandaModules import loadPrcFileData
    loadPrcFileData("", "multisamples 8")
    class Application(ShowBase):
    def __init__(self):
    ShowBase.__init__(self)
    self.pandas = []
    for i in range(4):
    panda = Actor("panda", {"walk": "panda-walk"})
    panda.reparentTo(render)
    panda.loop("walk")
    panda.setX(-10.5 + i * 7)
    self.pandas.append(panda)
    render.setAntialias(AntialiasAttrib.MAuto)
    mask = ColorWriteAttrib.CRed
    mask |= ColorWriteAttrib.CBlue
    mask |= ColorWriteAttrib.CAlpha
    self.pandas[0].setAttrib(ColorWriteAttrib.make(mask))
    self.pandas[1].setRenderMode(RenderModeAttrib.MWireframe, 1)
    self.pandas[2].setColorScale(0.5, 0.5, 0.5, 0.5)
    self.pandas[3].setColor(0.5, 0.5, 0.5, 0.5)
    self.smiley = loader.loadModel("smiley")
    self.smiley.reparentTo(render)
    self.smiley.setDepthWrite(False)
    self.smiley.setDepthTest(False)
    self.smiley.setPos(5, 20, 3)
    self.smiley.setScale(3)
    self.cam.setPos(0, -40, 6)
    
  2. Press F6 to start the application. You should now see the following scene:
How to do it...

How it works...

We start to compose this sample scene by loading and adding four pandas to the scene and setting them to walk in lockstep.

Then we set the whole scene graph to use Anti-aliasing. Note that the setAntialias() method on its own does not enable edge smoothing. We either need to add the statement multisamples 8 to the Config.prc file or use the loadPrcFileData() function to configure Panda3D to use antialiasing like we do in the sample code. This configuration option also controls the quality level to use for antialiasing, where a higher number means higher quality. In our code, we use eight samples, but other values can be used too. The number of samples must be a power of two. Feel free to try other quality settings! However, note that the given value only requests the given quality level from the graphics driver. If a multisampling mode is not supported, the closest matching quality setting will be chosen. If the graphics hardware doesn't support multisampling antialiasing at all, the setting is ignored.

The next feature of the renderer we use is a color write mask, which allows us to enable or disable drawing to the color and alpha channels of the screen buffer. We do this by creating a bit mask, adding the color channels that we wish to enable using bitwise OR operations. We turn off writing new color values to the green color channel, which explains the look of the panda on the outer left side of the scene. After the renderer clears the screen with the grey background color, the green color channel is static and cannot be changed anymore. As a result, the red and blue color channels are combined with the fixed value of the green color channel. In the end, this gives us a slightly pink and green panda.

The other three pandas get a different treatment: For the one second from the left, we enable the wireframe render mode, which can be useful for quickly visualizing the complexity of the geometry being rendered. The two pandas on the right show the difference between setColorScale() and setColor(). While the first method tints the second panda from right by multiplying the specified color with the original color, the latter method overrides the original color of the rightmost panda.

Lastly, we add the smiley model to the scene and set its position behind the four pandas, which should cause the model to be hidden. But because we disable writing to the depth buffer and depth testing, the position along the y-axis is completely ignored. So whatever is rendered last will be drawn over the pixels that already are in the color buffer. This feature may be used to ensure overlays like ammo and health meters to be always drawn on top of the rest of the scene, for example.

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

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