Pioneer
Drawables.h
Go to the documentation of this file.
1 // Copyright © 2008-2022 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  void Draw(Renderer *, Material *);
101 
102  private:
103  bool m_refreshVertexBuffer;
105  std::unique_ptr<VertexArray> m_va;
106  };
107  //------------------------------------------------------------
108 
109  // Screen aligned quad / billboard / pointsprite
110  class Points {
111  public:
112  Points();
113  void SetData(Renderer *, const int count, const vector3f *positions, const matrix4x4f &trans, const Color &color, const float size);
114  void SetData(Renderer *, const int count, const vector3f *positions, const Color *color, const matrix4x4f &trans, const float size);
115  void Draw(Renderer *, Material *);
116 
117  private:
118  void CreateVertexBuffer(Graphics::Renderer *r, const Uint32 size);
119 
120  bool m_refreshVertexBuffer;
121  RefCountedPtr<MeshObject> m_pointMesh;
122  std::unique_ptr<VertexArray> m_va;
123  };
124  //------------------------------------------------------------
125 
126  // Three dimensional sphere (subdivided icosahedron) with normals
127  // and spherical texture coordinates.
128  class Sphere3D {
129  public:
130  //subdivisions must be 0-4
131  Sphere3D(Renderer *, RefCountedPtr<Material> material, int subdivisions = 0, float scale = 1.f, AttributeSet attribs = (ATTRIB_POSITION | ATTRIB_NORMAL | ATTRIB_UV0));
132  void Draw(Renderer *r);
133 
134  RefCountedPtr<Material> GetMaterial() const { return m_material; }
135 
136  private:
137  std::unique_ptr<MeshObject> m_sphereMesh;
138  RefCountedPtr<Material> m_material;
139 
140  //std::unique_ptr<Surface> m_surface;
141  //add a new vertex, return the index
142  int AddVertex(VertexArray &, const vector3f &v, const vector3f &n);
143  //add three vertex indices to form a triangle
144  void AddTriangle(std::vector<Uint32> &, int i1, int i2, int i3);
145  void Subdivide(VertexArray &, std::vector<Uint32> &,
146  const matrix4x4f &trans, const vector3f &v1, const vector3f &v2, const vector3f &v3,
147  int i1, int i2, int i3, int depth);
148  };
149  //------------------------------------------------------------
150 
151  // a textured quad with reversed winding
152  /* TODO: reimplement this as an immediate-mode API for debug use
153  class TexturedQuad {
154  public:
155  // Simple constructor to build a textured quad from an image.
156  // Note: this is intended for UI icons and similar things, and it builds the
157  // texture with that in mind (e.g., no texture compression because compression
158  // tends to create visible artefacts when used on UI-style textures that have
159  // edges/lines, etc)
160  // XXX: This is totally the wrong place for this helper function.
161  TexturedQuad(Graphics::Renderer *r, const std::string &filename);
162 
163  // Build a textured quad to display an arbitrary texture.
164  TexturedQuad(Graphics::Renderer *r, Graphics::Texture *texture, const vector2f &pos, const vector2f &size, RenderState *state);
165  TexturedQuad(Graphics::Renderer *r, RefCountedPtr<Graphics::Material> &material, const Graphics::VertexArray &va, RenderState *state);
166 
167  void Draw(Graphics::Renderer *r);
168  void Draw(Graphics::Renderer *r, const Color4ub &tint);
169  const Graphics::Texture *GetTexture() const { return m_texture.Get(); }
170 
171  private:
172  RefCountedPtr<Graphics::Texture> m_texture;
173  RefCountedPtr<Graphics::Material> m_material;
174  RefCountedPtr<VertexBuffer> m_vertexBuffer;
175  Graphics::RenderState *m_renderState;
176  };
177  */
178 
179  //------------------------------------------------------------
180 
181  // a coloured rectangle
182  /* TODO: reimplement this as an immediate-mode API for debug use
183  class Rect {
184  public:
185  Rect(Graphics::Renderer *r, const vector2f &pos, const vector2f &size, const Color &c, RenderState *state, const bool bIsStatic = true);
186  void Update(const vector2f &pos, const vector2f &size, const Color &c);
187  void Draw(Graphics::Renderer *r);
188 
189  private:
190  RefCountedPtr<Graphics::Material> m_material;
191  RefCountedPtr<VertexBuffer> m_vertexBuffer;
192  Graphics::RenderState *m_renderState;
193  };
194  */
195 
196  //------------------------------------------------------------
197 
198  // a coloured rectangle
199  /* TODO: reimplement this as an immediate-mode API for debug use
200  class RoundEdgedRect {
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;
208  RefCountedPtr<Graphics::Material> m_material;
209  RefCountedPtr<VertexBuffer> m_vertexBuffer;
210  Graphics::RenderState *m_renderState;
211  };
212  */
213 
214  //------------------------------------------------------------
215 
216  //industry-standard red/green/blue XYZ axis indicator
217  class Axes3D {
218  public:
220  void Draw(Graphics::Renderer *r);
221 
222  private:
225  };
226 
228 
229  } // namespace Drawables
230 
231 } // namespace Graphics
232 
233 #endif
Definition: Drawables.h:217
void Draw(Graphics::Renderer *r)
Definition: Drawables.cpp:855
Axes3D(Graphics::Renderer *r)
Definition: Drawables.cpp:815
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: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:308
Definition: Drawables.h:110
void SetData(Renderer *, const int count, const vector3f *positions, const matrix4x4f &trans, const Color &color, const float size)
Definition: Drawables.cpp:338
void Draw(Renderer *, Material *)
Definition: Drawables.cpp:426
Points()
Definition: Drawables.cpp:331
Definition: Drawables.h:128
Sphere3D(Renderer *, RefCountedPtr< Material > material, int subdivisions=0, float scale=1.f, AttributeSet attribs=(ATTRIB_POSITION|ATTRIB_NORMAL|ATTRIB_UV0))
Definition: Drawables.cpp:464
void Draw(Renderer *r)
Definition: Drawables.cpp:509
RefCountedPtr< Material > GetMaterial() const
Definition: Drawables.h:134
Definition: Material.h:148
Definition: Renderer.h:44
Definition: VertexArray.h:19
Definition: RefCounted.h:36
Axes3D * GetAxes3DDrawable(Graphics::Renderer *r)
Definition: Drawables.cpp:862
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