Stencil shadows in Ogre and Blender model exportation

If you are gonna use stencil shadows in Ogre, you should carefully read the manual page about this shadowing technique, and be sure to fully understand what is explained there. Besides, you can find some useful hints and tips regarding this topic that I have discovered while adding shadows to my scenes.

  1. Any object (mesh) casting shadows must contain an edge list, which can be generated either when exporting the mesh in Blender (or any other modelling tool) or calling Mesh::buildEdgeList before the mesh is used in run-time. If you are using Blender, make sure that the option Edge Lists is enabled when exporting your model (like I explained in a previous post).

    Edge Lists option when exporting.

    Edge Lists option when exporting.

  2. Current Ogre release (v1.8.1) has a bug when computing stencil shadows if a light source is placed inside an object. Read this thread. Solutions: a) do not place a light source inside an object (with edge list), b) apply the patch posted in the previous thread to your Ogre sources (this patch will be included in the next maintenance release), c) consider switching to the texture shadows technique.
  3. Stencil shadows project the object geometry. The shadow caster class (OgreShadowCaster) has a 16-bit index limit on the number of vertexes. This limit will only be shown to the user in debug mode with an assert. In release mode, this limit will not trigger anything, but shadows will not work properly. In addition, the performance may be seriously penalized, if big meshes are used with this technique due to the geometry projection. Therefore, if shadows do not work properly, make sure none of your meshes exceeds the 16-bit limit executing in debug mode. If this requirement is not meet, you can split the mesh in different pieces (that will help Ogre to cull non visible parts), or reduce the number of vertexes.

Modelling the Earth with Blender and exporting to Ogre

If you don’t know anything about 3D modelling (like me) and you directly install and open Blender to play with, you will spend a lot of time with little or none profit. There are many websites with Blender online tutorials and videos. If you are using the graphic engine Ogre, I recommend to start reading this.

In this post, I will explain how to create a very simple model, export it to the Ogre format, and check it with an external tool. Prerequisites: you need Blender and its Ogre exporter addon installed.

Take  into account that a real time game engine needs to precompute as many things as possible. Therefore, surfaces are not suitable for using them in real time, and only meshes can be exported to Ogre. However, it is possible to work with surfaces, if the final result is converted to meshes prior to the exportation.

During the installation process, the exporter must be configured to work with some Ogre command-line tools, because the exporter will create an XML file (with a name ended in .mesh.xml) for each mesh with all its data (e.g. vertices and normals), and this XML mesh file must be compiled into a binary mesh file (with a file name extension .mesh) for run-time loading and usage.

I am a complete Blender noob, but I will try to clearly explain how to model the Earth and export the scene for using it with Ogre. These steps were obtained after applying reverse engineering to this Youtube video:

Watch the video and use the following instructions:

  1. Open Blender 2.65. Optional: at the upper area, click and drag at right bottom corner, so you can divide the upper client area into two halves. The right half may be switched to the UV/Image Editor with editor type switch at the bottom left. Note that with F12, it is possible to render the scene and see the final result in the right half. At the lower client area switch to the UV/Image Editor, too. We will use this are when editing the UV Map.

    Split screen.

    Split screen.

  2. Remove the cube at the left panel in Object Mode with the DEL.
  3. Create the sphere. Go to the Add menu > Mesh > UV Sphere. A new sphere should appear where the Blender cursor is.
  4. Parametrize the sphere. At the left, a new section Add UV Sphere, below the Object Tools section, contains the settings to parametrize the object. In this case, I need a mesh with a high resolution for looking at it from a closer point of view, with the sun light showing the day and night cycle.
    In order to avoid jagged borders in the illumination set:

    • The number of segments to 128.
    • The number of rings to 64.
    • Size 0.5, because I want a diameter of 1.0.
    • Make sure that the position is (0.0, 0.0, 0.0).

      Parametrized sphere.

      Parametrized sphere.

  5. Set the object name to Earth. Your scene should look similar to this one:

    Earth's sphere.

    Earth’s sphere.

  6. Add material. Go to the right tool box with the properties and click on the material tab:
    • Add a new material.
    • Change its name to EarthMaterial.
    • Now, switch to the texture tab.
    • Add a new texture.
    • Change its name to EarthTexture.
    • Change its type to Image or Movie.
    • Find the open button below and browse for the image texture.
    • Scroll down to Mapping. Change the projection to UV and leave the projection as Flat. This means that we will create the UVMap manually. It is mandatory to create a UV map for explicitly tell to Ogre how the texture is mapped on the sphere surface.
  7. The texture is associated to a given path. Therefore, if it is removed from the current location, the material will be broken. To avoid this situation, it is possible to pack the texture. Go to the lower client area and select the texture image instead of the render result. Click on the Image menu and select Pack Image. Save the scene in Blender format as a checkpoint.

    Packing texture.

    Packing texture.

  8. Go to the 3D View, switch to Edit mode, and rotate Earth until one of the poles is clearly visible.

    Pole view.

    Pole view.

  9. Select at the Mesh Tools box from the right the Loop Cut and Slide tool. Cut between the last ring and its previous one.

    Loop cut.

    Loop cut.

  10. Slide the cut until you almost touch the last ring.

    Loop slide.

    Loop slide.

  11. Press ALT+RMB (right mouse button) on the old ring, not the new one we have just created using the Loop Cut and Slide tool, to select it.
  12. Press SHIFT+RMB on the pole vertex for adding it to the selection.
  13. Change the pivot point to the active selection, which should be the pole vertex.

    Change pivot element.

    Change pivot element.

  14. Press S, to scale the old ring and shrink it as much as possible.

    Scale old ring.

    Scale old ring.

  15. Repeat steps 8 through 14 for the opposite pole vertex.
  16. Now, we are going to unwrap the sphere, and our first step is to define the seam used for cutting the object surface. Use again ALT+RMB on a segment to select it.

    Select a segment.

    Select a segment.

  17. Press CTRL+E and select Mark seam. Note that one everything is deselected, pressing A, the segment color has changed to red.

    Mark seam.

    Mark seam.

  18. Change viewport shading to texture mode.

    Change viewport shading to texture.

    Change viewport shading to texture.

  19. Change to select faces, right click on a adjacent face to the seam. Press A to select all faces. Finally, press U to open the UV unwrap menu. Select Follow Active Quads.

    Unwrap following active quads.

    Unwrap following active quads.

  20. Make sure that your texture image is visible in the lower area. Note that the cursor is at the left bottom corner. Don’t touch it, from now on, you are only allowed to use the MMB (middle mouse button) to pan 🙂 .

    Initial unwrapping.

    Initial unwrapping.

  21. Press A to select all the quads. In this particular case, we need to rotate the quads -90º. Press R, type -90 and press enter. Use only keys S and G to scale and translate the quads. If you want to scale only using the X or Y axis press S and then X or Y, respectively. Remember to use only the MMB to pan, and the mouse wheel to zoom, do not use the other mouse buttons.
  22. Match the left bottom corners of quad mesh and texture.

    Match left bottom corners.

    Match left bottom corners.

  23. Change the rotation and scaling pivot to 2D Cursor.

    Change pivot.

    Change pivot.

  24. Do the same for the right top corner, and the UV mapping will be almost complete.

    Completed UV mapping.

    Completed UV mapping.

  25. So far, we have mapped every sphere quad to the texture. However, do you remember the “old ring” that we shrink to the pole vertex? The ring and the pole vertex are joined with triangles, not quads. Therefore, these triangles were not included in the UV mapping and we should fix this. Rotate and translate the sphere to make the pole vertex visible. Make sure that vertex selection mode is on. Press CTRL+LMB and draw a circle for selecting the small ring surrounding the pole.
  26. Press SHIFT+RMB on the pole vertex for adding it to the selection.
  27. Press ALT+M to merge vertexes At Last. Make sure that every vertex of the ring is selected like in the following picture. You may need to scale the sphere and press CTRL+MMB moving the mouse in order to place as close as possible the pole vertex to the screen.

    Merge vertices.

    Merge vertices.

  28. We are almost there! Any transformation made to the object will not be exported, unless it is explicitly applied to the object, because it is not really part of it. This concept is very similar to the Ogre scene hierarchy. Every entity is assigned to a scene node. This scene node may have one or more nodes in its path to the root node of the scene tree. The transformations of each node in this path define the eventual transformation of the leaf node. Therefore, an entity and its transformations (translation, orientation and scaling) are decoupled. In Blender, this is identical. The entity is our mesh, and the eventual transformations are our Blender transformations. In short, you must always apply the transformations made to the object before you export it, or they will be lost.
    I placed my seam at the segment that crosses the Y axis. The reason is that I want to rotate -90º degreees using the X axis and make the Z axis cross near Africa and Europe, because Ogre uses a different orientation an the Y axis defines the up vector, instead of the Z axis used in Blender. Press R+X, type -90 and ENTER.
    Switch to the Object mode. Press CTRL+A and apply location, rotation and scale, if needed.

    Final rotation.

    Final rotation.

  29. We are now ready to export. Go to File > Export > Ogre 3D.

    Ogre export.

    Ogre export.

  30. The generated files should be something similar to this.

    Generated files.

    Generated files.

  31. The Earth.mesh file can be checked with Ogre Meshy.

    Ogre Meshy view.

    Ogre Meshy view.

Note: Alternatively, you might also try using a PolySphere, since the spherical surface division into quads is much more regular. However, the unwrapping for building the UV mapping may be more complicated. PolySphere is included with the Extra Object Addon. It looks spheric without the subsurf and smooth shading.

You can enable the Addon via:
File > User Preferences > Addons > Add Mesh > Add Mesh: Extra Objects.

And then add a PolySphere via:
View 3D > Add > Mesh > Extra Objects > PolySphere.

Installing Blender and Ogre scene and mesh exporter under Windows 7

If you are working with Ogre, you will need to model and export objects to build your scene. The following instructions will help you to install and configure Blender and the Ogre scene and mesh exporter (but don’t forget to read the official threads and readme files):

  1. Download the latest version of the Ogre scene and mesh exporter from the official Ogre forum thread. Unzip it and read carefully the documentation included.
  2. Download and install the exporter supported version of Blender.
  3. Find the Python version that Blender is using. Download it from the official page and install it.
  4. Add Python installation folder to the system variable PATH. Add a user or system variable named PYTHON pointing to the Python executable.
  5. Download and install the Ogre Command-line Tools. You may want to update OgreMeshUpgrader.exe and OgreXMLConverter.exe with the versions compiled from the latest Ogre sources. In this case, move the installed OgreMeshMagick.exe and OgreMain.dll to a sub or separate folder. Replace the mesh upgrader and XML converter executables and the OgreMain.dll with the ones you have compiled, as explained in another post. Add any created folder to the system variable PATH.
  6. Open Blender and go to File -> User Preferences. Click on Addons tab. At the bottom of the floating window, click on the “Install from file…” button and select the io_export_ogreDotScene.py script.
  7. Make sure that the check box at the right edge of the newly created addon is selected in order to activate it. Click on “Save it as default” for loading the addon each time Blender starts.
  8. Restart Blender and check that two new export formats have appear at File -> Export: Ogre3D and realXtend Tundra.
  9. Configure Ogre exporter options in Blender. Look for an “Ogre” checkbox at the end of the Blender menus (near “Ogre Help”), whose tooltip says “toggle Ogre UI”. Check it. Then, go to the tool box at the right, scene properties, and a new section “Ogre Configuration File” should have been appeared at the bottom. Make sure that, at least, the OGRETOOLS paths are pointing to the Ogre command-line executables.

    Ogre Exporter Options

    Ogre Exporter Options in Blender

  10. Download and install Ogre Meshy for being able to easily load, inspect and check your exported models. Add the executable path to the “Ogre Configuration File” section of the exporter.
  11. Download and install ImageMagick binaries for dealing with texture image conversions. Add the executable path to the “Ogre Configuration File”.
  12. Optional steps:
    If you need support for DDS texture files or realXtend Tundra, check the exporter readme file to download and install the utilities to support those formats.

The final step is to export the scene that Blender shows by default with a cube, a camera and a light, and check with Ogre Meshy that everything is properly exported.

Compiling Bullet v2.81 rev2613 under Windows 7 with Visual Studio

My project uses the Bullet physics engine together with the Ogre graphics engine, so the compilation of Bullet must be done with certain settings to properly build the final executable. These settings are, fast floting point and, the most important, DLL runtime of C/C++ (that is, /MD in release and /MDd in debug).

  1. You can download the latest version of Bullet and in the download section of the project’s code webpage.
  2. Open the CMake GUI and select the folder where you decompressed the source files using the “Browse Source” button. Select a folder where the library will be built with the “Browse Build” button.
  3. You can modify the options at your will, but I used the following:
    • BUILD_CPU_DEMOS
    • BUILD_DEMOS
    • BUILD_EXTRAS
    • BUILD_MULTITHREADING
    • USE_DX11
    • USE_GLUT
    • USE_GRAPHICAL_BENCHMARK
    • USE_MSVC_FAST_FLOATINGPOINT (as Ogre graphic engine does)
    • USE_MSVC_RUNTIME_LIBRARY_DLL (as Ogre graphic engine does)
    • USE_MSVC_SSE
    • In my CMake project, some libraries have a different name only in the debug configuration. Therefore, make sure that CMAKE_RELWITHDEBINFO_POSTFIX is empty.
    • Remove the MinSizeRel configuration if you are not using it.
  4. Configure and generate the project.
  5. Build it in MSVC and run some of the tests.
  6. You are now ready to include this library in your project together with Ogre.

Compiling Ogre v1.8.1 under Windows 7 with Visual Studio

Today I am struggling with the compilation of Ogre under Windows 7 x86-64 with SP1 with Visual Studio 2010 SP1. The easiest way of generating the solution files is using CMake and these instructions. However, there are some complementary details that you can find in the following steps, which I have followed to build this amazing 3D graphic engine.

  1. Install Boost. Under Windows it is advisable to use precompiled binaries that can be found at BoostPro. I used version 1.51 and I installed the chrono, date-time, threads and system libraries.
  2. Install DirectX SDK in order to build the renderer for this API. This will allow the user to choose between OpenGL and DirectX.The newest version is June 2010.
  3. Download Ogre and Ogre Dependencies from the download page.
  4. Generate the solution file with CMake for the dependencies.
  5. Build and install the dependencies.
  6. Add the following environment variables: BOOST_ROOT, DXSDK_HOME, and OGRE_DEPENDENCIES_DIR with their values set to the respective installation directory. Note that there is a DXSDK_DIR variable, but I create my own variables for every library as XXXX_HOME. However, Boost and Ogre dependencies variable names are imposed, and their names can not be changed or the Ogre CMake files will not “configure” properly.
  7. Generate the solution file for Ogre with CMake, and build the solution with Visual Studio.