Pioneer
Drawables.h
Go to the documentation of this file.
1 // Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _DRAWABLES_H
5 #define _DRAWABLES_H
6 
7 #include "graphics/Material.h"
8 #include "graphics/VertexArray.h"
10 #include "libs.h"
11 
12 namespace Graphics {
13  class Renderer;
14  class RenderState;
15 
16  namespace Drawables {
17 
18  // A thing that can draw itself using renderer
19  // (circles, disks, polylines etc)
20  //------------------------------------------------------------
21 
22  class Circle {
23  public:
24  Circle(Renderer *renderer, const float radius, const Color &c, RenderState *state);
25  Circle(Renderer *renderer, const float radius, const float x, const float y, const float z, const Color &c, RenderState *state);
26  Circle(Renderer *renderer, const float radius, const vector3f &center, const Color &c, RenderState *state);
27  void Draw(Renderer *renderer);
28 
29  private:
30  void SetupVertexBuffer(const Graphics::VertexArray &, Graphics::Renderer *);
31  RefCountedPtr<VertexBuffer> m_vertexBuffer;
32  RefCountedPtr<Material> m_material;
33  Color m_color;
34  Graphics::RenderState *m_renderState;
35  };
36  //------------------------------------------------------------
37 
38  // Two-dimensional filled circle
39  class Disk {
40  public:
41  Disk(Graphics::Renderer *r, Graphics::RenderState *, const Color &c, float radius);
42  Disk(Graphics::Renderer *r, RefCountedPtr<Material>, Graphics::RenderState *, const int edges = 72, const float radius = 1.0f);
43  void Draw(Graphics::Renderer *r);
44 
45  void SetColor(const Color &);
46 
47  private:
48  void SetupVertexBuffer(const Graphics::VertexArray &, Graphics::Renderer *);
49  std::unique_ptr<VertexBuffer> m_vertexBuffer;
50  RefCountedPtr<Material> m_material;
51  Graphics::RenderState *m_renderState;
52  };
53  //------------------------------------------------------------
54 
55  // A three dimensional line between two points
56  class Line3D {
57  public:
58  Line3D();
59  Line3D(const Line3D &b); // this needs an explicit copy constructor due to the std::unique_ptr below
60  ~Line3D() {}
61  void SetStart(const vector3f &);
62  void SetEnd(const vector3f &);
63  void SetColor(const Color &);
64  void Draw(Renderer *, RenderState *);
65 
66  private:
67  void CreateVertexBuffer(Graphics::Renderer *r, const Uint32 size);
68  void Dirty();
69 
70  bool m_refreshVertexBuffer;
71  float m_width;
72  RefCountedPtr<Material> m_material;
73  RefCountedPtr<VertexBuffer> m_vertexBuffer;
74  std::unique_ptr<Graphics::VertexArray> m_va;
75  };
76  //------------------------------------------------------------
77 
78  // Three dimensional line segments between two points
79  class Lines {
80  public:
81  Lines();
82  void SetData(const Uint32 vertCount, const vector3f *vertices, const Color &color);
83  void SetData(const Uint32 vertCount, const vector3f *vertices, const Color *colors);
85 
86  private:
87  void CreateVertexBuffer(Graphics::Renderer *r, const Uint32 size);
88 
89  bool m_refreshVertexBuffer;
90  RefCountedPtr<Material> m_material;
91  RefCountedPtr<VertexBuffer> m_vertexBuffer;
92  std::unique_ptr<VertexArray> m_va;
93  };
94  //------------------------------------------------------------
95 
96  // Screen aligned quad / billboard / pointsprite
97  class PointSprites {
98  public:
99  PointSprites();
100  void SetData(const int count, const vector3f *positions, const Color *colours, const float *sizes, Graphics::Material *pMaterial);
101  void Draw(Renderer *, RenderState *);
102 
103  private:
104  void CreateVertexBuffer(Graphics::Renderer *r, const Uint32 size);
105 
106  bool m_refreshVertexBuffer;
108  RefCountedPtr<VertexBuffer> m_vertexBuffer;
109  std::unique_ptr<VertexArray> m_va;
110  };
111  //------------------------------------------------------------
112 
113  // Screen aligned quad / billboard / pointsprite
114  class Points {
115  public:
116  Points();
117  void SetData(Renderer *, const int count, const vector3f *positions, const matrix4x4f &trans, const Color &color, const float size);
118  void SetData(Renderer *, const int count, const vector3f *positions, const Color *color, const matrix4x4f &trans, const float size);
119  void Draw(Renderer *, RenderState *);
120 
121  private:
122  void CreateVertexBuffer(Graphics::Renderer *r, const Uint32 size);
123 
124  bool m_refreshVertexBuffer;
125  RefCountedPtr<Material> m_material;
126  RefCountedPtr<VertexBuffer> m_vertexBuffer;
127  std::unique_ptr<VertexArray> m_va;
128  };
129  //------------------------------------------------------------
130 
131  // Three dimensional sphere (subdivided icosahedron) with normals
132  // and spherical texture coordinates.
133  class Sphere3D {
134  public:
135  //subdivisions must be 0-4
136  Sphere3D(Renderer *, RefCountedPtr<Material> material, Graphics::RenderState *, int subdivisions = 0, float scale = 1.f, const Uint32 attribs = (ATTRIB_POSITION | ATTRIB_NORMAL | ATTRIB_UV0));
137  void Draw(Renderer *r);
138 
139  RefCountedPtr<Material> GetMaterial() const { return m_material; }
140 
141  private:
142  std::unique_ptr<VertexBuffer> m_vertexBuffer;
143  std::unique_ptr<IndexBuffer> m_indexBuffer;
144  RefCountedPtr<Material> m_material;
145  Graphics::RenderState *m_renderState;
146 
147  //std::unique_ptr<Surface> m_surface;
148  //add a new vertex, return the index
149  int AddVertex(VertexArray &, const vector3f &v, const vector3f &n);
150  //add three vertex indices to form a triangle
151  void AddTriangle(std::vector<Uint32> &, int i1, int i2, int i3);
152  void Subdivide(VertexArray &, std::vector<Uint32> &,
153  const matrix4x4f &trans, const vector3f &v1, const vector3f &v2, const vector3f &v3,
154  int i1, int i2, int i3, int depth);
155  };
156  //------------------------------------------------------------
157 
158  // a textured quad with reversed winding
159  class TexturedQuad {
160  public:
161  // Simple constructor to build a textured quad from an image.
162  // Note: this is intended for UI icons and similar things, and it builds the
163  // texture with that in mind (e.g., no texture compression because compression
164  // tends to create visible artefacts when used on UI-style textures that have
165  // edges/lines, etc)
166  // XXX: This is totally the wrong place for this helper function.
167  TexturedQuad(Graphics::Renderer *r, const std::string &filename);
168 
169  // Build a textured quad to display an arbitrary texture.
170  TexturedQuad(Graphics::Renderer *r, Graphics::Texture *texture, const vector2f &pos, const vector2f &size, RenderState *state);
172 
173  void Draw(Graphics::Renderer *r);
174  void Draw(Graphics::Renderer *r, const Color4ub &tint);
175  const Graphics::Texture *GetTexture() const { return m_texture.Get(); }
176 
177  private:
180  RefCountedPtr<VertexBuffer> m_vertexBuffer;
181  Graphics::RenderState *m_renderState;
182  };
183  //------------------------------------------------------------
184 
185  // a coloured rectangle
186  class Rect {
187  public:
188  Rect(Graphics::Renderer *r, const vector2f &pos, const vector2f &size, const Color &c, RenderState *state, const bool bIsStatic = true);
189  void Update(const vector2f &pos, const vector2f &size, const Color &c);
190  void Draw(Graphics::Renderer *r);
191 
192  private:
194  RefCountedPtr<VertexBuffer> m_vertexBuffer;
195  Graphics::RenderState *m_renderState;
196  };
197  //------------------------------------------------------------
198 
199  // a coloured rectangle
201  public:
202  RoundEdgedRect(Graphics::Renderer *r, const vector2f &size, const float rad, const Color &c, RenderState *state, const bool bIsStatic = true);
203  void Update(const vector2f &size, float rad, const Color &c);
204  void Draw(Graphics::Renderer *r);
205 
206  private:
207  static const int STEPS = 6;
209  RefCountedPtr<VertexBuffer> m_vertexBuffer;
210  Graphics::RenderState *m_renderState;
211  };
212  //------------------------------------------------------------
213 
214  //industry-standard red/green/blue XYZ axis indicator
215  class Axes3D {
216  public:
217  Axes3D(Graphics::Renderer *r, Graphics::RenderState *state = nullptr);
218  void Draw(Graphics::Renderer *r);
219 
220  private:
222  RefCountedPtr<VertexBuffer> m_vertexBuffer;
223  Graphics::RenderState *m_renderState;
224  };
225 
227 
228  } // namespace Drawables
229 
230 } // namespace Graphics
231 
232 #endif
Definition: Drawables.h:215
void Draw(Graphics::Renderer *r)
Definition: Drawables.cpp:933
Axes3D(Graphics::Renderer *r, Graphics::RenderState *state=nullptr)
Definition: Drawables.cpp:884
Definition: Drawables.h:22
Circle(Renderer *renderer, const float radius, const Color &c, RenderState *state)
Definition: Drawables.cpp:14
void Draw(Renderer *renderer)
Definition: Drawables.cpp:48
Definition: Drawables.h:39
void Draw(Graphics::Renderer *r)
Definition: Drawables.cpp:112
Disk(Graphics::Renderer *r, Graphics::RenderState *, const Color &c, float radius)
Definition: Drawables.cpp:72
void SetColor(const Color &)
Definition: Drawables.cpp:118
Definition: Drawables.h:56
~Line3D()
Definition: Drawables.h:60
void Draw(Renderer *, RenderState *)
Definition: Drawables.cpp:188
void SetEnd(const vector3f &)
Definition: Drawables.cpp:169
Line3D()
Definition: Drawables.cpp:137
void SetColor(const Color &)
Definition: Drawables.cpp:178
void SetStart(const vector3f &)
Definition: Drawables.cpp:160
Definition: Drawables.h:79
Lines()
Definition: Drawables.cpp:233
void SetData(const Uint32 vertCount, const vector3f *vertices, const Color &color)
Definition: Drawables.cpp:241
void Draw(Renderer *, RenderState *, const PrimitiveType pt=Graphics::LINE_SINGLE)
Definition: Drawables.cpp:283
Definition: Drawables.h:97
void Draw(Renderer *, RenderState *)
Definition: Drawables.cpp:345
void SetData(const int count, const vector3f *positions, const Color *colours, const float *sizes, Graphics::Material *pMaterial)
Definition: Drawables.cpp:326
PointSprites()
Definition: Drawables.cpp:321
Definition: Drawables.h:114
void SetData(Renderer *, const int count, const vector3f *positions, const matrix4x4f &trans, const Color &color, const float size)
Definition: Drawables.cpp:383
void Draw(Renderer *, RenderState *)
Definition: Drawables.cpp:471
Points()
Definition: Drawables.cpp:377
Definition: Drawables.h:186
Rect(Graphics::Renderer *r, const vector2f &pos, const vector2f &size, const Color &c, RenderState *state, const bool bIsStatic=true)
Definition: Drawables.cpp:773
void Update(const vector2f &pos, const vector2f &size, const Color &c)
Definition: Drawables.cpp:802
void Draw(Graphics::Renderer *r)
Definition: Drawables.cpp:815
Definition: Drawables.h:200
void Update(const vector2f &size, float rad, const Color &c)
Definition: Drawables.cpp:844
RoundEdgedRect(Graphics::Renderer *r, const vector2f &size, const float rad, const Color &c, RenderState *state, const bool bIsStatic=true)
Definition: Drawables.cpp:822
void Draw(Graphics::Renderer *r)
Definition: Drawables.cpp:877
Definition: Drawables.h:133
Sphere3D(Renderer *, RefCountedPtr< Material > material, Graphics::RenderState *, int subdivisions=0, float scale=1.f, const Uint32 attribs=(ATTRIB_POSITION|ATTRIB_NORMAL|ATTRIB_UV0))
Definition: Drawables.cpp:522
void Draw(Renderer *r)
Definition: Drawables.cpp:587
RefCountedPtr< Material > GetMaterial() const
Definition: Drawables.h:139
Definition: Drawables.h:159
const Graphics::Texture * GetTexture() const
Definition: Drawables.h:175
void Draw(Graphics::Renderer *r)
Definition: Drawables.cpp:758
TexturedQuad(Graphics::Renderer *r, const std::string &filename)
Definition: Drawables.cpp:638
Definition: Material.h:81
Definition: RenderState.h:25
Definition: Renderer.h:39
Definition: Texture.h:106
Definition: VertexArray.h:19
Definition: RefCounted.h:36
T * Get() const
Definition: SmartPtr.h:37
Axes3D * GetAxes3DDrawable(Graphics::Renderer *r)
Definition: Drawables.cpp:940
Definition: Background.h:13
@ ATTRIB_UV0
Definition: Types.h:19
@ ATTRIB_POSITION
Definition: Types.h:16
@ ATTRIB_NORMAL
Definition: Types.h:17
PrimitiveType
Definition: Types.h:46
@ LINE_SINGLE
Definition: Types.h:48
Definition: Color.h:66