Banate Core Win32 glsl

As I have developed BanateCAD, I have gone back and forth in trying out various coding standards, and framework styles.  One of the most core components are simple data types such as how to represent a point, or vector, or color, and what have you.  Now that I have played around quite a lot, I am consolidating onto some core structures that make the most sense.

I have just added a couple to the core.  They are named in the style of GLSL, since I am definitely using OpenGL in general.  So, I’ve added: vec, ivec, bvec, uvec structures to the little library.  Since I have taken LuaJIT and its FFI as core dependencies, I can easily use FFI to define these structures using their native data types.  So, for example, vec3 looks like this:

typedef struct _vec3 {  float d1, d2, d3; } vec3;

Yes, using ‘float’ type, and not just the Lua “number” type.  This is good for a couple of reasons.  One is, floats simply take up less space than doubles do.  Perhaps less precision, but it’s good enough for the graphics work that I’m doing.  Another benefit is that many of the OpenGL calls that I make utilize the float value, so, by expressing as native data types, there will be less marshalling going on.  That’s a great performance boost, as marshalling often times takes up the lion’s share of time when doing interop.

One of the coolest things I find with the FFI is that I can essentially treat the native structures as if they were Lua tables.  that is, I can add metamethods to them.  What this means is that although the vec3 structure has fields named ‘d1’, ‘d2’, ‘d3’, I can actually do the same swizzling trick that glsl does, and access using more common names, such as ‘x’, ‘y’, ‘z’, and the like.  So, the following code works:

v1 = vec3(10, 20, 30)
print(v1.r, v1.g, v1.b)
print(v1.x, v1.y, v1.z)

Also, you can add the arithmetic metamethods as well, so:

v2 = vec3(5, 10, 15)
v3 = v1 + v2
print(v3)
==> vec3(15, 30, 45)

I’ve added, __add, __sub, __unm, __tostring.  The __mul and __div operators will be added as well.  With all these operators defined, it makes the structures feel as natural to use as the built in number type of Lua.

I find this to be a very interesting style of programming.  It nicely straddles the two worlds of Lua and C programming.  You can declare compatible strucutes in FFI, and then write their actual code in Lua itself, which allows me to spend more time in Lua, without having to compile any C specific interop code, which I really hate doing.  So, my time is focused on improving the Lua libraries, and not just becoming an interop to other libraries expert.

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s