Update: Quaternions Part 2 coming soon….

August 6th, 2010

Lots going on!

Sorry for the delay!

I thhhinnkkk there is still a bug in the code, i have the post written up i just want to do some tests to make sure.

Quaternions Part 1

April 15th, 2010

The very first thing I’m going to say about Quaternions is that they are EASY to understand how to use! They are hard to understand why they work (ie. proofs and such). With that said… us game/graphics programmers care a lot about how to use stuff, which is what I’m going to talk about.

When you rotate an object, you use some matrix math to rotate on either the X, Y, or Z axis.

A look at matrix transformations on wikipedia will give an idea. When you are limited to these 3 axes, problems arise when you you rotate a certain amount on one axis you can align it with another, and then rotating on another axis becomes the same. VERY CONFUSING i know, but the important thing to realize is that you are limited to 3 axes when rotating an object with a standard matrix.

A quternion is just a vector with a rotation applied to it. THAT is it! this vector represents an arbitrary (meaning you define it) axis which the rotation value is applied to.

what makes this different than matrices is you can add quaternions together to get ONE rotation, as opposed to 3 separate rotations! When you are done with the rotation, you can simply compose a matrix out of the quaternion and use that matrix with your translations to get the proper rotation!

I will leave it to you to research teh functions.

Part 2 will show some code for our camera.

-DB

Event Data from C++ to Lua using Luna(r).

April 8th, 2010

It has been a while since I have updated, but I have a couple of posts I will make within the next week. The first post is on the enhancements I have made to our Event System.

The basic implementation of this event system is from lua programming gems (very nice book). It consists of a lua object (otherwise known as a table) that contains a couple of tables that stores Events, Pending Subscribers and Pending Unsubscribers, as well as a couple of functions for updating subscribers and firing events. You can get the full source for the Event System here (toward the bottom, 21 I believe). This Event system has a C++ implementation that simply pushes a string onto the stack and calls the lua function like so.

The Fire Event function looks something like:

 function EventManager:FireEvent( eventName, ... )
   self.Events[ eventName ] = self.Events[ eventName ] or {}
   for subscriber,_ in pairs( self.Events[ eventName ] ) do
     subscriber[ eventName ]( subscriber, ... )
   end
 end

and in C++ looks like:

void FireEvent( lua_State* L, std::string eventName )
{
  //Call the lua FireEvent on this event manager object.
  lua_pushlightuserdata( L, this ); //STACK: em
  lua_gettable( L, LUA_REGISTRYINDEX ); //STACK: self
  lua_pushstring( L, "FireEvent" ); //STACK: "FireEvent", self
  lua_gettable( L, -2 ); //STACK: FireFunc, self
  lua_pushvalue( L, -2 ); //STACK: self, FireFunc, self
  lua_pushstring( L, eventName.c_str() ); //STACK: self, eventName, FireFunc, self
  lua_pcall( L, 2, 0, 0 ); //STACK: self
}

As you may see, there is some lost functionality in this process. Although the lua fire event function takes additional arguments (note the “…” ), the C++ function does not account for this. This allows event data to be passed around at the lua level, but not from C++ to lua. This is the primary use of an Event! when you have keyboard and mouse input you would like to fire an input event that contains the bytecodes of the input used!

This is okay, with a little effort and C++ template magic we can fill in the little gap. we need to be able to pass in any type of data and include it in the arguments to lua like anything else.
In order to do this, we need 2 things.

First we need to change the C++ FireEvent function to a template function, allowing us to only write it once, and still be able to take many types of event data.

Secondly, we need to expose our EventData classes to lua, and be able to push each EventData type onto the lua stack. This can get very complicated, and as i said before, there are many ways to expose data to lua, this is just one of them.

I used luna (actually lunar but I copy and pasted some of it into my luna class). Luna is somewhat of a pain if you want to expose multiple things into your lua environment from C++, but I find that it is mostly copy and paste and chaging names once you got it down.

Here now, is the new code:

C++ FireEvent and Push Function for a collision event

    template <typename T1>
    inline void fireEvent(std::string eventName, T1 a1)
    {
         EventLog << "FireEvent:: " << eventName << "\n";
        //Call the lua FireEvent on this event manager object.
        lua_pushlightuserdata( L, this );            //STACK:    em
        lua_gettable( L, LUA_REGISTRYINDEX );        //STACK:    self
        lua_pushstring( L, "FireEvent" );            //STACK:    "FireEvent", self
        lua_gettable( L, -2 );                        //STACK:    FireFunc, self
        lua_pushvalue( L, -2 );                        //STACK:    self, FireFunc, self
        lua_pushstring( L, eventName.c_str() );        //STACK:    self, eventName, FireFunc, self
        PushArg(a1);
        lua_pcall( L, 3, 0, 0 );
    }
 
    void PushArg(CollisionEvent* EV);
    void PushArg(PickEvent* PE);
    void PushArg(InputEvent * IE);
 
    //here is what the PushArg for a Collision event looks like and how the new fire event is    
    //used.
    void EventManager::PushArg(CollisionEvent* EV)
    {
        Luna<CollisionEvent>::push(L, EV, true);
    }
 
    //Here is the Event System in action, using a Collision Event to pass collision data to lua.    
 
    CollisionEvent* CE = new CollisionEvent();
    void* id1 = pair.actors[0]->userData;
    void* id2 = pair.actors[1]->userData;
    std::cout << "collision:" << (*(int*)id1) << "::"<< (*(int*)id2) << std::endl;
    CE->Obj1_ID = (*(int*)id1); //GameObject ID set at actor creation
    CE->Obj2_ID = (*(int*)id2);
    Events->fireEvent<CollisionEvent*>("Collision" , CE );
 
   //and lastly, the luna(r) implementation of a collision event 
 
#ifndef COLLISIONEVENT_H
#define COLLISIONEVENT_H
 
#include "luna.h"
#include <iostream>
 
using namespace std;
 
class CollisionEvent
{
  public:
    CollisionEvent();
    CollisionEvent(lua_State *L);
    ~CollisionEvent();
 
    static const char className[];
    static Luna<CollisionEvent>::RegType methods[];
    //static const Luna<Foo>::RegType Register[];
    int getObj1ID(lua_State *L);
    int getObj2ID(lua_State *L);
  //private:
    int Obj1_ID;
    int    Obj2_ID;
};
typedef struct { CollisionEvent *pT; } CollisionEventdataType;
 
#endif

Phew, quite a bit. Here is how that very collision event code example is handled by a subscriber in lua.

    function Obj:Collision( CEvent )
            t = CEvent:getObj2ID()
            d = CEvent:getObj1ID()
            print( t .. "," .. d .. ":and my ID:" .. self.ID )
    end

Not too bad huh?!

In conclusion, you can see that template functions and classes can go a long way in making it easy to provide many kinds of event types for an Event System. In Particular, you see how the update function in this event system lends itself to such an implementation in C++. I hope you found this post useful! Next will be on quaternions!

DB
PS. thanks to dmail, ddn, and m_switch on the gamedev forums for the help in coming up with this.

GDC, focus, and prototyping.

March 16th, 2010

The GDC was last week, and it gave me some perspective about what I’m doing and what we need. We have shifted gears and put this project into overdrive. We have prioritized what we need and have begun prototyping gameplay using the systems and sub-systems of the engine. It’s go time, crunch time, it’s time to make a game, make it happen, and show the world what we can do.

Be excited for some upcoming announcements!

DB

Checkin out the gdc today

March 12th, 2010

Hey everyone!

On my way to the gdc right now. I’ll provide a full update when I get back. Maybe I’ll see you there!

Db

Using Expression Blend to spiffy up the interface

February 27th, 2010

So I was referred to Microsofts Expression Blend 3 by my friend who works with silverlight. Expression Blend is a nice editor like tool used to design fancy shmancy gui’s in WPF and Silverlight.

The learning curve for it is slightly high (for the non designer, developer type like myself that at the moment is still new to WPF), but given a couple weeks and with my backend in need of a more robust frontend, i think the time invested will have a nice payoff. The problem is i need to take some time to learn the thing. anyways, here is a picture of the interface.

Expression Blend Interface

Expression Blend Interface

Excited for Nvidia’s Parallel Nsight

February 18th, 2010

I just watched the following video and I have to say this is what i have been waiting for! I hope the OpenGL support is as good as the D3D support.

OpenGL Engine in a WPF hosted Window.

February 8th, 2010

Wpf OpenGL

I have been working on my editor as of late in hopes of being able to use it to make my simple demo I have been talking about. I found a lot of doubt on the internet about the ability to interop OpenGL with the WPF rendering stack.

Well, we got it to work and it is very similar to the setup I had used to get it working in WinForms. Here is how we did it. First of all, you need a 3D Engine with the ability to take an HWND and use that for rendering. A complete tutorial on how to do this can be found in a gamedev article here.

Once you get this far, its only a matter of creating some xaml.

        <Window x:Class="GravEdit.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GravEdit" Height="800" Width="800">
    <WindowsFormsHost x:Name="OpenGlPanel"/>        
    </Window>

And your C# for this xaml would look something like:

public partial class MainWindow : Window
        {
            private GravWrap gravWrap = new GravWrap(); //an instance of the engine
            ILua LuaPortal;
 
        public MainWindow()
            {
                InitializeComponent();
                System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
                this.OpenGlPanel.Child = panel; //add the winform panel as a child to the winformhost
                IntPtr LuaState = this.gravWrap.Initialize(panel.Handle.ToInt32()); //pass the panels hwnd.

We created a Panel within the WinformsHost and that was it.

Cheers!

Saving and Loading Lua Tables to a file.

January 25th, 2010

So, you have integrated Lua into your game engine, you are able to write scripts that define game objects and program some game logic. But what happens when you want to stop your engine? Can you reproduce a game state 20 minutes in? Well, this is where saving/loading lua tables comes in handy. If your Lua environment is anything like mine, tables store everything. And being able to save tables to a file and then re-load them completely intact can achieve this result.

You have 1 and a half options. why the half? Because I soon found out that 1 option provided half the result (really nothing since it mangled tables). Those options? Lua Pickle and Table.save . These little Lua scripts provide the functionality described above.

Lua Pickle is the half, not coming with the file writing functionality but it was easy to put in. The problem with Lua Pickle was that if you saved and loaded and saved again, certain values would come back mangled when loaded, which doesn’t do us any good! The script is 10 years old though so it’s alright, it made a good run. Lastly, it does not store functions, which is not surprising if you do not think of them as first class variables. If your tables are very simple and do not have sub tables and complex self references, this is a decent option.

Table.save is what I use, however there is a catch. The test that runs it causes it to fail. They claim that using a tmpfile() in io is fast for huge tables you want to save, but the stdio tmpfile function was deprecated on the msdn site, so i had to avoid that “extra feature”. the cool thing about table save is it also can save functions, which, as long as you use some sort of encapsulation (meaning the functions only refer to variables that exist in the table) then you are good to go!

So that’s all for now, comments are welcome!

Update: Editor and Lua Lighting…

January 25th, 2010

I moved the lighting objects to lua, and more so removed some of the legacy lua code (no more sandbox). I’m running lua the way its intended to be run with the engine, initializing lua objects and relying on events for all of their updating.

I started converting my editor to wpf, which actually is not all that bad.