Rendering points as spheres/surfels and lines as cylinders/cones using imposters.
This tutorial shows how to give simple primitives a richer appearance with imposters: mesh vertices are drawn as shaded spheres, mesh edges as solid cylinders, and vertex normals as cones. A second copy of the mesh, shifted sideways, shows the same vertices rendered as surfels (oriented disks). All of this is done by switching a style flag on PointsDrawable / LinesDrawable — no extra geometry is generated.
An imposter (or "sprite") is a cheap primitive — a point or a line — that the fragment shader makes look like a more complex 3D object. For sphere imposters, each point is rasterized as a small screen-facing quad; the shader then computes, per pixel, the surface of the sphere that the quad represents: it discards pixels outside the circular silhouette, reconstructs the sphere's 3D normal and depth, and shades it with lighting. The result looks like a real lit sphere at a fraction of the cost of tessellating one. The same idea turns a line segment into a cylinder (constant radius, flat caps) or a cone (tapering from the first endpoint to the second) — ideal for drawing edges and vector arrows. A surfel ("surface
element") is an oriented disk: it uses the point's normal to tilt the disk so it lies in the local tangent plane, which makes point clouds look like sampled surface patches. Because imposters are analytic, they stay perfectly smooth at any zoom level. See e.g. the classic "GPU Gems" chapters on sprite/sphere rendering for the underlying ray-intersection math.
The window shows two spheres side by side: on the left, red sphere vertices, orange cylinder edges, and green cone normals; on the right, the same mesh with red surfel vertices. Navigate with the standard Viewer controls (left-drag rotate, scroll zoom, 'h' for help).
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/surface_mesh.h>
29#include <easy3d/renderer/drawable_points.h>
30#include <easy3d/renderer/drawable_lines.h>
31#include <easy3d/renderer/drawable_triangles.h>
32#include <easy3d/renderer/renderer.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
93 PointsDrawable *drawable = mesh->renderer()->add_points_drawable(
"vertices");
94 drawable->set_uniform_coloring(
vec4(1.0f, 0.0f, 0.0f, 1.0f));
95 drawable->set_point_size(16.0f);
97 drawable->set_impostor_type(PointsDrawable::SPHERE);
102 LinesDrawable *drawable = mesh->renderer()->add_lines_drawable(
"edges");
103 drawable->set_uniform_coloring(
vec4(1.0f, 0.67f, 0.5f, 1.0f));
104 drawable->set_impostor_type(LinesDrawable::CYLINDER);
105 drawable->set_line_width(6);
111 auto points = mesh->get_vertex_property<
vec3>(
"v:point");
113 mesh->update_vertex_normals();
114 auto normals = mesh->get_vertex_property<
vec3>(
"v:normal");
119 float length = mesh->bounding_box().diagonal_length() * 0.15f;
127 std::vector<vec3> normal_points;
128 for (
auto v : mesh->vertices()) {
129 const vec3 &s = points[v];
130 const vec3 &t = points[v] + normals[v] *
length;
131 normal_points.push_back(s);
132 normal_points.push_back(t);
135 LinesDrawable *drawable = mesh->renderer()->add_lines_drawable(
"normals");
138 drawable->update_vertex_buffer(normal_points);
139 drawable->set_uniform_coloring(
vec4(0.0f, 1.0f, 0.0f, 1.0f));
140 drawable->set_impostor_type(LinesDrawable::CONE);
141 drawable->set_line_width(8);
148 PointsDrawable *drawable = mesh->renderer()->add_points_drawable(
"vertices");
149 drawable->set_uniform_coloring(
vec4(1.0f, 0.0f, 0.0f, 1.0f));
150 drawable->set_point_size(16.0f);
151 drawable->set_impostor_type(PointsDrawable::SURFEL);
154int main(
int argc,
char **argv) {
160 Viewer viewer(EXAMPLE_TITLE);
163 const std::string file_name = resource::directory() +
"/data/sphere.obj";
168 auto mesh =
dynamic_cast<SurfaceMesh *
>(viewer.add_model(file_name,
false));
170 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
176 create_spheres(mesh);
180 create_cylinders(mesh);
192 const vec3 trans =
vec3(0, 1, 0) * mesh->bounding_box().diagonal_length() * 0.7f;
193 auto points = copy->get_vertex_property<
vec3>(
"v:point");
194 for (
auto v : copy->vertices())
197 viewer.add_model(std::shared_ptr<SurfaceMesh>(copy),
false);
199 create_surfels(copy);
The drawable for rendering a set of line segments, e.g., edges of a mesh, vector fields.
Definition drawable_lines.h:40
The drawable for rendering a set of points, e.g., point clouds, vertices of a mesh.
Definition drawable_points.h:42
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
void initialize(bool info_to_stdout, bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition initializer.cpp:39
T length(const Vec< N, T > &v)
Definition vec.h:341