Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
tcbspline.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : WWMath *
24 * *
25 * $Archive:: /VSS_Sync/wwmath/tcbspline.cpp $*
26 * *
27 * Author:: Greg Hjelstrom *
28 * *
29 * $Modtime:: 6/13/01 2:18p $*
30 * *
31 * $Revision:: 6 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37
38#include "tcbspline.h"
39#include "wwdebug.h"
40#include "persistfactory.h"
41#include "wwmathids.h"
42#include "wwhack.h"
43
44
45/*
46** Force-Link this module because the linker can't detect that we actually need it...
47*/
49
50/*
51** Save-Load stuff
52*/
54
55enum
56{
57 // ID's used by TCBSpline3D
58 TCB3D_CHUNK_HERMITE3D = 0x02071009,
60};
61
62
63/*
64** TCBSpline3DClass Implemenation
65*/
66
67int TCBSpline3DClass::Add_Key(const Vector3 & point,float t)
68{
69 int index;
70 index = HermiteSpline3DClass::Add_Key(point,t);
71
72 TCBClass params;
73 params.Tension = 0.0f;
74 params.Continuity = 0.0f;
75 params.Bias = 0.0f;
76 Params.Insert(index,params);
77 return index;
78}
79
85
91
92void TCBSpline3DClass::Set_TCB_Params(int i,float tension,float continuity,float bias)
93{
94 WWASSERT(i >= 0);
95 WWASSERT(i < Params.Count());
96 Params[i].Tension = tension;
97 Params[i].Continuity = continuity;
98 Params[i].Bias = bias;
99 TangentsDirty = true;
100}
101
102void TCBSpline3DClass::Get_TCB_Params(int i,float *tension,float *continuity,float *bias)
103{
104 if (tension) *tension = Params[i].Tension;
105 if (continuity) *continuity = Params[i].Continuity;
106 if (bias) *bias = Params[i].Bias;
107}
108
110{
111 if (Keys.Count() < 2) {
112 for (int i=0; i<Keys.Count(); i++) {
113 Tangents[0].InTangent.Set(0,0,0);
114 Tangents[0].OutTangent.Set(0,0,0);
115 }
116 }
117
118 // First and Last Key:
119 // Only need to compute the OutTangent for key[0] and the InTangent for key[end]
120 int end = Keys.Count() - 1;
121 Tangents[0].InTangent.Set(0,0,0);
122 Tangents[end].OutTangent.Set(0,0,0);
123
124 if (IsLooping) {
125
126 // This really only works if the start and end points have the same position...
127 // Also just using the TCB params from p0 for both
128 float k0 = 0.5f * ((1-Params[0].Tension) * (1-Params[0].Continuity) * (1-Params[0].Bias));
129 float k1 = 0.5f * ((1-Params[0].Tension) * (1+Params[0].Continuity) * (1+Params[0].Bias));
130 float k2 = 0.5f * ((1-Params[0].Tension) * (1+Params[0].Continuity) * (1-Params[0].Bias));
131 float k3 = 0.5f * ((1-Params[0].Tension) * (1-Params[0].Continuity) * (1+Params[0].Bias));
132
133 Vector3 dp_in;
134 Vector3 dp_out;
135 Vector3::Subtract(Keys[0].Point,Keys[end-1].Point,&dp_in);
136 Vector3::Subtract(Keys[1].Point,Keys[0].Point,&dp_out);
137
138 Vector3::Add(k0*dp_in, k1*dp_out, &Tangents[end].InTangent);
139 Vector3::Add(k2*dp_out, k3*dp_in, &Tangents[0].OutTangent);
140
141 } else {
142
143 float k2 = 0.25f * ((1-Params[0].Tension) * (1+Params[0].Continuity) * (1-Params[0].Bias));
144 float k3 = 0.25f * ((1-Params[0].Tension) * (1-Params[0].Continuity) * (1+Params[0].Bias));
145
146 Vector3 dp_in;
147 Vector3 dp_out;
148 Vector3::Subtract(Keys[1].Point,Keys[0].Point,&dp_out);
149 dp_in = dp_out;
150 Vector3::Add(k2*dp_out, k3*dp_in, &Tangents[0].OutTangent);
151
152 float k0 = 0.25f * ((1-Params[0].Tension) * (1-Params[0].Continuity) * (1-Params[0].Bias));
153 float k1 = 0.25f * ((1-Params[0].Tension) * (1+Params[0].Continuity) * (1+Params[0].Bias));
154 Vector3::Subtract(Keys[end].Point,Keys[end-1].Point,&dp_in);
155 dp_out = dp_in;
156 Vector3::Add(k0*dp_out, k1*dp_in, &Tangents[end].InTangent);
157 }
158
159 float total_time = (Keys[1].Time - Keys[0].Time) + (Keys[end].Time - Keys[end-1].Time);
160 float in_factor = 2.0f * (Keys[end].Time - Keys[end-1].Time) / total_time;
161 float out_factor = 2.0f * (Keys[1].Time - Keys[0].Time) / total_time;
162 Tangents[end].InTangent *= in_factor;
163 Tangents[0].OutTangent *= out_factor;
164
165
166 // Now compute the tangents of all of the normal keys...
167 for (int pi=1;pi<Keys.Count() - 1; pi++) {
168
169 float k0 = 0.5f * ((1-Params[pi].Tension) * (1-Params[pi].Continuity) * (1-Params[pi].Bias));
170 float k1 = 0.5f * ((1-Params[pi].Tension) * (1+Params[pi].Continuity) * (1+Params[pi].Bias));
171 float k2 = 0.5f * ((1-Params[pi].Tension) * (1+Params[pi].Continuity) * (1-Params[pi].Bias));
172 float k3 = 0.5f * ((1-Params[pi].Tension) * (1-Params[pi].Continuity) * (1+Params[pi].Bias));
173
174 Vector3 dp_in;
175 Vector3 dp_out;
176 Vector3::Subtract(Keys[pi].Point,Keys[pi-1].Point,&dp_in);
177 Vector3::Subtract(Keys[pi+1].Point,Keys[pi].Point,&dp_out);
178
179 Vector3::Add(k0*dp_out, k1*dp_in, &Tangents[pi].InTangent);
180 Vector3::Add(k2*dp_out, k3*dp_in, &Tangents[pi].OutTangent);
181
182 float total_time = (Keys[pi+1].Time - Keys[pi-1].Time);
183 float in_factor = 2.0f * (Keys[pi].Time - Keys[pi-1].Time) / total_time;
184 float out_factor = 2.0f * (Keys[pi+1].Time - Keys[pi].Time) / total_time;
185
186 Tangents[pi].InTangent *= in_factor; // compensating for un-even keys
187 Tangents[pi].OutTangent *= out_factor;
188
189 }
190 TangentsDirty = false;
191}
192
197
199{
202 csave.End_Chunk();
203
205 for (int i=0; i<Params.Count(); i++) {
206 csave.Write(&(Params[i].Tension),sizeof(Params[i].Tension));
207 csave.Write(&(Params[i].Continuity),sizeof(Params[i].Continuity));
208 csave.Write(&(Params[i].Bias),sizeof(Params[i].Bias));
209 }
210 csave.End_Chunk();
211 return true;
212}
213
215{
216 int i;
217 TCBClass param;
218
219 // reset the keys
220 Params.Delete_All();
221
222 // read in the chunks
223 while (cload.Open_Chunk()) {
224
225 switch(cload.Cur_Chunk_ID())
226 {
229 break;
230
232 for (i=0; i<Keys.Count(); i++) {
233 cload.Read(&(param.Tension),sizeof(param.Tension));
234 cload.Read(&(param.Continuity),sizeof(param.Continuity));
235 cload.Read(&(param.Bias),sizeof(param.Bias));
236 Params.Add(param);
237 }
238 break;
239
240 default:
241 WWDEBUG_SAY(("Unhandled Chunk: 0x%X File: %s Line: %d\r\n",__FILE__,__LINE__));
242 break;
243 }
244 cload.Close_Chunk();
245 }
246
247 return true;
248}
#define WWASSERT
bool Close_Chunk()
Definition chunkio.cpp:448
uint32 Cur_Chunk_ID()
Definition chunkio.cpp:484
uint32 Read(void *buf, uint32 nbytes)
Definition chunkio.cpp:692
bool Open_Chunk()
Definition chunkio.cpp:412
uint32 Write(const void *buf, uint32 nbytes)
Definition chunkio.cpp:264
bool Begin_Chunk(uint32 id)
Definition chunkio.cpp:108
bool End_Chunk()
Definition chunkio.cpp:148
bool IsLooping
Definition curve.h:103
DynamicVectorClass< KeyClass > Keys
Definition curve.h:104
virtual bool Save(ChunkSaveClass &csave)
virtual void Remove_Key(int i)
virtual void Clear_Keys(void)
virtual bool Load(ChunkLoadClass &cload)
DynamicVectorClass< TangentsClass > Tangents
virtual int Add_Key(const Vector3 &point, float t)
virtual void Set_TCB_Params(int i, float tension, float continuity, float bias)
Definition tcbspline.cpp:92
void Update_Tangents(void)
virtual int Add_Key(const Vector3 &point, float t)
Definition tcbspline.cpp:67
virtual bool Load(ChunkLoadClass &cload)
virtual void Clear_Keys(void)
Definition tcbspline.cpp:86
virtual void Remove_Key(int i)
Definition tcbspline.cpp:80
virtual void Get_TCB_Params(int i, float *tension, float *continuity, float *bias)
DynamicVectorClass< TCBClass > Params
Definition tcbspline.h:81
virtual const PersistFactoryClass & Get_Factory(void) const
virtual bool Save(ChunkSaveClass &csave)
static WWINLINE void Subtract(const Vector3 &a, const Vector3 &b, Vector3 *c)
Definition vector3.h:576
static WWINLINE void Add(const Vector3 &a, const Vector3 &b, Vector3 *c)
Definition vector3.h:555
@ TCB3D_CHUNK_HERMITE3D
Definition tcbspline.cpp:58
@ TCB3D_CHUNK_PARAMS
Definition tcbspline.cpp:59
SimplePersistFactoryClass< TCBSpline3DClass, WWMATH_CHUNKID_TCBSPLINE3D > _TCBSpline3DFactory
Definition tcbspline.cpp:53
#define WWDEBUG_SAY(x)
Definition wwdebug.h:114
#define DECLARE_FORCE_LINK(module)
Definition wwhack.h:48