Entropy 3D Object Tutorial

From LagoonWiki

Jump to: navigation, search

This tutorial gives more information on creating 3D objects and putting them into your Galaga/Entropy/Ogre3D demo.

Contents

Loading Static Entities

The basics were introduced in the Entropy Lighting Tutorial, as follows.

Work in the entropy/ directory. Create a sub-directory to hold your models:

mkdir galaga/data/models/

Be sure to add it to resources.cfg so that anything you put there will be loaded when you start the engine.

Copy a robot model from entropy_dependencies:

cp ../entropy_dependencies/ogrenew/Samples/Media/models/robot.mesh galaga/data/models

The model is loaded and positioned with a scene node, very similarly to the way your particle effect system was. Add the following code to ogreApplication.cc near where you load your particle system:

Entity *robot1 = sceneManager->createEntity( "Robot1", "robot.mesh" );
SceneNode *robot1node = sceneManager->getRootSceneNode()->createChildSceneNode( "Robot1Node", Vector3( 600, 50, 900 ) );
robot1node->attachObject( robot1 );

Scale and Orientation for Static Entities

The previous example only showed how to locate the entity in your scene. You can also scale and rotate the entity.

Add these lines after your attachObject command and observe the effect:

robot1->setNormaliseNormals(true); 
robot1node->scale( 2, 2, 2 );

The setNormaliseNormals(true) line is a technical requirement for when we add colors and textures later. Without it the scale will scale the model's normals along with the shape itself, and interfere with proper interaction with the lighting.

Change the three scale arguments one at a time, to see what each does. (Put a comment in your code so you will remember!) Notice that you can use fractions for the arguments:

robot1node->scale( .5, 1, 2.3 );

Now try the following commands, one at a time, to see their effects:

robot1node->yaw( Degree( 90 ) );
robot1node->pitch( Degree( 90 ) );
robot1node->roll( Degree( 90 ) );

Put a comment after each when you determine what it does, so you will remember later.

Child Nodes

Now put a second robot in your demo. You can use the same mesh, but the name "Robot1" must be changed to something else, to keep them unique. You must also displace it: there is nothing to prevent you from putting them in identical positions, in which case it will look like a single robot.

Put the second robot in the scene and make sure you can see it.

Now we will attach the second robot to the first one. You should have a line similar to this for your second robot:

SceneNode *robot2node = sceneManager->getRootSceneNode()->createChildSceneNode( "Robot2Node", Vector3( 600, 50, 900 ) );

Change it to something like this, using your variable names:

 SceneNode *robot2node = robot1node->createChildSceneNode( "RobotNode2", Vector3( 0, 0, 0 ) );

The coordinates are now with respect to the position of the parent robot. Change the location or orientation of the first robot and see what effect it has on the second one.

Now make a little robot stand on the shoulder of a big robot in your scene. Make sure it remains on the big robot's shoulder if you reposition the big robot.

Creating Your Own Objects with Blender

This section gives a very lightweight introduction to using Blender to create 3D models for your Galaga/Entropy/Ogre3d scene. Many extensive tutorials can be found on the WWW.

Working with the OGRE Meshes Exporter

Start blender from your menu, or by typing

blender &

at the prompt. (You will find it useful to start it from the prompt in an empty directory, to make it easier to see exactly what files you are creating.)

Click the splash image to make it go away. By default, Blender creates a cube model when it starts. It is already selected, so just press your delete key to get rid if it, and confirm the deletion in the dialog that pops up.

We will create custom objects below, but for now just add a pre-fabricated object while learning how to export models for use in the Galaga/Entropy/Ogre3d environment. From Blender's main menu, pick Add->Mesh->UVsphere. Click OK in the pop-up dialog; the default number of segments will suffice for now. You now see a sphere in the edit window, and it is automatically selected.

When you export for use with OGRE, the name of a material will be embedded in the .mesh file. There is no easy way to change that name later. What we will do is create a simple color scheme in Blender, give it the name of the material we want to use, and export it. Then we can use the usual methods of creating a material with the name we told Blender to use, and since that name is embedded in the .mesh file, that is what our engine will use whenever it loads the model.

In Blender's lower panel there is a toolbar with a row of buttons. Click the button with the shaded sphere on it in order to bring up the material dialogs. There you will see a Links and Pipeline dialog, and one of the buttons in it says Add New. Click that button to get some more dialogs.

  • You may get more dialogs than will fit into the lower panel. If so, use your mouse wheel to shift the dialogs left or right in the panel. You can also minimize individual dialogs by clicking the arrows at their upper left corners.
  • Notice the the sphere you clicked has nothing to do with the fact that your model is a sphere. It's just the conventional icon for Blender's shading controls.

The leftmost dialog is a previewer. You will not do anything there, but you will see it change as you make the following modifications.

The second dialog has two tabs. In the Material tab, use the sliders to change the sphere's color, and observe the effect in the previewer.

  • This is not strictly necessary; I am just having you do it so you will understand what is going on better. You will later modify the resulting .material file just as you did in earlier tutorials.

The third dialog is called Links and Pipeline. Its top section is called Link to Object, and at the upper left is a field that contains text something like MA:Material.001. Click in that field and change the text after the colon to the name of the material you want to use.

  • Remember that all the materials loaded into the engine must have unique names. If you want to use a material you have already created, put that material's name here. Otherwise just use something like SphereMaterial. (Remember that this is the name of a material, not the filename of a material script. It is case sensitive.)

Now you are ready to export the model. In the main menu of the edit window, select File->Export->OGRE Meshes. The edit window will be replaced by the exporter dialog. Do the following in the dialog.

  • At the very top, the name of the selected object is displayed. For this example it should say Sphere.
  • Near the bottom of the dialog there is a section named Material Settings. In that section, enter the name of the material file you want to create when you export the model.
    • If you are going to use one of your existing materials, just leave this at the default because you will throw it away later. If you are going to create a new material for this model, give the name of the .material file that you will use to specify it, e.g. Sphere.material.
  • Further down there is a button named OgreXMLConverter. Click it to the 'in' position.
    • The exporter will normally create a .material file and a .mesh.xml file. If you have this button selected it will also run the external OgreXMLConverter program to create a .mesh file from the .mesh.xml file. If you do not have this button selected, you can run the program from the prompt later.
    • The dialog also lets you specify a path for the converter's output. If you leave it blank it will write the file to the directory where you started blender. If you fill it in, be sure you specify a directory where it will not overwrite your existing material scripts!
  • Now click the Export button. A log of the operations will pop up. Click OK to return to the exporter dialog, or Quit to dismiss the dialog entirely and return to the edit window.
  • Remember to use File->Save to save your model in the native Blender format as well. As with The GIMP when you are using layers, you want to save your 3D model in its native format in case you want to modify it later, in addition to exporting it in a format that can be used by the game engine.

In your output directory you will now find files such as these, though you may have changed some of the names:

OgreXMLConverter.log  Sphere.material  Sphere.mesh  Sphere.mesh.xml
  • The .log file can be ignored for now. Delete it if you do not need it for troubleshooting.
  • The .mesh.xml file is the main output of the exporter. You can delete it if you wish, because this is not the file you will actually load into the engine.
  • The .mesh file is what the OgreXMLConverter created from the .mesh.xml file. It is the actual mode that will be put into the engine. Move it to galaga/data/models/.
    • If you did not have the converter button selected, this file will not exist. You can create it by running the converter from the prompt, passing the name of the .mesh.xml file as an argument:
OgreXMLConverter Sphere.mesh.xml
  • The .material file is a material script that includes the material name you specified in Blender, as well as the color you put on the sphere.
    • If you just want to use the colors you set in Blender, move this file to galaga/data/materials/scripts/. (If you look at the file you will see that it is a normal .material file, but it specifies the colors directly rather than loading a PNG.)
    • If you told Blender to use the name of an existing material, just delete this and make sure the existing material gets loaded when you start the engine.
    • If you told Blender to use a new material that does not exist yet, edit this file to specify the material, and put it and any texture files it references in the usual places. Do not change the name of the material at the top of the file, because this is now embedded in your .mesh file. If you want to change the name of the material, change it in Blender and export it again.

Introduction to UV Mapping

This is a very simple introduction to a complex topic. There are many tutorials on the web, but be forewarned that some of them refer to elements of the Blender user interface that have changed since the tutorial was written, so some tutorials are very hard to decipher.

UV mapping is what we call it when we define a way to map a texture onto a 3D model. "UV" is like the familiar "x,y", but with a different choice of letters because we are not really using the same coordinate system.

Start Blender and add a UVSphere as before. (Other shapes will also work, but this makes a nice illustration of what is going on.)

Split your display. Leave one in "3D View", but change the other to "UV/Image Editor".

On the "3D View", set the view to "Perspective" to give you a slightly better feel for what you are looking at.

Change "Edit Mode" to "UV Face Select". The entire object should still be selected, but you can select it by pressing "a" when your mouse is over the "3D View" window if it is not.

Now, with your mouse still over the "3D View" window, press "u" to pop up the "UV Calculation" menu. Pick "Unwrap (smart projections)" down near the bottom of that menu. This will automatically create a UV map in the other window. You can see that the sphere has been chopped up and the pieces stretched flat on the rectangular area. This is a map that shows where the coordinates of an image corresponding to that rectangular area will be aligned with the vertices on the model.

For a complex model such as a robot, the artist usually takes a more direct hand in creating the map rather than doing it automatically. Then the map would be used as a layer in an image editor, to guide the artist in painting the model. For this example we will just put an existing texture on the sphere.

In the second window, use Image->Open and pick the texture you want to paste onto the sphere. The second window will now show this texture with the UV map overlain.

Now use the exporter as before, except you do not need to set the colors. You can still define the name of the material file, but it will automatically generate the name of the material itself, such as material SOLID/TEX/plasma.png. (Notice that the slashes are not part of a path; the whole thing is just a string that serves as the material's name.)

Move the material and model to their usual places, and examine them in the engine. Notice that since you did not carefully paint an image where the texture lines up across the splits in the sphere you will be able to see seams on the rendered object.

Introductory Blender Tutorials

The following tutorials will get you started creating your own models with Blender:

  1. Read the overview of the Blender user interface at blender.org.
  2. Work through the 'die' tutorial at blendernation.com.
  3. Work through the 'heart' tutorial at blendernation.com. Warning: NURBS are not currently supported in Ogre, so if you create a NURBS object according to the tutorial's instuctions you will not be able to export it to the Ogre format. Use the menu option Add->Mesh->UVSphere instead of the NURBS object.

Update Your Demonstration Scenes

Create several simple models of your own to add to your two scenes. Experiment with the various techniques described above, such as creating one object as a child of another.

There are additional tutorials at blender.org and blendernation.com that will help you discover new tricks. However, do not get distracted with animation at this point; we will cover that later.

Do not forget to save your new assets in your own directory, and update your scripts to copy them into the engine.