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

vsg 1.1.3 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 VSG_type_name(vsg::StateInfo);
41
43 struct GeometryInfo
44 {
45 GeometryInfo() = default;
46
47 template<typename T>
48 explicit GeometryInfo(const t_box<T>& bb) { set(bb); }
49
50 template<typename T>
51 explicit GeometryInfo(const t_sphere<T>& sp) { set(sp); }
52
53 vec3 position = {0.0f, 0.0f, 0.0f};
54 vec3 dx = {1.0f, 0.0f, 0.0f};
55 vec3 dy = {0.0f, 1.0f, 0.0f};
56 vec3 dz = {0.0f, 0.0f, 1.0f};
57 vec4 color = {1.0f, 1.0f, 1.0f, 1.0f};
58 mat4 transform;
59
61 bool cullNode = false;
62
63 template<typename T>
64 void set(const t_box<T>& bb)
65 {
66 position = (bb.min + bb.max) * 0.5f;
67 dx.set(bb.max.x - bb.min.x, 0.0f, 0.0f);
68 dy.set(0.0f, bb.max.y - bb.min.y, 0.0f);
69 dz.set(0.0f, 0.0f, bb.max.z - bb.min.z);
70 }
71
72 template<typename T>
73 void set(const t_sphere<T>& sp)
74 {
75 position = sp.center;
76 dx.set(sp.radius * 2.0f, 0.0f, 0.0f);
77 dy.set(0.0f, sp.radius * 2.0f, 0.0f);
78 dz.set(0.0f, 0.0f, sp.radius * 2.0f);
79 }
80
82 ref_ptr<Data> positions;
83 ref_ptr<Data> colors;
84
85 bool operator<(const GeometryInfo& rhs) const
86 {
87 int result = compare_region(position, transform, rhs.position);
88 if (result) return result < 0;
89
90 if ((result = compare_pointer(positions, rhs.positions))) return result < 0;
91 return compare_pointer(colors, rhs.colors) < 0;
92 }
93 };
94 VSG_type_name(vsg::GeometryInfo);
95
99 class VSG_DECLSPEC Builder : public Inherit<Object, Builder>
100 {
101 public:
102 bool verbose = false;
103 ref_ptr<Options> options;
104 ref_ptr<SharedObjects> sharedObjects;
105 ref_ptr<ShaderSet> shaderSet;
106
107 ref_ptr<Node> createBox(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
108 ref_ptr<Node> createCapsule(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
109 ref_ptr<Node> createCone(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
110 ref_ptr<Node> createCylinder(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
111 ref_ptr<Node> createDisk(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
112 ref_ptr<Node> createQuad(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
113 ref_ptr<Node> createSphere(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
114 ref_ptr<Node> createHeightField(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
115
116 ref_ptr<StateGroup> createStateGroup(const StateInfo& stateInfo = {});
117
119 void assignCompileTraversal(ref_ptr<CompileTraversal> ct);
120
121 ref_ptr<CompileTraversal> compileTraversal;
122
123 protected:
124 void transform(const mat4& matrix, ref_ptr<vec3Array> vertices, ref_ptr<vec3Array> normals);
125 ref_ptr<Data> instancePositions(const GeometryInfo& info, uint32_t& instanceCount);
126 ref_ptr<Data> instanceColors(const GeometryInfo& info, uint32_t instanceCount);
127 vec3 y_texcoord(const StateInfo& info) const;
128
129 ref_ptr<Node> decorateAndCompileIfRequired(const GeometryInfo& info, const StateInfo& stateInfo, ref_ptr<Node> node);
130
131 ref_ptr<ShaderSet> _flatShadedShaderSet;
132 ref_ptr<ShaderSet> _phongShaderSet;
133
134 using GeometryMap = std::map<GeometryInfo, ref_ptr<Node>>;
135 GeometryMap _boxes;
136 GeometryMap _capsules;
137 GeometryMap _cones;
138 GeometryMap _cylinders;
139 GeometryMap _quads;
140 GeometryMap _spheres;
141 GeometryMap _heightfields;
142
143 // used for comparisons
144 mat4 identity;
145 };
146 VSG_type_name(vsg::Builder);
147
148} // namespace vsg
vsg::Builder
Definition Builder.h:100
vsg::Builder::assignCompileTraversal
void assignCompileTraversal(ref_ptr< CompileTraversal > ct)
assign compile traversal to enable compilation.
vsg::Inherit
Definition Inherit.h:28
vsg::ref_ptr
Definition ref_ptr.h:22
vsg::GeometryInfo
GeometryInfo struct provides geometry related settings supported by Builder.
Definition Builder.h:44
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:82
vsg::GeometryInfo::cullNode
bool cullNode
cullNode flag indicates whether a CullNode should decorate the creted subgraph
Definition Builder.h:61
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_mat4< float >
vsg::t_sphere
template sphere class
Definition sphere.h:34
vsg::t_vec3< float >
vsg::t_vec4< float >