As a small Christmas present, I want to show you how easy it has become to make Augmented Reality yourself thanks to Ogre and OpenCV. You should know that my other interest, besides graphics, lies with Computer Vision. The demo will not rely on proprietary solutions like ARCore or ARKit – all will be done with open-source code that you can inspect an learn from. But lets start with a teaser:
This demo can be put together in less than 50 lines of code, thanks to the OpenCV ovis module that glues Ogre and OpenCV together. Next, I will briefly walk you through the steps that are needed:
First, we have to capture some images to get by the Reality part in AR. Here, OpenCV provides us an unified API that you can use for your Webcam, Industrial Cameras or a pre-recorded video:
import cv2 as cv
imsize = (1280, 720) # the resolution to use
cap = cv.VideoCapture(0)
img = cap.read() # grab an image
then, we have to set up the most crucial part in AR: camera tracking. For this, we will use the ArUco markers – the QR-like quads that surround Sinbad. To no surprise, OpenCV comes with this vision algorithm:
adict = cv.aruco.Dictionary_get(cv.aruco.DICT_4X4_50)
# extract 2D marker-corners from image
corners, ids = cv.aruco.detectMarkers(img, adict)[:2]
# convert corners to 3D transformations [R|t]
rvecs, tvecs = cv.aruco.estimatePoseSingleMarkers(corners, 5, K, None)[:2]
If you look closely, you see that we are using a undefined variable "K" – this is the intrinsic matrix specific for your camera. If you want precise results, you should calibrate your camera to measure those. For instance using the web-service at calibdb.net, which will also just give you the parameters, if your camera is already known.
However, if you just want to continue, you can use the following values that should roughly match any webcam at 1280x720px
import numpy as np
K = np.array(((1000, 0, 640), (0, 1000, 360), (0, 0, 1.)))
So now we have the image and the according 3D transformation for the camera – only the Augmented part is missing. This is where Ogre/ ovis come into play:
# reference the 3D mesh resources
# create an Ogre window for rendering
win = cv.ovis.createWindow("OgreWindow", imsize, flags=cv.ovis.WINDOW_SCENE_AA)
# make Ogre renderings match your camera images
# create the virtual scene, consisting of Sinbad and a light
win.createEntity("figure", "Sinbad.mesh", tvec=(0, 0, 5), rot=(1.57, 0, 0))
win.createLightEntity("sun", tvec=(0, 0, 100))
# position the camera according to the first marker detected
win.setCameraPose(tvecs.ravel(), rvecs.ravel(), invert=True)
To record the results, you can use win.getScreenshot() and dump it into a cv.VideoWriter – contrary to the name, this works in real-time.
Extending the above code to use cv.aruco.GridBoard as done in the teaser video is left as an exercise for the reader as this is more on the OpenCV side.
Also, If you rather want to use ARCore on Android, there is a Sample how to use the SurfaceTexture with Ogre. Using this, you should be able to modify the hello_ar_java sample from the arcore-sdk to use Ogre.
Due to some work on WGL, you can now build and use the GLES2 RenderSystem on windows without any glue libraries like ANGLE. Consequently it is included in the SDK, so it is easy to try. If you are on NVIDA/ Intel that is – AMD does not support the respective extension. There are no screenshots here, as they would look almost exactly like on GL3+.
Shaders bound to RenderToVertexBuffer (aka. Transform Feedback/ Stream Output) can now use auto parameters (param_named_auto) as in any other stage.
Furthermore the D3D11 implementation was updated and the GPU particles samples now also runs on D3D11 too.
The ParticleSystem and the default BillboardParticleRenderer received some optimization regarding Colour and bounds related computations.
While working on the Particles I got the idea that one could re-purpose the existing BillboardSet for 3D text rendering, and indeed it fits perfectly as you see above. No more snippets from the Wiki needed and everything integrates with the existing API as:
auto font = FontManager::getSingleton().getByName("SdkTrays/Caption");
auto bbs = mSceneMgr->createBillboardSet();
font->putText(bbs, "OGRE\n ...", 10);
Furthermore, I fixed the glyph packing code which now correctly aligns the font-atlas elements, thus obsoleting the character_spacer tuning option.
We asked the team behind the game if they could share some insights into the Ogre3D usage and how the game was built in general, and Robin was kind enough to provide those:
Spellheart is a MOBA (Multiplayer online battle arena) game. You can build your entirely own class by choosing items and abilities, and with an extremely customizable server that anyone can host, the possibilities are endless.
The game is based upon an idea that I have never seen before. Most RPG games are static, limiting your build options and forcing you to min/max. In this game, there is no best build because there are no classes. You create your own build without limitations or restrictions. With a customizable server, gameplay can be balanced in real time.
I have been working on Spellheart myself from the start, though some friends have helped me with a few assets. As I am only a programmer, I can only do that part of the game. The 3D models and sounds in the game are mostly from people that put them out for free with good copyrights.
I am using Ogre version 1.11.2, but I have modified the source for some minor things, such as making particles be able to use an atlas. I have also made a lot of custom particle affectors. All shaders in the game are written by hand, there is no on-the-fly automatic generation of shaders. Though I have written a program that helps me with this, so that I only have to alter one base shader to then generate all shaders at once for D3D9/ CG/ D3D11.
I do not use Deferred shading or anything like that, just normal Forward shading. Since my shaders can handle up to 20 lights and the game being top-down, there is no need for me to have support for more lights. Forward shading is therefore perfect for this game, while being faster than the newer techniques. I use a lot of batching through a custom-written ManualObject class to make a lot of smaller objects into one single batch in a very optimized manner. This happens for things in the game such as grass and footsteps on sand. I also use the built-in Instancing (HW Basic) on the static objects in my game. Since the game is top-down, this usually does not help that much, but “Many a little makes a mickle”. I live by that expression, optimizing everything I see can be a bottleneck. That is why the game can run in a very high FPS (500 FPS with a few changes in the options menu on my computer).
An edited version of Gorilla is used for the GUI of the game, but I also made a custom atlas generator for it to make it much easier to use. This also enables me to use a normalmap for each GUI element which is shown by a light from the cursor.
At the moment, the game only works for Windows (x64), but as Ogre can be compiled for all platforms, Linux and Mac could be added in the future (and also then using GLSL instead of CG).
The option menu in the game has extremely many options to make the game run on any kind of hardware (even Fixed Function Pipeline is possible through this).
I like Ogre because its community is helpful and that you can easily alter the source code if you want. Many game engines out there are based around being a tool-user instead of being a programmer, and I don’t want that. Those engines also seem to have performance issues in many of their games, unless you have a very high-end computer. Therefore, Ogre is the ultimate engine for me. It allows me to be a programmer and not just a tool-user, while being able to make games for low-end computers all they way up to high-end.
This work was possible because user Hotshot5000 took my branch, forked it, and advanced it further. The Vulkan port was a daunting, overwhelming task and his contributions greatly helped me figure out the way to make it work.
It also saved me a lot of time. Even though around 40% of his code couldn’t make it into the final version, it was still very important as a proof of concept or as a reference implementation to base from, or as a way to compare new non-working code against a working reference.
Existing applications may need to perform additional work to get Vulkan running (e.g. port shaders to Vulkan). While this isn’t difficult, there is no guide written yet.
The 2.3 preparations ticket has a list of things that have changed that may require a dev’s attention when porting from 2.2 to 2.3
This list is updated at irregular intervals; and once 2.3 is out this page is probably going to be moved somewhere else (in fact it is a draft for the News post whenever we release 2.3). But for the time being that ticket is our hub for checking 2.2 -> 2.3 changes.
Ogre 1.12.9 was just released. Typically we do not write a specific announcement for minor updates, however this one contains some major new features that warrant this one.
Multi Language GPU Programs
As noted in the last progress report, even when using OgreUnifiedShader.h one had to duplicate the GpuProgram definitions in the Ogre .material files. This has been fixed in master by allowing multi-language programs to be defined like
The separate ogre-assimp project was merged into the main repository as the Codec_Assimp Plugin, which allows loading arbitrary meshes and runtime and the OgreAssimpConverter Tool for converting meshes to the Ogre .mesh format offline (which improves loading time).
Especially the introduction of Codec_Assimp is notable, as mesh loading now goes through the same Codec dispatching as image formats.
On the application side, you can then just call sceneManager->createEntity("Bike.obj") like I did in OgreMeshViewer below:
so simply loading Codec_Assimp turned that into a general-purpose mesh-viewer.
Descriptive Hardware Buffer Usage
Did you ever wonder whether your buffer usage is rather static or rather dynamic? Well at least I did as these rather abstract names do not tell you much what it means in terms of memory allocation – but wonder no more! There are now new, actually descriptive, aliases:
New descriptive alias
So while previously you were told to use HBU_STATIC_WRITE_ONLY, you now immediately see that the buffer will end up in GPU memory. Likewise, if you use the DYNAMIC variant, the buffer will be optimized for recurrent CPU writes.
I came up with those when planning the Vulkan RenderSystem backport and those are actually borrowed from the Vulkan Memory Allocator library. There you can also find all the subtleties regarding Vulkan, that I did not bother copying to the Ogre manual.
While these flags were named with Vulkan in mind, they map surprisingly well to the Ogre ones and, most importantly, to the actual Ogre usage. After refining the meaning, it allowed dropping several superficial copies and readbacks in the D3D11 & D3D9 RenderSystems. Yes, these even map well to D3D9.
Super-fast debug drawing
Debug drawing in Ogre has been refactored and is now abstracted by the DebugDrawer API with a DefaultDebugDrawer implementation.
One advantage of this is that debug drawing code will not be sprayed across core classes. At least with 1.13 – for now we keep things as is not to break any obscure use-cases.
The main advantage however is, that debug drawing is now properly batched – whereas there was one draw-call per WireBox previously, there is now one draw-call for all wireboxes in the scene. This is also true for coordinate crosses and camera frustums.
Ogre can now be built using the latest Emscripten SDK, thaks to a contribution by Gustavo Branco
The Config dialog on Windows & Linux was updated to use the new Ogre logo