sunnuntai 2. kesäkuuta 2013

Utilizing the UML in the game project

In my project I have studied a bit how to desing software with UML and I have to say that it totally pays. Creating a diagram about relationships of the main classes in your application and visual image of general architecture of the software can help you to understand your software better. In this blog writing I'll tell you how I utilize UML in my current game project and what benefits I see utilizing the UML in software design.

Some forewords

Because programmers are usually quite literal minded, working with UML might first feel very slow and frustrating process. At first I had thought like how am I going to know all the classes and associations even if I haven't made a single line of code? How I can desing anything, if I don't have any project plan? Do I have to create requirements analysis to create a project plan and then to create an UML design? I bet that you also can see how deep it goes when you start to think UML diagram as literal as your code would be. At worst you might fall to some kind of waterfall project, which would take eternity before you could create just a single line of code. Nobody wants that at nowdays. What I'm trying to say, is that you can utilize UML in your project as part of coding work. Most important is to have a visual image of your software architecture and the main class associations.

Try to keep it simple

I found out a quite good general rule about UML design from another web site. Don't model things in the UML what you want your coder to do. Even if you're doing your software by yourself, it's a very good rule. I see the rule in a sense, that don't do unneseccary accurate diagram and leave out all the stuff what programmer can create during the coding.

Creating a very accurate UML diagram takes a lot of time and a very accurate project plan and requirement analysis. In one man project, like I have, that would take way too much time to do. And in worst case if something is wrong in the plan, you have to do again whole design progress. Not good. That is perhaps why most of people just turn away from the UML.

Visualizing helps to avoid pitfalls

Visualizing your code with UML can help you to avoid some pitfalls what at first you might not detect. Let's say that I have a class Gameobject. I want that my gameobject is a renderable mesh, it's collidable and it has physics on on it. Pretty soon I have monolithic class hierarchy which limits design choices. I usually want to keep my class hierarchy flat and nice, so that the class hierarchy is nice and simple. With UML you might notice if the class hierarchy gets too deep and you might want to change that. These kind of design problems are good to notice before coding, hence it saves time. Nothing frustrates more than to code something again, but slightly different way.

Here I have a picture from previous example. It's OK to inherit Piece and Board types from GameObject class, because the hierarchy stays quite flat. But I have made an aggregation to GameObject class with Transform and MeshInstance classes, because I can keep code more adjustable and to prevent monolithic class hierarchy. You can easily decide things like these in advance before going to coding and save some time and archictectural troubles.

I don't also usually bother to create perfect classes in UML, like filling perfect information of members and functions. Those things I usually decide while coding. Some main functions and members I add to UML diagram which helps me to visualize the class itself. These are usually related to direct x stuff, like objects needed to hold rendering data and COM objects to interact with devices. Filling that kind of information just makes the class more instructible.


It's good to atleast learn some basics about UML diagrams, because quite many programming books uses the UML to describe class relationships. That is because in UML you can usually describe quite a complicated relationship whitout writing a novel. I also think that when you understand UML, you can also explain your code to somebody else in more standardized way and understand better when somebody else talks about programming.

sunnuntai 31. maaliskuuta 2013

Back To The Drawing Desk

It has been a little quiet time in the project. I had to go back to the drawing desk to research more about game programming. I found out, that my method to build game engine piece by piece was a sligthly poor approach.

I had to get more knowledge about the game engine programming, so I went and bought a book called "Game Engine Architectures" 1st edition by Jason Gregory. It took few weeks to read the book, hence it has about 800 pages, but I think it was totally worth of. I found about, that modern game engine is veerryyyy complex piece of engineering work. Especially if you really want to take care of protability, performance and scalability issues.

It is not just one executable which runs game content, but the game engine consists many support applications, what you need to do in order to create a good game engine system. Game engine has to have game content creation applications, game content database and the engine which drives built game content. The complexity is so vast, that it is really hard to explain in just one blog page. I can only suggest, to everyone interested in the industry, to get one good book about game engine architectures to get an idea about the topic.

I also took a level of abstraction and started to design the engine architecture by UML modeling. I think it really helps to clarify the idea about document flow in the system and what kind of components you need for your system.

Speaking of game content creation

I also started practicing 3d modeling with 3ds max by doing tutorial in the book "Game Character Modeling and Animation with 3ds Max" by Yancey Clinton. At some point I have to create content for my game, so it is better to learn necessary skills at the beginning.

Here are some pictures about the progress:

1. Half of the model body

2. Same with surface added and shading on. Background planes are also
visible in this picture.
3. Whole body. Same than previous, but
mirror attribute added.

4. Creating the face.
4. More accurate face.
5. Face with surface modifier added.

So I think I don't continue development at code level for awhile. Not until I get the tutorial at least done. When I have completed the model and made some animations to it, I think I try to run model and animations with DirectX. But progress to that point should take about one month or so.

perjantai 8. helmikuuta 2013

Controlling Meshes with UI

One object done. Now I can control my meshes via UI. I made an indicator, which consists of "hard coded" primitive geometric types. I build for each mesh it's own unique indicator, whenever a mesh is created. When the mesh is selected I can control it's position and orientation.

Here I have three meshes which I have transformed to some nice position and orientation. One chair is selected, which transformation I can control with UI. Nice!

I also improved quite a lot of my camera movement in the world. Now I have implemented regular WASD controls, what every first person shooter has. I also learned quite a lot of linear algebra during this feature.

I really recommend good mathematics book if you're into the industry. I use finnish 3D-graphics book by Antti Puhakka, which is really good. It covers most of must know topics about linear algebra if you're new to 3D calculation. It has also nice solutions for most general 2D and 3D geometric problems. I.e. line/plane intersection. It also introduces to spatial data structures.

I am waiting to get Game Engine Architectures book. I've heard that it has got some good reviews, so I decided to get one. I really need some information about game engine architectures, because if I just create "some" engine by implementing feature by feature, I might get some really awful engine which is slow as hell and hard to maintain or manage.

sunnuntai 3. helmikuuta 2013

For DirectX beginners - Accessing The Vertex Buffer in DirectX 9

Okay, I thought to write post how to access to contents of vertex buffer in Directx 9. This is for DX 9 beginners who doesn't know how to manage and acces the content of vertex buffer. I don't go any vertex buffer details here, because you can find them from quite many other blogs and sites in the internet. In quite many tutorials and samples, they are represting only how to create the content of the vertex buffer. In this example, I show that you can also use the contents of vertex buffer for your own purposes.

Vertex buffer

So what is vertex buffer? In short, vertex buffer holds the data, what you use when you're rendering any kind of 3d object with direct x. You can find more deeply information from Microsoft's library: MSDN - Vertex Buffer

How to access it's content

The way to acces created vertex buffer contents is done same way as you insert the content to the vertex buffer.

This is quite common way to insert the data to the vertex buffer. Seen in most tutorials and directx books:

vertexBuffer->Lock( 0, 0,(void**)&pData, 0);
//Copy vertex data to Vertex Buffer
memcpy(pData, &vertices, bytes );

Where "vertexBuffer" is IDirect3DVertexBuffer9 variable, "pData" is void pointer and "vertices" is array of structs. Let's say that the struct holds float x,y,z variable and dword color data. Name of the struct is "CUSTOM_VERTEX". With this way memcpy funtion insertes the data to the vertex buffer.

With quite same mannor you can access to the data of allready intialized vertex buffer.

vertexBuffer->Lock( 0, 0, (void**)&pData, D3DLOCK_READONLY );
p = static_cast< CUSTOM_VERTEX* >(pData);

Here we again take the address of memory buffer (vertex buffer data) to pData void pointer. Because void pointer can point to any kind of data type, we can cast it to the data type what refers to the same data type we used when we created the contents of vertex buffer. Now you can use "p" variable as an ordinary array of elements.

Two zeros in the beginning of "Lock" function tells that we are using whole buffer. You can also use only part of the buffer by setting the offset and size of the vertex data. Those integer values represents the size in bytes.

In example:

vertexBuffer->Lock( sizeof( CUSTOM_VERTEX, sizeof( CUSTOM_VERTEX), (void**)&pData, D3DLOCK_READONLY );

This would lock the second vertice in the vertex buffer. Remember that "sizeof" returns amount of bytes in integer.

Remember to ensure, that when iterating over the pointer, you don't go over the reserved memory area. You can easily determine amount of the data types in the vertex buffer by using Directx 9 D3DVERTEXBUFFER_DESC stuct. Vertex buffer has function "GetDesc" which takes the struct address as  parameter. From the struct you can get size of the vertex buffer in bytes. When you know size, you can calculate amount of data types in the buffer.

In example:

vertexBuffer->GetDesc( &desc );
int vertices = desc.Size / sizeof( CUSTOM_VERTEX );

And to iterate over elements:

float x;
float y;
float z;

for (int i = 0; i < vertices; i++ )
   //Do something with variables
   x = p[i].x;
   y = p[i].y;
   z = p[i].z;

Hope this helps somebody who is wondering how to use contents of the vertex buffer for your own purposes.

keskiviikko 16. tammikuuta 2013

Loading multiple OBJs completed!

Again I have made some nice progress.

I have managed to:

  • load multiple meshes and material data for the mesh
  • OBJ loader loads also multiple material data and application tracks correct texture the mesh
  • I can insert dynamically * amount of meshes to the world
  • I can test if ray (user mouse click on the screen) has intersected a mesh
Currently I'm doing:

  • Lines which indicates that a mesh has been selected

Next I'm going to do:

  • When a mesh is selected, user can alter transformation of the mesh

Here I have a picture where I have inserted two own meshes to the world. This starts to look quite cool. It is not so far away that I can start to create some content to the game.

It was a bit of pain to manage resource de-allocation. I constantly got invalid pointer, when I tried to release DX data. Altough I have now managed to get rid of those errors. It was funny to notice, that when I used memcpy to copy meshes, I also copied it's destructor address and that way de-allocation was total failure at every time.

torstai 27. joulukuuta 2012

Nice progress

I have done so much work for my game in the past three weeks and now I can really see it! I have learned so much about DirectX 9 API and I start to feel quite comfortable with it. I have also learned nice bunch of linear algebra, which I can see now why it's so important part of game programming,

My accomplishes at December:

  • OBJ-loader. Done! (Thanks to forum for a bug fix!)
  • Dynamic Mesh creation. Done!
  • Controlling meshes so that user can change position and rotation of the mesh. Done!
  • Better camera controlling, than in previous post. Done!
  • I have a plane where I can insert a line dynamically with cursor -> Casting a ray and checking collision with ray and plane. Done and done!

Here I have a picture where I have inserted lines dynamically by clicking the plane with cursor. Green lines are single line primitives and red,green,blue cross is origin of the world.

I have now line-plane intersection test and now I have to change it so that I can check collision with ray and a mesh.

keskiviikko 24. lokakuuta 2012

So far so good...

Time for my second post and give you some info what I allready has made. It's been awhile since last post but be patient, I will update this blog from time to time.

Ok, so in this post I'll give heads up what I have allready done and what is my next step.


I have set up basic environment where I can render a 3d object, get user input and with user input to move and rotate camera in the 3d world. So far I have a static single 3d object in the world what I can view as I please. The 3d object is a cat from the DirectX Mesh sample. Also my algorithm to load a 3d model from a file is copied from that source. You can find the sample from DirectX SDK kit.

I think what I really should do, is to keep track of hours what I have spent for my project. Currently I have no idea how much of time I have used so far.

Here is the picture of my application. As you can see I have named this application as "Level Editor" which I will explain in the next post. The post includes also what "administrative" tools I will create for my game and also a little bit of overall view of the project. I also try to get a picture of my KanBan board to the post, so stay tuned!

I will name this picture as "Hello Kitty" picture, like the famous "Hello world!" application, which we all have done in some stage of our life.

To do

So the next task is to create more dynamical way to load 3d models to the Level editor tool. I think the algorithm should load the models from some source and user then could choose in the application what model the user wants to use. I think it will demand more coding than so far and I also have to think a little bit future as well, that I am not doing something stupid which I will most certainly rewrite.

It will be another task, that user can place the model in the world.