HOG2
Graphics.h
Go to the documentation of this file.
1 //
2 // Graphics.h
3 // hog2
4 //
5 // Created by Nathan Sturtevant on 7/10/16.
6 // Copyright © 2016 NS Software. All rights reserved.
7 //
8 
9 #ifndef Graphics_h
10 #define Graphics_h
11 
12 #include <vector>
13 #include <math.h>
14 #include "GLUtil.h" // TODO: needs to be renamed, if data structures are to be more widely re-used
15 #include "FPUtil.h"
16 
17 namespace Graphics {
18 struct point;
19 enum textAlign {
23 };
24 
29 };
30 
31 
32 struct point {
33  //point(point3d p) :x(p.x), y(p.y), z(p.z) {}
34  point(float x = 0, float y = 0, float z = 0)
35  :x(x), y(y), z(z) {}
36  float x, y, z;
37 
38  bool operator==(const point &p) const
39  { return fequal(p.x, x) && fequal(p.y, y) && fequal(p.z, z); }
40  bool operator!=(const point &p) const
41  { return !(p==*this); }
42 
43  point &operator+=(const float v)
44  { x += v; y += v; z += v; return *this; }
45  point &operator-=(const float v)
46  { x -= v; y -= v; z -= v; return *this; }
47  point &operator*=(const float v)
48  { x *= v; y *= v; z *= v; return *this; }
49  point &operator/=(const float v)
50  { x /= v; y /= v; z /= v; return *this; }
51 
52  point &operator+=(const point &i)
53  { x+=i.x; y+=i.y; z+=i.z; return *this; }
54  point &operator-=(const point &i)
55  { x-=i.x; y-=i.y; z-=i.z; return *this; }
56 
57  point operator*(float i) const
58  { point p = *this; p*=i; return p; }
59  point operator+(const point &i) const
60  { point p = *this; p+=i; return p; }
61  point operator-(const point &i) const
62  { point p = *this; p-=i; return p; }
63 
64 
65  float length() const
66  { return sqrtf(x * x + y * y + z * z); }
67  float squaredLength() const
68  { return (x * x + y * y + z * z); }
69  void normalise()
70  {
71  float length = this->length();
72  if (length != 0)
73  {
74  x /= length; y /= length; z /= length;
75  }
76  else {
77  x = 0; y = 0; z = 0;
78  }
79  }
80  static float Dot(point a, point b)
81  { return a.x * b.x + a.y*b.y + a.z*b.z; }
82  // cross product
83  point operator*(const point &val) const
84  {
85  point result;
86  result.x = this->y*val.z - this->z*val.y;
87  result.y = this->z*val.x - this->x*val.z;
88  result.z = this->x*val.y - this->y*val.x;
89  result.normalise();
90  return result;
91  }
92 };
93 
94 struct rect {
95  rect() {}
96  rect(point center, float rad) : left(center.x-rad), top(center.y-rad), right(center.x+rad), bottom(center.y+rad) {}
97  rect(point tl, point br) :left(tl.x), top(tl.y), right(br.x), bottom(br.y) {}
98  rect(float l, float t, float r, float b)
99  :left(l), top(t), right(r), bottom(b) {}
100  float left, top, right, bottom;
101  rect inset(float delta)
102  { return rect(left+delta, top+delta, right-delta, bottom-delta); }
103  rect expand(float delta)
104  { return rect(left-delta, top-delta, right+delta, bottom+delta); }
105  rect &operator*=(const point &val)
106  {
107  left = left*val.x;
108  right = right*val.x;
109  top = top*val.y;
110  bottom = bottom*val.y;
111  return *this;
112  }
113  rect &operator|=(const rect &val)
114  {
115  left = std::min(left, val.left);
116  right = std::max(right, val.right);
117  top = std::min(top, val.top);
118  bottom = std::max(bottom, val.bottom);
119  return *this;
120  }
121  void lerp(const rect &val, float percentage)
122  {
123  left = left*(1-percentage)+val.left*percentage;
124  right = right*(1-percentage)+val.right*percentage;
125  top = top*(1-percentage)+val.top*percentage;
126  bottom = bottom*(1-percentage)+val.bottom*percentage;
127  }
128 };
129 
130 inline std::ostream &operator<<(std::ostream &o, const rect&r)
131 { o << r.left << ", " << r.top << ", " << r.right << ", " << r.bottom; return o; }
132 
133 inline std::ostream &operator<<(std::ostream &o, const point&r)
134 { o << "(" << r.x << ", " << r.y << ", " << r.z << ")"; return o; }
135 
136 //bool PointInRect(const point3d &p, const rect &r);
137 bool PointInRect(const point &p, const rect &r);
138 
139 /*
140  * This class represents an abstract display.
141  *
142  * All hog2 classes do their drawing to the display. This is an abstraction for a true
143  * display. Any actual display then must collect the data from this display and show it,
144  * as appropriate.
145  */
146 class Display {
147 public:
148  Display();
149  void StartFrame();
150  void EndFrame();
151  void StartBackground();
152  void EndBackground();
153  bool BackgroundNeedsRedraw() const;
154  void SetViewport(uint8_t v);
155  void SetNumViewports(uint8_t v);
156  int GetNumViewports() { return numViewports; }
157  void FrameRect(rect r, rgbColor c, float lineWidth);
158  void FillRect(rect r, rgbColor c);
159  void FrameSquare(point p, float radius, rgbColor c, float lineWidth);
160  void FillSquare(point p, float radius, rgbColor c);
161  void FrameCircle(rect r, rgbColor c, float lineWidth); // FIXME: Should be a point and a radius!
162  void FrameCircle(point r, float radius, rgbColor c, float lineWidth); // FIXME: Should be a point and a radius!
163  void FillCircle(rect r, rgbColor c);
164  void FillCircle(point p, float radius, rgbColor c);
165  void FillTriangle(point p1, point p2, point p3, rgbColor c);
166  void FrameTriangle(point p1, point p2, point p3, float lineWidth, rgbColor c);
167 
168 
169  void FillNGon(point p, float radius, int sides, float rotation, rgbColor c);
170  void FrameNGon(point p, float radius, float width, int sides, float rotation, rgbColor c);
171 
172  void DrawLine(point start, point end, float lineWidth, rgbColor c);
173  void DrawLineSegments(const std::vector<point> &points, float lineWidth, rgbColor c);
174  void FillLineSegments(const std::vector<point> &points, float lineWidth, rgbColor c);
175  void DrawArrow(point start, point end, float lineWidth, rgbColor c);
176  void DrawText(const char *text, point location, rgbColor c, float height, const char *typeface = 0);
177  void DrawText(const char *text, point location, rgbColor c, float height, textAlign align, const char *typeface = 0);
178  void DrawText(const char *text, point location, rgbColor c, float height, textAlign align, textBaseline base, const char *typeface = 0);
179 
180  struct drawInfo {
183  float width;
184  };
185  struct triangleInfo {
188  float width;
189  };
190  struct shapeInfo {
193  float radius;
194  int segments;
195  float rotate;
196  float width;
197  };
198  struct lineInfo {
201  float width;
202  bool arrow;
203  };
204  struct textInfo {
205  std::string s;
208  float size;
209  std::string typeface;
212  uint8_t viewport;
213  };
215  {
225  };
226  struct data {
227  data(shapeInfo d, tDrawClass t, uint8_t view)
228  {
229  what = t;
230  polygon = d;
231  viewport = view;
232  }
233  data(drawInfo d, tDrawClass t, uint8_t view)
234  {
235  what = t;
236  shape = d;
237  viewport = view;
238  }
239  data(lineInfo l, uint8_t view)
240  {
241  what = kLine;
242  line = l;
243  viewport = view;
244  }
245  data(triangleInfo t, tDrawClass d, uint8_t view)
246  {
247  what = d;
248  triangle = t;
249  viewport = view;
250  }
252  union {
257  };
258  uint8_t viewport;
259  };
260  struct segments {
262  float size;
263  std::vector<point> points;
264  bool fill;
265  uint8_t viewport;
266  };
267  // These are dynamic items that change from frame to frame
268  std::vector<data> drawCommands;
269  std::vector<textInfo> text;
270  std::vector<segments> lineSegments;
271  // These are static items that don't usually change from frame to frame
272  std::vector<data> backgroundDrawCommands;
273  std::vector<textInfo> backgroundText;
274  std::vector<segments> backgroundLineSegments;
275  uint64_t backgroundFrame;
276  uint64_t foregroundFrame;
277 private:
278  uint8_t viewport;
279  uint8_t numViewports;
281 };
282 
283 }
284 
285 #endif /* Graphics_h */
Graphics::Display::drawInfo::r
rect r
Definition: Graphics.h:181
Graphics::Display::DrawLine
void DrawLine(point start, point end, float lineWidth, rgbColor c)
Definition: Graphics.cpp:184
Graphics::point
Definition: Graphics.h:32
Graphics::Display::data::shape
drawInfo shape
Definition: Graphics.h:253
Graphics::point::y
float y
Definition: Graphics.h:36
rgbColor
A color; r/g/b are between 0...1.
Definition: Colors.h:17
Graphics::point::squaredLength
float squaredLength() const
Definition: Graphics.h:67
Graphics::Display::backgroundText
std::vector< textInfo > backgroundText
Definition: Graphics.h:273
Graphics::Display::textInfo::base
textBaseline base
Definition: Graphics.h:211
Graphics::Display::FillSquare
void FillSquare(point p, float radius, rgbColor c)
Definition: Graphics.cpp:92
Graphics::Display::backgroundFrame
uint64_t backgroundFrame
Definition: Graphics.h:275
min
double min(double a, double b)
Definition: FPUtil.h:35
Graphics::point::operator+
point operator+(const point &i) const
Definition: Graphics.h:59
Graphics::rect::rect
rect(point tl, point br)
Definition: Graphics.h:97
Graphics::Display::tDrawClass
tDrawClass
Definition: Graphics.h:214
Graphics::point::operator-
point operator-(const point &i) const
Definition: Graphics.h:61
Graphics::Display::SetNumViewports
void SetNumViewports(uint8_t v)
Definition: Graphics.cpp:63
Graphics::Display::textInfo::c
rgbColor c
Definition: Graphics.h:207
Graphics::Display::EndFrame
void EndFrame()
Definition: Graphics.cpp:37
Graphics::Display::FillTriangle
void FillTriangle(point p1, point p2, point p3, rgbColor c)
Definition: Graphics.cpp:146
Graphics::textBaseline
textBaseline
Definition: Graphics.h:25
Graphics::textAlignRight
@ textAlignRight
Definition: Graphics.h:22
Graphics::Display::textInfo::s
std::string s
Definition: Graphics.h:205
Graphics::Display::FillLineSegments
void FillLineSegments(const std::vector< point > &points, float lineWidth, rgbColor c)
Definition: Graphics.cpp:239
d
mcData d[]
Definition: MotionCaptureMovement.cpp:21
Graphics::Display::shapeInfo::segments
int segments
Definition: Graphics.h:194
Graphics::Display::data::data
data(shapeInfo d, tDrawClass t, uint8_t view)
Definition: Graphics.h:227
Graphics::Display::kFrameRectangle
@ kFrameRectangle
Definition: Graphics.h:217
Graphics::Display::data::polygon
shapeInfo polygon
Definition: Graphics.h:254
Graphics::Display::backgroundDrawCommands
std::vector< data > backgroundDrawCommands
Definition: Graphics.h:272
Graphics::Display::textInfo::size
float size
Definition: Graphics.h:208
Graphics::Display::kLine
@ kLine
Definition: Graphics.h:224
Graphics::rect
Definition: Graphics.h:94
Graphics::Display::data::line
lineInfo line
Definition: Graphics.h:255
Graphics::Display::shapeInfo
Definition: Graphics.h:190
Graphics::Display::triangleInfo
Definition: Graphics.h:185
Graphics::Display::segments
Definition: Graphics.h:260
Graphics::Display::kFillRectangle
@ kFillRectangle
Definition: Graphics.h:216
Graphics::Display::textInfo
Definition: Graphics.h:204
FPUtil.h
Graphics::point::point
point(float x=0, float y=0, float z=0)
Definition: Graphics.h:34
width
int width
Definition: SFML_HOG.cpp:54
rotation
float rotation[3][3]
Definition: RC.cpp:21
Graphics::Display::StartBackground
void StartBackground()
Definition: Graphics.cpp:41
Graphics::point::normalise
void normalise()
Definition: Graphics.h:69
Graphics::point::operator/=
point & operator/=(const float v)
Definition: Graphics.h:49
Graphics::Display::lineInfo::start
point start
Definition: Graphics.h:199
Graphics::Display::textInfo::viewport
uint8_t viewport
Definition: Graphics.h:212
Graphics::Display::EndBackground
void EndBackground()
Definition: Graphics.cpp:53
Graphics::Display::Display
Display()
Definition: Graphics.cpp:22
Graphics::Display::SetViewport
void SetViewport(uint8_t v)
Definition: Graphics.cpp:68
Graphics::point::operator-=
point & operator-=(const point &i)
Definition: Graphics.h:54
Graphics::Display::FillNGon
void FillNGon(point p, float radius, int sides, float rotation, rgbColor c)
Definition: Graphics.cpp:164
Graphics::point::operator*
point operator*(float i) const
Definition: Graphics.h:57
Graphics::Display::triangleInfo::width
float width
Definition: Graphics.h:188
Graphics::Display::StartFrame
void StartFrame()
Definition: Graphics.cpp:29
Graphics::Display::segments::viewport
uint8_t viewport
Definition: Graphics.h:265
Graphics::Display::data::data
data(drawInfo d, tDrawClass t, uint8_t view)
Definition: Graphics.h:233
Graphics::Display::kFillTriangle
@ kFillTriangle
Definition: Graphics.h:218
Graphics::Display::numViewports
uint8_t numViewports
Definition: Graphics.h:279
Graphics::Display::kFrameOval
@ kFrameOval
Definition: Graphics.h:221
Graphics::Display::textInfo::align
textAlign align
Definition: Graphics.h:210
Graphics::Display::drawInfo
Definition: Graphics.h:180
Graphics::Display::drawInfo::width
float width
Definition: Graphics.h:183
Graphics::Display::lineInfo::width
float width
Definition: Graphics.h:201
Graphics::rect::operator*=
rect & operator*=(const point &val)
Definition: Graphics.h:105
viewport
Definition: Common.h:56
Graphics::point::operator!=
bool operator!=(const point &p) const
Definition: Graphics.h:40
Graphics::textBaselineMiddle
@ textBaselineMiddle
Definition: Graphics.h:27
Graphics::rect::expand
rect expand(float delta)
Definition: Graphics.h:103
Graphics::Display::FrameNGon
void FrameNGon(point p, float radius, float width, int sides, float rotation, rgbColor c)
Definition: Graphics.cpp:173
Graphics::PointInRect
bool PointInRect(const point &p, const rect &r)
Definition: Graphics.cpp:17
Graphics::Display::kFrameNGon
@ kFrameNGon
Definition: Graphics.h:223
Graphics::Display::BackgroundNeedsRedraw
bool BackgroundNeedsRedraw() const
Definition: Graphics.cpp:58
Graphics::point::Dot
static float Dot(point a, point b)
Definition: Graphics.h:80
Graphics::Display::lineInfo::arrow
bool arrow
Definition: Graphics.h:202
Graphics::point::operator-=
point & operator-=(const float v)
Definition: Graphics.h:45
Graphics::rect::top
float top
Definition: Graphics.h:100
Graphics::Display::drawingBackground
bool drawingBackground
Definition: Graphics.h:280
Graphics::point::operator*
point operator*(const point &val) const
Definition: Graphics.h:83
Graphics::Display::drawCommands
std::vector< data > drawCommands
Definition: Graphics.h:268
Graphics::Display::lineInfo::c
rgbColor c
Definition: Graphics.h:200
Graphics::Display::kFillOval
@ kFillOval
Definition: Graphics.h:220
Graphics::Display::lineInfo
Definition: Graphics.h:198
Graphics::Display::data::what
tDrawClass what
Definition: Graphics.h:251
Graphics::Display::triangleInfo::p3
point p3
Definition: Graphics.h:186
Graphics::Display::shapeInfo::radius
float radius
Definition: Graphics.h:193
Graphics::Display
Definition: Graphics.h:146
Graphics::point::operator*=
point & operator*=(const float v)
Definition: Graphics.h:47
Graphics::point::operator+=
point & operator+=(const float v)
Definition: Graphics.h:43
Graphics::Display::FrameSquare
void FrameSquare(point p, float radius, rgbColor c, float lineWidth)
Definition: Graphics.cpp:82
Graphics
Definition: Graphics.cpp:11
Graphics::Display::drawInfo::c
rgbColor c
Definition: Graphics.h:182
Graphics::rect::right
float right
Definition: Graphics.h:100
Graphics::Display::kFillNGon
@ kFillNGon
Definition: Graphics.h:222
Graphics::rect::rect
rect()
Definition: Graphics.h:95
height
int height
Definition: SFML_HOG.cpp:54
Graphics::Display::shapeInfo::rotate
float rotate
Definition: Graphics.h:195
Graphics::Display::foregroundFrame
uint64_t foregroundFrame
Definition: Graphics.h:276
Graphics::Display::FrameCircle
void FrameCircle(rect r, rgbColor c, float lineWidth)
Definition: Graphics.cpp:110
max
#define max(a, b)
Definition: MinimalSectorAbstraction.cpp:40
Graphics::Display::triangleInfo::p1
point p1
Definition: Graphics.h:186
GLUtil.h
Graphics::textAlignCenter
@ textAlignCenter
Definition: Graphics.h:20
Graphics::Display::shapeInfo::c
rgbColor c
Definition: Graphics.h:192
Graphics::Display::text
std::vector< textInfo > text
Definition: Graphics.h:269
Graphics::Display::DrawArrow
void DrawArrow(point start, point end, float lineWidth, rgbColor c)
Definition: Graphics.cpp:193
Graphics::point::length
float length() const
Definition: Graphics.h:65
Graphics::textBaselineTop
@ textBaselineTop
Definition: Graphics.h:26
Graphics::Display::shapeInfo::center
point center
Definition: Graphics.h:191
Graphics::Display::data::data
data(lineInfo l, uint8_t view)
Definition: Graphics.h:239
Graphics::Display::textInfo::typeface
std::string typeface
Definition: Graphics.h:209
Graphics::Display::shapeInfo::width
float width
Definition: Graphics.h:196
Graphics::point::x
float x
Definition: Graphics.h:36
Graphics::Display::FillCircle
void FillCircle(rect r, rgbColor c)
Definition: Graphics.cpp:128
Graphics::Display::FrameRect
void FrameRect(rect r, rgbColor c, float lineWidth)
Definition: Graphics.cpp:73
Graphics::point::z
float z
Definition: Graphics.h:36
Graphics::textAlign
textAlign
Definition: Graphics.h:19
Graphics::rect::rect
rect(point center, float rad)
Definition: Graphics.h:96
Graphics::Display::segments::c
rgbColor c
Definition: Graphics.h:261
Graphics::Display::FrameTriangle
void FrameTriangle(point p1, point p2, point p3, float lineWidth, rgbColor c)
Definition: Graphics.cpp:155
Graphics::Display::lineSegments
std::vector< segments > lineSegments
Definition: Graphics.h:270
Graphics::rect::inset
rect inset(float delta)
Definition: Graphics.h:101
Graphics::textAlignLeft
@ textAlignLeft
Definition: Graphics.h:21
Graphics::Display::triangleInfo::c
rgbColor c
Definition: Graphics.h:187
Graphics::operator<<
std::ostream & operator<<(std::ostream &o, const rect &r)
Definition: Graphics.h:130
Graphics::Display::GetNumViewports
int GetNumViewports()
Definition: Graphics.h:156
Graphics::rect::left
float left
Definition: Graphics.h:100
Graphics::Display::DrawLineSegments
void DrawLineSegments(const std::vector< point > &points, float lineWidth, rgbColor c)
Definition: Graphics.cpp:230
Graphics::rect::operator|=
rect & operator|=(const rect &val)
Definition: Graphics.h:113
Graphics::Display::data::viewport
uint8_t viewport
Definition: Graphics.h:258
Graphics::textBaselineBottom
@ textBaselineBottom
Definition: Graphics.h:28
Graphics::Display::lineInfo::end
point end
Definition: Graphics.h:199
Graphics::rect::rect
rect(float l, float t, float r, float b)
Definition: Graphics.h:98
Graphics::point::operator+=
point & operator+=(const point &i)
Definition: Graphics.h:52
Graphics::Display::data::triangle
triangleInfo triangle
Definition: Graphics.h:256
Graphics::Display::kFrameTriangle
@ kFrameTriangle
Definition: Graphics.h:219
Graphics::Display::triangleInfo::p2
point p2
Definition: Graphics.h:186
Graphics::Display::DrawText
void DrawText(const char *text, point location, rgbColor c, float height, const char *typeface=0)
Definition: Graphics.cpp:221
Graphics::Display::data::data
data(triangleInfo t, tDrawClass d, uint8_t view)
Definition: Graphics.h:245
Graphics::Display::segments::size
float size
Definition: Graphics.h:262
Graphics::Display::viewport
uint8_t viewport
Definition: Graphics.h:278
Graphics::rect::lerp
void lerp(const rect &val, float percentage)
Definition: Graphics.h:121
Graphics::Display::segments::points
std::vector< point > points
Definition: Graphics.h:263
Graphics::rect::bottom
float bottom
Definition: Graphics.h:100
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
Graphics::Display::FillRect
void FillRect(rect r, rgbColor c)
Definition: Graphics.cpp:101
Graphics::Display::backgroundLineSegments
std::vector< segments > backgroundLineSegments
Definition: Graphics.h:274
Graphics::Display::textInfo::loc
point loc
Definition: Graphics.h:206
Graphics::Display::segments::fill
bool fill
Definition: Graphics.h:264
Graphics::point::operator==
bool operator==(const point &p) const
Definition: Graphics.h:38
Graphics::Display::data
Definition: Graphics.h:226