Monday, August 30, 2010

[luabind] Global table of classes from C++

These days I am playing with Lua and LuaBind (yet another game project, more on that hopefuly soon). LuaBind is really quite amazing and is a perfect complement to Lua for C++ programmers.

There is one thing, however, that gave me a lot of trouble. I wanted to define from C++ a global table of class instances in the Lua script. With LuaBind you can easily define globals using luabind::globals. For instance, here is how to define, from C++, a global named 'test' and having 1234 for value:

luabind::globals(luaState)[ "test" ] = 1234;

In LuaBind (v0.9), a table is created using luabind::newtable, which returns a generic object. For instance:


luabind::object table = luabind::newtable( luaState );
table[ "Instance1" ] = new MyClass( "A" );
table[ "Instance2" ] = new MyClass( "B" );
table[ "Instance3" ] = new MyClass( "C" );


where 'MyClass' is a C++ class previously registered into LuaBind.

Defining the table as a global is as easy as:


luabind::globals(luaState)[ "AllInstances" ] = table;


This was working fine, but the code would keep randomly crashing after the lua script execution. This was really a strange behavior. Fortunately, this post saved my day.

The problem is not with the code above, but with the context around it. Here is the entire function running my script:


void threadLua()
{
...
lua_State* luaState = lua_open();
luabind::open(luaState);
...
luabind::object table = luabind::newtable(luaState);
table[ "Instance1" ] = new MyClass( 1 );
table[ "Instance2" ] = new MyClass( 2 );
table[ "Instance3" ] = new MyClass( 2 );
luabind::globals(luaState)["TableOfInstances"] = table;
...
int ret = luaL_dostring(luaState, program);
...
lua_close(luaState);
} /// things would crash here


So what's wrong? Well, lua_close destroys the lua context. Unfortunately, my seemingly innocent object 'table' is still alive when this happens. And as it turns out, the destructor of a luabind::object does expect the lua context to still be valid. Hence the crash. The fix is desperately simple - it is enough to limit the scope of the variable:


void threadLua()
{
...
lua_State* luaState = lua_open();
luabind::open(luaState);
...
{
luabind::object table = luabind::newtable(luaState);
table[ "Instance1" ] = new MyClass( 1 );
table[ "Instance2" ] = new MyClass( 2 );
table[ "Instance3" ] = new MyClass( 2 );
luabind::globals(luaState)["TableOfInstances"] = table;
} /// keep this: 'table' must not live after this point
...
int ret = luaL_dostring(luaState, program);
...
lua_close(luaState);
} /// no crash!


This one was painful to find out so I thought it was worth a post!

Friday, July 23, 2010

[research] By-example synthesis of architectural textures

I am leaving for SIGGRAPH tomorrow, where we will present our paper 'By-example synthesis of architectural textures'. This is joint work with Samuel Hornus and Anass Lasram.
The paper is available on this web page.

See you at SIGGRAPH :-)

Tuesday, April 20, 2010

[wubi] X11 trouble during install

At work I have a computer with a Quadro FX. For some reason, the Wubi 9.10 install has trouble with it. The first reboot under Ubuntu is supposed to launch the install process. However, I was left with a blinking screen and a text console. After a bit of struggle, I was able to install the NVidia drivers:

0. sudo stop gdm
(in between two blinks!) This stops gdm and stops the painful blinking.
1. sudo apt-get install lynx
(a text based browser)
2. Launch lynx, navigate through NVidia website
3. Download the dirvers (AMD64 bits for 64 bits computers)
4. Install the drivers
5. Start gdm

After this the X server should start fine. However there is a catch: Ubuntu is not installed yet, in fact you are running in a virtual RAM disk. If you reboot at this stage, all changes are lost!
The trick is simply to launch 'ubiquity', and select the 'loop' partition as install partition. Then the install completes and you are ready to enjoy Ubuntu!

Hope this helps.

Monday, February 1, 2010

[code] Visual Express 2010

I am trying out Visual Studio 2010. Again, thanks to CMake the transition is mostly painless.

I had one issue though, related to global include paths.
As explained here, they can no longer be set for Visual Studio as a whole but only on a per-project basis. Let's not argue whether this is good or bad but let's assume you need to set them globally (I my case, I just want the DirectX SDK include / lib paths to be set once and for all).

In fact, there is a very simple workaround. All projects seem to include the following property sheet: Microsoft.Cpp.Win32.User
Any change to these properties will happen in all your projects. Just edit it and modify the include / lib of the "VC++ directories" entry.

Note that there seem to be issues under XP (I am under Windows 7) => so read the full story if you need to set global paths using XP and 2010.

Saturday, January 2, 2010

[movies] Avatar and 3d glasses

Avatar was a good opportunity to compare different 3d technologies in movie theaters. I have seen it twice, once with shutter glasses, and a second time with the Dolby technology, that is the Infitec color filters. I did not see the movie through polarized glasses, but I used some in the past.

The first time I saw the movie was through shutter glasses. I was a bit disappointed at first since I was eager to try the Infitec approach. In addition, it felt a bit old-school since in 1999 I had shutter glasses at home! (the cheap but effective Elsa Revelator, much less expensive than the ones currently on shelves). Nevertheless the experience was extremely good, with very impressive depth and comfortable images. Key drawbacks where not-so-clean glasses (but I had brought glass-cleaners) and significant loss of brightness.

The second time I saw the movie, in a different theater, was through the Infitec (Dolby) color interference filters. The main advantages are the lightweight cheap glasses (still sold for 1 euro in this theater...), the fact that you can tilt your head (compared to polarized glasses) and the much less significant loss of brightness (compared to shutter glasses).
However, when the first images came I immediately felt very uncomfortable with the 3d perception. Something was slightly wrong, especially in the interior scenes. After several experiments (good thing I had seen the movie before!) I noticed that many specular reflections - especially colored ones - where strongly reduced on one eye while still present on the other. This creates an odd feeling, quite tiring for the eyes. I am told by a friend that you can experience a similar effect if you look outside putting a polarized glass in front of only one eye, since the polarized glass filters out reflections . (Nothing to do with watching a 3d movie with polarized glasses - just another use for polarized glass).

In the end, while the Infitec glasses do work well - much better than early 3D glasses - personally I would recommend seeing the movie through shutter glasses. It was much more comfortable for a movie of this length.

And, by the way, the movie is great, but there is no James Cameron's movie I did not enjoy ;-)

[code] Code editor under Linux

For almost 3 years now I developed mainly under Windows. Not that I have anything against Linux, but I just did not feel the need to use it anymore. I recently transitioned to Linux again, mainly to benefit from latest advances of g++. I have to admit it was really enjoyable to get back to it, especially using Wubi / Ubuntu.

CMake was instrumental in ensuring a smooth transition of my code to Linux. Thanks to Anteru who helped a great deal, I was able to compile all my code on both Windows / Linux (almost) without trouble.
The only thing I was missing was a good code editor. I mean, gedit works great, but it's missing many of the features I came to enjoy in Visual C++ (and well, all these little seconds you gain add up to a great deal in the end).

At first I tried 'apt-get install eclipse-cdt'. CMake is able to directly generate project files for eclipse. Unfortunately, this did not work too well for me. Maybe because of our current project architecture (quite big, many dependencies), eclipse had difficulties and regularly crashed.

A bit disapointed, I then tried Code::blocks ('apt-get install codeblocks'). And, well, this is a very promising tool for me. It just works, it is fast, CMake has a back end for it as well. In just 10 minutes, I was feeling like home. So, I highly recommand it to those who are used to Visual C++ like IDEs.

Small tip: If like me you use dark backgrounds, you will find the color setting for the caret in "Settings > Editor > Margin and caret".

And btw, Happy new year ;-)