vsg  1.0.4
VulkanSceneGraph library
Builder.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission 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 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE 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 
21 namespace 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 GeometyInfo.position with vec3Array containing positions
34  bool billboard = false; // user must assign GeometyInfo.position 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 
60  template<typename T>
61  void set(const t_box<T>& bb)
62  {
63  position = (bb.min + bb.max) * 0.5f;
64  dx.set(bb.max.x - bb.min.x, 0.0f, 0.0f);
65  dy.set(0.0f, bb.max.y - bb.min.y, 0.0f);
66  dz.set(0.0f, 0.0f, bb.max.z - bb.min.z);
67  }
68 
69  template<typename T>
70  void set(const t_sphere<T>& sp)
71  {
72  position = sp.center;
73  dx.set(sp.radius * 2.0f, 0.0f, 0.0f);
74  dy.set(0.0f, sp.radius * 2.0f, 0.0f);
75  dz.set(0.0f, 0.0f, sp.radius * 2.0f);
76  }
77 
80  ref_ptr<Data> colors;
81 
82  bool operator<(const GeometryInfo& rhs) const
83  {
84  int result = compare_region(position, transform, rhs.position);
85  if (result) return result < 0;
86 
87  if ((result = compare_pointer(positions, rhs.positions))) return result < 0;
88  return compare_pointer(colors, rhs.colors) < 0;
89  }
90  };
91  VSG_type_name(vsg::GeometryInfo);
92 
96  class VSG_DECLSPEC Builder : public Inherit<Object, Builder>
97  {
98  public:
99  bool verbose = false;
100  ref_ptr<Options> options;
101  ref_ptr<SharedObjects> sharedObjects;
102  ref_ptr<ShaderSet> shaderSet;
103 
104  ref_ptr<Node> createBox(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
105  ref_ptr<Node> createCapsule(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
106  ref_ptr<Node> createCone(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
107  ref_ptr<Node> createCylinder(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
108  ref_ptr<Node> createDisk(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
109  ref_ptr<Node> createQuad(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
110  ref_ptr<Node> createSphere(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
111  ref_ptr<Node> createHeightField(const GeometryInfo& info = {}, const StateInfo& stateInfo = {});
112 
113  ref_ptr<StateGroup> createStateGroup(const StateInfo& stateInfo = {});
114 
117 
118  ref_ptr<CompileTraversal> compileTraversal;
119 
120  private:
121  void transform(const mat4& matrix, ref_ptr<vec3Array> vertices, ref_ptr<vec3Array> normals);
122  ref_ptr<Data> instancePositions(const GeometryInfo& info, uint32_t& instanceCount);
123  ref_ptr<Data> instanceColors(const GeometryInfo& info, uint32_t instanceCount);
124  vec3 y_texcoord(const StateInfo& info) const;
125 
126  using GeometryMap = std::map<GeometryInfo, ref_ptr<Node>>;
127  GeometryMap _boxes;
128  GeometryMap _capsules;
129  GeometryMap _cones;
130  GeometryMap _cylinders;
131  GeometryMap _quads;
132  GeometryMap _spheres;
133  GeometryMap _heightfields;
134 
135  // used for comparisons
136  mat4 identity;
137  };
138  VSG_type_name(vsg::Builder);
139 
140 } // namespace vsg
Definition: Builder.h:97
void assignCompileTraversal(ref_ptr< CompileTraversal > ct)
assign compile traversal to enable compilation.
Definition: Inherit.h:28
Definition: ref_ptr.h:22
GeometryInfo struct provides geometry related settings supported by Builder.
Definition: Builder.h:44
ref_ptr< Data > positions
when using geometry instancing use vec3Array with vec3{x,y,z} and for billboard use vec4Array with ve...
Definition: Builder.h:79
StateInfo struct provides state related settings supported by Builder.
Definition: Builder.h:26
bool wireframe
greyscale image
Definition: Builder.h:31
t_box template class that represent a axis aligned bounding box
Definition: box.h:24
template sphere class
Definition: sphere.h:34