scenepic 1.1.0
3D Visualization Made Easy
mesh.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#ifndef _SCENEPIC_MESH_H_
5#define _SCENEPIC_MESH_H_
6
7#include "camera.h"
8#include "color.h"
9#include "json_value.h"
10#include "matrix.h"
11#include "mesh_info.h"
12
13#include <array>
14#include <cstdint>
15#include <string>
16#include <vector>
17
18namespace scenepic
19{
37 class Mesh
38 {
39 public:
49 const std::string& texture_id = "");
50
52 std::uint32_t count_vertices() const;
53
55 Vector center_of_mass() const;
56
62
67 void apply_transform(const Transform& transform);
68
75 void apply_rotation(const Transform& transform);
76
81 Mesh get_transformed(const Transform& transform);
82
87 Mesh get_rotated(const Transform& transform);
88
92 void append_mesh(const Mesh& mesh);
93
108 const Color& color = Color::None(),
109 const Vector& pos0 = Vector(0, 0, 0),
110 const Vector& pos1 = Vector(1, 0, 0),
111 const Vector& pos2 = Vector(0, 1, 0),
112 const Vector& normal = VectorNone(),
113 bool fill_triangles = true,
114 bool add_wireframe = false,
115 const UV& uv_0 = UV(0, 0),
116 const UV& uv_1 = UV(1, 0),
117 const UV& uv_2 = UV(0, 1));
118
137 const Color& color = Color::None(),
138 const Vector& pos0 = Vector(0, -0.5f, -0.5f),
139 const Vector& pos1 = Vector(0, +0.5f, -0.5f),
140 const Vector& pos2 = Vector(0, +0.5f, +0.5f),
141 const Vector& pos3 = Vector(0, -0.5f, +0.5f),
142 const Vector& normal = VectorNone(),
143 bool fill_triangles = true,
144 bool add_wireframe = false,
145 const Transform& transform = Transform::Identity(),
146 const UV& uv_0 = UV(0, 0),
147 const UV& uv_1 = UV(1, 0),
148 const UV& uv_2 = UV(1, 1),
149 const UV& uv_3 = UV(0, 1));
150
167 const Vector& origin = Vector(-0.5f, -0.5f, 0),
168 const Vector& x_axis = Vector(1, 0, 0),
169 const Vector& y_axis = Vector(0, 1, 0),
170 const Vector& normal = VectorNone(),
171 const UV& uv_0 = UV(0, 0),
172 const UV& uv_1 = UV(1, 0),
173 const UV& uv_2 = UV(1, 1),
174 const UV& uv_3 = UV(0, 1),
175 bool double_sided = true,
176 const Transform& transform = Transform::Identity());
177
186 const Color& color = Color::None(),
187 const Transform& transform = Transform::Identity(),
188 bool fill_triangles = true,
189 bool add_wireframe = false);
190
203 const Color& color = Color::None(),
204 const Vector& start_point = Vector(-0.5, 0, 0),
205 const Vector& end_point = Vector(0.5, 0, 0),
206 float start_thickness = 0.1f,
207 float end_thickness = 0.1f,
208 const Transform& transform = Transform::Identity(),
209 bool fill_triangles = true,
210 bool add_wireframe = false);
211
224 const Color& color = Color::None(),
225 const Transform& transform = Transform::Identity(),
226 float truncation_height = 1,
227 std::uint32_t lat_count = 10,
228 std::uint32_t long_count = 10,
229 bool fill_triangles = true,
230 bool add_wireframe = false);
231
238 float length = 1.0f,
239 float thickness = 0.1f,
240 const Transform& transform = Transform::Identity());
241
252 const Color& color = Color::None(),
253 float fov_y_degrees = 45.0f,
254 float aspect_ratio = 16.0f / 9.0f,
255 float depth = 1.0f,
256 float thickness = 0.02f,
257 const Transform& transform = Transform::Identity());
258
267 const Camera& camera,
268 const Color& color = Color::None(),
269 float thickness = 0.02f,
270 float depth = 1.0f);
271
276 void add_camera_image(const Camera& camera, float depth = 1.0f);
277
287 const Color& color = Color::None(),
288 const Transform& transform = Transform::Identity(),
289 std::uint32_t segment_count = 10,
290 bool fill_triangles = true,
291 bool add_wireframe = false);
292
302 const Color& color = Color::None(),
303 const Transform& transform = Transform::Identity(),
304 std::uint32_t segment_count = 10,
305 bool fill_triangles = true,
306 bool add_wireframe = false);
307
316 const Color& color = Color::None(),
317 const Transform& transform = Transform::Identity(),
318 bool fill_triangles = true,
319 bool add_wireframe = false);
320
331 const Color& color = Color::None(),
332 const Transform& transform = Transform::Identity(),
333 std::uint32_t steps = 0,
334 bool fill_triangles = true,
335 bool add_wireframe = false);
336
349 const Color& color = Color::None(),
350 const Transform& transform = Transform::Identity(),
351 std::uint32_t lat_count = 10,
352 std::uint32_t long_count = 10,
353 bool fill_triangles = true,
354 bool add_wireframe = false);
355
369 const ConstVectorBufferRef& vertices,
370 const ConstTriangleBufferRef& triangles,
371 const ConstColorBufferRef& colors = ColorBufferNone(),
372 const ConstUVBufferRef& uvs = UVBufferNone(),
373 const Transform& transform = Transform::Identity(),
374 bool reverse_triangle_order = false,
375 bool fill_triangles = true,
376 bool add_wireframe = false);
377
386 static VectorBuffer compute_normals(
387 const ConstVectorBufferRef& vertices,
388 const ConstTriangleBufferRef& triangles,
389 bool reverse_triangle_order = false);
390
404 const ConstVectorBufferRef& vertices,
405 const ConstVectorBufferRef& normals,
406 const ConstTriangleBufferRef& triangles,
407 const ConstColorBufferRef& colors = ColorBufferNone(),
408 const ConstUVBufferRef& uvs = UVBufferNone(),
409 const Transform& transform = Transform::Identity(),
410 bool reverse_triangle_order = false,
411 bool fill_triangles = true,
412 bool add_wireframe = false);
413
424 const std::shared_ptr<MeshInfo>& mesh_info,
425 const Transform& transform = Transform::Identity(),
426 bool reverse_triangle_order = false,
427 bool fill_triangles = true,
428 bool add_wireframe = false);
429
440 const ConstVertexBufferRef& start_points,
441 const ConstVertexBufferRef& end_points,
442 const Color& color = Color::None(),
443 const Transform& transform = Transform::Identity());
444
455 const ConstVectorBufferRef& positions,
456 const ConstQuaternionBufferRef& rotations = QuaternionBufferNone(),
457 const ConstColorBufferRef& colors = ColorBufferNone());
458
460 std::string to_string() const;
461
466
469 const Color& shared_color() const;
470
474
477 const std::string& texture_id() const;
478
481 Mesh& texture_id(const std::string& texture_id);
482
484 const std::string& mesh_id() const;
485
491 const std::string& layer_id() const;
492
498 Mesh& layer_id(const std::string& layer_id);
499
503 bool double_sided() const;
504
509
514 bool camera_space() const;
515
521
525 bool nn_texture() const;
526
531
536 bool use_texture_alpha() const;
537
543
548 bool is_billboard() const;
549
555
559 bool is_label() const;
560
565
567 const ConstTriangleBufferRef triangles() const;
568
570 VertexBlock vertex_positions();
571
573 const ConstVertexBlock vertex_positions() const;
574
576 Mesh& vertex_positions(const VertexBlock& vertices);
577
579 VertexBlock vertex_normals();
580
582 Mesh& vertex_normals(const VertexBlock& normals);
583
585 const ConstVertexBlock vertex_normals() const;
586
588 VertexBlock vertex_colors();
589
591 Mesh& vertex_colors(const VertexBlock& colors);
592
594 const ConstVertexBlock vertex_colors() const;
595
597 VertexBlock vertex_uvs();
598
600 Mesh& vertex_uvs(const VertexBlock& uvs);
601
603 const ConstVertexBlock vertex_uvs() const;
604
611 VertexBufferRef vertex_buffer();
612
620 InstanceBufferRef instance_buffer();
621
622 private:
623 friend class Scene;
624
628 Mesh(const std::string& mesh_id);
629
630 bool is_instanced() const;
631 void check_instances();
632 void check_color(const Color& color);
633 std::uint32_t
634 append_vertex(const Vector& pos, const Vector& normal, const Color& color);
635 std::uint32_t
636 append_vertex(const Vector& pos, const Vector& normal, UV texture_uv);
637 std::uint32_t append_vertex(const Vector& pos, const Vector& normal);
638 void append_triangle(
639 std::uint32_t index0, std::uint32_t index1, std::uint32_t index2);
640 void append_line(std::uint32_t index0, std::uint32_t index1);
641 JsonValue definition_to_json() const;
642
643 VertexBuffer m_vertices;
644 TriangleBuffer m_triangles;
645 LineBuffer m_lines;
646 Color m_shared_color;
647 std::string m_texture_id;
648 std::string m_mesh_id;
649 std::string m_layer_id;
650 bool m_double_sided;
651 bool m_camera_space;
652 bool m_nn_texture;
653 bool m_use_texture_alpha;
654 bool m_is_billboard;
655 bool m_is_label;
656
657 InstanceBuffer m_instance_buffer;
658 bool m_instance_buffer_has_rotations;
659 bool m_instance_buffer_has_colors;
660 };
661} // namespace scenepic
662
663#endif
A camera type.
Definition: camera.h:15
Class for representing color values.
Definition: color.h:19
static const Color None()
Constant value indicating "no color".
Representation of a JSON value according to the specification at https://www.json....
Definition: json_value.h:30
The basic ScenePic mesh class, containing vertex, triangle, and line buffers.
Definition: mesh.h:38
void add_cube(const Color &color=Color::None(), const Transform &transform=Transform::Identity(), bool fill_triangles=true, bool add_wireframe=false)
Add a unit diameter cube to this mesh.
bool is_label() const
This mesh will be treated specially as a label.
Mesh & camera_space(bool camera_space)
Whether this Mesh is defined in camera space (cannot be moved in the ScenePic user interface) or worl...
bool is_billboard() const
Draw this Mesh as a billboard (i.e.
Mesh(const Color &shared_color=Color::None(), const std::string &texture_id="")
Constructor.
const ConstVertexBlock vertex_uvs() const
const Reference to the vertex uvs.
void add_cylinder(const Color &color=Color::None(), const Transform &transform=Transform::Identity(), std::uint32_t segment_count=10, bool fill_triangles=true, bool add_wireframe=false)
Add a x-axis aligned unit diameter cylinder to this Mesh.
void enable_instancing(const ConstVectorBufferRef &positions, const ConstQuaternionBufferRef &rotations=QuaternionBufferNone(), const ConstColorBufferRef &colors=ColorBufferNone())
Makes ScenePic render this Mesh with multiple instances, e.g.
Vector center_of_mass() const
The mean of mesh vertex positions.
void add_image(const Vector &origin=Vector(-0.5f, -0.5f, 0), const Vector &x_axis=Vector(1, 0, 0), const Vector &y_axis=Vector(0, 1, 0), const Vector &normal=VectorNone(), const UV &uv_0=UV(0, 0), const UV &uv_1=UV(1, 0), const UV &uv_2=UV(1, 1), const UV &uv_3=UV(0, 1), bool double_sided=true, const Transform &transform=Transform::Identity())
Add an image as a textured quad to this Mesh.
JsonValue to_json() const
Convert this mesh into ScenePic json.
Mesh & is_label(bool is_label)
This mesh will be treated specially as a label.
void add_mesh(const std::shared_ptr< MeshInfo > &mesh_info, const Transform &transform=Transform::Identity(), bool reverse_triangle_order=false, bool fill_triangles=true, bool add_wireframe=false)
Add a triangle mesh to this ScenePic Mesh, with normals computed automatically.
void add_sphere(const Color &color=Color::None(), const Transform &transform=Transform::Identity(), bool fill_triangles=true, bool add_wireframe=false)
Add a default (ico) unit diameter sphere primitive to the mesh.
void add_mesh_with_normals(const ConstVectorBufferRef &vertices, const ConstVectorBufferRef &normals, const ConstTriangleBufferRef &triangles, const ConstColorBufferRef &colors=ColorBufferNone(), const ConstUVBufferRef &uvs=UVBufferNone(), const Transform &transform=Transform::Identity(), bool reverse_triangle_order=false, bool fill_triangles=true, bool add_wireframe=false)
Add a triangle mesh to this ScenePic Mesh, with normals provided.
const ConstVertexBlock vertex_normals() const
const Reference to the vertex normals.
void add_cone(const Color &color=Color::None(), const Transform &transform=Transform::Identity(), float truncation_height=1, std::uint32_t lat_count=10, std::uint32_t long_count=10, bool fill_triangles=true, bool add_wireframe=false)
Add a cone to this Mesh.
Mesh get_transformed(const Transform &transform)
Get a transformed copy of this Mesh.
void reverse_triangle_order()
Reverses the winding order of all triangles in this mesh.
Mesh & vertex_uvs(const VertexBlock &uvs)
Set the vertex uvs.
void add_mesh_without_normals(const ConstVectorBufferRef &vertices, const ConstTriangleBufferRef &triangles, const ConstColorBufferRef &colors=ColorBufferNone(), const ConstUVBufferRef &uvs=UVBufferNone(), const Transform &transform=Transform::Identity(), bool reverse_triangle_order=false, bool fill_triangles=true, bool add_wireframe=false)
Add a triangle mesh to this ScenePic Mesh, with normals computed automatically.
void add_coordinate_axes(float length=1.0f, float thickness=0.1f, const Transform &transform=Transform::Identity())
Add a set of coordinate axes to this Mesh.
Mesh & vertex_positions(const VertexBlock &vertices)
Set the vertex positions.
void add_triangle(const Color &color=Color::None(), const Vector &pos0=Vector(0, 0, 0), const Vector &pos1=Vector(1, 0, 0), const Vector &pos2=Vector(0, 1, 0), const Vector &normal=VectorNone(), bool fill_triangles=true, bool add_wireframe=false, const UV &uv_0=UV(0, 0), const UV &uv_1=UV(1, 0), const UV &uv_2=UV(0, 1))
Adds a triangle to the mesh.
void add_disc(const Color &color=Color::None(), const Transform &transform=Transform::Identity(), std::uint32_t segment_count=10, bool fill_triangles=true, bool add_wireframe=false)
Add a x-axis aligned unit diameter disc to this Mesh.
InstanceBufferRef instance_buffer()
References to the instance buffer.
const std::string & layer_id() const
An identifier to allow several Meshes to be grouped together into a "layer".
static VectorBuffer compute_normals(const ConstVectorBufferRef &vertices, const ConstTriangleBufferRef &triangles, bool reverse_triangle_order=false)
Compute the vertex normals given a set of triangles and vertices.
Mesh get_rotated(const Transform &transform)
Get a rotated copy of this Mesh.
VertexBlock vertex_positions()
Reference to the vertex positions.
std::uint32_t count_vertices() const
The number of vertices in the mesh.
Mesh & texture_id(const std::string &texture_id)
Specifies the id of a ScenePic Image to be used as a texture in this Mesh.
const ConstVertexBlock vertex_colors() const
const Reference to the vertex colors.
void apply_transform(const Transform &transform)
Apply a 3D homogeneous matrix transform (i.e.
const std::string & texture_id() const
Specifies the id of a ScenePic Image to be used as a texture in this Mesh.
Mesh & double_sided(bool double_sided)
Whether to turn off back face culling and draw the Mesh's triangles as double sided.
VertexBlock vertex_uvs()
Reference to the vertex uvs.
Mesh & use_texture_alpha(bool use_texture_alpha)
Whether to use the alpha channel in the texture for transparency (only relevant for textured Meshes).
Mesh & is_billboard(bool is_billboard)
Draw this Mesh as a billboard (i.e.
void add_camera_frustum(const Camera &camera, const Color &color=Color::None(), float thickness=0.02f, float depth=1.0f)
Add a camera frustum visualization to this Mesh.
bool camera_space() const
Whether this Mesh is defined in camera space (cannot be moved in the ScenePic user interface) or worl...
const std::string & mesh_id() const
A unique identifier for the Mesh.
bool double_sided() const
Whether to turn off back face culling and draw the Mesh's triangles as double sided.
void add_camera_frustum(const Color &color=Color::None(), float fov_y_degrees=45.0f, float aspect_ratio=16.0f/9.0f, float depth=1.0f, float thickness=0.02f, const Transform &transform=Transform::Identity())
Add a camera frustum visualization to this Mesh.
Mesh & nn_texture(bool nn_texture)
Whether the shader should use nearest-neighbor texture interpolation.
void add_lines(const ConstVertexBufferRef &start_points, const ConstVertexBufferRef &end_points, const Color &color=Color::None(), const Transform &transform=Transform::Identity())
Add a line cloud to this Mesh.
VertexBufferRef vertex_buffer()
Reference to the vertex buffer.
const Color & shared_color() const
Whole-mesh color (reduces memory requirements but makes Mesh monochrome).
void add_quad(const Color &color=Color::None(), const Vector &pos0=Vector(0, -0.5f, -0.5f), const Vector &pos1=Vector(0,+0.5f, -0.5f), const Vector &pos2=Vector(0,+0.5f,+0.5f), const Vector &pos3=Vector(0, -0.5f,+0.5f), const Vector &normal=VectorNone(), bool fill_triangles=true, bool add_wireframe=false, const Transform &transform=Transform::Identity(), const UV &uv_0=UV(0, 0), const UV &uv_1=UV(1, 0), const UV &uv_2=UV(1, 1), const UV &uv_3=UV(0, 1))
Adds a quad to the mesh.
void add_uv_sphere(const Color &color=Color::None(), const Transform &transform=Transform::Identity(), std::uint32_t lat_count=10, std::uint32_t long_count=10, bool fill_triangles=true, bool add_wireframe=false)
Add a unit diameter UV sphere to this Mesh.
void apply_rotation(const Transform &transform)
Apply a 3D homogeneous matrix rotation (i.e.
void add_icosphere(const Color &color=Color::None(), const Transform &transform=Transform::Identity(), std::uint32_t steps=0, bool fill_triangles=true, bool add_wireframe=false)
Add a unit diameter ico-sphere to this Mesh.
VertexBlock vertex_normals()
Reference to the vertex normals.
void append_mesh(const Mesh &mesh)
Append another mesh object to this mesh.
std::string to_string() const
Return a JSON string representing the object.
void add_thickline(const Color &color=Color::None(), const Vector &start_point=Vector(-0.5, 0, 0), const Vector &end_point=Vector(0.5, 0, 0), float start_thickness=0.1f, float end_thickness=0.1f, const Transform &transform=Transform::Identity(), bool fill_triangles=true, bool add_wireframe=false)
Add a thick line to this Mesh.
VertexBlock vertex_colors()
Reference to the vertex colors.
Mesh & shared_color(const Color &shared_color)
Whole-mesh color (reduces memory requirements but makes Mesh monochrome).
void add_camera_image(const Camera &camera, float depth=1.0f)
Add the image plane for a camera into the scene.
Mesh & vertex_colors(const VertexBlock &colors)
Set the vertex colors.
Mesh & layer_id(const std::string &layer_id)
An identifier to allow several Meshes to be grouped together into a "layer".
bool nn_texture() const
Whether the shader should use nearest-neighbor texture interpolation.
const ConstTriangleBufferRef triangles() const
The triangles (i.e.
Mesh & vertex_normals(const VertexBlock &normals)
Set the vertex normals.
bool use_texture_alpha() const
Whether to use the alpha channel in the texture for transparency (only relevant for textured Meshes).
const ConstVertexBlock vertex_positions() const
const Reference to the vertex positions.
Top level container representing an entire ScenePic.
Definition: scene.h:58
Definition: audio_track.h:14