Skip to content
Alexander Scheurer edited this page Feb 14, 2014 · 4 revisions

Structure - GameEntity

The first logical step is to build our GameEntity class, even though it was only second on the list of the previous lesson. It renders, so we need it for testing, the GameWorld is just a container for keeping a bunch of GameEntities and all the other stuff together.

Variables

private readonly Mesh _mesh;
private readonly RenderContext _rc;

protected float4x4 Position;
protected float4x4 CorrectionMatrix = float4x4.Identity;

private readonly ShaderProgram _shaderProgram;
private readonly IShaderParam _shaderParam;
private float4 _color = new float4(0.5f, 0.5f, 0.5f, 1);

At first we introduce variabales for our mesh and RenderContext (we want the GameEntity to render itself so it needs to know its RenderContext). Next we have position and "CorrectionMatrix". Position in this context means position as well as orientation of the object. The CorrectionMatrix is intended for scaling and to fix issues with the pivot point. For example you want to rotate a cube around one of its edges and not around its center, you apply a translation matrix here that moves it by the needed amount and we will apply this matrix befor any other opperation (we in this case means, you have to code that further down the line). At last we have shaders and a color variable.

Constructors

public GameEntity(String meshPath, RenderContext rc, float posX = 0, float posY = 0, float posZ = 0, float angX = 0, float angY = 0, float angZ = 0)
{
   _mesh = MeshReader.LoadMesh(meshPath);
   _rc = rc;

   Position = float4x4.CreateRotationX(angX) *
              float4x4.CreateRotationY(angY) *
              float4x4.CreateRotationZ(angZ) *
              float4x4.CreateTranslation(posX, posY, posZ);

   _shaderProgram = MoreShaders.GetDiffuseColorShader(rc);
   _shaderParam = _shaderProgram.GetShaderParam("color");
}

We basically put everything from lesson 1 and 2 in the constructor of our GameEntity. Load mesh, get our RenderContext, build the "Position" for our model-view matrix and set up the shaders.

Methods

There is only one method yet that does anything (beside the getter/setter-methods):

public void Render(float4x4 camMatrix)
{
   _rc.ModelView = CorrectionMatrix * Position * camMatrix;
   _rc.SetShader(_shaderProgram);
   _rc.SetShaderParam(_shaderParam, _color);
   _rc.Render(_mesh);
}

Nothing new here, the methode gets called with a camera matrix, works its matrices and shaders, and finally renders the mesh.

To finish our GameEntity (for now) we need getter/setter for all the non-read-only variables, also I have some convenience functions like public float3 GetPositionVector() {} in there, but if you've gotten this far there is nothing that needs more explanation.

Main.cs

We can now do with just the GameEntity, what we did in Lesson 1 with Mesh, ShaderProgram and IShaderParam, since GameEntity holds all the information.

private GameEntity _gameEntity;

public override void Init()
{
  [..]
  _gameEntity = new GameEntity("Assets/cube.obj.model", RC);
}

public override void RenderAFrame()
{
   [..]
   var mtxCam = float4x4.LookAt(0, 200, 500, 0, 0, 0, 0, 1, 0);
   _gameEntity.Render(mtxCam);
   [..]
}

[> Lesson 05 - Structure: GameWorld](Lesson 05)

Clone this wiki locally