官术网_书友最值得收藏!

Exploring more vectors

At some point later on in this book, you will need to utilize two- and four-component vectors as well. The two- and four-component vectors don't need any mathematical functions defined as they will be used exclusively as containers used to pass data to the GPU.

Unlike the three-component vector you have implemented, the two- and four-component vectors need to exist as both integer and floating point vectors. To avoid duplicating code, both structures will be implemented using a template:

  1. Create a new file, vec2.h, and add the definition of the vec2 struct. All the vec2 constructors are inline; there is no need for a cpp file. The TVec2 struct is templated and typedef is used to declare vec2 and ivec2:

    template<typename T>

    struct TVec2 {

        union {

            struct {

                T x;

                T y;

            };

            T v[2];

        };

        inline TVec2() : x(T(0)), y(T(0)) { }

        inline TVec2(T _x, T _y) :

            x(_x), y(_y) { }

        inline TVec2(T* fv) :

            x(fv[0]), y(fv[1]) { }

    };

    typedef TVec2<float> vec2;

    typedef TVec2<int> ivec2;

  2. Similarly, create a vec4.h file, which will hold the vec4 structure:

    template<typename T>

    struct TVec4 {

        union {

            struct {

                T x;

                T y;

                T z;

                T w;

            };

            T v[4];

        };

        inline TVec4<T>(): x((T)0),y((T)0),z((T)0),w((T)0){}

        inline TVec4<T>(T _x, T _y, T _z, T _w) :

            x(_x), y(_y), z(_z), w(_w) { }

        inline TVec4<T>(T* fv) :

            x(fv[0]), y(fv[ ]), z(fv[2]), w(fv[3]) { }

    };

    typedef TVec4<float> vec4;

    typedef TVec4<int> ivec4;

    typedef TVec4<unsigned int> uivec4;

The declaration of the vec2, ivec2, vec4, and ivec4 structs are all very similar to the declaration of the vec3 struct. All these structures can be accessed using component subscripts or as a pointer to a linear array of memory. They all have similar constructors, as well.

主站蜘蛛池模板: 沾化县| 沙雅县| 宣汉县| 鄂伦春自治旗| 仁寿县| 昭觉县| 邓州市| 新巴尔虎右旗| 江城| 旌德县| 宜城市| 奉化市| 庄浪县| 湟源县| 石狮市| 鄯善县| 广宁县| 葵青区| 南安市| 南康市| 广东省| 武川县| 乐昌市| 洞口县| 伽师县| 沁水县| 磐石市| 阿拉善右旗| 吉木乃县| 肥东县| 柳江县| 玛沁县| 修文县| 金堂县| 蕲春县| 武隆县| 中江县| 凤庆县| 石门县| 隆回县| 盖州市|