-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstance.cpp
More file actions
131 lines (111 loc) · 5.08 KB
/
Instance.cpp
File metadata and controls
131 lines (111 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
///
/// Langulus::Module::Physics
/// Copyright (c) 2017 Dimo Markov <team@langulus.com>
/// Part of the Langulus framework, see https://langulus.com
///
/// SPDX-License-Identifier: GPL-3.0-or-later
///
#include "Instance.hpp"
#include "Physics.hpp"
#include <Langulus/Mesh.hpp>
using namespace Euclidean;
/// Instance construction
/// @param producer - the world that owns the instance
/// @param descriptor - instance descriptor
Instance::Instance(World* producer, const Many& descriptor)
: Resolvable {this}
, ProducedFrom {producer, descriptor} {
VERBOSE_PHYSICS("Initializing...");
Couple(descriptor);
SeekValueAux<Traits::Place >(descriptor, mData.mPosition );
SeekValueAux<Traits::Size >(descriptor, mData.mScale );
SeekValueAux<Traits::Aim >(descriptor, mData.mAim );
SeekValueAux<Traits::Level >(descriptor, mData.mLevel );
SeekValueAux<Traits::Acceleration>(descriptor, mData.mAcceleration);
SeekValueAux<Traits::Bilateral >(descriptor, mData.mBilateral );
SeekValueAux<Traits::Pickable >(descriptor, mData.mPickable );
SeekValueAux<Traits::Solid >(descriptor, mData.mSolid );
SeekValueAux<Traits::Velocity >(descriptor, mData.mVelocity );
SeekValueAux<Traits::Static >(descriptor, mData.mStatic );
SeekValueAux(descriptor, mColor);
mData.mAim.w = 1;
VERBOSE_PHYSICS("Initialized");
}
/// Update the instance
/// @param dt - time between updates, in seconds
void Instance::Update(Real dt) {
// Apply one-time impulse
mData.mImpulse +=
(mData.mUseImpulse * mData.mUseBoundness) +
(mData.mSimImpulse * mData.mSimBoundness);
mData.mUseImpulse = 0;
mData.mSimImpulse = 0;
mData.mPosition += mData.mImpulse * dt;
mData.mImpulse = 0;
// Change velocity and apply it
mData.mVelocity +=
(mData.mUseVelocity * mData.mUseBoundness) +
(mData.mSimVelocity * mData.mSimBoundness);
mData.mUseVelocity = 0;
mData.mSimVelocity = 0;
mData.mPosition = mData.GetPositionNext(dt);
mData.mVelocity = mData.GetVelocityNext(dt);
// Change level
mData.mLevel +=
(mData.mUseLevelChange * Level(dt * mData.mUseBoundness)) +
(mData.mSimLevelChange * Level(dt * mData.mSimBoundness));
mData.mSimLevelChange = 0;
mData.mUseLevelChange = 0;
}
/// First stage destruction
void Instance::Teardown() {
mDomain.Reset();
mData.mParent.Reset();
}
/// Refresh the instance's properties on environment change
void Instance::Refresh() {
mDomain = SeekUnit<A::Mesh>();
}
/// Move, rotate, resize verb
/// @param verb - the move verb
void Instance::Move(Verb& verb) {
mData.Move(verb);
}
/// Cull the instance, based on lod state
/// @param state - the lod state to test
/// @return true if instance is culled (doesn't intersect frusta)
bool Instance::Cull(const LOD& state) const noexcept {
// Quick octave-based cull
if (state.mLevel >= mData.mLevel + 1) {
// We're looking at the instance from a higher octave
// Discard - the thing is likely too small to be seen
// Small things will be batched as points/volumes and drawn
// separately. This is not the place for them
return true;
}
if (mData.mScale.IsDegenerate()) {
// If scale is zero, then instance is never culled
//TODO maybe do it opposite and use infinite scale to not cull
return false;
}
const auto box = mData.GetRangeRotated(state.mLevel);
return not state.mFrustum.Intersects(box);
}
Level Instance::GetLevel() const noexcept {
return mData.mLevel;
}
Mat4 Instance::GetModelTransform(const LOD& lod) const noexcept {
return mData.GetModelTransform(lod.mLevel);
}
Mat4 Instance::GetModelTransform(const Level& level) const noexcept {
return mData.GetModelTransform(level);
}
Mat4 Instance::GetViewTransform(const LOD& lod) const noexcept {
return mData.GetViewTransform(lod.mLevel);
}
Mat4 Instance::GetViewTransform(const Level& level) const noexcept {
return mData.GetViewTransform(level);
}
auto Instance::GetColor() const noexcept -> RGBA {
return *mColor;
}