2 This file is part of the graphics core library.
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* motion controllers (part of the animation system)
23 * Author: John Tsiombikas 2004
26 #include "3dengfx_config.h"
28 #include "controller.hpp"
30 MotionController::MotionController(ControllerClass ctype, TimelineMode mode) {
31 start_time = end_time = 0;
37 freq_func = ampl_func = 0;
39 slope = Vector3(1, 1, 1);
41 axis_flags = CTRL_XYZ;
44 MotionController::MotionController(Curve *curve, unsigned long start, unsigned long end, TimelineMode mode) {
50 ctrl_type = CTRL_CURVE;
52 freq_func = ampl_func = 0;
54 slope = Vector3(1, 1, 1);
56 axis_flags = CTRL_XYZ;
59 void MotionController::set_curve(Curve *curve) {
63 void MotionController::set_sin_func(scalar_t freq, scalar_t ampl, scalar_t phase) {
69 void MotionController::set_sin_func(scalar_t (*freq_func)(scalar_t), scalar_t (*ampl_func)(scalar_t)) {
70 this->freq_func = freq_func;
71 this->ampl_func = ampl_func;
74 void MotionController::set_origin(scalar_t orig) {
75 this->orig = Vector3(orig, orig, orig);
78 void MotionController::set_origin(const Vector3 &orig_vec) {
82 void MotionController::set_slope(scalar_t slope) {
83 this->slope = Vector3(slope, slope, slope);
86 void MotionController::set_slope(const Vector3 &slope_vec) {
90 void MotionController::set_timing(unsigned long start, unsigned long end) {
95 void MotionController::set_timeline_mode(TimelineMode tmode) {
99 void MotionController::set_controller_type(ControllerClass ctype) {
103 void MotionController::set_control_axis(unsigned int axis_flags) {
104 this->axis_flags = axis_flags;
107 Curve *MotionController::get_curve() {
111 unsigned long MotionController::get_start_time() const {
115 unsigned long MotionController::get_end_time() const {
119 TimelineMode MotionController::get_timeline_mode() const {
123 unsigned int MotionController::get_control_axis() const {
128 Vector3 MotionController::operator ()(unsigned long time) const {
129 time = get_timeline_time(time, start_time, end_time, time_mode);
131 double (*sinusoidal)(double);
137 scalar_t t = (scalar_t)(time - start_time) / (scalar_t)(end_time - start_time);
138 Vector3 vec = (*curve)(t);
139 if(!(axis_flags & CTRL_X)) vec.x = 0;
140 if(!(axis_flags & CTRL_Y)) vec.y = 0;
141 if(!(axis_flags & CTRL_Z)) vec.z = 0;
149 scalar_t t = (scalar_t)time / 1000.0f;
150 scalar_t frequency = freq_func ? freq_func(t) : freq;
151 scalar_t amplitude = ampl_func ? ampl_func(t) : ampl;
152 scalar_t result = sinusoidal((phase + t) * frequency) * amplitude;
154 Vector3 vec(0, 0, 0);
155 if(axis_flags & CTRL_X) vec.x = result;
156 if(axis_flags & CTRL_Y) vec.y = result;
157 if(axis_flags & CTRL_Z) vec.z = result;
163 scalar_t t = (scalar_t)time / 1000.0f;
164 Vector3 result = orig + slope * t;
166 Vector3 vec(0, 0, 0);
167 if(axis_flags & CTRL_X) vec.x = result.x;
168 if(axis_flags & CTRL_Y) vec.y = result.y;
169 if(axis_flags & CTRL_Z) vec.z = result.z;
173 return Vector3(); // Should not happen