1 /* bzflag
2  * Copyright (c) 1993-2021 Tim Riker
3  *
4  * This package is free software;  you can redistribute it and/or
5  * modify it under the terms of the license found in the file
6  * named COPYING that should have accompanied this file.
7  *
8  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */
12 
13 #ifndef _TRANSFORM_H_
14 #define _TRANSFORM_H_
15 
16 
17 #include <string>
18 #include <vector>
19 #include <iostream>
20 
21 enum TransformType
22 {
23     ShiftTransform = 0,
24     ScaleTransform = 1,
25     ShearTransform = 2,
26     SpinTransform  = 3,
27     IndexTransform = 4,
28     LastTransform
29 };
30 
31 typedef struct
32 {
33     TransformType type;
34     int index;
35     float data[4];
36 } TransformData;
37 
38 class MeshTransform
39 {
40 public:
41     MeshTransform();
42     MeshTransform(const MeshTransform&);
43     ~MeshTransform();
44 
45     MeshTransform& operator=(const MeshTransform& transform);
46     void append(const MeshTransform& transform);
47     void prepend(const MeshTransform& transform);
48 
49     bool setName(const std::string& name);
50     void addShift(const float shift[3]);
51     void addScale(const float scale[3]);
52     void addShear(const float shear[3]);
53     void addSpin(const float degrees, const float normal[3]);
54     void addReference(int transform);
55 
isEmpty()56     bool isEmpty() const
57     {
58         return (transforms.size() <= 0);
59     }
60 
61     bool isValid();
62     void finalize();
63 
64     const std::string& getName() const;
65 
66     int packSize() const;
67     void* pack(void*) const;
68     const void* unpack(const void*);
69 
70     void print(std::ostream& out, const std::string& indent) const;
71     void printTransforms(std::ostream& out, const std::string& indent) const;
72 
73 private:
74 
75     std::string name;
76     std::vector<TransformData> transforms;
77 
78 public:
79     class Tool
80     {
81     public:
82         Tool(const MeshTransform& transform);
83         ~Tool();
84 
85         bool isInverted() const;
86         bool isSkewed() const; // scaled or sheared
87         void modifyVertex(float vertex[3]) const;
88         void modifyNormal(float normal[3]) const;
89         void modifyOldStyle(float pos[3], float size[3],
90                             float& angle, bool& flipz) const;
91         const float* getMatrix() const;
92 
93     private:
94         void processTransforms(const std::vector<TransformData>& tforms);
95 
96         bool empty;
97         bool inverted;
98         bool skewed;
99         float vertexMatrix[4][4];
100         float normalMatrix[3][3];
101     };
102 
103     friend class MeshTransform::Tool;
104 };
105 
isInverted()106 inline bool MeshTransform::Tool::isInverted() const
107 {
108     return inverted;
109 }
110 
isSkewed()111 inline bool MeshTransform::Tool::isSkewed() const
112 {
113     return skewed;
114 }
115 
getMatrix()116 inline const float* MeshTransform::Tool::getMatrix() const
117 {
118     return (const float*)vertexMatrix;
119 }
120 
121 
122 class MeshTransformManager
123 {
124 public:
125     MeshTransformManager();
126     ~MeshTransformManager();
127     void update();
128     void clear();
129     int addTransform(MeshTransform* driver);
130     int findTransform(const std::string& name) const;
131     const MeshTransform* getTransform(int id) const;
132 
133     int packSize() const;
134     void* pack(void*) const;
135     const void* unpack(const void*);
136 
137     void print(std::ostream& out, const std::string& indent) const;
138 
139 private:
140     std::vector<MeshTransform*> transforms;
141 };
142 
getTransform(int id)143 inline const MeshTransform* MeshTransformManager::getTransform(int id) const
144 {
145     if ((id >= 0) && (id < (int)transforms.size()))
146         return transforms[id];
147     else
148         return NULL;
149 }
150 
151 
152 extern MeshTransformManager TRANSFORMMGR;
153 
154 
155 #endif //_TRANSFORM_H_
156 
157 // Local Variables: ***
158 // mode: C++ ***
159 // tab-width: 4 ***
160 // c-basic-offset: 4 ***
161 // indent-tabs-mode: nil ***
162 // End: ***
163 // ex: shiftwidth=4 tabstop=4
164