Mon, 20 Jun 2022 22:21:53 +0300
fix typo
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
1 | #pragma once |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
2 | #include <glm/gtc/matrix_transform.hpp> |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
3 | #include "basics.h" |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
4 | #include "model.h" |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
5 | #include "ldrawalgorithm.h" |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
6 | |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
7 | template<typename Fn> |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
8 | void rasterize(const CircularPrimitive& circ, Fn&& fn) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
9 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
10 | std::vector<ModelElement> result; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
11 | const auto xform = [&circ](const glm::vec2& p, float y){ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
12 | return glm::vec3{circ.transformation * glm::vec4{p.x, y, p.y, 1}}; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
13 | }; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
14 | const glm::vec3 origin = xform({0, 0}, 0); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
15 | switch(circ.type) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
16 | case CircularPrimitive::Circle: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
17 | ldraw::circle(circ.fraction.segments, circ.fraction.divisions, [&] |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
18 | (const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
19 | fn(edge(xform(p1, 0), xform(p2, 0))); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
20 | }); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
21 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
22 | case CircularPrimitive::Disc: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
23 | ldraw::circle(circ.fraction.segments, circ.fraction.divisions, [&] |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
24 | (const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
25 | fn(triangle(origin, xform(p1, 0), xform(p2, 0))); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
26 | }); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
27 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
28 | case CircularPrimitive::Cylinder: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
29 | ldraw::circle(circ.fraction.segments, circ.fraction.divisions, [&] |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
30 | (const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
31 | fn(quadrilateral(xform(p1, 1), xform(p2, 1), xform(p2, 0), xform(p1, 0))); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
32 | }); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
33 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
34 | case CircularPrimitive::CylinderOpen: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
35 | rasterize(CircularPrimitive{ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
36 | .type = CircularPrimitive::Cylinder, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
37 | .fraction = circ.fraction, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
38 | .transformation = circ.transformation, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
39 | }, fn); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
40 | rasterize(CircularPrimitive{ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
41 | .type = CircularPrimitive::Circle, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
42 | .fraction = circ.fraction, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
43 | .transformation = circ.transformation, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
44 | }, fn); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
45 | rasterize(CircularPrimitive{ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
46 | .type = CircularPrimitive::Circle, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
47 | .fraction = circ.fraction, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
48 | .transformation = glm::translate(circ.transformation, {0, 1, 0}), |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
49 | }, fn); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
50 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
51 | case CircularPrimitive::CylinderClosed: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
52 | rasterize(CircularPrimitive{ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
53 | .type = CircularPrimitive::CylinderOpen, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
54 | .fraction = circ.fraction, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
55 | .transformation = circ.transformation, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
56 | }, fn); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
57 | rasterize(CircularPrimitive{ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
58 | .type = CircularPrimitive::Disc, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
59 | .fraction = circ.fraction, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
60 | .transformation = circ.transformation, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
61 | }, fn); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
62 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
63 | case CircularPrimitive::DiscNegative: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
64 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
65 | unsigned int i = 0; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
66 | ldraw::circle(circ.fraction.segments, circ.fraction.divisions, [&] |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
67 | (const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
68 | constexpr glm::vec2 corners[4] = { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
69 | {+1, +1}, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
70 | {-1, +1}, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
71 | {-1, -1}, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
72 | {+1, -1}, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
73 | }; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
74 | const glm::vec2& corner = corners[i * 4 / circ.fraction.divisions]; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
75 | fn(triangle(xform(p2, 0), xform(p1, 0), xform(corner, 0))); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
76 | ++i; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
77 | }); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
78 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
79 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
80 | case CircularPrimitive::Chord: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
81 | for (unsigned int i = 1; i < circ.fraction.segments; ++i) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
82 | const glm::vec2& p1 = ldraw::rimpoint(circ.fraction.divisions, i); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
83 | const glm::vec2& p2 = ldraw::rimpoint(circ.fraction.divisions, i + 1); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
84 | fn(triangle(xform(p2, 0), xform(p1, 0), xform({1, 0}, 0))); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
85 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
86 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
87 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
88 | } |