Swizzling

Posted: October 29, 2010 in General

Since using high level shader languages like DirectX HLSL, I’ve become quite the fan of vector swizzling, and wanted to use it with my maths libray. My solution was to write a quick console app to write the code for me, and then just include those files in my vector classes. Probably not the most ingenious solution there is, but it worked well for me. Using this technique does produce a fair number of new member functions though :

Vec2 = 2^2 + 2^3 + 2^4 = 28
Vec3 = 3^2 + 3^3 + 3^4 = 117
Vec4 = 4^2 + 4^3 + 4^4 = 336

But now I can write code like :

fsVec2 a = fsVec2( 1.0f, 2.0f );
fsVec4 b = a.xxxx();
fsVec3 c = b.xyz() + a.xyx() + b.www();

I chose not to add MACROS to enable the “.xxxx” syntax, as opposed to the “.xxxx()” as I don’t think the fewer keystrokes justify polluting the global namepspace, specially as I like to use names like “xy” and “xx” for local variables.

Below is the source for doing this.

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