Ok problem finally sussed! After half a day of trawling through arrays and pointer code, checking for possible overflows and other things, I couldn't find a problem. Turns out it's something to do with what happens to an object during a dbSync()
This code works fine:
LMStart();
dbLoadObject("..\\..\\Models\\arena1-top.x",1);
LMAddCollisionObject(1);
LMAddLightMapObject(1);
LMBuildCollisionData();
LMAddPointLight(12.5, 6, 25, 25, 1, 1, 0.5);
LMBuildLightMaps(1024,6.7,2);
However, if you add a dbSync() command anywhere in that code AFTER the object load but BEFORE the LMAddLightMapObject() the model data will be corrupted and the program will most likely crash. The reason I was having the problem is my models are loaded in my level editor, loads of syncs happen as the user works on the model and then only when they hit "Render Lights" do the models get added to the light mapper and rendered.
So I think the solution will be:
- Run LMStart() when my program first loads
- Run LMAddCollisionObject and LMAddLightMapObject directly after loading your model (or making it with mesh commands)
- Then you can sync away as much as you like before you make the light maps
Unfortunately this solution won't work for me, as model prefabs may be removed before the light mapping is rendered, and there is no command to remove an individual model from the light mapper. So my only solution is, when the user clicks to render lighting:
- Save all models to disk with all positional information etc.
- Load and rebuild the entire scene
- Render the lighting
- Continue as if that wasn't a pain in the arse
I need to work on scene saving code anyway, so I guess I'll kill two birds.
Also setting the lightmap file names only accepts a single char, rather than a char* as suggested by the manual. So it doesn't look like you can actually name your lightmaps, or give them a directory, which I'm sure is going to cause me massive headaches soon. I think I'm going to have to manually edit the generated DBO files binary to get them to use unique lightmap texture names, then rename the generic 1.png etc. files made.
Unless there's something I'm missing? It seems too ridiculous to have a core command simply not work and nobody to have been bothered by that. So I must be doing something wrong?