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.
And your C# for this xaml would look something like:
publicpartialclass MainWindow : Window
{private GravWrap gravWrap =new GravWrap();//an instance of the engine
ILua LuaPortal;public MainWindow(){
InitializeComponent();System.Windows.Forms.Panel panel =newSystem.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.
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!
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.
I added another framebuffer at the tail-end of the rendering pipe, this is where I do the culling of the lighting volumes for Deferred Shading. This is also the beginnings of any future post-processing work I do (HDR/Tonemapping/ambient occlusion).
I feel like the lighting is done enough to move into Lua.
My next project is to make a realtime editing of light values in the editor I have (the purpose of its early existance).
Here is an example of where i hope to get with my editor eventually, but for now im just going to do lights =). I by no means have an Uber-Shader yet, but I’m working towards that.
I added point lights over the weekend. Aside from getting the proper light volume shape (a sphere geometry) and proper values for my attenuation, its almost there. Here’s a pic.
So my Deferred Shading shaders were messed up, and after some clarification on theory and math I am starting to see some real working results. here is a picture of a light moving from right to left right behind the camera.
directional light
Next is point lights and a bunch of lights moving around the screen.
Cheers!
PS. Once this is working and im comfy with it i will be sure to talk about it =)
Here is a link to someone who has given me some critiquing on my deferred renderer. We have similar goals with different tools and different things we are currently working on. Another great source for engine stuff.
I just managed to get running the Lua Module system I described toward the end of my presentation. link.
To sum up the reason for this post, i point you to the google search results of “Lua modules” where you will see that the second page in the list is titled “lua’s module function critiqued”. If you have gone down this route before you know what I am talking about. It is NOT clear cut and dry how to implement modules in lua in the lua book and in a lot of the tutorials online.
The process is not like an import or #include, since “require” searches in the package.loaded table for anything that you have defined in module()… and anything defined WITH module() CANNOT call ANY globals (like print or another require) once this module function is called. (if you go to that critique web page you will see how to overcome this)
Confused? Well it seems like Lua and the Lua team have opportunity to improve the system and straighten things out. Just know that when you call module, the environment is wiped nearly entirely, meaning that those nifty functions like print and such will no longer be with you.
Why don’t I fix it? Because i can see why they do it the way they do, to make each module lightweight and somewhat private… but… there has to be a better way. I really think this is important ALSO because this is the essence of sharing code, and if you cant get the system of sharing code right… how will anyone be able to use it (espcially with the MIT license just begging for free sharing of code).
So the whole point of me doing more Lua stuff right now is so I can go back to working on my lighting model. I want to be able to add cameras, lights and objects to anywhere in my scene by the click of a button, even change normal maps. This makes debugging an OpenGL lighting model easier, since the debugging tools aren’t as plentiful as DirectX.
However, I have also been trying to build up enough stuff to put out a demo to test how the engine is holding up with all of these structural changes. So, I am going to come up with a very very simple demo using the new lua structure.
So Be on the lookout for some future posts on Deferred Shading. If anyone has any requests about how this post should be tailored, I’m all ears. I was thinking about doing a 3 part post. 1st on ABSOLUTE beginner intro 2nd on architecture, and 3rd on implementation.