Textogl  1.1.2
OpenGL Text renderer
types.hpp
Go to the documentation of this file.
1 
4 // Copyright 2022 Matthew Chandler
5 
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 
24 #ifndef TYPES_HPP
25 #define TYPES_HPP
26 
27 #include <utility>
28 #include <cstdlib>
29 
30 #ifdef USE_GLM
31 #include <glm/glm.hpp>
32 #endif
33 
34 
36 
38 namespace textogl
39 {
40  namespace detail
41  {
42  // fallback types to use if GLM is not available
43 
45  template<typename T>
46  struct Vec2
47  {
48  union {T x = {}, r;};
49  union {T y = {}, g;};
50 
51  Vec2() = default;
52  Vec2(T x, T y): x(x), y(y) {}
53 
55 
58  T & operator[](std::size_t i) { return (&x)[i]; }
59  const T & operator[](std::size_t i) const { return (&x)[i]; }
61  };
62 
64  template<typename T>
65  struct Vec4
66  {
67  union {T x = {}, r;};
68  union {T y = {}, g;};
69  union {T z = {}, b;};
70  union {T w = {}, a;};
71 
72  Vec4() = default;
73  Vec4(T x, T y, T z, T w): x(x), y(y), z(z), w(w) {}
74 
76 
79  T & operator[](std::size_t i) { return (&x)[i]; }
80  const T & operator[](std::size_t i) const { return (&x)[i]; }
82  };
83 
85  template <typename T>
86  class Mat4
87  {
88  private:
89  Vec4<T> data_[4]{{}, {}, {}, {}};
90  public:
91 
92  Mat4() = default;
93 
94  Mat4(Vec4<T> & x, const Vec4<T> & y, const Vec4<T> & z, const Vec4<T> &w)
95  {
96  data_[0] = x; data_[1] = y; data_[2] = z; data_[3] = w;
97  }
98 
99  Mat4(T xx, T xy, T xz, T xw,
100  T yx, T yy, T yz, T yw,
101  T zx, T zy, T zz, T zw,
102  T wx, T wy, T wz, T ww)
103  {
104  data_[0][0] = xx; data_[0][1] = xy; data_[0][2] = xz; data_[0][3] = xw;
105  data_[1][0] = yx; data_[1][1] = yy; data_[1][2] = yz; data_[1][3] = yw;
106  data_[2][0] = zx; data_[2][1] = zy; data_[2][2] = zz; data_[2][3] = zw;
107  data_[3][0] = wx; data_[3][1] = wy; data_[3][2] = wz; data_[3][3] = ww;
108  }
109 
110  Mat4(T diagonal): Mat4(diagonal, 0, 0, 0,
111  0, diagonal, 0, 0,
112  0, 0, diagonal, 0,
113  0, 0, 0, diagonal)
114  {}
115 
117 
120  Vec4<T> & operator[](std::size_t i) { return data_[i]; }
121  const Vec4<T> & operator[](std::size_t i) const { return data_[i]; }
123 
124 #ifndef USE_GLM
126  template <typename U>
127  Mat4<decltype(std::declval<T>() * std::declval<U>())> operator*(const Mat4<U> & b) const
128  {
129  Mat4<T> out;
130  for(int col = 0; col < 4; ++col)
131  {
132  for(int row = 0; row < 4; ++row)
133  {
134  out[col][row] = 0;
135  for(int k = 0; k < 4; ++k)
136  out[col][row] += data_[k][row] * b[col][k];
137  }
138  }
139  return out;
140  }
141 
143  template <typename U>
144  Mat4<decltype(std::declval<T>() * std::declval<U>())> & operator*=(const Mat4<U> & b)
145  {
146  return *this = *this * b;
147  }
148 #endif
149  };
150 
151  // for template alias specialization
152  template<typename T> struct Vec2_t { using type = Vec2<T>; };
153  template<typename T> struct Vec4_t { using type = Vec4<T>; };
154  template<typename T> struct Mat4_t { using type = Mat4<T>; };
155 
156 #ifdef USE_GLM
157  // specialize to glm types
158  template<> struct Vec2_t<float> { using type = glm::vec2; };
159  template<> struct Vec2_t<double> { using type = glm::dvec2; };
160  template<> struct Vec2_t<int> { using type = glm::ivec2; };
161  template<> struct Vec2_t<unsigned int> { using type = glm::uvec2; };
162 
163  template<> struct Vec4_t<float> { using type = glm::vec4; };
164  template<> struct Vec4_t<double> { using type = glm::dvec4; };
165  template<> struct Vec4_t<int> { using type = glm::ivec4; };
166  template<> struct Vec4_t<unsigned int> { using type = glm::uvec4; };
167 
168  template<> struct Mat4_t<float> { using type = glm::mat4; };
169  template<> struct Mat4_t<double> { using type = glm::dmat4; };
170 #endif
171  }
172 
174 
176  template<typename T = float> using Vec2 = typename detail::Vec2_t<T>::type;
177 
179 
181  template<typename T = float> using Vec4 = typename detail::Vec4_t<T>::type;
182 
184 
186  template<typename T = float> using Mat4 = typename detail::Mat4_t<T>::type;
187 
189 
192 
193 }
194 
195 #endif // TYPES_HPP
OpenGL Font rendering types.
Definition: font.hpp:35
Vec4< float > Color
Color vector
Definition: types.hpp:191
typename detail::Mat4_t< T >::type Mat4
4D Matrix
Definition: types.hpp:186
typename detail::Vec4_t< T >::type Vec4
4D Vector
Definition: types.hpp:181
typename detail::Vec2_t< T >::type Vec2
2D Vector
Definition: types.hpp:176