Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Box3D.cpp
Go to the documentation of this file.
1
2
3#include "Headers/Box3D.h"
4
8
10
11namespace Divide {
12
13namespace
14{
15 static const U16 indices[] =
16 {
17 0, 1, 3, 0, 3, 2,
18 4, 5, 7, 4, 7, 6,
19 8, 9, 11, 8, 11, 10,
20 12, 13, 15, 12, 15, 14,
21 16, 17, 19, 16, 19, 18,
22 20, 21, 23, 20, 23, 22,
23 };
24
25 static const vec3<F32> vertices[4 * 6] =
26 {
27 {-1.0f, -1.0f, 1.0f},
28 { 1.0f, -1.0f, 1.0f},
29 {-1.0f, 1.0f, 1.0f},
30 { 1.0f, 1.0f, 1.0f},
31
32 { 1.0f, -1.0f, 1.0f},
33 { 1.0f, -1.0f, -1.0f},
34 { 1.0f, 1.0f, 1.0f},
35 { 1.0f, 1.0f, -1.0f},
36
37 { 1.0f, -1.0f, -1.0f},
38 {-1.0f, -1.0f, -1.0f},
39 { 1.0f, 1.0f, -1.0f},
40 {-1.0f, 1.0f, -1.0f},
41
42 {-1.0f, -1.0f, -1.0f},
43 {-1.0f, -1.0f, 1.0f},
44 {-1.0f, 1.0f, -1.0f},
45 {-1.0f, 1.0f, 1.0f},
46
47 {-1.0f, -1.0f, -1.0f},
48 { 1.0f, -1.0f, -1.0f},
49 {-1.0f, -1.0f, 1.0f},
50 { 1.0f, -1.0f, 1.0f},
51
52 {-1.0f, 1.0f, 1.0f},
53 { 1.0f, 1.0f, 1.0f},
54 {-1.0f, 1.0f, -1.0f},
55 { 1.0f, 1.0f, -1.0f}
56 };
57};
58
59Box3D::Box3D( const ResourceDescriptor<Box3D>& descriptor )
60 : Object3D( descriptor, GetSceneNodeType<Box3D>() )
61 , _descriptor(descriptor)
62{
63}
64
65bool Box3D::load( PlatformContext& context )
66{
67 constexpr F32 s_minSideLength = 0.0001f;
68
69 const vec3<F32> targetSize
70 {
71 std::max( Util::UINT_TO_FLOAT( _descriptor.data().x ), s_minSideLength ),
72 std::max( Util::UINT_TO_FLOAT( _descriptor.data().y ), s_minSideLength ),
73 std::max( Util::UINT_TO_FLOAT( _descriptor.data().z ), s_minSideLength )
74 };
75
76 static const vec2<F32> texCoords[4] =
77 {
78 {0.0f, 0.0f},
79 {1.0f, 0.0f},
80 {0.0f, 1.0f},
81 {1.0f, 1.0f}
82 };
83
84 static const vec3<F32> normals[] =
85 {
86 { 0.f, 0.f, 1.f},
87 { 1.f, 0.f, 0.f},
88 { 0.f, 0.f, -1.f},
89 {-1.f, 0.f, 0.f},
90 { 0.f, 1.f, 0.f},
91 { 0.f, -1.f, 0.f}
92 };
93
94
95 _halfExtent.set( targetSize / 2);
96
97 const size_t vertexCount = std::size( vertices );
98 VertexBuffer::Descriptor vbDescriptor{};
99 vbDescriptor._name = resourceName();
100 vbDescriptor._allowDynamicUpdates = true;
101 vbDescriptor._keepCPUData = true;
102 vbDescriptor._largeIndices = vertexCount + 1 > U16_MAX;
103
104 auto vb = context.gfx().newVB( vbDescriptor );
105 vb->setVertexCount( vertexCount );
106 vb->reserveIndexCount( vertexCount );
107
108 for (const U16 idx : indices)
109 {
110 vb->addIndex(idx);
111 }
112
113 for (U32 i = 0u; i < std::size(vertices); ++i)
114 {
115 vb->modifyPositionValue(i, vertices[i] * _halfExtent);
116 vb->modifyTexCoordValue(i, texCoords[i % 4]);
117 vb->modifyNormalValue(i, normals[i / 4]);
118 }
119
120 geometryBuffer(vb);
121 setBounds(BoundingBox(-_halfExtent, _halfExtent));
122
123 if ( !_descriptor.flag() )
124 {
125 ResourceDescriptor<Material> matDesc( "Material_" + resourceName() );
126 matDesc.waitForReady( true );
127 Handle<Material> matTemp = CreateResource( matDesc );
128 Get( matTemp )->properties().shadingMode( ShadingMode::PBR_MR );
129 setMaterialTpl( matTemp );
130 }
131
132 return Object3D::load( context );
133}
134
135void Box3D::setHalfExtent(const vec3<F32>& halfExtent)
136{
137 _halfExtent = halfExtent;
138
139 for (U32 i = 0u; i < std::size(vertices); ++i)
140 {
141 geometryBuffer()->modifyPositionValue(i, vertices[i] * _halfExtent);
142 }
143 setBounds(BoundingBox(-_halfExtent, _halfExtent));
144}
145
146void Box3D::fromPoints(const std::initializer_list<vec3<F32>>& points,
147 const vec3<F32>& halfExtent) {
148
149 geometryBuffer()->modifyPositionValues(0, points);
150 _halfExtent = halfExtent;
151 setBounds(BoundingBox(-_halfExtent * 0.5f, _halfExtent * 0.5f));
152}
153
154const vec3<F32>& Box3D::getHalfExtent() const noexcept {
155 return _halfExtent;
156}
157
158void Box3D::saveToXML(boost::property_tree::ptree& pt) const {
159 pt.put("halfExtent.<xmlattr>.x", _halfExtent.x);
160 pt.put("halfExtent.<xmlattr>.y", _halfExtent.y);
161 pt.put("halfExtent.<xmlattr>.z", _halfExtent.z);
162
163 Object3D::saveToXML(pt);
164}
165
166void Box3D::loadFromXML(const boost::property_tree::ptree& pt) {
167 setHalfExtent(vec3<F32>(pt.get("halfExtent.<xmlattr>.x", 1.0f),
168 pt.get("halfExtent.<xmlattr>.y", 1.0f),
169 pt.get("halfExtent.<xmlattr>.z", 1.0f)));
170
171 Object3D::loadFromXML(pt);
172}
173
174}; //namespace Divide
F32 UINT_TO_FLOAT(U32 src)
Definition: MathHelper.cpp:341
static const vec3< F32 > vertices[4 *6]
Definition: Box3D.cpp:25
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr SceneNodeType GetSceneNodeType()
Definition: SceneNodeFwd.h:111
constexpr U16 U16_MAX
uint16_t U16
FORCE_INLINE Handle< T > CreateResource(const ResourceDescriptor< T > &descriptor, bool &wasInCache, std::atomic_uint &taskCounter)
FORCE_INLINE T * Get(const Handle< T > handle)
uint32_t U32