6 min read

OGRE 3D 1.7 Beginner’s Guide

Create real time 3D applications using OGRE 3D from scratch

  • Easy-to-follow introduction to OGRE 3D
  • Create exciting 3D applications using OGRE 3D
  • Create your own scenes and monsters, play with the lights and shadows, and learn to use plugins
  • Get challenged to be creative and make fun and addictive games on your own
  • A hands-on do-it-yourself approach with over 100 examples

Creating a white quad

We will use this to create a sample quad that we can experiment with.

Time for action – creating the quad

We will start with an empty application and insert the code for our quad into the createScene() function:

  1. Begin with creating the manual object:
    Ogre::ManualObject* manual = mSceneMgr-
    >createManualObject("Quad");
    manual->begin("BaseWhiteNoLighting", RenderOperation::OT_TRIANGLE_
    LIST);
  2. Create four points for our quad:

    manual->position(5.0, 0.0, 0.0);
    manual->textureCoord(0,1);
    manual->position(-5.0, 10.0, 0.0);
    manual->textureCoord(1,0);
    manual->position(-5.0, 0.0, 0.0);
    manual->textureCoord(1,1);
    manual->position(5.0, 10.0, 0.0);manual->textureCoord(0,0);


    
    
  3. Use indices to describe the quad:

    manual->index(0);
    manual->index(1);
    manual->index(2);

    manual->index(0);
    manual->index(3);
    manual->index(1);

    
    
  4. Finish the manual object and convert it to a mesh:

    manual->end();
    manual->convertToMesh(“Quad”);

    
    
  5. Create an instance of the entity and attach it to the scene using a scene node:

    Ogre::Entity * ent = mSceneMgr->createEntity(“Quad”);
    Ogre::SceneNode* node = mSceneMgr->getRootSceneNode()-
    >createChildSceneNode(“Node1”);
    node->attachObject(ent);

    
    
  6. Compile and run the application. You should see a white quad.

What just happened?

We used our knowledge to create a quad and attach to it a material that simply renders everything in white. The next step is to create our own material.

Creating our own material

Always rendering everything in white isn’t exactly exciting, so let’s create our first material.

Time for action – creating a material

Now, we are going to create our own material using the white quad we created.

  1. Change the material name in the application from BaseWhiteNoLighting to MyMaterial1:

    manual->begin(“MyMaterial1”, RenderOperation::OT_TRIANGLE_LIST);

    
    
  2. Create a new file named Ogre3DBeginnersGuide.material in the mediamaterialsscripts folder of our Ogre3D SDK.
  3. Write the following code into the material file:

    material MyMaterial1
    {
    technique
    {
    pass
    {
    texture_unit
    {
    texture leaf.png
    }
    }
    }
    }

    
    
  4. Compile and run the application. You should see a white quad with a plant drawn onto it.

What just happened?

We created our first material file. In Ogre 3D, materials can be defined in material files. To be able to find our material files, we need to put them in a directory listed in the resources.cfg, like the one we used. We also could give the path to the file directly in code using the ResourceManager.

To use our material defined in the material file, we just had to use the name during the begin call of the manual object.

The interesting part is the material file itself.

Materials

Each material starts with the keyword material, the name of the material, and then an open curly bracket. To end the material, use a closed curly bracket—this technique should be very familiar to you by now. Each material consists of one or more techniques; a technique describes a way to achieve the desired effect. Because there are a lot of different graphic cards with different capabilities, we can define several techniques and Ogre 3D goes from top to bottom and selects the first technique that is supported by the user’s graphic cards. Inside a technique, we can have several passes. A pass is a single rendering of your geometry. For most of the materials we are going to create, we only need one pass. However, some more complex materials might need two or three passes, so Ogre 3D enables us to define several passes per technique. In this pass, we only define a texture unit. A texture unit defines one texture and its properties. This time the only property we define is the texture to be used. We use leaf.png as the image used for our texture. This texture comes with the SDK and is in a folder that gets indexed by resources.cfg, so we can use it without any work from our side.

Have a go hero – creating another material

Create a new material called MyMaterial2 that uses Water02.jpg as an image.

Texture coordinates take two

There are different strategies used when texture coordinates are outside the 0 to 1 range. Now, let’s create some materials to see them in action.

Time for action – preparing our quad

We are going to use the quad from the previous example with the leaf texture material:

  1. Change the texture coordinates of the quad from range 0 to 1 to 0 to 2. The quad code should then look like this:

    manual->position(5.0, 0.0, 0.0);
    manual->textureCoord(0,2);
    manual->position(-5.0, 10.0, 0.0);
    manual->textureCoord(2,0);
    manual->position(-5.0, 0.0, 0.0);
    manual->textureCoord(2,2);
    manual->position(5.0, 10.0, 0.0);
    manual->textureCoord(0,0);

    
    
  2. Now compile and run the application. Just as before, we will see a quad with a leaf texture, but this time we will see the texture four times.

What just happened?

We simply changed our quad to have texture coordinates that range from zero to two. This means that Ogre 3D needs to use one of its strategies to render texture coordinates that are larger than 1. The default mode is wrap. This means each value over 1 is wrapped to be between zero and one. The following is a diagram showing this effect and how the texture coordinates are wrapped. Outside the corners, we see the original texture coordinates and inside the corners, we see the value after the wrapping. Also for better understanding, we see the four texture repetitions with their implicit texture coordinates.

We have seen how our texture gets wrapped using the default texture wrapping mode. Our plant texture shows the effect pretty well, but it doesn’t show the usefulness of this technique. Let’s use another texture to see the benefits of the wrapping mode.

Using the wrapping mode with another texture

Time for action – adding a rock texture

For this example, we are going to use another texture. Otherwise, we wouldn’t see the effect of this texture mode:

  1. Create a new material similar to the previous one, except change the used texture to: terr_rock6.jpg:

    material MyMaterial3
    {
    technique
    {
    pass
    {
    texture_unit
    {
    texture terr_rock6.jpg
    }
    }
    }
    }

    
    
  2. Change the used material from MyMaterial1 to MyMaterial3:

    manual->begin(“MyMaterial3”, RenderOperation::OT_TRIANGLE_LIST)

    
    
  3. Compile and run the application. You should see a quad covered in a rock texture.

What just happened?

This time, the quad seems like it’s covered in one single texture. We don’t see any obvious repetitions like we did with the plant texture. The reason for this is that, like we already know, the texture wrapping mode repeats. The texture was created in such a way that at the left end of the texture, the texture is started again with its right side and the same is true for the lower end. This kind of texture is called seamless. The texture we used was prepared so that the left and right side fit perfectly together. The same goes for the upper and lower part of the texture. If this wasn’t the case, we would see instances where the texture is repeated.

LEAVE A REPLY

Please enter your comment!
Please enter your name here