November 2005 - Posts

Refactoring and rewriting some tutorials and design layout

Now that i'm on holiday it might be a good idea to revise my tutorials and see what I can change to provide a more constant and good base for people to learn from. Aswell as thinking of a good way to redesign my site. If anyone has any comments and or suggestions please don't hesitate to contact me via pieter at pieterg dot com.

I am busy working on them as we speak. I hope to get them up as soon as possible.

Posted by pieterg | with no comments

1 More

Only 1 more exam and i'm finished with this year. Next year will be my last year at Technikon and then i'll join the working world and contribute to society ;) I will do alot of work this holiday such as writing articles and updating my site. A whole 2 months of DirectX goodness. Now that is going to be alot of fun.

Posted by pieterg | 1 comment(s)

Xilath - Visual Studio 2005 (Direct3D Template)

Xilath - Direct3D9 Visual Studio 2005 template Download
I've had the chance to slap together a simple and easy to use template for people interested in playing with a small framework. The directory structure is hopefully simple to follow and here is the breakdown.
1) Drop the xilath_template.zip file into your Visual Studio 2005\Templates\ProjectTemplates directory.
2) File -> New Project -> Xilath
3) This will give you a new project and you are ready to add some goodies and extend the framework as you see fit.

Core
    Debug - Handles output to the screen. Debugging information such as frames per second
    Engine - Main entry point for initializing entities and includes 4 methods such as Update, Pre-Render, Render, Post-Render
   
Game - This is where the Application.OnIdle event is hooked up to the window.
    Input - Handles input from the mouse and keyboard.
    NativeMethods- Enables the program to extract the PeekMessage, QueryPerformanceFrequency and the QueryPerformanceCounter methods
    Timer - Calculates the frames per second and acts as a timer to do framerateless movement.
Display
    XilathWindow
- Will be the display surface for our engine.
Graphics
    Renderer
- Handles rendering and device management
View
    Camera
- Use of this class will enable freely moving such as a freeform camera.

I really hope this will help some people get to grips with some of their own engine design and this might serve as a template for people who want to quickly prototype small demos.
Posted by pieterg | 4 comment(s)

PieterG.com Up

It seems that my site is back up ;)
Let the tutorials commence ;)

Posted by pieterg | with no comments

PieterG.com down - Server Maintanence

It seems by the strike of my luck today, not just my car's fanbelt started threading but now my server's doing maintanence. I would like to make those of you that read my blog and use the tutorials on pieterg.com that my site will be down for about 2 days. Sorry for the inconvenience.

Posted by pieterg | with no comments

Terrain generation - The Math

One of the most interesting topics for me is the way terrain is generated and the feeling of outdoor scenes. What I am going to do today is refactor my terrain engine but as an added bonus is I am going to list the terrain generation algorithm here for future references for anyone that wants to do a quick lookup and understand it. In Direct3D all counter clockwise created triangles are culled. This means that if you specify a triangle creation in counter clockwise order by default Direct3D will not render it. This is just a little information before we continue ;)

So what do we need to do?
We need to create a triangle mesh that will be used for a base of our terrain. Right now we aren't really concerned about the heights as that will involve another post but let's continue...
Well here is a little image that will probably make things a little more clear.


Do you notice anything out of place? Notice that I have specified triangles in the counter clockwise order, from what I said earlier Direct3D culls counter clockwise triangles. Why did I choose it? Well it's because the algorithm I have generates the triangles in that order and it's easy to read. You could probably go further and work out an algorithm that will generate your triangles in a clockwise ordering but it's just 2 renderstates that I need to set so that's not really going to bother me.

Device.RenderState.CullMode = Cull.Clockwise;
//Render my terrain
Device.RenderState.CullMode = Cull.CounterClockwise;

The Algorithm
Let's first look at the vertices. For performance reasons we will use a vertex and buffer setup. Index buffers are basically indices that show direct3d how different vertices are connected to form primitives. So we just need 1 set of non-duplicated vertices and the index generation method will be the real challenge here.

Vertex Generation
So the vertex generation is simple.
Example
ArrayList vertices = new ArrayList();
for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {
        vertices.Add(new Point(x, y));
    }
}
This will just generate vertices based on the width and height of the terrain, very clean and simple.

Index Generation
The index generation is a bit tricky and the easiest way to solve these problems is to take some test data and write them down. So let's take some test data.

As you can see the data is exactly what we wanted and now we can start to notice some cool things about the data and working out algorithms are really fun. So let's match these up with the indices that we want created... to see how we can work out this algorithm.
For a 3 x 3 terrain we have the following data set.
Vertex Data
(0, 0), (1, 0), (2, 0), (0, 1), (1, 1), (2, 1), (0, 2), (1, 2), (2, 2)
Index Data
Triangle 1 :  0, 1, 4
Triangle 2 :  0, 4, 3

Triangle 3 :  1, 2, 5
Triangle 4 :  1, 5, 4

Triangle 5 :  3, 4, 7
Triangle 6 :  3, 7, 6

Triangle 7 :  4, 5, 8
Triangle 8 :  4, 8, 7

How interesting is this? We can now figure out what's going on here...
So let's take a though pattern step and look at x and y in each iteration of our nested for loop.
Iteration 1
x = 0; and y = 0;
Iteration 2
x = 1; and y = 0;
Iteration 3
x = 1; and y = 0; and so on....

The Complete Algorithm
One thought is that we might be able to generate a complete primitive in each iteration... Yep that's a good idea so let's work on that.
Iteration 1
x = 0; and y = 0;
Triangle 1 :  0, 1, 4
Triangle 2 :  0, 4, 3
We need to get 0, 1, 4, 0, 4, 3 in order.
So how does this look?
ArrayList indices = new ArrayList();
for (int y = 0; y < height - 1; y++)
{
    for (int x = 0; x < width - 1; x++)
    {
        indices.Add(y * width + x);
        indices.Add(y * width + x + 1);
        indices.Add((y + 1) * width + x + 1);
        indices.Add(y * width + x);
        indices.Add((y + 1) * width + x + 1);
        indices.Add((y + 1) * width + x);
    }
}

That works and so I carry on to explore the deeper meaning of terrain generation and tommorow I will post some screenshots...
Till then...
Posted by pieterg | with no comments

Engine design (continued...)

So far I have tried to assemble piece on piece the engine layout and this is what I came up with so far. The resource and entity managers are prototyped and are doing some pretty basic stuff. As my previous post mentioned the resource manager will contain resources only and manage the loading of them and passing them to an assembly line where they will be used to construct entities.

Right now let's step through what I have.
Game
The game object will manage attaching the appropriate delegates. The window is a simple form that will act as our display mechanism. We have an application idle event that will be our looping structure where we check the messages and act accordingly. This gets hook to the application's idle event.
XilathWindow window = new XilathWindow(800, 600, "Xilath"); //Create a new control to render to
Application.Idle += new EventHandler(window.OnApplicationIdle); //Hook the application idle delegate
Application.Run(window); //Run the application

Engine
The engine acts as the driving force and overseer. It manages the managers (heh...) and makes the needed init calls. It contains 5 methods namely the constructor, update, pre-render, render and post-render. The constructor inits the managers and needed resources. The update method updates entities in the current entity list. Processing of the entities and sending the list to the appropriate managers like the physics manager and more. The Pre-Render method then takes the updated entity list and batches and sorts it so that they can be passed to the renderer in the rendering stage to be rendered. That's at a macro level what happens.

The Renderer
This is probably the most exciting part out of everything. It renders the renderable entities in the game. It will later include shaders and other cool parts but for now it's rendering entities and manages the rendering device.



That's it for now...
Posted by pieterg | with no comments

Resource and Entity Management

Today I am going to try and map out what I have in store for my resource and entity manager.
When working with large amounts of data such as textures, meshes and other variables also known as resources you need to be pretty sure that you manage them well.

Let's go over some of the basic methods that present themselves.
Entity self loading
You could have an entity manager that has a couple of methods for handling it's own resources. This is quite a good concept at first and let's look at the basic outline
/// <summary>
/// Handles specific entities and queries the resource list to instantiate them
/// </summary>

class EntityManager
    {
        #region Members
        private ArrayList entityList;
        #endregion

        #region Properties
        public ArrayList EntityList
        {
            get { return entityList; }
        }
        #endregion

        #region Methods
        public EntityManager()
        {
            entityList = new ArrayList();
        }
        public void Add(Entity entity)
        {
            entityList.Add(entity);
        }
        public void Remove(Entity entity)
        {
            entityList.Remove(entity);
        }
        #endregion
}

From first glance this seems pretty useless as it's just a skeleton and it could easily be just an ArrayList can't it. The thing to note is that you could enhance the entity manager to control certain parts of each entity like provide a cull method or provide a property that will only return renderable entities. This is really just a simple entity manager.

The problem comes in resource management and that you might have 50 entities using the same texture or mesh or both. So when loading your entities and letting them handle their resources themselves present the problem of duplicating alot of resources which is "bad". What else can we do?

The Resource Manager
We could create a resource manager. This resource manager will keep a list of all available resources that are loaded and ready to use. This will include textures, models and more.. This will enable the entity manager or scenegraph whatever the structure of your engine is to query the resource manager for a set of specific resources and create an entity object and place it in a entity list.

PS: The entity list is usefull for doing queries from as it will enable you to pass the list between manager such as your physics engine that will do it's magic on the entities and pass the object to a pre-render batching system that will compute a PVS (Potentially visible set) which will basically cull some objects and pass a modified list to the renderer for rendering.

/// <summary>
/// Manages resources such as Meshes, Animations, Textures.
/// </summary>

class ResourceManager
    {
        #region Members
        private Hashtable resourceList;
        #endregion

        #region Properties
        public Hashtable ResourceList
        {
            get { return resourceList; }
        }
       #endregion

        #region Methods
        /// <summary>
        /// Constructor : Initializes the resource list
        /// </summary>

        public ResourceManager()
        {
            resourceList = new Hashtable();
        }
        public void Add(string key, object resourceItem)
        {
            resourceList.Add(key, resourceItem);
        }
         #endregion
    }

You could provide methods on getting the resources which should be trivial.
As I continue to make adjustments to this work in progress I shall post updated parts of the resource and entiy manager.

Posted by pieterg | with no comments

Renderer class - Managed DirectX (C#2.0)

Today I spent some time refactoring my renderer and as you know I rewrote it in C#2.0.
As always I will spend even more time expanding and developing me engine and making use of comments and suggestions.

So what do we want? We want a simple wrapper aswell as a manager that will render our entities and other arbitrary objects. So the renderer will handle the device. This will include devices that has been lost and resources that needs to be reset... but after some thought it might be more efficient to have the engine/core handle the resources as the renderer should just maintain the device and the state of the device. With this in mind let's look at the basic outline of the class.

Posted by pieterg | with no comments

Time for a change - Development Diary

I think it's very helpfull for anyone to make a change now and then and I would like to restart development on my framework as I have spent alot of time on the tutorials and updating them. I would like to move over to C#2.0 and develop my framework using the new features such as generics and partial classes.

I am going to be keeping a development diary on my development of my framework in C#2.0 and why I am doing different things the way I am. I am going to be implementing some design patterns such as the factory pattern and also just get a scripting engine running so that you can instantiate different objects in runtime. It's very exciting and I am looking forward to getting some source code out to you and we can comment and discuss it.

If there is anyone that would like me to write a tutorial on a specific subject please feel free to contact me. I love the community and I like spending time with people and discussing direct3d development and game development in general. It's really an awesome and broad subject and I can spend countless hours just talking about it.

So today it starts. The Development of Xilath2.0.

Posted by pieterg | 2 comment(s)

More tutorials on the way

I have been so busy in the last couple of days with studies and answering posts etc.. that I have neglected my site. I have more tutorials planned and I am thinking of combining them in a .pdf for download so that you guys/gals could just grab a zip/rar and read it offline.

I also have a series of tutorials planned on real life game development but I really need to structure this correctly. I want some comments/advice from you, terrain rendering... etc.. and more shader tutorials. What's really needed so I can plan this properly.

Other than that it's been a really awesome year and thanks for those of you that have supported the growth of this site and as long as you keep on reading i'll keep on posting and writing.

Posted by pieterg | 1 comment(s)
Filed under: