Wednesday, January 20, 2010

Day 0.2 Creating a drawing Surface.

On to drawing! The first that I will need is a drawing surface. In the interest of speed, windows compatability, and looks I chose to go with 32bpp color which uses 1 byte for Red, Blue, Green and Alpha (8 bits per byte, for a total of 32 thus 32 bits per pixel) .

For my the drawing surface I created an array of byte 4 * width * height - four bytes for each enough to handle all my color information. Some initial profiling showed that looping through every pixel on every frame to clear it was taking a lot of time so I created a cached copy of background color pixels (black in this case). Copying this via System.Buffer proved to be much faster.

public class Surface
{
private const int BytesPerPixel = 4;
private readonly byte[] clearPixels;
private readonly int height;
private readonly byte[] pixels;
private readonly int size;
private readonly int width;

public Surface(int width, int height)
{
this.width = width;
this.height = height;
size = height*width*BytesPerPixel;
pixels = new byte[size];
clearPixels = new byte[size];
Clear();
}

public byte[] Pixels
{
get { return pixels; }
}

public void CreateClearPixles()
{
for (int index = 0; index < clearPixels.Length;)
{
clearPixels[index] = 0;
clearPixels[index + 1] = 0;
clearPixels[index + 2] = 0;
clearPixels[index + 3] = 255;
index += 4;
}
}
}

I added drawPoint funtcion, and a draw line function. Again profile showed that Buffer.BlockCopy is much faster than writing the four bytes one by one. For line drawing I used Bresenham's line algorithm.


public void Draw(int x, int y, byte[] color)
{
int byteOffset = BytesPerPixel*(x + y*width);
Buffer.BlockCopy(color, 0, pixels, byteOffset, 4);
}

I also added a GetBitmap function that would return a System.Drawing.Bitmap.
I'm not happy with how this is working at all, so I am going to omit it for now.

public Bitmap ToBitmap()
{
// very ugly implementation that I won't show until I fix.
Buffer.BlockCopy(clearPixels, 0, Pixels, 0, size); // reset the picture.
return null;
}
}


So we're able to draw now, but we're still stuck in the 2D world. we can draw shapes but we can only map X and Y to the screen. Z is nowhere to be found. Next up: 3-D!

Day 0.1 Points Shapes and Worlds

The very basics. Points, Shapes and the World.

To that end I created 4 classes: Point, Shape, World and Viewport.

Point. Originally I wrote point as an array of int[3]: X Y and Z.
Shape - a list of points.
World - a list of shapes.

This was enough for me to define some basic shapes a Cube (e.g.) a cube and add them to the world. I couldn't draw them yet, but they existed. My fiancee was unimpressed.

Building a Game Engine: Day 0 (ish)

It's not really Day 0, but I feel the need to start keeping an engineering log, and this unused blog account seems to be the perfect place.

So, I'm re-inventing the wheel. I've decided to build a 3D rendering Engine, and a Game engine from scratch. I'm not trying to build a better wheel. I'm just trying to learn a little about wheel building.

Today, I can draw wireframe shapes on the screen and rotate them about 3 axis - though not very well. I'm still a little (a lot?) fuzzy on points and vectors and what w is. If you're reading this please feel free to send me corrections (or useful links !) I don't want to disseminate bad information if I can help it.

So my game engine began with the idea of writing a quick (ha!) software only 3d rendering engine and then adding some game rules to it. I hope one day to to produce a multiplayer game that can be played over the web. but for now I'm just building the very basics.