Pioneer
Loading...
Searching...
No Matches
Drawables.h
Go to the documentation of this file.
1// Copyright © 2008-2024 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"
10
11#include <memory>
12
13struct Aabb;
14
15namespace Graphics {
16 class Renderer;
17
18 namespace Drawables {
19
20 // A thing that can draw itself using renderer
21 // (circles, disks, polylines etc)
22 //------------------------------------------------------------
23
24 // Two-dimensional open circle.
25 /* TODO: reimplement as an immediate-mode API writing to a shared vertex array for debug use
26 class Circle {
27 public:
28 Circle(Renderer *renderer, const float radius, const Color &c, RenderState *state);
29 Circle(Renderer *renderer, const float radius, const float x, const float y, const float z, const Color &c, RenderState *state);
30 Circle(Renderer *renderer, const float radius, const vector3f &center, const Color &c, RenderState *state);
31 void Draw(Renderer *renderer);
32
33 private:
34 void SetupVertexBuffer(const Graphics::VertexArray &, Graphics::Renderer *);
35 RefCountedPtr<VertexBuffer> m_vertexBuffer;
36 RefCountedPtr<Material> m_material;
37 Color m_color;
38 Graphics::RenderState *m_renderState;
39 };
40 */
41 //------------------------------------------------------------
42
43 // Two-dimensional filled circle
44 // Generates a TRIANGLE_FAN primitive
45 class Disk {
46 public:
47 Disk(Renderer *r, const int edges = 72, const float radius = 1.0f);
48 void Draw(Renderer *r, Material *mat);
49
50 private:
51 std::unique_ptr<MeshObject> m_diskMesh;
52 };
53 //------------------------------------------------------------
54
55 // A three dimensional line between two points
56 /* TODO: reimplement as an immediate-mode API writing to a shared vertex array for debug use
57 class Line3D {
58 public:
59 Line3D();
60 Line3D(const Line3D &b); // this needs an explicit copy constructor due to the std::unique_ptr below
61 ~Line3D() {}
62 void SetStart(const vector3f &);
63 void SetEnd(const vector3f &);
64 void SetColor(const Color &);
65 void Draw(Renderer *, RenderState *);
66
67 private:
68 void CreateVertexBuffer(Graphics::Renderer *r, const Uint32 size);
69 void Dirty();
70
71 bool m_refreshVertexBuffer;
72 float m_width;
73 RefCountedPtr<Material> m_material;
74 RefCountedPtr<VertexBuffer> m_vertexBuffer;
75 std::unique_ptr<Graphics::VertexArray> m_va;
76 };
77 */
78
79 //------------------------------------------------------------
80
81 // Three dimensional line segments between two points
82 // Data can be drawn with any of the LINE_* primitive types depending on what the calling code intends
83 class Lines {
84 public:
85 Lines();
86 void SetData(const Uint32 vertCount, const vector3f *vertices, const Color &color);
87 void SetData(const Uint32 vertCount, const vector3f *vertices, const Color *colors);
88 void Draw(Renderer *, Material *);
89
90 private:
91 bool m_refreshVertexBuffer;
93 std::unique_ptr<VertexArray> m_va;
94 };
95 //------------------------------------------------------------
96
97 // Screen aligned quad / billboard / pointsprite
98 // Material must be created with a primitive type of Graphics::POINTS
100 public:
101 PointSprites();
102 void SetData(const int count, const vector3f *positions, const Color *colours, const float *sizes);
103
104 // Transfer ownership of the vertex data to the PointSprites instance
105 void SetData(const int count, std::vector<vector3f> &&positions, std::vector<Color> &&colors, std::vector<float> &&sizes);
106
107 void Draw(Renderer *, Material *);
108
109 private:
110 bool m_refreshVertexBuffer;
112 std::unique_ptr<VertexArray> m_va;
113 };
114 //------------------------------------------------------------
115
116 // Screen aligned quad / billboard / pointsprite
117 class Points {
118 public:
119 Points();
120 void SetData(Renderer *, const int count, const vector3f *positions, const matrix4x4f &trans, const Color &color, const float size);
121 void SetData(Renderer *, const int count, const vector3f *positions, const Color *color, const matrix4x4f &trans, const float size);
122 void Draw(Renderer *, Material *);
123
124 private:
125 void CreateVertexBuffer(Graphics::Renderer *r, const Uint32 size);
126
127 bool m_refreshVertexBuffer;
128 RefCountedPtr<MeshObject> m_pointMesh;
129 std::unique_ptr<VertexArray> m_va;
130 };
131
132 //------------------------------------------------------------
133
134 // Helper class to generate an Icosphere mesh
135 class Icosphere {
136 public:
137 static Graphics::MeshObject *Generate(Graphics::Renderer *r, int subdivisions = 0, float scale = 1.f, AttributeSet attribs = (ATTRIB_POSITION | ATTRIB_NORMAL | ATTRIB_UV0));
138
139 private:
140 //add a new vertex, return the index
141 static int AddVertex(VertexArray &, const vector3f &v, const vector3f &n);
142 //add three vertex indices to form a triangle
143 static void AddTriangle(std::vector<Uint32> &, int i1, int i2, int i3);
144 static void Subdivide(VertexArray &, std::vector<Uint32> &,
145 const matrix4x4f &trans, const vector3f &v1, const vector3f &v2, const vector3f &v3,
146 int i1, int i2, int i3, int depth);
147 };
148
149 // Three dimensional sphere (subdivided icosahedron) with normals
150 // and spherical texture coordinates.
151 class Sphere3D {
152 public:
153 //subdivisions must be 0-4
154 Sphere3D(Renderer *, int subdivisions = 0, float scale = 1.f, AttributeSet attribs = (ATTRIB_POSITION | ATTRIB_NORMAL | ATTRIB_UV0));
155 void Draw(Renderer *r, Material *m);
156
157 private:
158 std::unique_ptr<MeshObject> m_sphereMesh;
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
251 public:
252 GridSphere(Graphics::Renderer *r, uint32_t numSubdivs = 4);
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 //------------------------------------------------------------
285
286 // axis-aligned bounding box visualizer
287
288 class AABB {
289 public:
290 static void DrawVertices(Graphics::VertexArray &va, const matrix4x4f &transform, const Aabb &aabb, Color color);
291 };
292
293 } // namespace Drawables
294
295} // namespace Graphics
296
297#endif
Definition: Drawables.h:288
static void DrawVertices(Graphics::VertexArray &va, const matrix4x4f &transform, const Aabb &aabb, Color color)
Definition: Drawables.cpp:999
Definition: Drawables.h:272
void Draw(Graphics::Renderer *r)
Definition: Drawables.cpp:984
Definition: Drawables.h:45
void Draw(Renderer *r, Material *mat)
Definition: Drawables.cpp:100
Definition: Drawables.h:228
void Draw(Graphics::Renderer *r, vector2f grid_size, float cell_size)
Definition: Drawables.cpp:873
void SetLineColors(Color minorLineColor, Color majorLineColor, float lineWidth=2.0)
Definition: Drawables.cpp:866
Definition: Drawables.h:250
void Draw(Graphics::Renderer *r, float lineSpacing=2.0)
Definition: Drawables.cpp:927
void SetLineColors(Color minorLineColor, Color majorLineColor, float lineWidth=2.0)
Definition: Drawables.cpp:920
Definition: Drawables.h:135
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:484
Definition: Drawables.h:83
Lines()
Definition: Drawables.cpp:207
void Draw(Renderer *, Material *)
Definition: Drawables.cpp:257
void SetData(const Uint32 vertCount, const vector3f *vertices, const Color &color)
Definition: Drawables.cpp:215
Definition: Drawables.h:99
PointSprites()
Definition: Drawables.cpp:287
void SetData(const int count, const vector3f *positions, const Color *colours, const float *sizes)
Definition: Drawables.cpp:293
void Draw(Renderer *, Material *)
Definition: Drawables.cpp:328
Definition: Drawables.h:117
void SetData(Renderer *, const int count, const vector3f *positions, const matrix4x4f &trans, const Color &color, const float size)
Definition: Drawables.cpp:358
void Draw(Renderer *, Material *)
Definition: Drawables.cpp:446
Points()
Definition: Drawables.cpp:351
Definition: Drawables.h:151
void Draw(Renderer *r, Material *m)
Definition: Drawables.cpp:579
Definition: Material.h:148
Definition: VertexBuffer.h:156
Definition: Renderer.h:45
Definition: VertexArray.h:22
Definition: RefCounted.h:36
Axes3D * GetAxes3DDrawable(Graphics::Renderer *r)
Definition: Drawables.cpp:991
Definition: Background.h:14
@ ATTRIB_UV0
Definition: Types.h:17
@ ATTRIB_POSITION
Definition: Types.h:14
@ ATTRIB_NORMAL
Definition: Types.h:15
Definition: Aabb.h:11
Definition: Color.h:66
Definition: Types.h:27
Definition: Drawables.cpp:842
Definition: Drawables.cpp:899