Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Sphere3D.cpp
Go to the documentation of this file.
1
2
3#include "Headers/Sphere3D.h"
4
8
10
11namespace Divide
12{
13namespace
14{
15 constexpr F32 s_minRadius = 0.0001f;
16}
17
18Sphere3D::Sphere3D( const ResourceDescriptor<Sphere3D>& descriptor )
19 : Object3D( descriptor, GetSceneNodeType<Sphere3D>() )
20 , _radius( std::max( Util::UINT_TO_FLOAT( descriptor.enumValue() ), s_minRadius ) )
21 , _resolution( descriptor.ID() == 0u ? 16u : descriptor.ID() )
22 , _descriptor(descriptor)
23{
24}
25
26bool Sphere3D::load( PlatformContext& context )
27{
28 const U32 vertexCount = SQUARED( _resolution );
29
30 VertexBuffer::Descriptor vbDescriptor{};
31 vbDescriptor._name = resourceName();
32 vbDescriptor._allowDynamicUpdates = false;
33 vbDescriptor._keepCPUData = true;
34 vbDescriptor._largeIndices = vertexCount + 1 > U16_MAX;
35
36 auto vb = context.gfx().newVB( vbDescriptor );
37 vb->setVertexCount( vertexCount );
38 vb->reserveIndexCount( vertexCount );
39 geometryBuffer( vb );
40
41 geometryDirty( true );
42
43 if ( !_descriptor.flag() )
44 {
45 ResourceDescriptor<Material> matDesc( "Material_" + resourceName() );
46 matDesc.waitForReady( true );
47 Handle<Material> matTemp = CreateResource( matDesc );
48 Get( matTemp )->properties().shadingMode( ShadingMode::PBR_MR );
49 setMaterialTpl( matTemp );
50 }
51
52 return Object3D::load(context);
53}
54
55void Sphere3D::setRadius(const F32 radius) noexcept
56{
57 _radius = radius;
58 geometryDirty(true);
59}
60
61void Sphere3D::setResolution(const U32 resolution) noexcept
62{
63 _resolution = resolution;
64 geometryDirty(true);
65}
66
67// SuperBible stuff
68void Sphere3D::rebuildInternal()
69{
70 const U32 slices = _resolution;
71 const U32 stacks = _resolution;
72
73 geometryBuffer()->reset();
74 const F32 drho = M_PI_f / stacks;
75 const F32 dtheta = 2.0f * M_PI_f / slices;
76 const F32 ds = 1.0f / slices;
77 const F32 dt = 1.0f / stacks;
78 F32 t = 1.0f;
79 U32 index = 0;
80
81 geometryBuffer()->setVertexCount(stacks * ((slices + 1) * 2));
82
83 for (U32 i = 0u; i < stacks; i++) {
84 const F32 rho = i * drho;
85 const F32 srho = std::sin(rho);
86 const F32 crho = std::cos(rho);
87 const F32 srhodrho = std::sin(rho + drho);
88 const F32 crhodrho = std::cos(rho + drho);
89
90 // Many sources of OpenGL sphere drawing code uses a triangle fan
91 // for the caps of the sphere. This however introduces texturing
92 // artifacts at the poles on some OpenGL implementations
93 F32 s = 0.0f;
94 for (U32 j = 0; j <= slices; j++) {
95 const F32 theta = j == slices ? 0.0f : j * dtheta;
96 const F32 stheta = -std::sin(theta);
97 const F32 ctheta = std::cos(theta);
98
99 F32 x = stheta * srho;
100 F32 y = ctheta * srho;
101 F32 z = crho;
102
103 geometryBuffer()->modifyPositionValue(index, x * _radius, y * _radius, z * _radius);
104 geometryBuffer()->modifyTexCoordValue(index, s, t);
105 geometryBuffer()->modifyNormalValue(index, x, y, z);
106 geometryBuffer()->addIndex(index++);
107
108 x = stheta * srhodrho;
109 y = ctheta * srhodrho;
110 z = crhodrho;
111 s += ds;
112
113 geometryBuffer()->modifyPositionValue(index, x * _radius, y * _radius, z * _radius);
114 geometryBuffer()->modifyTexCoordValue(index, s, t - dt);
115 geometryBuffer()->modifyNormalValue(index, x, y, z);
116 geometryBuffer()->addIndex(index++);
117 }
118 t -= dt;
119 }
120
121 efficient_clear( _geometryTriangles );
122
123 // ToDo: add some depth padding for collision and nav meshes
124 setBounds(BoundingBox(vec3<F32>(-_radius), vec3<F32>(_radius)));
125
126 Object3D::rebuildInternal();
127}
128
129void Sphere3D::saveToXML(boost::property_tree::ptree& pt) const {
130 pt.put("radius", _radius);
131 pt.put("resolution", _resolution);
132
133 Object3D::saveToXML(pt);
134}
135
136void Sphere3D::loadFromXML(const boost::property_tree::ptree& pt) {
137 setRadius(pt.get("radius", 1.0f));
138 setResolution(pt.get("resolution", 16u));
139
140 Object3D::loadFromXML(pt);
141}
142
143} //namespace Divide
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr F32 M_PI_f
Definition: MathHelper.h:86
constexpr SceneNodeType GetSceneNodeType()
Definition: SceneNodeFwd.h:111
constexpr T SQUARED(T input) noexcept
Definition: MathHelper.inl:156
constexpr U16 U16_MAX
FORCE_INLINE Handle< T > CreateResource(const ResourceDescriptor< T > &descriptor, bool &wasInCache, std::atomic_uint &taskCounter)
void efficient_clear(eastl::fixed_vector< T, nodeCount, bEnableOverflow, OverflowAllocator > &fixed_vector)
Definition: Vector.h:52
FORCE_INLINE T * Get(const Handle< T > handle)
uint32_t U32