Pioneer
Drawables.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 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 
15  namespace Drawables {
16 
17  // A thing that can draw itself using renderer
18  // (circles, disks, polylines etc)
19  //------------------------------------------------------------
20 
21  // Two-dimensional open circle.
22  /* TODO: reimplement as an immediate-mode API writing to a shared vertex array for debug use
23  class Circle {
24  public:
25  Circle(Renderer *renderer, const float radius, const Color &c, RenderState *state);
26  Circle(Renderer *renderer, const float radius, const float x, const float y, const float z, const Color &c, RenderState *state);
27  Circle(Renderer *renderer, const float radius, const vector3f &center, const Color &c, RenderState *state);
28  void Draw(Renderer *renderer);
29 
30  private:
31  void SetupVertexBuffer(const Graphics::VertexArray &, Graphics::Renderer *);
32  RefCountedPtr<VertexBuffer> m_vertexBuffer;
33  RefCountedPtr<Material> m_material;
34  Color m_color;
35  Graphics::RenderState *m_renderState;
36  };
37  */
38  //------------------------------------------------------------
39 
40  // Two-dimensional filled circle
41  // Generates a TRIANGLE_FAN primitive
42  class Disk {
43  public:
44  Disk(Renderer *r, const int edges = 72, const float radius = 1.0f);
45  void Draw(Renderer *r, Material *mat);
46 
47  private:
48  std::unique_ptr<MeshObject> m_diskMesh;
49  };
50  //------------------------------------------------------------
51 
52  // A three dimensional line between two points
53  /* TODO: reimplement as an immediate-mode API writing to a shared vertex array for debug use
54  class Line3D {
55  public:
56  Line3D();
57  Line3D(const Line3D &b); // this needs an explicit copy constructor due to the std::unique_ptr below
58  ~Line3D() {}
59  void SetStart(const vector3f &);
60  void SetEnd(const vector3f &);
61  void SetColor(const Color &);
62  void Draw(Renderer *, RenderState *);
63 
64  private:
65  void CreateVertexBuffer(Graphics::Renderer *r, const Uint32 size);
66  void Dirty();
67 
68  bool m_refreshVertexBuffer;
69  float m_width;
70  RefCountedPtr<Material> m_material;
71  RefCountedPtr<VertexBuffer> m_vertexBuffer;
72  std::unique_ptr<Graphics::VertexArray> m_va;
73  };
74  */
75 
76  //------------------------------------------------------------
77 
78  // Three dimensional line segments between two points
79  // Data can be drawn with any of the LINE_* primitive types depending on what the calling code intends
80  class Lines {
81  public:
82  Lines();
83  void SetData(const Uint32 vertCount, const vector3f *vertices, const Color &color);
84  void SetData(const Uint32 vertCount, const vector3f *vertices, const Color *colors);
85  void Draw(Renderer *, Material *);
86 
87  private:
88  bool m_refreshVertexBuffer;
89  RefCountedPtr<MeshObject> m_lineMesh;
90  std::unique_ptr<VertexArray> m_va;
91  };
92  //------------------------------------------------------------
93 
94  // Screen aligned quad / billboard / pointsprite
95  // Material must be created with a primitive type of Graphics::POINTS
96  class PointSprites {
97  public:
98  PointSprites();
99  void SetData(const int count, const vector3f *positions, const Color *colours, const float *sizes);
100 
101  // Transfer ownership of the vertex data to the PointSprites instance
102  void SetData(const int count, std::vector<vector3f> &&positions, std::vector<Color> &&colors, std::vector<float> &&sizes);
103 
104  void Draw(Renderer *, Material *);
105 
106  private:
107  bool m_refreshVertexBuffer;
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 *, Material *);
120 
121  private:
122  void CreateVertexBuffer(Graphics::Renderer *r, const Uint32 size);
123 
124  bool m_refreshVertexBuffer;
125  RefCountedPtr<MeshObject> m_pointMesh;
126  std::unique_ptr<VertexArray> m_va;
127  };
128 
129  //------------------------------------------------------------
130 
131  // Helper class to generate an Icosphere mesh
132  class Icosphere {
133  public:
134  static Graphics::MeshObject *Generate(Graphics::Renderer *r, int subdivisions = 0, float scale = 1.f, AttributeSet attribs = (ATTRIB_POSITION | ATTRIB_NORMAL | ATTRIB_UV0));
135 
136  private:
137  //add a new vertex, return the index
138  static int AddVertex(VertexArray &, const vector3f &v, const vector3f &n);
139  //add three vertex indices to form a triangle
140  static void AddTriangle(std::vector<Uint32> &, int i1, int i2, int i3);
141  static void Subdivide(VertexArray &, std::vector<Uint32> &,
142  const matrix4x4f &trans, const vector3f &v1, const vector3f &v2, const vector3f &v3,
143  int i1, int i2, int i3, int depth);
144  };
145 
146  // Three dimensional sphere (subdivided icosahedron) with normals
147  // and spherical texture coordinates.
148  class Sphere3D {
149  public:
150  //subdivisions must be 0-4
151  Sphere3D(Renderer *, RefCountedPtr<Material> material, int subdivisions = 0, float scale = 1.f, AttributeSet attribs = (ATTRIB_POSITION | ATTRIB_NORMAL | ATTRIB_UV0));
152  void Draw(Renderer *r);
153 
154  RefCountedPtr<Material> GetMaterial() const { return m_material; }
155 
156  private:
157  std::unique_ptr<MeshObject> m_sphereMesh;
158  RefCountedPtr<Material> m_material;
159  };
160  //------------------------------------------------------------
161 
162  // a textured quad with reversed winding
163  /* TODO: reimplement this as an immediate-mode API for debug use
164  class TexturedQuad {
165  public:
166  // Simple constructor to build a textured quad from an image.
167  // Note: this is intended for UI icons and similar things, and it builds the
168  // texture with that in mind (e.g., no texture compression because compression
169  // tends to create visible artefacts when used on UI-style textures that have
170  // edges/lines, etc)
171  // XXX: This is totally the wrong place for this helper function.
172  TexturedQuad(Graphics::Renderer *r, const std::string &filename);
173 
174  // Build a textured quad to display an arbitrary texture.
175  TexturedQuad(Graphics::Renderer *r, Graphics::Texture *texture, const vector2f &pos, const vector2f &size, RenderState *state);
176  TexturedQuad(Graphics::Renderer *r, RefCountedPtr<Graphics::Material> &material, const Graphics::VertexArray &va, RenderState *state);
177 
178  void Draw(Graphics::Renderer *r);
179  void Draw(Graphics::Renderer *r, const Color4ub &tint);
180  const Graphics::Texture *GetTexture() const { return m_texture.Get(); }
181 
182  private:
183  RefCountedPtr<Graphics::Texture> m_texture;
184  RefCountedPtr<Graphics::Material> m_material;
185  RefCountedPtr<VertexBuffer> m_vertexBuffer;
186  Graphics::RenderState *m_renderState;
187  };
188  */
189 
190  //------------------------------------------------------------
191 
192  // a coloured rectangle
193  /* TODO: reimplement this as an immediate-mode API for debug use
194  class Rect {
195  public:
196  Rect(Graphics::Renderer *r, const vector2f &pos, const vector2f &size, const Color &c, RenderState *state, const bool bIsStatic = true);
197  void Update(const vector2f &pos, const vector2f &size, const Color &c);
198  void Draw(Graphics::Renderer *r);
199 
200  private:
201  RefCountedPtr<Graphics::Material> m_material;
202  RefCountedPtr<VertexBuffer> m_vertexBuffer;
203  Graphics::RenderState *m_renderState;
204  };
205  */
206 
207  //------------------------------------------------------------
208 
209  // a coloured rectangle
210  /* TODO: reimplement this as an immediate-mode API for debug use
211  class RoundEdgedRect {
212  public:
213  RoundEdgedRect(Graphics::Renderer *r, const vector2f &size, const float rad, const Color &c, RenderState *state, const bool bIsStatic = true);
214  void Update(const vector2f &size, float rad, const Color &c);
215  void Draw(Graphics::Renderer *r);
216 
217  private:
218  static const int STEPS = 6;
219  RefCountedPtr<Graphics::Material> m_material;
220  RefCountedPtr<VertexBuffer> m_vertexBuffer;
221  Graphics::RenderState *m_renderState;
222  };
223  */
224 
225  //------------------------------------------------------------
226 
227  // Shader-based anti-aliased XZ-plane grid rendering.
228  class GridLines {
229  public:
231 
232  void SetLineColors(Color minorLineColor, Color majorLineColor, float lineWidth = 2.0);
233 
234  void Draw(Graphics::Renderer *r, vector2f grid_size, float cell_size);
235 
236  private:
237  struct GridData;
238 
239  std::unique_ptr<Graphics::Material> m_gridMat;
240  Color m_minorColor;
241  Color m_majorColor;
242  float m_lineWidth;
243  };
244 
245  //------------------------------------------------------------
246 
247  // Shader-based anti-aliased UV-sphere grid rendering.
248  // Grid has primary lines at 90 and 10 degree intervals, and subdivides by tenths from there
249  // The visual density of the grid can be controlled by the lineSpacing parameter
250  class GridSphere {
251  public:
252  GridSphere(Graphics::Renderer *r, uint32_t numSubdivs = 12);
253 
254  void SetLineColors(Color minorLineColor, Color majorLineColor, float lineWidth = 2.0);
255 
256  void Draw(Graphics::Renderer *r, float lineSpacing = 2.0);
257 
258  private:
259  struct GridData;
260 
261  std::unique_ptr<Graphics::Material> m_gridMat;
262  std::unique_ptr<Graphics::MeshObject> m_sphereMesh;
263  Color m_minorColor;
264  Color m_majorColor;
265  float m_lineWidth;
266  uint32_t m_numSubdivs;
267  };
268 
269  //------------------------------------------------------------
270 
271  //industry-standard red/green/blue XYZ axis indicator
272  class Axes3D {
273  public:
275  void Draw(Graphics::Renderer *r);
276 
277  private:
280  };
281 
283 
284  } // namespace Drawables
285 
286 } // namespace Graphics
287 
288 #endif
Definition: Drawables.h:272
void Draw(Graphics::Renderer *r)
Definition: Drawables.cpp:982
Axes3D(Graphics::Renderer *r)
Definition: Drawables.cpp:942
Definition: Drawables.h:42
void Draw(Renderer *r, Material *mat)
Definition: Drawables.cpp:97
Disk(Renderer *r, const int edges=72, const float radius=1.0f)
Definition: Drawables.cpp:78
Definition: Drawables.h:228
void Draw(Graphics::Renderer *r, vector2f grid_size, float cell_size)
Definition: Drawables.cpp:871
void SetLineColors(Color minorLineColor, Color majorLineColor, float lineWidth=2.0)
Definition: Drawables.cpp:864
GridLines(Graphics::Renderer *r)
Definition: Drawables.cpp:851
Definition: Drawables.h:250
GridSphere(Graphics::Renderer *r, uint32_t numSubdivs=12)
Definition: Drawables.cpp:904
void Draw(Graphics::Renderer *r, float lineSpacing=2.0)
Definition: Drawables.cpp:925
void SetLineColors(Color minorLineColor, Color majorLineColor, float lineWidth=2.0)
Definition: Drawables.cpp:918
Definition: Drawables.h:132
static Graphics::MeshObject * Generate(Graphics::Renderer *r, int subdivisions=0, float scale=1.f, AttributeSet attribs=(ATTRIB_POSITION|ATTRIB_NORMAL|ATTRIB_UV0))
Definition: Drawables.cpp:481
Definition: Drawables.h:80
Lines()
Definition: Drawables.cpp:204
void Draw(Renderer *, Material *)
Definition: Drawables.cpp:254
void SetData(const Uint32 vertCount, const vector3f *vertices, const Color &color)
Definition: Drawables.cpp:212
Definition: Drawables.h:96
PointSprites()
Definition: Drawables.cpp:284
void SetData(const int count, const vector3f *positions, const Color *colours, const float *sizes)
Definition: Drawables.cpp:290
void Draw(Renderer *, Material *)
Definition: Drawables.cpp:325
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:355
void Draw(Renderer *, Material *)
Definition: Drawables.cpp:443
Points()
Definition: Drawables.cpp:348
Definition: Drawables.h:148
Sphere3D(Renderer *, RefCountedPtr< Material > material, int subdivisions=0, float scale=1.f, AttributeSet attribs=(ATTRIB_POSITION|ATTRIB_NORMAL|ATTRIB_UV0))
Definition: Drawables.cpp:568
void Draw(Renderer *r)
Definition: Drawables.cpp:577
RefCountedPtr< Material > GetMaterial() const
Definition: Drawables.h:154
Definition: Material.h:148
Definition: VertexBuffer.h:156
Definition: Renderer.h:44
Definition: VertexArray.h:19
Definition: RefCounted.h:36
Axes3D * GetAxes3DDrawable(Graphics::Renderer *r)
Definition: Drawables.cpp:989
Definition: Background.h:14
@ ATTRIB_UV0
Definition: Types.h:17
@ ATTRIB_POSITION
Definition: Types.h:14
@ ATTRIB_NORMAL
Definition: Types.h:15
Definition: Color.h:66
Definition: Types.h:27
Definition: Drawables.cpp:840
Definition: Drawables.cpp:897