vsg: include/vsg/utils/Builder.h Source File

vsg 1.1.13 VulkanSceneGraph library

Loading...
Searching...
No Matches
Builder.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/app/CompileTraversal.h>
16#include <vsg/maths/box.h>
17#include <vsg/maths/sphere.h>
18#include <vsg/utils/ShaderSet.h>
19#include <vsg/utils/SharedObjects.h>
20
21namespace vsg
22{
23
25 struct StateInfo
26 {
27 bool lighting = true;
28 bool two_sided = false;
29 bool blending = false;
30 bool greyscale = false;
31 bool wireframe = false;
32 bool instance_colors_vec4 = true;
33 bool instance_positions_vec3 = false; // user must assign GeometryInfo::positions with vec3Array containing positions
34 bool billboard = false; // user must assign GeometryInfo::positions with vec4Array containing position_scaleDistance, overrides instance_positions_vec3 setting
35
36 ref_ptr<Data> image;
37 ref_ptr<Data> displacementMap;
38 ref_ptr<DescriptorSetLayout> viewDescriptorSetLayout;
39
40 bool operator<(const StateInfo& rhs) const
41 {
42 int result = compare_region(lighting, billboard, rhs.lighting);
43 if (result) return result < 0;
44
45 if ((result = compare_pointer(image, rhs.image))) return result < 0;
46 if ((result = compare_pointer(displacementMap, rhs.displacementMap))) return result < 0;
47 return compare_pointer(viewDescriptorSetLayout, rhs.viewDescriptorSetLayout) < 0;
48 }
49 };
50 VSG_type_name(vsg::StateInfo);
51
53 struct GeometryInfo
54 {
55 GeometryInfo() = default;
56
57 template<typename T>
58 explicit GeometryInfo(const t_box<T>& bb) { set(bb); }
59
60 template<typename T>
61 explicit GeometryInfo(const t_sphere<T>& sp) { set(sp); }
62
63 vec3 position = {0.0f, 0.0f, 0.0f};
64 vec3 dx = {1.0f, 0.0f, 0.0f};
65 vec3 dy = {0.0f, 1.0f, 0.0f};
66 vec3 dz = {0.0f, 0.0f, 1.0f};
67 vec4 color = {1.0f, 1.0f, 1.0f, 1.0f};
68 mat4 transform;
69
71 bool cullNode = false;
72
73 template<typename T>
74 void set(const t_box<T>& bb)
75 {
76 position = (bb.min + bb.max) * 0.5f;
77 dx.set(bb.max.x - bb.min.x, 0.0f, 0.0f);
78 dy.set(0.0f, bb.max.y - bb.min.y, 0.0f);
79 dz.set(0.0f, 0.0f, bb.max.z - bb.min.z);
80 }
81
82 template<typename T>
83 void set(const t_sphere<T>& sp)
84 {
85 position = sp.center;
86 dx.set(sp.radius * 2.0f, 0.0f, 0.0f);
87 dy.set(0.0f, sp.radius * 2.0f, 0.0f);
88 dz.set(0.0f, 0.0f, sp.radius * 2.0f);
89 }
90
92 ref_ptr<Data> positions;
93 ref_ptr<Data> colors;
94
95 bool operator<(const GeometryInfo& rhs) const
96 {
97 int result = compare_region(position, transform, rhs.position);
98 if (result) return result < 0;
99
100 if ((result = compare_pointer(positions, rhs.positions))) return result < 0;
101 return compare_pointer(colors, rhs.colors) < 0;
102 }
103 };
104 VSG_type_name(vsg::GeometryInfo);
105
109 class VSG_DECLSPEC Builder : public Inherit<Object, Builder>
110 {
111 public:
112 Builder();
113 Builder(const Builder& rhs) = delete;
114 Builder& operator=(const Builder& rhs) = delete;
115
116 ~Builder();
117
118 bool verbose = false;
119 ref_ptr<Options> options;
120 ref_ptr<SharedObjects> sharedObjects;
121 ref_ptr<ShaderSet> shaderSet;
122
123 ref_ptr<Node> createBox(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
124 ref_ptr<Node> createCapsule(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
125 ref_ptr<Node> createCone(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
126 ref_ptr<Node> createCylinder(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
127 ref_ptr<Node> createDisk(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
128 ref_ptr<Node> createQuad(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
129 ref_ptr<Node> createSphere(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
130 ref_ptr<Node> createHeightField(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
131
132 ref_ptr<StateGroup> createStateGroup(const StateInfo& stateInfo = {});
133
135 void assignCompileTraversal(ref_ptr<CompileTraversal> ct);
136
137 ref_ptr<CompileTraversal> compileTraversal;
138
139 protected:
140 void transform(const mat4& matrix, ref_ptr<vec3Array> vertices, ref_ptr<vec3Array> normals);
141 ref_ptr<Data> instancePositions(const GeometryInfo& info, uint32_t& instanceCount);
142 ref_ptr<Data> instanceColors(const GeometryInfo& info, uint32_t instanceCount);
143 vec3 y_texcoord(const StateInfo& info) const;
144
145 ref_ptr<Node> decorateAndCompileIfRequired(const GeometryInfo& info, const StateInfo& stateInfo, ref_ptr<Node> node);
146
147 ref_ptr<ShaderSet> _flatShadedShaderSet;
148 ref_ptr<ShaderSet> _phongShaderSet;
149
150 using GeometryMap = std::map<std::pair<GeometryInfo, StateInfo>, ref_ptr<Node>>;
151 GeometryMap _boxes;
152 GeometryMap _capsules;
153 GeometryMap _cones;
154 GeometryMap _cylinders;
155 GeometryMap _disks;
156 GeometryMap _quads;
157 GeometryMap _spheres;
158 GeometryMap _heightfields;
159
160 // used for comparisons
161 mat4 identity;
162 };
163 VSG_type_name(vsg::Builder);
164
165} // namespace vsg
vsg::Builder
Definition Builder.h:110
vsg::Builder::assignCompileTraversal
void assignCompileTraversal(ref_ptr< CompileTraversal > ct)
assign compile traversal to enable compilation.
vsg::ref_ptr
Definition ref_ptr.h:22
vsg::GeometryInfo
GeometryInfo struct provides geometry related settings supported by Builder.
Definition Builder.h:54
vsg::GeometryInfo::positions
ref_ptr< Data > positions
when using geometry instancing use vec3Array with vec3{x,y,z} and for billboards use vec4Array with v...
Definition Builder.h:92
vsg::GeometryInfo::cullNode
bool cullNode
cullNode flag indicates whether a CullNode should decorate the created subgraph
Definition Builder.h:71
vsg::StateInfo
StateInfo struct provides state related settings supported by Builder.
Definition Builder.h:26
vsg::StateInfo::wireframe
bool wireframe
greyscale image
Definition Builder.h:31
vsg::t_box
t_box template class that represents an axis aligned bounding box
Definition box.h:24
vsg::t_sphere
template sphere class
Definition sphere.h:34