- Hands-On C++ Game Animation Programming
- Gabor Szauer
- 289字
- 2021-06-30 14:45:49
Creating a vector
Vectors will be implemented as structures, not classes. The vector struct will contain an anonymous union that allows the vector's components to be accessed as an array or as individual elements.
To declare the vec3 structure and the function headers, create a new file, vec3.h. Declare the new vec3 structure in this file. The vec3 struct needs three constructors—a default constructor, one that takes each component as an element, and one that takes a pointer to a float array:
#ifndef _H_VEC3_
#define _H_VEC3_
struct vec3 {
union {
struct {
float x;
float y;
float z;
};
float v[3];
};
inline vec3() : x(0.0f), y(0.0f), z(0.0f) { }
inline vec3(float _x, float _y, float _z) :
x(_x), y(_y), z(_z) { }
inline vec3(float *fv) :
x(fv[0]), y(fv[1]), z(fv[2]) { }
};
#endif
The anonymous union in the vec3 struct allows data to be accessed using .x, .y, and .z notation, or as a contiguous array using .v. Before moving on to implementing functions that work on the vec3 struct, you need to consider comparing floating point numbers and whether or not to use an epsilon value.
- Mastering Visual Studio 2017
- 深入淺出Spring Boot 2.x
- Effective Python Penetration Testing
- 概率成形編碼調制技術理論及應用
- QTP自動化測試進階
- Symfony2 Essentials
- Node.js Design Patterns
- Python算法指南:程序員經典算法分析與實現
- C和C++游戲趣味編程
- C語言從入門到精通
- Getting Started with Eclipse Juno
- Python全棧數據工程師養成攻略(視頻講解版)
- C++ Application Development with Code:Blocks
- Elastix Unified Communications Server Cookbook
- Learning Google Apps Script