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

Creating quaternions

Quaternions are used to encode rotation data. In code, quaternions will have four components. They resemble vec4 in that they have an x, y, z, and w component.

As with vec4, the w component comes last.

The quat structure should have two constructors. The default constructor creates an identity quaternion, (0, 0, 0, 1). The (0, 0, 0, 1) identity quaternion is like 1. Any number multiplied by 1 remains the same. Similarly, any quaternion multiplied by the identity quaternion remains the same:

Create a new file, quat.h, to declare the quaternion structure. The quat structure is going to be used throughout the rest of this book to represent rotations:

#ifndef _H_QUAT_

#define _H_QUAT_

#include "vec3.h"

#include "mat4.h"

struct quat {

   union {

       struct {

           float x;

           float y;

           float z;

           float w;

       };

       struct {

           vec3 vector;

           float scalar;

       };

       float v[4];

   };

   inline quat() :

       x(0), y(0), z(0), w(1) { }

   inline quat(float _x, float _y, float _z, float _w)

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

};

#endif

The anonymous union inside the quat structure will allow you to access the data inside a quaternion through X, Y, Z, and W subscript notation, as a vector and scalar pair, or as an array of floating-point values.

Next, you're going to learn how to start creating quaternions.

主站蜘蛛池模板: 马关县| 会东县| 祥云县| 九寨沟县| 横峰县| 九龙城区| 登封市| 巴林右旗| 东莞市| 汪清县| 屯昌县| 怀仁县| 仁化县| 准格尔旗| 台州市| 榆林市| 镇赉县| 荥阳市| 广水市| 清水县| 高邑县| 冕宁县| 安福县| 西贡区| 岳普湖县| 临城县| 伽师县| 呼图壁县| 黄冈市| 南宁市| 阳曲县| 东海县| 蒙山县| 四平市| 福清市| 昌图县| 哈巴河县| 平塘县| 光泽县| 玉屏| 双流县|