- 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.
- 演進(jìn)式架構(gòu)(原書(shū)第2版)
- Advanced Splunk
- JavaScript+jQuery開(kāi)發(fā)實(shí)戰(zhàn)
- Vue.js 3.0源碼解析(微課視頻版)
- UML+OOPC嵌入式C語(yǔ)言開(kāi)發(fā)精講
- Magento 1.8 Development Cookbook
- Easy Web Development with WaveMaker
- HTML5+CSS3網(wǎng)頁(yè)設(shè)計(jì)
- Python深度學(xué)習(xí)原理、算法與案例
- ElasticSearch Cookbook(Second Edition)
- Spring Boot+MVC實(shí)戰(zhàn)指南
- Flowable流程引擎實(shí)戰(zhàn)
- JBoss:Developer's Guide
- Magento 2 Beginners Guide
- SignalR:Real-time Application Development(Second Edition)