Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On C++ Game Animation Programming

You're reading from   Hands-On C++ Game Animation Programming Learn modern animation techniques from theory to implementation with C++ and OpenGL

Arrow left icon
Product type Paperback
Published in Jun 2020
Publisher Packt
ISBN-13 9781800208087
Length 368 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Gabor Szauer Gabor Szauer
Author Profile Icon Gabor Szauer
Gabor Szauer
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Chapter 1: Creating a Game Window 2. Chapter 2: Implementing Vectors FREE CHAPTER 3. Chapter 3: Implementing Matrices 4. Chapter 4: Implementing Quaternions 5. Chapter 5: Implementing Transforms 6. Chapter 6: Building an Abstract Renderer 7. Chapter 7: Exploring the glTF File Format 8. Chapter 8: Creating Curves, Frames, and Tracks 9. Chapter 9: Implementing Animation Clips 10. Chapter 10: Mesh Skinning 11. Chapter 11: Optimizing the Animation Pipeline 12. Chapter 12: Blending between Animations 13. Chapter 13: Implementing Inverse Kinematics 14. Chapter 14: Using Dual Quaternions for Skinning 15. Chapter 15: Rendering Instanced Crowds 16. Other Books You May Enjoy

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.

You have been reading a chapter from
Hands-On C++ Game Animation Programming
Published in: Jun 2020
Publisher: Packt
ISBN-13: 9781800208087
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image