Our first GameObject

Placing a cube into our 3D world

Placing a cube in the world follows similar principles to the Camera object. This is because, simply put, we create a new game object and instantiate. However, it does come with one major difference. This is because we now want to use a shader (Which is a piece of code that is compiled on the GPU and tells it how to manage pixels and colour them).

Now, luckily the game engine is shipped with some standard shaders that should allow you to perfom basic rendering. We also come with a built in cube mesh that allow us to easily render a cube to the screen without the complicated coordinates and indices.

But in order to access this stuff we need a couple more includes.

#include <Unified-Engine/Objects/gameObject.h>
#include <Unified-Engine/Objects/Mesh/Defaults/cube.h>
#include <Unified-Engine/Core/Rendering/shader.h>
#include <Unified-Engine/Objects/Components/shaderObject.h>

So lets start of by defining the core components to our game object, this is the shader and a mesh (which defines the vertices of the shape):

UnifiedEngine::Shader shader();
UnifiedEngine::ShaderObject shaderObj(&shader);

UnifiedEngine::Mesh mesh;
mesh = UnifiedEngine::Cube();

Now that we have these setup we want to create and initialise the game object, we will do this at an offset as we will want to be able to see it in the camera.

UUnifiedEngine::GameObject gOBJ(mesh, &shaderObj);
gOBJ.transform.Position.z -= 5;
UnifiedEngine::instantiate(&gOBJ);

Now with this code running, you should find a white square appear in-front of you. There it is that is your cube.

As an extra task see if you can make it rotate using the .transform.rotate.x variable. Be carefull with large values, it will spin fast. Do you know why?

Last updated