Archive for February, 2009

Simple Scene Graph in C++

Uncategorized 4 Comments »

There are several articles gathering dust bunnies on the internet on creating a scene graph class in C++ for your 3D engine but most are pretty vague and quite old. Hopefully, this post will give you a foot in the door in creating your own scene graph for your engine.

To start off, let’s go over some basics. A scene graph is a tree-like data structure which holds information about the scene you want to display. Every node has a parent and every node may have children. In this post we’ll use the std::vector to store the scene nodes but you can substitute this with whatever you want. A scene graph can be visualized like so:

             [root node]
                  |
        o=========o=========o
        |         |         |
    [child 1] [child 2] [child 3]
        |
    o===o====o
    |        |
[child 4][child 5]
             |
         [child 6]

Any node may have any number of children who’s children may have any number of children, etc. Each node in the scene graph may have a name for lookup functionality so a very lookup system will be implemented. We will also need functionality to add a child node, remove a child node, set/get a node’s parent and an update function for updating the graph hierarchically.
Read the rest of this entry »

Little Endians, Bytes and Binary

Uncategorized No Comments »

I’ve recently had the need to read an entire file into a byte array but still had the need to extract integers from it. Turns out, it’s very possible to do this but platform dependently since the byte-ordering (or endianness) on different machines can differ.

For example, Little-Endian machine A has an integer that it needs to write to disk: 12345. Big-Endian machine B has the same integer that it needs to write to disk as well.

The hexadecimal representation of the file on machine A would look like: 00 00 30 39 while machine B’s output would look like: 39 30 00 00.

If we were to read machine A’s file on machine B, the output would not be 12345 as expected but 245618442240 instead. Now that’s quite a problem. Any file written on machine A would be useless in any other environment.

In the meantime, be aware that there’s no way to determine if a file is Big or Little Endian so you would need to set a standard for your file. I use Little-Endian byte ordering since that’s my machine’s native format and 99% of the time, yours as the x86 family of processors is Little-Endian.

So in order to read any of the files we have on disk, regardless of endianness, we first need to detect the endianness of our current machine and somehow detect the endianness of the file we’re trying to read. This is not a big pain in the ass as you might suspect since machines, in addition to files, also order their memory in Big- or Little-Endian byte ordering.
Read the rest of this entry »

Minor Update

Uncategorized No Comments »

As you might have noticed, the URL of the blog changed from http://scriptionary.com/blog to blog.scriptionary.com. This is because of some changes that are going to happen to the main website that require the web-root to be cleaned up. :)

Happy Friday the 13th