OpenSceneGraph 3.6.5
Target
Go to the documentation of this file.
1/* -*-c++-*-
2 * Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
3 *
4 * This library is open source and may be redistributed and/or modified under
5 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
6 * (at your option) any later version. The full license is in LICENSE file
7 * included with this distribution, and on the openscenegraph.org website.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * OpenSceneGraph Public License for more details.
13*/
14
15
16#ifndef OSGANIMATION_TARGET
17#define OSGANIMATION_TARGET 1
18
19#include <vector>
20#include <osg/Quat>
21#include <osg/Vec3>
22#include <osg/Vec2>
23#include <osg/Vec4>
24#include <osg/Referenced>
25#include <osgAnimation/Export>
26
27namespace osgAnimation
28{
29
30 class Channel;
31
33 {
34 public:
35
37 virtual ~Target() {}
38 void reset() { _weight = 0; _priorityWeight = 0; }
39 int getCount() const { return referenceCount(); }
40 float getWeight() const { return _weight; }
41 protected:
42 float _weight;
45 };
46
47
48 template <class T>
49 class TemplateTarget : public Target
50 {
51 public:
52
54 TemplateTarget(const T& v) { setValue(v); }
56
57 inline void lerp(float t, const T& a, const T& b);
58
68 void update(float weight, const T& val, int priority)
69 {
71 {
72 if (_lastPriority != priority)
73 {
74 // change in priority
75 // add to weight with the same previous priority cumulated weight
76 _weight += _priorityWeight * (1.0 - _weight);
78 _lastPriority = priority;
79 }
80
81 _priorityWeight += weight;
82 float t = (1.0 - _weight) * weight / _priorityWeight;
83 lerp(t, _target, val);
84 }
85 else
86 {
87 _priorityWeight = weight;
88 _lastPriority = priority;
89 _target = val;
90 }
91 }
92 const T& getValue() const { return _target; }
93
94 void setValue(const T& value) { _target = value; }
95
96 protected:
97
99 };
100
101 template <class T>
102 inline void TemplateTarget<T>::lerp(float t, const T& a, const T& b)
103 {
104 _target = a * (1.0f - t) + b * t;
105 }
106
107 template <>
108 inline void TemplateTarget<osg::Quat>::lerp(float t, const osg::Quat& a, const osg::Quat& b)
109 {
110 if (a.asVec4() * b.asVec4() < 0.0)
111 {
112 _target = a * (1.0f - t) + b * -t;
113 }
114 else
115 {
116 _target = a * (1.0f - t) + b * t;
117 }
118
119 osg::Quat::value_type len2 = _target.length2();
120 if ( len2 != 1.0 && len2 != 0.0)
121 _target *= 1.0/sqrt(len2);
122 }
123
131
132}
133
134#endif
The osgAnimation library provides general purpose utility classes for animation.
Definition Action:34
TemplateTarget< double > DoubleTarget
Definition Target:130
TemplateTarget< osg::Vec4 > Vec4Target
Definition Target:127
TemplateTarget< osg::Vec2 > Vec2Target
Definition Target:128
TemplateTarget< osg::Vec3 > Vec3Target
Definition Target:126
TemplateTarget< osg::Matrixf > MatrixTarget
Definition Target:124
TemplateTarget< osg::Quat > QuatTarget
Definition Target:125
TemplateTarget< float > FloatTarget
Definition Target:129
A quaternion class.
Definition Quat:30
Vec4d asVec4() const
Definition Quat:116
double value_type
Data type of vector components.
Definition Quat:38
Base class for providing reference counted objects.
Definition Referenced:44
int referenceCount() const
Return the number of pointers currently referencing this object.
Definition Referenced:97
int getCount() const
Definition Target:39
virtual ~Target()
Definition Target:37
float _weight
Definition Target:42
float _priorityWeight
Definition Target:43
void reset()
Definition Target:38
float getWeight() const
Definition Target:40
int _lastPriority
Definition Target:44
Definition Target:50
void lerp(float t, const T &a, const T &b)
Definition Target:102
const T & getValue() const
Definition Target:92
TemplateTarget()
Definition Target:53
void setValue(const T &value)
Definition Target:94
TemplateTarget(const T &v)
Definition Target:54
void update(float weight, const T &val, int priority)
The priority is used to detect a change of priority It's important to update animation target in prio...
Definition Target:68
UsingType _target
Definition Target:98
TemplateTarget(const TemplateTarget &v)
Definition Target:55
#define OSGANIMATION_EXPORT
Definition Export:40

osg logo
Generated at Wed Jul 23 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.