-
Notifications
You must be signed in to change notification settings - Fork 0
/
C3DModel.cpp
executable file
·287 lines (232 loc) · 6.09 KB
/
C3DModel.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
C3DModel.cpp
Author: Tom Naughton
Description: <describe the C3DModel class here>
*/
#include "C3DModel.h"
#include "CPakStream.h"
#include "CGLImage.h"
#include "CResourceManager.h"
float avertexnormals[NUMVERTEXNORMALS][3] =
{
#include "anorms.h" // big fat table of normals
};
C3DModel::C3DModel(CResourceManager *resources)
{
_textures = new vector<CGLImage*>(10);
_resources = resources;
_textureCount = 0;
}
C3DModel::~C3DModel()
{
// throw away all the textures
if (_textures) {
vector<CGLImage*>::iterator e = _textures->begin();
while (e != _textures->end()) {
CGLImage* image = *e;
if (image)
delete image;
e++;
}
delete _textures;
}
}
SInt16 C3DModel::frameCount()
{
return 0;
}
UInt32 C3DModel::tagNum()
{
return 0;
}
UInt32 C3DModel::meshNum()
{
return 0;
}
ModelType C3DModel::modelType()
{
return unknown_model;
}
Boolean C3DModel::shouldDraw(renderingAttributes_t *rendering, CShader *shader)
{
Boolean result = rendering->_renderOpaque;
if (shader) {
result = (shader->isTransparent() && rendering->_renderBlend)
|| (!shader->isTransparent() && rendering->_renderOpaque)
|| rendering->_renderTextureCoordinates
|| rendering->_renderTexturePreview;
}
return result;
}
Boolean C3DModel::needsNormals(renderingAttributes_t *rendering, CShader *shader)
{
Boolean result = rendering->_lighting || rendering->_showNormals;
if (shader)
result = result || shader->needsNormals();
return result;
}
void C3DModel::loadMeshTextures(CModelInstance *instance)
{
#pragma unused (instance)
}
int C3DModel::tagIndexWithName(const char *name)
{
#pragma unused (name)
return -1;
}
string C3DModel::tagNameForFrameAtIndex(short frame, short index)
{
#pragma unused (frame, index)
return "";
}
md3_tag_t *C3DModel::tagForFrameAtIndex(short frame, short index)
{
#pragma unused (frame, index)
return 0;
}
void C3DModel::DrawLinks(CModelInstance *instance, renderingAttributes_t *rendering)
{
#pragma unused (instance, rendering)
}
Mat4 C3DModel::PushTagTransformMatrix(CModelInstance *instance, int tagindex)
{
#pragma unused (instance, tagindex)
Mat4 result;
return result;
}
Boolean C3DModel::canExportFormat(ExportFormatT format)
{
#pragma unused (format)
return false;
}
CMd3AnimationInfo *C3DModel::animationInfo()
{
return 0;
}
void C3DModel::addTexture(CGLImage* texture)
{
(*_textures)[_textureCount] = texture;
_textureCount++;
}
CGLImage *C3DModel::textureWithName(string textureName, string skinnname)
{
#pragma unused (textureName, skinnname)
string keyname = textureName;
CGLImage *texture = 0;
texture = _textureMap[keyname];
if (texture)
return texture;
return texture;
}
void C3DModel::setTextureWithName(CGLImage *texture, string textureName)
{
_textureMap[textureName] = texture;
}
/*
Function: Interpolate
Usage: valut = Interpolate(interpSet, u);
---------------------------------------------------------------------------
Interpolate ranges from 0.0 to 1.0
where 0.0 is for at p0 (pn1 for cubic)
and 1.0 is for at p1 (p0 for cubic)
Method Result
INTERP_NONE p0
INTERP_LINEAR straight line between p0 and p1
INTERP_CUBIC smooth curve between pn1 and p0
Note that since out of 4 points are needed for cubic and the interpolation
is done between the middle two, so without knowing 2 frames ahead what frame
will come, cubic interpolations are lagged behind by a frame.
The uLast value is stored as well as u2 and u3 so that repetative u's
don't require recalculating u2 and u3.
*/
float C3DModel::Interpolate(float p[], float u, InterpMethodT method)
{
// to make indexing of the points array p[] make more sense
#define pn2 0
#define pn1 1
#define p0 2
#define p1 3
static float uLast = 0; // u value for last interpolation
static float u2 = 0; // u^2 value (preserved between calls)
static float u3 = 0; // u^3 value (preserved between calls)
// float a,b,c,d; // parameters for cubic interpolation
float tn1, t0; // tangents at 2 points being interpolated
switch(method)
{
case INTERP_NONE:
return p[p0];
// break;
case INTERP_LINEAR:
// values are frequently the same, so we will optimize for this
if(p[p0] == p[p1])
return p[p0];
else
return (p[p0] * (1 - u) + p[p1] * (u));
case INTERP_CUBIC:
/* Since we don't know 2 frames ahead, we will interpolate between pn1 and p0
as in:
(interpolate a
curve HERE)
pn1 --------- p0
/ \
/ \
/ \
pn2 \
p1
Points along an interpolated curve are returned according to the parameter u.
The curve is made using what I believe is called a Cardinal Spline algorithm.
For more info on interpolating splines, look up Hermite Splines, Cardinal
Splines, or Cubic Spline Interpolation Methods.
*/
// only calculate u2 and u3 if u has changed
if(u != uLast)
{
//u = interpolate;
u2 = u*u;
u3 = u2*u;
uLast = u;
}
/*
// Simple, unoptimized method of computing interpolated value
// tangent at pn1
t0 = (p[p1] - p[pn1]) / 2;
// tangent at p0
tn1 = (p[p0] - p[pn2]) / 2;
a = (2 * p[pn1]) + (-2 * p[p0]) + (1 * tn1) + (1 * t0);
b = (-3 * p[pn1]) + (3 * p[p0]) + (-2 * tn1) + (-1 * t0);
c = (0 * p[pn1]) + (0 * p[p0]) + (1 * tn1) + (0 * t0);
d = (1 * p[pn1]) + (0 * p[p0]) + (0 * tn1) + (0 * t0);
return a*u3 + b*u2 + c*u + d;
*/
// slightly optimized (I think):
tn1 = ((p[p0] - p[pn2]) / 2);
t0 = ((p[p1] - p[pn1]) / 2);
return
(
+ ( + (2 * p[pn1])
+ (-2 * p[p0])
+ (1 * (tn1))
+ (1 * (t0))
) * u3
+ ( + (-3 * p[pn1])
+ (3 * p[p0])
+ (-2 * tn1)
+ (-1 * t0)
) * u2
+ ( + tn1
) * u
+ ( + p[pn1]
) * 1
);
//break;
default:
dprintf("Invalid interpolation method\n");
break;
}
return 0; // actual return done by the switch cases
}
string C3DModel::frameName(SInt16 frame)
{
#pragma unused (frame)
return string("unimplemented");
}