Sunday, February 26, 2012

I have scene graph!

I finally got my scene graph working. It looks like THIS:
Oops, wrong picture, here we go:

It's not beautiful, but now I can create different objects to my hearts content and transform them as I wish. If you want you can download the .exe(and the shader file) or the source.
Next in line I guess is adding textures, lighting and keyboard input, in whichever order is the simplest.  

The importance of Zero, and not overcomplicating things..

I haven't made any update for a while, I've been trying to split the code in initDevice that shouldn't really be in the init function if we want to create objects freely and not only once in the initialization of the game.. It would also get really cluttery and outright non-modular to create all objects we want in the initialization function.. We want to split the graphics code from the game code and instead provide tools to create objects freely from the main part of the game code.

So I've made a separate .h for the graphics, which declares a TSceneGraph class, this class will be used to handle what we see, so far it's very simple, and in fact only supports one(!!) object, only supporting one object is of course a rather useless scene graph(if it should even be called a scene graph), but let's take simple steps.

Now, the TSceneGraph class is based on the code from the DX11 SDK tutorial 5, but split into functions in the class. Most of the functions are as they where, except that the object creation code has been taken of of InitDevice() and put into CreateObject(), and the vertex data is expected to be written where the CreateObject() function is called.
I ran into some trouble doing this, which is why there hasn't been an update for a while(also, I'm on an internship, so I'm rather busy).

Anyway, the first problem:
Calling CreateBuffer() returned E_INVALIDARG and the application quit from InitDevice().
After finally making sure that the D3D11Device was created with the flag D3D11_CREATE_DEVICE_DEBUG, I found that I was my buffer description bd containd some very strange flags, apparently, this was because I had not done ZeroMemory(db). This leaves some strange random data in the fields of the buffer description that are not specifically defined.

Second problem:
I tried to create the objects as pointers to 3dObject structs containing a VertexBuffer and an IndexBuffer each.. This all got really confusing and I probably just didn't manage to access them in the correct way, leading to exceptions when trying to create the buffers. I realized that such a struct only contains pointers anyway, so there is not really any use of making them pointer variables.
So I just skipped the complicated stuff for now and made regular variables.

So after some time of trouble, I finally got it compiling again. Way!
 It's still just the same triforce, and it's not even rotating! :O
BUT, it's all just a bit more modular and uses a "scene graph".

PS, since I'm away from home for a while I have to do this on a laptop, making development really tedious due the small screen(note the lack of plural).. sigh.

Saturday, February 18, 2012

DirectX Tutorial 1

I finished the first DirectX tutorial, it takes you through setting up a project in Visual Studio C++ Express 2010, and linking in the DX11 SDK libraries.

Read it here.

Progress Report

I have no fancy pictures of any moving textured objects yet, but there has been some progress.

I've realized that the way the code is structured in the DX11SDK tutorials simply won't do even as a starting point for a game engine, for a few different reasons..
First off, the triangles shown are defined and put into a vertex buffer in the InitDevice() function, and while we could change the triangles in some other function later, this would get rather messy, since the same kinds of things would be done at different places. Also, after Tutorial 5 we draw out two objects, but we actually only have one, we transform it and draw it at two places, but it's only one object. This is kind of a problem.

So we need to add something that manages different objects, where they're at, how large they are, what triangles they contain, how they are drawn, and so on. I *think* this is what's called a Scene Graph. We want to do these things in a separate file from the main .cpp, because in the main .cpp we will want to focus on handling events in the game.

So I'm restructuring all the code(not much code, but hey), making a class called TSceneGraph in a different .cpp file, that will do all of the above. This also means moving all the DX11 initialization to this TSceneGraph.
Inside this class I can create a variable that decides what API to use, making it possible to chose if I want it to initialize an OpenGL window or a DX11 window, while keeping track of all the objects separately from the API.
Making this new structure is kind of hard for me and requires some thought, and also, since I'm note really used to C++ anymore, lots and lots of compiling errors. To brush up I read about header file and source file standard practice here, and about class definition here. Hopefully, I'll have a rudimentary scene graph working soon.

I also found how to chose which pages to include or not in the menu, so from now on any tutorials will have their own pages, instead of posts, linked from the Tutorials page in the menu above, and from a list to the right.

Friday, February 17, 2012

Tutorial: Linear algebra 1

New tutorial! it was here in this post, but I moved it to here, it's also linked from the tutorials page for your convenience.
It's a short introduction to some of the maths you'll need in game programming.

Small Status Update

No major progress codewise the last few days, I've been looking through the last 3 tutorials in the DX11 SDK, and they basically take you up to the point to where you can apply textures. But after that all information provided in the SDK is samples using DXUT. I'm probably going to post a dedicated tutorial that takes you to the same level of functionality, but in one tutorial expanded step by step, with suitable points to compile and test your code. This will hopefully spare you the generation of colored vertices and other stuff you will most probably not be using in a game(you'll want textured triangles).

Also I've been working on a very short tutorial on linear algebra, it will take you to matrix multiplication. Hopefully it will be fairly understandable without being TLDR, so that you can understand what's happening even if you haven't gotten to university level maths yet.

I've been looking for other game development resources, and I find that OpenGL and Direct3D both changed their architecture a few years ago, and the way triangles are rendered and shaded has changed. It makes the learning curve slightly steeper, but from what I can gather very good improvements. This change however makes it fairly hard to find tutorials that are relevant and up to date. Actually, I was using NeHe's tutorials almost 10 years ago when I was making a game for a project in high school with a friend, but the changes make it fairly difficult to adapt to newer versions of OpenGL. I've found various other resources as well, and I'll keep a list managed on the tutorials page once I've finished with the linear algebra tutorial.

The change in architecture will probably also make it very hard to implement several different versions of DX and OpenGL in the same engine, but we'll see as time go if it seems possible. In any event it will first be based solely on DX11 anyway. I'll make a managed about page that lists the intended and implemented functionalities.
On a related note I've decided to add Collada as the main intended model format.

Tuesday, February 14, 2012

More Triangles

Double the amount of triangles, in fact. (and also rotating, I would have made a .gif if I wasn't so lazy..)

Tutorial 03 in the DX11 SDK is the same code as 02, but it explained how the shaders work.

I read Tutorial 04 and took the important parts into my own little test application.
Tutorial 04 adds spaces, the object, world and view spaces, to be specific(not counting Projection and Screen Spaces). And this enables us to actually move things around, without really changing any vertex data, but by transforming the spaces. I would really recommend reading Tutorial 04, it's quite well written and I believe the concepts handled are very central.

Be warned that this is where people who have not learned Linear algebra would otherwise have to give up, and if you don't, don't be afraid if you don't understand the transformations part.These transformations are handled by the XNAmath functions in this tutorial anyway, so just ignore that part if it's too hard. The important thing is to learn thinking in three dimensions..

It also makes some use of these included spaces by rotating a colored cube.

I didn't like the cube and made a rotating triforce instead, because rendered triangles only have one side, I made it double sided, hence double the amount of triangles.

Here are the vertices if you want to try ;)

SimpleVertex vertices[] =
{
{XMFLOAT3( 0.0f, 0.5f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
{XMFLOAT3( 0.25f, 0.0f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
 {XMFLOAT3( -0.25f,0.0f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},

{XMFLOAT3( 0.25f, 0.0f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
 {XMFLOAT3( 0.5f, -0.5f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
 {XMFLOAT3( 0.0f, -0.5f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},

{XMFLOAT3( -0.25f, 0.0f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
 {XMFLOAT3( 0.0f, -0.5f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
 {XMFLOAT3( -0.5f, -0.5f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},

{XMFLOAT3( 0.0f, 0.5f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
{XMFLOAT3( -0.25f,0.0f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
{XMFLOAT3( 0.25f, 0.0f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
        
{XMFLOAT3( 0.25f, 0.0f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
 {XMFLOAT3( 0.0f, -0.5f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
{XMFLOAT3( 0.5f, -0.5f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
        
{XMFLOAT3( -0.25f, 0.0f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
 {XMFLOAT3( -0.5f, -0.5f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
{XMFLOAT3( 0.0f, -0.5f, 0.0f ),XMFLOAT4( 1.0f, 1.0f, 0.0f, 0.8f )},
};


To use it, you also need to make sure not to use the index buffer defined in the tutorial.