Time for action — inheriting from a material

We will create two new materials and one new quad. We will also change how our quad is defined:

  1. For this example, we need a quad that simply displays one texture. Change the quad definition to use only texture coordinates between 0 and 1 and remember to change the used material to MyMaterial11, which we will create soon:
    manual->begin("MyMaterial11", RenderOperation::OT_TRIANGLE_LIST);
    manual->position(5.0, 0.0, 0.0);
    manual->textureCoord(0.0,1.0);
    manual->position(-5.0, 10.0, 0.0);
    manual->textureCoord(1.0,0.0);
    manual->position(-5.0, 0.0, 0.0);
    manual->textureCoord(1.0,1.0);
    manual->position(5.0, 10.0, 0.0);
    manual->textureCoord(0.0,0.0);
    manual->index(0);
    manual->index(1);
    manual->index(2);
    manual->index(0);
    manual->index(3);
    manual->index(1);
    manual->end();
    
  2. The new material will use the rock texture and use the attribute rotate_anim, which rotates the texture with the given speed. But the most important thing is to name the texture unit texture1:
    material MyMaterial11
    {
    technique
    {
    pass
    {
    texture_unit texture1
    {
    texture terr_rock6.jpg
    rotate_anim 0.1
    }
    }
    }
    }
    
  3. Now create a second quad and translate it 15 units on the x-axis so that it doesn't intersect with the first quad. Also use the setMaterialName() function to change the material used by the entity to MyMaterial12:
    ent = mSceneMgr->createEntity("Quad");
    ent->setMaterialName("MyMaterial12");node = mSceneMgr->getRootSceneNode()->createChildSceneNode("Node2",Ogre::Vector3(15,0,0));
    node->attachObject(ent);
    
  4. The last thing to do is to create MyMaterial12. We will inherit from MyMaterial11 and set the texture alias to another texture that we want to use:
    material MyMaterial12 : MyMaterial11
    {
    set_texture_alias texture1 Water02.jpg
    }
    
  5. Compile and run the application, and you should see two quads with rotating textures one is a rock texture and the other one is a water texture.
    Time for action — inheriting from a material

What just happened?

We created two quads, each with its own material. Steps 1 and 2 just modified the quad to only use texture coordinates in the range of [0,1]. In step 2, we created our material for the quad and used the new attribute rotate_anim x, which rotates the texture x turns per second nothing fancy. Also we gave the texture unit the name texture1; we need this name later. In step 3, we created another instance of the quad and used the setMaterialName() function to change the material used by the entity. The important part was step 4. Here we created a new material by using inheritance, a concept which should be familiar. The syntax is the same as in C++, NewName : ParentName. In this case, MyMaterial12 inherits from MyMaterial11. Then we use the attribute set_texture_alias that binds the texture Water02.jpg to the texture unit texture1. In this case, we replace terr_rock6.jpg with Water02.jpg. Because this is the only change we wanted to make with our new material, we can stop here.

The use of texture aliases enables us to create a lot of materials that only differ in the used texture without the need to write each material from the ground up, and we all know that duplication should always be avoided, if possible.

We have covered a lot of things about materials, but there is a lot more that we can do. We have covered the basics and with the help of the documentation, it should be possible to understand most of the other attributes that can be used in materials. Just take a look here http://www.ogre3d.org/docs/manual/manual_14.html#SEC23. We will now go a bit deeper and learn how to program our graphics card with the so-called shaders.

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

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