====== LAB4: Resources ======
This lab is only losely based on Chapter 6 on resource management in the text book. The focus here is on being able to create a new resource (a 3d model) and bringing it into an Ogre 3D scene.
===== Discussion =====
Discussion thread for this lab is here: [[https://piazza.com/class/ij2xbg3ztb41qt?cid=27|Lab 4 Discussion Thread]]
===== Goal ======
The primary goal with this lab is to go through the process of creating an original external resource (a 3D model in Blender) and bringing it "unharmed" into an Ogre 3D scene. The secondary goal is to gain familiarity with the Ogre 3D "mesh" and "material" file formats.
===== Preparation =====
As always before, create a new project based. Use the guides in Lab 1 or 2 in-case you have forgotten.
You need to do the following to set up the right tools:
* Download and install the [[http://www.blender.org/|Blender 3D]] modeling and rendering environment, use the latest version (2.76b)
* Install the Blender2Ogre Plug-in for Blender
* Download the file [[http://code.google.com/p/blender2ogre/downloads/detail?name=blender2ogre-0.6.0.zip|blender2ogre-0.6.0.zip]]
* Run Blender and from **File->User Preferences...** choose the **Addons** panel and click the **Install Add-On** button. Navigate to and select your downloaded ZIP archive.
* Once Blender has processed the ZIP file you should see the exporter listed, but grayed out. Check the check-box next to it to activate it.
* Now you can run the exporter from the Blender **File->Export** menu (pick the Ogre 3D export) and make sure that the object you want to export has been selected in the 3D scene.
* Make sure that the **OgreXMLConverter.exe** (or OgreXMLConverter_d.exe) that comes with Ogre is in your executable system path, you will find this tool inside your Ogre **bin\Release** (or **bin\Debug**) folder
===== Lab Project =====
Follow these steps to complete the lab project:
- **Create a custom Model Folder** Go to the folder LabFiles that you created in Lab 1 and create a sub-folder called ''Models''. In order for this folder to be found by your Ogre application, you need to add it to the list of FileSystems in your application's ''resources_d.cfg'' file. You can use a relative path (e.g. ''FileSystem=../LabFiles/Models'').
- **Create a 3D Model in Blender** If you are completely new to Blender, then you can even just use the default cube you get when you start up the Blender environment. However, you should try to modify the cube a little bit if you can. There are a lot of good resources on Blender modeling, including the [[http://wiki.blender.org/index.php/Doc:2.6/Tutorials/Resources/External_links|Official Getting Started]] tutorials out on the Internet.
* It is a good idea to save your model in the Blender file format early and often
* Pay attention to what you name your geometry, this becomes the name of the exported mesh. You can browse your object hierarchy in the upper right corner of the Blender screen. Right-click on the cube mesh node and select **Rename** to give the mesh a different name.
- **Export and Convert Model** Export the model using the Blender2Ogre exporter and save it in your application's **Models** folder. Make sure that any texture imported, baked or generated is saved as a file in the same folder. Run the **OgreXMLConverter** command line tool on the exported **XML** mesh file to turn it into a binary Ogre **mesh** file (The OgreXmlConverter should be in your path if you followed the instructions in Lab1).
- **Create a New Entity** In your ''createScene'' method in your application, you should now create a new Entity that gets associated with the model resource you just created. Make sure that your code makes this new Entity visible in the scene by attaching it to the scene graph.
* In C++ it would look like this:
Ogre::Entity* myModel = _sceneManager->createEntity("MyModel", "MyModelMesh.mesh");
Ogre::SceneNode* modelNode = _sceneManager->getRootSceneNode()->createChildSceneNode();
modelNode->attachObject(myModel);
modelNode->setScale(50,10,50); // You may have to scale your object to see it well\\
- **Play with Mesh File** Open up your exported **mesh** **XML** file and make sure you understand its structure. Try changing values inside it and observe the changes in your Ogre application, but remember to run the OgreXMLConverter again!
- **Play with Material File** Notice that inside the **XML** file the submesh refers to a **Material** by name, find the corresponding ** *.material ** file in your model folder (it got exported along with the mesh XML file) and read through it. This is the material that Ogre reads in along with the mesh. Try changing values of the material (e.g. the diffuse values) inside the exported material file and observe the changes in your Ogre application (you do not have to run the OgreXMLConverter again for this, the material is already in the right format)
- **Create more Entities** Create more entities from the same mesh, but assign different materials to them. Note that you can still use the ''_sceneManager->createEntity("MyModel2", "MyModelMesh.mesh");'' method. The resource manager is clever enough not to load the model twice from disk. Create a new material inside the same material file you looked at earlier, by copy-pasting the material definition within the same file and changing its name (e.g. into **BlueMaterial**) and give it different values (e.g. making the diffuse color blue). Aassign the new material to a new entity in your Ogre application with the **setMaterialName** method like this:
myModel->setMaterialName("BlueMaterial");
===== Bonus Points =====
- **Add a new color to Sinbad**. In the zip file ''Sinbad.zip'' in the folder ''"../OgreSDK/media/pakcs/"''. There are various files that are related to mr. Sinbad. Including a Material file, some textures and extra mesh for Sinbads swords.
- **Sinbad has 7 "submeshes"** that can be accessed like so ''_SinbadEnt->getSubEntity( number here )''
- **The list of submeshes are:** 0 -> Eyes.
1 -> Green Skin.
2 -> Jewelry, cuffs, belt buckle.
3 -> Teeth.
4 -> Sword Sheaths.
5 -> Accessory spikes.
6 -> Boots, Pants, hat, accessories color.
- **Now change the material of a submesh.**
- Create a file ''MyMaterial.material'' and save it in a folder ''Materials'' next to the Model folder.
- Add the line ''FileSystem=../LabFiles/Materials'' to the ''resources_d.cfg''
- paste this material script into the file ''MyMaterial.material'' material Sinbad/Body2
{
receive_shadows on
technique
{
pass
{
ambient 0.75 0.75 0.75
diffuse 1 1 1 1
texture_unit
{
texture my_sinbad_body.png
}
}
}
}
- **Copy {{:public:t-gede-14-1:my_sinbad_body.png?linkonly| this_image }} into the folder** ''Materials'' And now finally apply this mesh to the body of Sinbad, you can tweak the texture if that's your fancy :) // In the MyApplication::createScene function
_SinbadEnt->getSubEntity(1)->setMaterialName("Sinbad/Body2");
- **Add additional entities into the scene** As you could have noticed in the ''Sinbad.zip'' pack file, there was also a mesh for his sword ''sword.mesh''.
- **Add a couple of swords to the sheaths on Sinbads back.**
- Create an ''Entity*'' for each sword using the ''sword.mesh'' as a mesh file.
- **Now you need to attach the swords to the sheath.** Each sheath has a "bone" that is an anchor point to attach the swords. This can be done using the function: Ogre::Entity::attachObjectToBone(std::string boneName, Ogre::Entity* object);
// Usage would be like
_sinbadEnt->attachObjectToBone("OhMyBone", mySword);
//(**Briefly about bones:** Bones are the logical structure of an animatable 3D object, bones are points of a skeleton that you can move, and assigned vertices will be transformed with the bones thus giving the illusion that the model has joints and movable limbs that follow a skeleton. Objects can be attached to those bones and they will be inserted to that bones transformation space.)//
- **A list of bones on the Sinbad model** can be accessed very similarly as the Animation states:// Create a bone iterator.
Ogre::Skeleton::BoneIterator biter = _Sinbad->sinbadEnt->getSkeleton()->getBoneIterator();
// Iterate over the available bones and write their names to the console.
while (biter.hasMoreElements()) {
Ogre::Bone *bone = biter.getNext();
std::cout << bone->getName() << std::endl;
}
- **With the click of a button or some event; equip the swords to his hands.** The bones of Sinbads hands are labeled Handle in the list of bones. Even though Sinbad has animation for drawing the swords, implementing that will not be required to get this bonus point. Though implementing that Sinbad clasps his hands around the hilts is easy to do using the animations ''HandsRelaxed and HandsClosed''\\ //**Note:** Enabling one and disabling the other is enough for the animation, adding time to them has no effect at all.//
===== When You Are Finished =====
Upload your **commented if necessary SOURCE files, no solution or project files please** into Lab4 in MySchool (zip them up if more than one) also include the required files such as materials or mesh files.