- Hands-On C++ Game Animation Programming
- Gabor Szauer
- 338字
- 2021-06-30 14:46:00
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.
- Beginning Java Data Structures and Algorithms
- Building a RESTful Web Service with Spring
- Mastering Scientific Computing with R
- JavaScript程序設(shè)計(jì)(第2版)
- Mastering Elixir
- 基于GPU加速的計(jì)算機(jī)視覺編程:使用OpenCV和CUDA實(shí)時(shí)處理復(fù)雜圖像數(shù)據(jù)
- 從零開始學(xué)算法:基于Python
- Python數(shù)據(jù)可視化之matplotlib實(shí)踐
- 讀故事學(xué)編程:Python王國(guó)歷險(xiǎn)記
- jQuery Essentials
- Learning Puppet
- HTML5+jQuery Mobile移動(dòng)應(yīng)用開發(fā)
- Flink原理深入與編程實(shí)戰(zhàn):Scala+Java(微課視頻版)
- C語言從入門到精通(第4版)
- Visual C++實(shí)用教程