Showcasing Beta v0.0.6 of the game engine

Hi there,

These past two weeks I've been working on implementing a Frustum Culling algorithm for the engine. Here is a video of beta v0.0.6 of the engine with Frustum Culling.

 
 

In the previous beta version of the game engine, I had 20 soccer players on the screen. And I noticed that the game was a bit choppy. I recall the Frames-Per-Second (fps) being below 30. The engine was rendering all 20 players, the two goals, and soccer field at every frame without analyzing if the camera was able to see these models or not. You can agree that this is a problem.

The logic behind a Frustum Culling algorithm is essentially this:

"If the camera does not see the 3D model, then the engine should not render it".

Implementing this logic, allows you to have 100 models in a game, but only 10 or so many models being rendered at any time. Thus, improving the game experience.

For example, the minimum acceptable fps in a game is 30 fps. With Frustum Culling, the engine was able to render the entities at 60 fps and the lowest it got was 49 fps.

 
Screen Shot 2017-10-06 at 11.40.37 PM.png
 
 
Screen Shot 2017-10-06 at 11.41.09 PM.png
 

If you are entirely new to Frustum Culling, keep reading. I'm going to give you a brief tutorial on how Frustum Culling works.

What is a Frustum

A game engine requires a camera. During initialization of a game, the engine computes a Frustum. A frustum is a chopped pyramid. And 3D models within the frustum are visible by the camera and by YOU.

Frustum.jpg

Notice how the frustum is composed of six planes.

What is Frustum Culling

A Frustum Culling Algorithm tests which 3D models lie within the frustum. If the 3D model lies outside the frustum, then it is ignored by the rendering engine. If the 3D model lies within the frustum, then it is rendered.

Frustum-1.jpg

Frustum Culling Test

Testing if a 3D model lies within a frustum is quite simple. First, the 3D model is wrapped within a box, in this case, with an AABB (Axis-Aligned Boundary Box).

Then, the eight vertices of the AABB are tested against a plane. In a Frustum Culling algorithm, the AABB's vertices are tested against all six frustum's planes.

If all vertices lie on the negative side of the plane, then the model is considered being outside the frustum. Otherwise, it lies within the frustum.

And that is it. You repeat the process for all game entities in your game.

Improving the Frustum Culling Algorithm

Frustum Culling improves the efficiency of a game engine, but it is not enough. If your game only has ten 3D models, the engine can quickly do a Frustum Culling on all these entities. However, if your game has 200 or 1000 objects, then Frustum Culling by itself may not improve the efficiency of the engine.

What you need is an algorithm that can analyze the spatial area seen by the camera. For example, if the camera does not see the upper-left quadrant, then the engine should not perform Frustum Culling on the entities that lie on this quadrant. There are many algorithms that you can use. In the engine, I implemented a Boundary Volume Hierarchy (BVH) tree.

The image below shows the idea. I recursively wrapped the game entities in an AABB, until there is only one model per AABB. Then, starting from the root node, the engine test if the camera sees the AABB node or not. If the camera does not see the AABB node, then all the children node, containing the 3D models, are not rendered.

If the camera does see the AABB node, then it keeps performing Frustum Culling on the next AABB child node, until it reaches the leaf node.

Frustum with BVH.jpg

In the example shown above, the algorithm rejects the red AABB box and does not need to test if the red and gray car is seen by the camera.

The camera does see the blue AABB box. Thus it tests the children nodes of the blue AABB box. It does this on and on until it reaches the leaf nodes. In this case, it sees the blue and orange car, but not the yellow car.

And that is Frustum Culling. I hope you learned something.

Thanks for reading.

Showcasing Beta v0.0.5 of the game engine

In version v0.0.5, I ported the game engine from OpenGL to Apple's Metal API.

Initially, I planned to keep working on the 3D soccer game using v0.0.4 of the engine. However, I decided to port the engine once I saw an Augmented Reality demo. I realized that Augmented Reality (AR) is the future of gaming and it may be a good idea to have this feature available in the engine. However, the AR framework only works with the Metal API; it does not support OpenGL.

To be honest, my goal was to port the engine to Metal in a year or two. I was not planning to port it so soon. Part of it was that I feel I'm still learning Game Engine development and didn't want to overwhelm myself. The other reason was that porting a Game Engine from one API to another is not an easy task. When I saw the AR demo, it was the push that I needed.

To my surprise, porting the engine to Metal was quite simple. In less than two weeks I had ported the major components of the rendering engine. I also became aware of some misconceptions I had with computer graphics and OpenGL; essentially with Normal Maps and Shadows. Metal is so simple to use that it highlighted some errors I was making with OpenGL.

Overall, I would say that the porting took about a month to complete. It does not include the time it took to learn Metal, which it should take you about a week; assuming you know computer graphics.

Here is a video showcasing the 3D soccer game with Metal. In my opinion, it looks better than when I was using OpenGL, but then again I am using better 3D models.

 
This video shows the game engine using the Metal API for its rendering operations. The game engine no longer uses OpenGL.
 

So, the engine now runs entirely on Metal.

Goodbye OpenGL

Hola, Metal

Thanks for reading.

The state of the Game Engine (Sept 2017)

I want to give you a brief update on the current state of the engine. With my day job and other responsibilities, I had to scale down the amount of time I spent writing on this blog. However, not for a second, have I stopped working on the engine. If you haven't seen any updates, is because I decided to do something unorthodox.

Back in July 2017, I decided to take a detour. As you may recall, I was working on developing a 3D soccer game using my game engine. At that time, the engine was being powered by the OpenGL API. I have nothing but great things to say about OpenGL. I think it is an amazing API and if you are interested in learning computer graphics, I strongly suggest to start by using it. However, the time had come for me to port the engine to Metal.

Metal is the new Graphics API from Apple. In my opinion, Metal is a lot easier to work than OpenGL. It is less convoluted, and it leads to clean code. And more importantly, it is a lot easier to grasp. If you already know OpenGL, learning Metal is going to be a walk in the park.

So, why did I decided to port the engine to Metal? Part of the answer came down to future OpenGL support. I learned that Apple is very committed to Metal and there are no plans to keep updating OpenGL on mobile devices. I also found out about Apple Augmented Reality framework. Looking at some demos, it is apparent that Augmented Reality will play a huge part in the future of mobile gaming. And it so happens, that the Augmented Reality framework from apple only works with Metal. Of course, I plan to implement AR features in the engine.

I have to admit, porting the game engine from one graphics API to another is not something to look forward. However, it was easier than I thought. And I believe that Metal's clean paradigm had a lot to do with the ease of this task.

As of today, Sept 16, 2017, the engine has been completely ported to Metal.

So why haven't I given an update on the engine?

It has to do with the fact, that through the porting, I learned of many mistakes I had made using OpenGL. And I'm finding of these errors through the mobile soccer game. So, that is what I'm doing now. I'm going back and forth, tweaking issues in the engine and the game. Making sure everything is correct and working properly. Unfortunately, this is taking longer than I planned, but I think is important for the engine to be reliable before I release it to the public.

I plan to release version 1.0 of the soccer game by the end of 2017. Once released, I plan to complete the API documentation for the engine, User's Guide and tutorial by May 2018. The release of the engine will soon follow after this. I haven't decided to release the engine as open source or closed source yet. I'm still evaluating this decision.

Well, that is what is going on in my world. Thanks for reading.

The purpose of a scenegraph in a game engine

Scenegraphs are generic trees which provide an efficient way to traverse game entities for rendering.

As the image below shows, a scenegraph node can have as many children as it desires. This property is in complete contrast with a binary tree; which allows at most two children per node.

On each game tick, the scenegraph traverses its lists and sends each game object to an Engine Loop. Where it gets rendered, and its coordinate space gets updated.

Aside from its fast traversal property, a scenegraph provides a clean method to transform a child node space with respect to its parent node space.

For example, in the image below, both the soccer player and the soccer ball are children of the World Entity.

Thus, if the world entity (grid) rotates, both children nodes will be affected by their parent space-transformation, as shown below.

However, a child node does not affect the space of a sibling node, nor its parent. Thus, rotating the player or the ball about their y-axis does not affect either or, nor the World entity, as shown below:

Now, if the ball was a child of the soccer player, as shown below:

then rotating the player causes the ball to rotate as well.

A scenegraph provides a clean method to establish a parent-child relationship and fast traversal of its entities. Such feature is useful when the number of game objects grows, and efficiency is of utmost importance.

Hope this helps.

A sneak peek at the Game Engine's API

I know that I tend to talk a lot about my game engine. I also noticed that haven't given you a sneak peek at the Game Engine's API.

When I started development of the engine, one of my primary goals was to create an engine with an user-friendly API. That is the API needed to be compact, small and easy to remember.

In short, I wanted an engine that helps me focus on developing a game rather than wasting time reading through tons of API documents.

So, let me give you a sneak peek.

Initializing Objects (3D models)

The engine works hand in hand with Blender 3D. The engine includes a Digital Asset Exporter, written in Python, which exports your 3D models done in Blender, into data the engine understands.

The initialization is quite simple. For example, let's say that you have modeled this 3D model in Blender and you want to use it in the engine.

After you run the python script, a file is generated. This file contains all the attribute data of your model, such as vertices, normals, UV maps, etc. This file is copied into your project.

In your initialization routine of the engine, you ask the engine to render this model by just calling the init method:

myModel->init("guardian","guardianfile.u4d");

The method requires the name of the 3D model and the name of the file you imported.

If the engine confirms that all the 3D model data is valid, it loads all the rendering information into the engine.

The only thing left to do is to add the model into the scenegraph. You do so by calling the addChild method:

addChild(myModel);

And that is it. The engine will render your model on your mobile device.

Enabling Lights and Shadows

WIthout light, you can't see. And without shadows, you can't make out visual details. Currently, the engine allows only one Light object per scene. That is, the light object is implemented as a singleton. You need to initialize a Light object, else, the whole scene will appear dark. Luckily, to initialize a light object is as simple as doing this:

//Create a light object
U4DLights *light=U4DLights::sharedInstance();

//Translate the light
light->translateTo(0.0,10.2,1.0); 

The engine gives you the flexibility to choose which model to cast shadows and which one not to. Again, doing so is very simple:

myModel->setEnableShadow(true);

The engine will cast a shadow on the selected objects:

Enabling Physics and Collisions

If you want a model to be affected by external forces, such as gravity, or any other applied forces, the engine gives you the flexibility to do so with one line of code. To enable such feature, simply call the enableKineticsBehavior method on the model.

myModel->enableKineticsBehavior();

And if you want to enable collisions between objects, you simply call the enableCollisionBehavior method:

myModel->enableCollisionBehavior();

The engine will enable gravity and collision properties on the selected objects:

Initializing Animations

Playing an animation is as simple as the initialization method explained above. 100% of the time, you would have rigged and animated your model in Blender. The Digital Asset Exporter also imports armature and animation data into the engine. All that you need to do is provide the name of the animation and the name of the file containing this information. To initialize an animation, you create an animation object and load animation data, as shown below:

walkingAnimation=new U4DAnimation(this);

if (loadAnimationToModel(walkingAnimation, "walking", "walkinganimation.u4d")) {
}

To play the animation, simply call the play method:

walkingAnimation->play();

If the animation has been loaded properly, the engine will play the animation:

As you can see, you can load a model onto your mobile device using a few lines of code. And the engine allows you to enable shadows, collision, and animation with a single line of code.

Thanks for reading.