Sat, 08 Apr 2023 12:55:11 +0300
Fix BFC formatting not working due to being evaluated after comment format
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> |
264
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
250
diff
changeset
|
3 | #include "src/basics.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
250
diff
changeset
|
4 | #include "src/model.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
250
diff
changeset
|
5 | #include "src/ldrawalgorithm.h" |
232
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 | }; |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
247
diff
changeset
|
14 | const glm::vec3 primitiveOrigin = xform({0, 0}, 0); |
247
07ad61423c3c
fix cylinders being possibly rendered inside out
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
15 | const bool invertedMatrix = (glm::determinant(circ.transformation) < 0) != circ.inverted; |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
16 | switch(circ.type) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
17 | case CircularPrimitive::Circle: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
18 | 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
|
19 | (const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){ |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
20 | fn(LineSegment{xform(p1, 0), xform(p2, 0)}, EDGE_COLOR); |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
21 | }); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
22 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
23 | case CircularPrimitive::Disc: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
24 | 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
|
25 | (const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){ |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
26 | fn(Triangle{primitiveOrigin, xform(p1, 0), xform(p2, 0)}, MAIN_COLOR); |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
27 | }); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
28 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
29 | case CircularPrimitive::Cylinder: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
30 | 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
|
31 | (const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){ |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
32 | Quadrilateral quad{xform(p1, 1), xform(p2, 1), xform(p2, 0), xform(p1, 0)}; |
247
07ad61423c3c
fix cylinders being possibly rendered inside out
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
33 | if (invertedMatrix) { |
07ad61423c3c
fix cylinders being possibly rendered inside out
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
34 | std::swap(quad.p2, quad.p4); |
07ad61423c3c
fix cylinders being possibly rendered inside out
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
35 | } |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
36 | fn(quad, MAIN_COLOR); |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
37 | }); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
38 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
39 | case CircularPrimitive::CylinderOpen: |
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::Cylinder, |
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 = circ.transformation, |
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 | rasterize(CircularPrimitive{ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
51 | .type = CircularPrimitive::Circle, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
52 | .fraction = circ.fraction, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
53 | .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
|
54 | }, fn); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
55 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
56 | case CircularPrimitive::CylinderClosed: |
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::CylinderOpen, |
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 | rasterize(CircularPrimitive{ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
63 | .type = CircularPrimitive::Disc, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
64 | .fraction = circ.fraction, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
65 | .transformation = circ.transformation, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
66 | }, fn); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
67 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
68 | case CircularPrimitive::DiscNegative: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
69 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
70 | unsigned int i = 0; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
71 | 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
|
72 | (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
|
73 | constexpr glm::vec2 corners[4] = { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
74 | {+1, +1}, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
75 | {-1, +1}, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
76 | {-1, -1}, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
77 | {+1, -1}, |
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 | const glm::vec2& corner = corners[i * 4 / circ.fraction.divisions]; |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
80 | fn(Triangle{xform(p2, 0), xform(p1, 0), xform(corner, 0)}, MAIN_COLOR); |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
81 | ++i; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
82 | }); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
83 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
84 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
85 | case CircularPrimitive::Chord: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
86 | 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
|
87 | 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
|
88 | const glm::vec2& p2 = ldraw::rimpoint(circ.fraction.divisions, i + 1); |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
89 | fn(Triangle{xform(p2, 0), xform(p1, 0), xform({1, 0}, 0)}, MAIN_COLOR); |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
90 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
91 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
92 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
93 | } |