-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjImp_2.cppbak
More file actions
306 lines (245 loc) · 6.93 KB
/
ObjImp_2.cppbak
File metadata and controls
306 lines (245 loc) · 6.93 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
extern "C"
{
#include "lwhost.h"
#include "lwobjimp.h"
#include "LWPlugBase.h"
#include "LoadQuakeMD.h"
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ModelMD3.h"
// Just shields to cover void datatypes
static LWMessageFuncs *Messages = 0;
static LWObjectImport *OI = 0;
static char *SurfaceNames[255];
static LWPntID *PointIDs = 0; // Array of point IDs in order of creation
static int LocalPointCount = 0;
static int SkinPointCount = 0;
/*
* Points added to LW 60 can' be 'merged' and then different surfaces can be applied by
* Vmaps. To do this we need to map from coordinates to point id
*/
typedef struct
{
LWPntID pointid;
LWFVector position;
} Sorted_LWPntID;
static Sorted_LWPntID *SortedPointIDs = 0; // Array of point IDs sorted by X - low to high
static int SortedPointCount = 0;
#define CLOSEENOUGH 0.001
static void AddPointtoLists(LWFVector ptBuf)
{ // See if we can find this value in the sorted list
int low = 0, high = SortedPointCount -1;
int middle = (low + high) >> 1;
LWPntID checkPt = {0};
if (SortedPointCount == 0)
goto ADDNEW;
for (;middle < SortedPointCount; middle++)
{
if (ptBuf[0] < SortedPointIDs[middle].position[0])
continue;
else if (ptBuf[0] > SortedPointIDs[middle].position[0])
break;
for (high = 0; high < 3; high ++)
{
if (ptBuf[high] != SortedPointIDs[middle].position[high])
break;
}
if (high == 3)
{ // this is a match - repeat the ID
PointIDs[LocalPointCount++] = SortedPointIDs[middle].pointid;
return;
}
}
/*
do
{
if (ptBuf[0] > SortedPointIDs[middle].position[0]) // check higher
{
low = middle +1;
middle = (low + high) >> 1;
continue;
}
else if (ptBuf[0] < SortedPointIDs[middle].position[0]) // check lower
{
high = middle -1;
middle = (low + high) >> 1;
continue;
}
break;
} while (middle < high);
// either we found nothing, or we found a match;
if (middle != high)
{ // potentially found a match
while (middle > 0 && ptBuf[0] == SortedPointIDs[middle -1].position[0])
--middle;
for (;middle < SortedPointCount && ptBuf[0] == SortedPointIDs[middle].position[0]
;middle ++)
{
for (low = 0; low < 3; low ++)
{
if (ptBuf[low] != SortedPointIDs[middle].position[low])
break;
}
if (low == 3)
{ // this is a match - repeat the ID
PointIDs[LocalPointCount++] = SortedPointIDs[middle].pointid;
return;
}
}
}
*/
ADDNEW:
// found nothing - move evrything up and shove this one in here
int newslot = middle;
while (middle < SortedPointCount)
SortedPointIDs[++middle] = SortedPointIDs[middle -1];
for (middle = 0; middle < 3; middle ++)
SortedPointIDs[newslot].position[middle] = ptBuf[middle];
++SortedPointCount;
SortedPointIDs[newslot].pointid =
PointIDs[LocalPointCount++] = OI->point(OI->data,ptBuf);
}
static unsigned char surfacechunk[_SURFACE_MAX +1][20] =
{
{'C','O','L','R',
0,14,
0x3F,0x70,0xF0,0xFA,
0x3F,0x70,0xF0,0xFA,
0x3F,0x70,0xF0,0xFA,
0,0},
{'C','O','L','R',
0,14,
0x3F,0x70,0xF0,0xFA,
0x3F,0x48,0xC8,0xCD,
0,0,0,0,
0,0},
{0}
};
// We really need to call these in the proper order
static void AddMD3SkinPoint(MD3_Point_Skin *p, const char *matl)
{
float vbuf[2];
vbuf[0] = p->tex[0];
vbuf[1] = 1.0 - p->tex[1]; // why is this upside down?
if (strncmp(matl,"tag_",4))
{ // Add a VMAP too if this isn't a tag
OI->vmap(OI->data,LWVMAP_TXUV,2,matl);
OI->vmapVal(OI->data,PointIDs[SkinPointCount++],vbuf);
}
}
static void AddMD3Point(MD3_Point_Frame *p)
{
LWFVector ptBuf;
ptBuf[0] = ((float)(p->v[0]))/64;
ptBuf[1] = ((float)(p->v[1]))/64;
ptBuf[2] = ((float)(p->v[2]))/64;
MoveVecbyOffsets(ptBuf);
AddPointtoLists(ptBuf);
}
static void AddMD3Polygon(MD3_Poly *p, int vertoffset, int srfIdx)
{
LWPolID polyid = 0;
LWPntID ptBuf[3];
ptBuf[0] = PointIDs[p->vind[0] + vertoffset];
ptBuf[1] = PointIDs[p->vind[1] + vertoffset];
ptBuf[2] = PointIDs[p->vind[2] + vertoffset];
polyid = OI->polygon(OI->data,LWPOLTYPE_FACE,0, 3, ptBuf);
OI->polTag(OI->data, polyid, LWPTAG_SURF, SurfaceNames[srfIdx]);
}
static void AddMD3TagPoints(MD3_Tag *t)
{
LWFVector ptBuf;
// Add a point 4 units forward (on the MD3 X)
ptBuf[0] = t->Position[0] + TAG_POLY_LEN * t->Matrix[0][0];
ptBuf[1] = t->Position[1] + TAG_POLY_LEN * t->Matrix[0][1];
ptBuf[2] = t->Position[2] + TAG_POLY_LEN * t->Matrix[0][2];
MoveVecbyOffsets(ptBuf);
AddPointtoLists(ptBuf);
// Add a point 2 units right (on the MD3 -Y)
ptBuf[0] = t->Position[0] - TAG_POLY_WIDTH * t->Matrix[1][0];
ptBuf[1] = t->Position[1] - TAG_POLY_WIDTH * t->Matrix[1][1];
ptBuf[2] = t->Position[2] - TAG_POLY_WIDTH * t->Matrix[1][2];
MoveVecbyOffsets(ptBuf);
AddPointtoLists(ptBuf);
// Add anchor position as a point
ptBuf[0] = t->Position[0];
ptBuf[1] = t->Position[1];
ptBuf[2] = t->Position[2];
MoveVecbyOffsets(ptBuf);
AddPointtoLists(ptBuf);
}
static int AddMD3Surf(char *name, SURFACE_TYPES surftype)
{
int srfIdx = 0;
for (;SurfaceNames[srfIdx]; srfIdx++)
{
if (strcmp(name,SurfaceNames[srfIdx]) == 0)
break;
}
if (SurfaceNames[srfIdx] == 0)
{ // New one
SurfaceNames[srfIdx] = name;
SurfaceNames[srfIdx +1] = 0;
}
OI->surface(OI->data,name,NULL,20,(void *)(surfacechunk[surftype]));
return srfIdx;
}
static BuildFuncs BF = {AddMD3SkinPoint, AddMD3Point, AddMD3Polygon, AddMD3TagPoints, AddMD3Surf};
static int ParseMD3FromFile()
{
int retval = AFUNC_OK;
LWFVector pivot = {0.0f};
CurrentFunc = &BF;
// Parse file into local memory
MD3 *mdl = new MD3(OI->filename);
// Do Panel stuff
if ((retval = GetDataFromUser(mdl)) != AFUNC_OK)
return retval;
// ===Now build the object inside Lightwave===
OI->layer(OI->data, 1,0);
OI->pivot(OI->data, pivot );
// State the number of points ...
int TotalPntCount = (mdl->TagCount() * 3) + mdl->TotalMeshPoints();
// leave out skin points, we're doing this the new way
PointIDs = (LWPntID *)calloc(TotalPntCount+1, sizeof(LWPntID));
SortedPointIDs = (Sorted_LWPntID *)calloc(TotalPntCount+1, sizeof(Sorted_LWPntID));
//OI->numPoints(OI->data,TotalPntCount);
return (BuildMD3(mdl, TotalPntCount));
}
int LW60_LoadinMD3(BuildData *myData)
{
SurfaceNames[0] = 0;
int retval = AFUNC_OK;
CurrentData = myData;
Messages = (LWMessageFuncs *)(CurrentData->Message);
OI = (LWObjectImport *)(CurrentData->Funcs);
OI->result = LWOBJIM_FAILED;
if(!myData->Panel)
{
(*Messages->error)("Unable to activate global "PANEL_SERVICES_NAME, " please add plugin lwpanels.p" );
goto RETURN;
}
if (! IsMD3NameOK(OI->filename))
{
SetFailureStuff(OI,"Bad Filename: ",(char *)OI->filename);
OI->result = LWOBJIM_NOREC ;
goto RETURN;
}
retval = ParseMD3FromFile();
if (retval != AFUNC_OK)
{
if (PointIDs) delete PointIDs;
OI->result = LWOBJIM_ABORTED;
goto RETURN;
}
FindLastPathfromName(OI->filename);
OI->result = LWOBJIM_OK;
OI->done(OI->data);
OI->failedLen = 0;
RETURN:
if (PointIDs) delete PointIDs;
LocalPointCount = SkinPointCount = 0;
return retval;
}