scimesh 0.3.4
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
clipping.h
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <scimesh/types.h>
12#include <scimesh/math_utils.h>
13#include <vector>
14
15namespace scimesh {
16
27
43 const ClipVertex &v0,
44 const ClipVertex &v1,
45 const ClipVertex &v2,
46 std::vector<ClipVertex> &output_vertices,
47 std::vector<Triangle> &output_triangles);
48
86 const Vec3 &eye,
87 const Mat4 &view) {
89 out.space = plane.space;
90
91 float len = glm::length(plane.normal);
92 if (len < 1e-12f) {
93 // Degenerate normal: keep everything (no clipping) instead of
94 // discarding the whole scene.
95 out.normal = Vec3(0.0f, 0.0f, 0.0f);
96 out.offset = 0.0f;
97 return out;
98 }
99
100 Vec3 n = plane.normal / len;
101 out.normal = transform_direction(view, n);
102 if (plane.space == PlaneSpace::WORLD) {
103 // Shift the plane constant so that the cut stays at its world
104 // position (see the derivation above).
105 out.offset = glm::dot(n, eye) + plane.offset;
106 } else {
107 out.offset = plane.offset;
108 }
109 return out;
110}
111
133 const Vec3 &v0, const Vec3 &v1, const Vec3 &v2,
134 const Vec3 &n0, const Vec3 &n1, const Vec3 &n2,
135 const Color &c0, const Color &c1, const Color &c2,
136 const Vec2 &uv0, const Vec2 &uv1, const Vec2 &uv2,
137 const ClipPlane &plane,
138 std::vector<ClipVertex> &output_vertices,
139 std::vector<Triangle> &output_triangles);
140
141} // namespace scimesh
Low-level math utilities for the rendering pipeline.
glm::vec2 Vec2
2-component floating-point vector (xy).
Definition types.h:32
glm::mat4 Mat4
4×4 floating-point matrix.
Definition types.h:65
glm::vec3 Vec3
3-component floating-point vector (xyz).
Definition types.h:46
Vec3 transform_direction(const Mat4 &m, const Vec3 &d)
Transform a direction vector by a 4×4 matrix (with implicit w=0).
Definition math_utils.h:90
int clip_triangle_view_plane(const Vec3 &v0, const Vec3 &v1, const Vec3 &v2, const Vec3 &n0, const Vec3 &n1, const Vec3 &n2, const Color &c0, const Color &c1, const Color &c2, const Vec2 &uv0, const Vec2 &uv1, const Vec2 &uv2, const ClipPlane &plane, std::vector< ClipVertex > &output_vertices, std::vector< Triangle > &output_triangles)
Clip a triangle against an arbitrary plane in view space.
Definition clipping.cpp:162
ClipPlane clip_plane_to_view_space(const ClipPlane &plane, const Vec3 &eye, const Mat4 &view)
Convert a ClipPlane to view (eye) space, ready for clipping.
Definition clipping.h:85
@ WORLD
World (scene) space: the plane is fixed in the scene, so it does not move when the camera moves.
int clip_triangle_near_plane(const ClipVertex &v0, const ClipVertex &v1, const ClipVertex &v2, std::vector< ClipVertex > &output_vertices, std::vector< Triangle > &output_triangles)
Clip a triangle against the near clipping plane in clip space.
Definition clipping.cpp:67
glm::vec4 Vec4
4-component floating-point vector (xyzw).
Definition types.h:52
Vec2 uv1
Definition renderer.cpp:25
Vec2 uv0
Definition renderer.cpp:25
Vec2 uv2
Definition renderer.cpp:25
A clipping plane that can hide parts of the scene.
Definition types.h:263
PlaneSpace space
The space ClipPlane::normal and ClipPlane::offset refer to.
Definition types.h:286
A processed vertex in clip space, ready for perspective divide.
Definition clipping.h:21
Color color
Vertex color.
Definition clipping.h:23
Vec2 uv
Texture coordinates.
Definition clipping.h:25
Vec3 normal
Vertex normal (in view space).
Definition clipping.h:24
Vec4 position
Homogeneous clip-space position (before divide by w).
Definition clipping.h:22
An RGBA color with floating-point components.
Definition types.h:88
int x
Left edge of the bitmap, in image pixels.
Definition text.cpp:223
Fundamental types used throughout the scimesh rendering engine.