Time for action — adding a compositor

This is going to be our first compositor. We will use the scene and prepare to see its effect.

  1. We need a new material for this, which does nothing at the moment. Add this new material in the material file that we previously used and name it Ogre3DBeginnersGuide/Comp1:
    material Ogre3DBeginnersGuide/Comp1
    {
    technique
    {
    pass
    {
    texture_unit
    {
    }
    }
    }
    }
    
  2. Next, create a new file for storing our compositor scripts. In the same directory as the material file, create a file named Ogre3DBeginnersGuide.compositor.
  3. In this file, define our compositor using the same scheme as we did for materials:
    compositor Compositor1
    {
    technique
    {
    
  4. Next, define a target where our scene is rendered to before we can modify it:
    texture scene target_width target_height PF_R8G8B8
    
  5. Define the content of our target. In this case, it's the scene that was rendered before:
    target scene
    {
    input previous
    }
    
  6. The last step in the compositor script is to define the output:
    target_output
    {
    
  7. The compositor doesn't need any input and renders its result to a quad that covers the whole screen. This quad uses the material Ogre3DBeginnersGuide/Comp1 and needs our scene target as a texture input:
    input none
    pass render_quad
    {
    material Ogre3DBeginnersGuide/Comp1
    input 0 scene
    }
    
  8. That's all for this compositor. Close all open curly brackets:
    }
    }
    }
    
  9. Now that we have a finished a compositor script, let's add it to our scene. For this, we use CompositorManager and the viewport of our camera. Add the code to the createScene() function:
    Ogre::CompositorManager::getSingleton().addCompositor(mCamera->getViewport(), "Compositor1");
    Ogre::CompositorManager::getSingleton().setCompositorEnabled(mCamera->getViewport(), "Compositor1", true);
    
  10. Compile and run the application, and you should see the same scene as before.

What just happened?

We added our first compositor using a compositor file containing our compositor script. Step 1 just created an empty material, which simply rendered everything it got without any added effects. Step 2 created a new file to store our compositor scripts; it's like material files just for compositors. Step 3 is also pretty familiar: we named our first compositor Compositor1 and defined a technique to use just like materials compositors have different techniques for different target graphic cards. The interesting part starts with step 4: here we created a new texture named scene which has the same dimensions as the target texture we are rendering to, and this texture used eight bits for each color part. This is defined by PF_R8G8B8.

How the compositor works

But why do we need to create a new texture in the first place? To understand this, we need to understand how a compositor works. Compositors modify the appearance of a scene after it has been rendered. It's like post processing in movies where they add all the computer effects after the shooting of a movie. To be able to do this, the compositor needs the rendered scene as a texture, so it can be modified further. We created this texture in step 4, and in step 5, we told Ogre 3D to fill this texture with the scene we rendered before. This was done with input previous. Now we have a texture that contains our scene rendered, the next thing our compositor needs to do is create a new image, which is displayed on the monitor. This is done in step 6 using the keyword output in combination with a target block. Step 7 defines this output. We don't need any input because we already have our scene in the scene texture. To render the modified texture, we need a quad that covers the whole monitor, onto which we can render the texture with the modified scene. This is achieved with the pass keyword followed by the render_quad identifier. There are several other identifiers we can use in combination with the pass keyword. They can be found in the documentation (http://www.ogre3d.org/docs/manual/manual_32.html#SEC163). Inside the pass block, we define several attributes we want to use for this rendering pass. This is the first material we want the quad to use; here we just use the material we defined beforehand, which renders the texture we are giving it to the quad without modifying it. The next attribute defines additional input like textures; here we say the first texture we want as input should be the scene texture. In fact, this means we want our scene rendered to a texture and applied to a quad that covers the whole screen. With this, we won't see any difference between the rendered scene with or without a compositor. This will change when we add some code to our material, which modifies the incoming texture to add additional effects.

Step 9 adds the compositor to our viewport and then enables it; we add compositors to a viewport and not to our scene because compositors modify the appearance of a scene seen through a camera, and what a camera sees is defined within the viewport. So if we want to modify the appearance of a complete scene, we would add the compositor to the object that defines the appearance of the scene itself.

The following is a simplified diagram showing the workflow of our compositor and an abridged version of the compositor script, which is the code for the step shown by the diagram.

How the compositor works
..................Content has been hidden....................

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