fredrik.eriksson

Coffee and a keyboard

CG vector in C++

I have always liked how the vector data types in CG (C for Graphics) works. And I have been looking for something similar for C++ but never found anything. So I decided to create a C++ class with similar functionality.

thebc::vector

Where N is the number of elements (with the restriction of 1 >= N <= 4) and T is the type that is being stored. With this generic class I am able to create eight of the vector data types provided by CG:

  • float1
  • float2
  • float3
  • float4
  • double1
  • double2
  • double3
  • double4

 

Support for swizzling

Swizzling is a way of rearranging components of vector values and constructing shorter or longer vectors:

float4 v1(4.0f, -2.0f, 5.0f, 3.0f);

float2 v2 = v1.yx();    // v2 == [-2.0, 4.0]
float1 scalar = v1.w;   // scalar = [3.0]
float3 v3 = s.xxx();    // [3.0, 3.0, 3.0]

The vector elements can also be accessed with standard array indexing syntax

Project link