Skip to main content
Using Ogre3D 1.9 Statically        

This tutorial is based on the first one created to explain how Ogre3d 1.9 can be built statically in Mac OS X. You can find the first tutorial here: Building Ogre3D 1.9 Statically. The idea of this tutorial is to continue the previous work in order to render an Ogre Head inside a Window.

If you followed the previous tutorial, you should have the following two files:
CMakeLists.txt file:

Copy to clipboard
PROJECT(tutorial1) CMAKE_MINIMUM_REQUIRED(VERSION 2.8) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -std=c++11") SET(OGRE_BASE_FOLDER "/opt/dev/ogre3d-1.9.0/") SET(OGRE_INCLUDE_FOLDER "/opt/dev/ogre3d-1.9.0/build/sdk/include/OGRE") SET(OGRE_LIB_FOLDER "/opt/dev/ogre3d-1.9.0/build/sdk/lib") INCLUDE_DIRECTORIES(${OGRE_INCLUDE_FOLDER}) LINK_DIRECTORIES(${OGRE_LIB_FOLDER}) FILE(GLOB_RECURSE SRCS "${PROJECT_SOURCE_DIR}/src/*.cpp") FILE(GLOB_RECURSE HDRS "${PROJECT_SOURCE_DIR}/src/*.h") ADD_EXECUTABLE(${PROJECT_NAME} MACOSX_BUNDLE ${SRCS} ${HDRS}) TARGET_LINK_LIBRARIES( ${PROJECT_NAME} ${OGRE_LIB_FOLDER}/libOgreMainStatic.a )

A main.cpp file:

Copy to clipboard
#include <iostream> #include <OgreString.h> using namespace std; int main() { Ogre::String variable = "My first string using Ogre3D"; cout << "Hello World: " << variable << endl; return 0; }

Now the idea is to render a basic figure in our project. In order to do that, lets first include all the necessary libraries in our project. Change your CMakeLists.txt to look like this:

Copy to clipboard
PROJECT(tutorial1) CMAKE_MINIMUM_REQUIRED(VERSION 2.8) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -std=c++11") FIND_LIBRARY(COCOA_LIB Cocoa) IF(NOT COCOA_LIB) MESSAGE(FATAL_ERROR "Cocoa framework not found") ELSE() MESSAGE(STATUS "Cocoa framework found. Location: " ${COCOA_LIB}) ENDIF() FIND_LIBRARY(IOKIT_LIB IOKit) IF(NOT IOKIT_LIB) MESSAGE(FATAL_ERROR "IOKit framework not found") ELSE() MESSAGE(STATUS "IOKit framework found. Location: " ${IOKIT_LIB}) ENDIF() FIND_LIBRARY(OPENGL_LIB OpenGL) IF(NOT OPENGL_LIB) MESSAGE(FATAL_ERROR "OpenGL framework not found") ELSE() MESSAGE(STATUS "OpenGL framework found. Location: " ${OPENGL_LIB}) ENDIF() FIND_LIBRARY(AGL_LIB AGL) IF(NOT AGL_LIB) MESSAGE(FATAL_ERROR "AGL framework not found") ELSE() MESSAGE(STATUS "AGL framework found. Location: " ${AGL_LIB}) ENDIF() MESSAGE(STATUS "Including project libraries") INCLUDE_DIRECTORIES(/opt/1.9/ogre/build/sdk/include/OGRE) LINK_DIRECTORIES(/opt/1.9/ogre/build/sdk/lib) INCLUDE_DIRECTORIES(/opt/1.9/ogre/build/sdk/include/OGRE/RenderSystems/GL) LINK_DIRECTORIES(/opt/1.9/ogre/build/sdk/lib) INCLUDE_DIRECTORIES(/opt/1.9/ogre/build/sdk/include/OGRE/Plugins/ParticleFX) LINK_DIRECTORIES(/opt/1.9/ogre/build/sdk/lib) INCLUDE_DIRECTORIES(/opt/1.9/dependencies/include/OIS) LINK_DIRECTORIES(/opt/1.9/dependencies/lib/Release) INCLUDE_DIRECTORIES(/opt/1.9/dependencies/include/boost) INCLUDE_DIRECTORIES(/opt/1.9/dependencies/include) LINK_DIRECTORIES(/opt/1.9/dependencies/lib) INCLUDE_DIRECTORIES(/opt/1.9/dependencies/include/boost) LINK_DIRECTORIES(/opt/1.9/dependencies/lib) INCLUDE_DIRECTORIES(/opt/1.9/dependencies/include/freetype) LINK_DIRECTORIES(/opt/1.9/dependencies/lib/Release) INCLUDE_DIRECTORIES(/opt/1.9/dependencies/include/zzip) LINK_DIRECTORIES(/opt/1.9/dependencies/lib/Release) INCLUDE_DIRECTORIES(/opt/1.9/dependencies/include) LINK_DIRECTORIES(/opt/1.9/dependencies/lib/Release) INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src) INCLUDE_DIRECTORIES(/opt/1.9/ogre/OgreMain/include/OSX) FILE(GLOB_RECURSE SRCS "${PROJECT_SOURCE_DIR}/src/*.cpp") FILE(GLOB_RECURSE HDRS "${PROJECT_SOURCE_DIR}/src/*.h") FILE(GLOB RESOURCES_FOLDER ${PROJECT_SOURCE_DIR}/Resources) ADD_EXECUTABLE(${PROJECT_NAME} MACOSX_BUNDLE ${SRCS} ${HDRS}) TARGET_LINK_LIBRARIES( ${PROJECT_NAME} ${COCOA_LIB} ${IOKIT_LIB} ${OPENGL_LIB} ${AGL_LIB} /opt/1.9/ogre/build/sdk/lib/libOgreMainStatic.a /opt/1.9/ogre/build/sdk/lib/libRenderSystem_GLStatic.a /opt/1.9/ogre/build/sdk/lib/libPlugin_ParticleFXStatic.a /opt/1.9/dependencies/lib/libboost_system.a /opt/1.9/dependencies/lib/libboost_thread.a /opt/1.9/dependencies/lib/Release/libOIS.a /opt/1.9/dependencies/lib/Release/libfreetype.a /opt/1.9/dependencies/lib/Release/libzziplib.a /opt/1.9/dependencies/lib/Release/libFreeImage.a ) # Change the hardcoded name of the project here: FILE(COPY ${RESOURCES_FOLDER} DESTINATION ${PROJECT_SOURCE_DIR}/build/tutorial1.app/Contents)

Changes:

  • From line 6-32: Added basic frameworks (From the Apple page: "Frameworks is a hierarchical directory that encapsulates shared resources, such as a dynamic shared library, nib files, image files, localized strings, header files, and reference documentation in a single package."), including: Cocoa (graphical user interface), IOKit (inner core stuff for the the OS X platform), OpenGL (2D & 3D graphics), AGL (Apple Graphics Library).
  • From line 36-62: Including the projects libraries. You may not need all of them but usually for a basic Ogre3D project you will need them. Feel free to remove those that you don't need.
  • Line 68: You need to create a folder called Resources in you project's main folder, right next to the src folder. This folder will include the resources that the application (the bundle) file will need. This line copies the Resources folder from your project to the app bundle.

Your project's files structure should look like this:

Copy to clipboard
/opt/dev/ogreprojects/tutorial1 $>ls -l total 64 8 -rw-r--r-- 1 USER wheel 2910 Jan 31 22:50 CMakeLists.txt 32 -rw-r--r-- 1 USER wheel 12289 Jan 31 22:49 CMakeLists.txt.user 0 drwxr-xr-x 2 USER wheel 68 Jan 31 22:53 Resources 0 drwxr-xr-x 9 USER wheel 306 Dec 11 19:50 build 0 drwxr-xr-x 3 USER wheel 102 Dec 11 20:34 src

Now, run the CMake command from Build menu in QtCreator. After that Run your application. Everything should be working fine; in the Application Output window in QtCreator, you get this message:

Copy to clipboard
Starting /opt/dev/ogreprojects/tutorial1/build/tutorial1.app/Contents/MacOS/tutorial1... /opt/dev/ogreprojects/tutorial1/build/tutorial1.app/Contents/MacOS/tutorial1 exited with code 0

And if you go the application bundle directory you should see the Resources folder:

Copy to clipboard
/opt/dev/ogreprojects/tutorial1/build $>cd tutorial1.app/Contents /opt/dev/ogreprojects/tutorial1/build/tutorial1.app/Contents $>ls -l total 8 -rw-r--r-- 1 USER wheel 986 Jan 31 23:09 Info.plist drwxr-xr-x 3 USER wheel 102 Jan 31 23:09 MacOS drwxr-xr-x 2 USER wheel 68 Jan 31 23:09 Resources

Now add two files called application.h and application.cpp inside the src folder. The content of those files is this:
application.h:

Copy to clipboard
#ifndef APPLICATION_H #define APPLICATION_H namespace Ogre { class Root; class RenderWindow; class RenderSystem; class SceneManager; } class Application { public: Application(); ~Application(); void startApplication(); void loadPlugins(); bool setRenderSystem(); void initializeRenderSystem(); void createRenderWindow(); void initializeResources(); void parseResourceFileConfiguration(); void createAndSetTheScene(); private: Ogre::Root *root; }; #endif // APPLICATION_H

application.cpp:

Copy to clipboard
#include "application.h" #include <OgreRoot.h> Application::Application() : root( 0 ) { } Application::~Application() { if( root ) { delete root; } } void Application::startApplication() { root = new Ogre::Root( Ogre::StringUtil::BLANK ); }

Edit the main.cpp file to this:

Copy to clipboard
#include <OgreException.h> #include "application.h" int main() { Application application; try { application.startApplication(); }catch( Ogre::Exception &e ) { std::cerr << "An exception has occured: " << e.getFullDescription() << std::endl; } return 0; }

Compile and Run the project; in the Application Output window in QtCreator you should see:

Copy to clipboard
Starting /opt/dev/ogreprojects/tutorial1/build/tutorial1.app/Contents/MacOS/tutorial1... Creating resource group General Creating resource group Internal Creating resource group Autodetect SceneManagerFactory for type 'DefaultSceneManager' registered. Registering ResourceManager for type Material Registering ResourceManager for type Mesh Registering ResourceManager for type Skeleton MovableObjectFactory for type 'ParticleSystem' registered. ArchiveFactory for archive type FileSystem registered. ArchiveFactory for archive type Zip registered. ArchiveFactory for archive type EmbeddedZip registered. DDS codec registering FreeImage version: 3.15.3 This program uses FreeImage, a free, open source image library supporting all common bitmap formats. See http://freeimage.sourceforge.net for details Supported formats: bmp,ico,jpg,jif,jpeg,jpe,jng,koa,iff,lbm,mng,pbm,pbm,pcd,pcx,pgm,pgm,png,ppm,ppm,ras,tga,targa,tif,tiff,wap,wbmp,wbm,psd,cut,xbm,xpm,gif,hdr,g3,sgi,exr,j2k,j2c,jp2,pfm,pct,pict,pic,3fr,arw,bay,bmq,cap,cine,cr2,crw,cs1,dc2,dcr,drf,dsc,dng,erf,fff,ia,iiq,k25,kc2,kdc,mdc,mef,mos,mrw,nef,nrw,orf,pef,ptx,pxn,qtk,raf,raw,rdc,rw2,rwl,rwz,sr2,srf,srw,sti Registering ResourceManager for type HighLevelGpuProgram Registering ResourceManager for type Compositor MovableObjectFactory for type 'Entity' registered. MovableObjectFactory for type 'Light' registered. MovableObjectFactory for type 'BillboardSet' registered. MovableObjectFactory for type 'ManualObject' registered. MovableObjectFactory for type 'BillboardChain' registered. MovableObjectFactory for type 'RibbonTrail' registered. *-*-* OGRE Initialising *-*-* Version 1.9.0 (Ghadamon) *-*-* OGRE Shutdown Unregistering ResourceManager for type Compositor Unregistering ResourceManager for type Skeleton Unregistering ResourceManager for type Mesh Unregistering ResourceManager for type HighLevelGpuProgram Unregistering ResourceManager for type Material /opt/dev/ogreprojects/tutorial1/build/tutorial1.app/Contents/MacOS/tutorial1 exited with code 0

Before continuing, there are two things to do first:

  • Copy the resources.cfg file located in the path:
Copy to clipboard
/opt/dev/ogre3d-1.9.0/build/bin/resources.cfg

inside the Resources folder of your project.

  • Copy the Media folder located in the path:
Copy to clipboard
/opt/dev/ogre3d-1.9.0/build/sdk/Media

inside the Resources folder of your project.

 Note
The Media folder includes some files that you may not need right now (this was done for the sake of brevity). Among these files you can find the ogre head materials and mesh.

Now let's add more code to the Application class in order to render the Ogre head:

Copy to clipboard
__application.h__ #ifndef APPLICATION_H #define APPLICATION_H namespace Ogre { class Root; class RenderWindow; class SceneManager; class Camera; } class Application { public: Application(); ~Application(); void startApplication(); void loadPlugins(); void setRenderSystem(); void initializeRenderSystem(); void createRenderWindow(); void parseResourceFileConfiguration(); void initializeResources(); void createAndSetTheScene(); private: Ogre::Root *root; Ogre::RenderWindow* renderWindow; Ogre::SceneManager* sceneManager; Ogre::Camera* camera; }; #endif // APPLICATION_H

application.cpp

Copy to clipboard
#include "application.h" #include <OgreRoot.h> #include <OgreGLPlugin.h> #include <OgreParticleFXPlugin.h> #include <OgreConfigFile.h> #include <OgreCamera.h> #include <OgreViewport.h> #include <OgreSceneManager.h> #include <OgreRenderWindow.h> #include <OgreEntity.h> #include <macUtils.h> Application::Application() : root( 0 ) { } Application::~Application() { if( root ) { delete root; } } void Application::startApplication() { root = new Ogre::Root( Ogre::StringUtil::BLANK ); loadPlugins(); setRenderSystem(); initializeRenderSystem(); createRenderWindow(); parseResourceFileConfiguration(); initializeResources(); createAndSetTheScene(); root->startRendering(); } void Application::loadPlugins() { // Set the render system. In this case OpenGL Ogre::GLPlugin* gLPlugin = new Ogre::GLPlugin(); root->installPlugin( gLPlugin ); Ogre::ParticleFXPlugin *particleFXPlugin = new Ogre::ParticleFXPlugin(); root->installPlugin( particleFXPlugin ); } void Application::setRenderSystem() { const Ogre::RenderSystemList &renderSystemList = root->getAvailableRenderers(); if( renderSystemList.size() == 0 ) { throw Ogre::Exception(Ogre::Exception::ERR_RENDERINGAPI_ERROR, "Sorry, no rendersystem was found.", __FILE__); } Ogre::RenderSystem *lRenderSystem = renderSystemList[ 0 ]; Ogre::String renderSystemName = lRenderSystem->getName(); Ogre::LogManager::getSingleton().logMessage( "Render System found: " + renderSystemName, Ogre::LML_NORMAL ); root->setRenderSystem( lRenderSystem ); } void Application::initializeRenderSystem() { bool createAWindowAutomatically = false; Ogre::String windowTitle = ""; Ogre::String customCapacities = ""; root->initialise( createAWindowAutomatically, windowTitle, customCapacities ); } void Application::createRenderWindow() { Ogre::String windowTitle = "Tutorial 1"; unsigned int windowSizeX = 800; unsigned int WindowSizeY = 600; bool isFullscreen = false; Ogre::NameValuePairList additionalParameters; additionalParameters[ "FSAA" ] = "0"; additionalParameters["VSync"]="No"; additionalParameters["macAPICocoaUseNSView"] = "true"; additionalParameters["FSAAH"] = "Quality"; additionalParameters["contentScalingFactor"] = "1"; additionalParameters["displayFrequency"] = "0"; additionalParameters[ "macAPI" ] = "cocoa"; renderWindow = root->createRenderWindow( windowTitle, windowSizeX, WindowSizeY, isFullscreen, &additionalParameters ); } void Application::initializeResources() { // Set default mipmap level (note: some APIs ignore this) Ogre::TextureManager::getSingleton().setDefaultNumMipmaps( 5 ); Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); } void Application::parseResourceFileConfiguration() { // set up resources and load resource paths from config file Ogre::String resourcesConfigurationPath = Ogre::macBundlePath() + "/Contents/Resources/resources.cfg"; Ogre::ConfigFile configurationFile; configurationFile.load( resourcesConfigurationPath ); // Go through all sections & settings in the resource file Ogre::ConfigFile::SectionIterator sectionNameIterator = configurationFile.getSectionIterator(); Ogre::String sectionNameOfTheResource; Ogre::String typeNameOfTheResource; Ogre::String absolutePathToTheResource; while( sectionNameIterator.hasMoreElements() ) { sectionNameOfTheResource = sectionNameIterator.peekNextKey(); Ogre::ConfigFile::SettingsMultiMap *settings = sectionNameIterator.getNext(); Ogre::ConfigFile::SettingsMultiMap::iterator i; for( i = settings->begin(); i != settings->end(); ++i ) { typeNameOfTheResource = i->first; absolutePathToTheResource = i->second; Ogre::ResourceGroupManager::getSingleton().addResourceLocation( absolutePathToTheResource, typeNameOfTheResource, sectionNameOfTheResource ); } } const Ogre::ResourceGroupManager::LocationList genLocs = Ogre::ResourceGroupManager::getSingleton().getResourceLocationList("General"); absolutePathToTheResource = Ogre::macBundlePath() + "/Contents/Resources/Media"; typeNameOfTheResource = "FileSystem"; sectionNameOfTheResource = "Popular"; // Add locations for supported shader languages if(Ogre::GpuProgramManager::getSingleton().isSyntaxSupported("glsles")) { Ogre::ResourceGroupManager::getSingleton().addResourceLocation(absolutePathToTheResource + "/materials/programs/GLSLES", typeNameOfTheResource, sectionNameOfTheResource); } else if(Ogre::GpuProgramManager::getSingleton().isSyntaxSupported("glsl")) { if(Ogre::GpuProgramManager::getSingleton().isSyntaxSupported("glsl150")) { Ogre::ResourceGroupManager::getSingleton().addResourceLocation(absolutePathToTheResource + "/materials/programs/GLSL150", typeNameOfTheResource, sectionNameOfTheResource); } else { Ogre::ResourceGroupManager::getSingleton().addResourceLocation(absolutePathToTheResource + "/materials/programs/GLSL", typeNameOfTheResource, sectionNameOfTheResource); } if(Ogre::GpuProgramManager::getSingleton().isSyntaxSupported("glsl400")) { Ogre::ResourceGroupManager::getSingleton().addResourceLocation(absolutePathToTheResource + "/materials/programs/GLSL400", typeNameOfTheResource, sectionNameOfTheResource); } } else if(Ogre::GpuProgramManager::getSingleton().isSyntaxSupported("hlsl")) { Ogre::ResourceGroupManager::getSingleton().addResourceLocation(absolutePathToTheResource + "/materials/programs/HLSL", typeNameOfTheResource, sectionNameOfTheResource); } # ifdef OGRE_BUILD_PLUGIN_CG Ogre::ResourceGroupManager::getSingleton().addResourceLocation(absolutePathToTheResource + "/materials/programs/Cg", typeNameOfTheResource, sectionNameOfTheResource); # endif # ifdef INCLUDE_RTSHADER_SYSTEM if(Ogre::GpuProgramManager::getSingleton().isSyntaxSupported("glsles")) { Ogre::ResourceGroupManager::getSingleton().addResourceLocation(absolutePathToTheResource + "/RTShaderLib/GLSLES", typeNameOfTheResource, sectionNameOfTheResource); } else if(Ogre::GpuProgramManager::getSingleton().isSyntaxSupported("glsl")) { Ogre::ResourceGroupManager::getSingleton().addResourceLocation(absolutePathToTheResource + "/RTShaderLib/GLSL", typeNameOfTheResource, sectionNameOfTheResource); if(Ogre::GpuProgramManager::getSingleton().isSyntaxSupported("glsl150")) { Ogre::ResourceGroupManager::getSingleton().addResourceLocation(absolutePathToTheResource + "/RTShaderLib/GLSL150", typeNameOfTheResource, sectionNameOfTheResource); } } else if(Ogre::GpuProgramManager::getSingleton().isSyntaxSupported("hlsl")) { Ogre::ResourceGroupManager::getSingleton().addResourceLocation(absolutePathToTheResource + "/RTShaderLib/HLSL", typeNameOfTheResource, sectionNameOfTheResource); } # ifdef OGRE_BUILD_PLUGIN_CG Ogre::ResourceGroupManager::getSingleton().addResourceLocation(absolutePathToTheResource + "/RTShaderLib/Cg", typeNameOfTheResource, sectionNameOfTheResource); # endif # endif // include_rtshader_system } void Application::createAndSetTheScene() { sceneManager = root->createSceneManager(Ogre::ST_GENERIC); sceneManager->setAmbientLight( Ogre::ColourValue( 0.5, 0.5, 0.5 ) ); camera = sceneManager->createCamera("PlayerCam"); camera->setPosition(0, 0, 120); camera->setNearClipDistance( 5 ); Ogre::Viewport* viewPort = renderWindow->addViewport( camera ); viewPort->setBackgroundColour( Ogre::ColourValue( 0, 0, 0 ) ); camera->setAspectRatio( Ogre::Real( viewPort->getActualWidth() ) / Ogre::Real( viewPort->getActualHeight() ) ); // Create an Entity Ogre::Entity* ogreHead = sceneManager->createEntity("Head", "ogrehead.mesh"); // Create a SceneNode and attach the Entity to it Ogre::SceneNode* headNode = sceneManager->getRootSceneNode()->createChildSceneNode("HeadNode"); headNode->attachObject(ogreHead); // Create a Light and set its position Ogre::Light* light = sceneManager->createLight("MainLight"); light->setPosition(20.0f, 80.0f, 50.0f); }

The explanation about the previous code won't be covered here because you can find it (with more detail) in the Tutorials section. Take a look at these tutorials: Basic Tutorial 1 and Basic Mesh Loading. Now rebuild the project and run it. You should see something like this:
Ogre window showing the ogre head

 Note
When you change something in the Resources folder or anything that will end up in the project's app bundle. First delete the app bundle of your build folder and then rebuild your project. This forces QtCreator to recreate the new app bundle with the updated Resources folder information.

Final Thoughts

Now that you're able to create a project in QtCreator using CMake and Ogre3D, you are now ready to continue with the tutorials (Ogre3D's Tutorials). Additionally you should take care of the following:

  • Most of the tutorials in the Wiki use the Ogre3D's SDK (which is compiled dynamically) or the Ogre3D project compiled dynamically (not statically like this tutorial). So, anytime a tutorial mentions that you should use a special plugin (remember that plugins are loaded in a different way when Ogre3D has been compiled statically) load it just like it appears in the Application::loadPlugins() method in the code above.
  • If you're having trouble including any framework (via the CMakeLists.txt configuration file) to your project, try to include it in the following way:
Copy to clipboard
FIND_LIBRARY(COCOA_FRAMEWORK_PATH NAMES Cocoa PATHS /System/Library PATH_SUFFIXES Frameworks NO_DEFAULT_PATH) IF(NOT COCOA_FRAMEWORK_PATH) MESSAGE(FATAL_ERROR "Cocoa framework not found") ELSE() MESSAGE(STATUS "Cocoa framework found. Path: " ${COCOA_FRAMEWORK_PATH}) ENDIF()
  • The render loop in Mac OS X cannot be done in the traditional way (the way it's explained in the tutorials - using a while render loop). The reason is this (I'm quoting form one of the forum posts):
"Cocoa dispatches events differently than Carbon or Win32. You don't pump messages, instead you wait for them to be delivered to you. If you are running a never ending loop for rendering and events handling, your app never has a chance for those events to be delivered to it. Which is why it is hanging. Check out SampleBrowser_OSX.h to see an example of using renderOneFrame with Cocoa."

You can find more information in the following posts:

Okay, that's it. If you have any doubt please consult the forum (Ogre3D Forum).