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
|
/* Starshatter: The Open Source Project
Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
Copyright (c) 1997-2006, Destroyer Studios LLC.
AUTHOR: John DiCamillo
OVERVIEW
========
File loader for 3DStudio MAX 3DS format models
*/
#include "stdafx.h"
#include "Magic.h"
#include "MagicDoc.h"
#include "ModelFile3DS.h"
#include "Bitmap.h"
#include "Polygon.h"
#include "Text.h"
#include "List.h"
#include "l3ds.h"
// +--------------------------------------------------------------------+
ModelFile3DS::ModelFile3DS(const char* fname)
: ModelFile(fname)
{
}
ModelFile3DS::~ModelFile3DS()
{
}
// +--------------------------------------------------------------------+
static int mcomp(const void* a, const void* b)
{
Poly* pa = (Poly*) a;
Poly* pb = (Poly*) b;
if (pa->sortval == pb->sortval)
return 0;
if (pa->sortval < pb->sortval)
return 1;
return -1;
}
bool
ModelFile3DS::Load(Model* model, double scale)
{
if (model && scale > 0 && strlen(filename) > 0) {
ModelFile::Load(model, scale);
L3DS loader;
if (!loader.LoadFile(filename)) {
::MessageBox(0, "3DS Import Failed: Magic could not open the file for reading", "ERROR", MB_OK);
return false;
}
int ntex = 0;
int nsurfs = 0;
int nverts = 0;
int npolys = 0;
int nmatls = 0;
int nmeshs = loader.GetMeshCount();
int* mesh_verts = new int[nmeshs];
for (int m = 0; m < nmeshs; m++) {
LMesh& mesh = loader.GetMesh(m);
mesh_verts[m] = nverts;
// count verts and polys:
nverts += mesh.GetVertexCount();
npolys += mesh.GetTriangleCount();
}
if (nverts > Model::MAX_VERTS || npolys > Model::MAX_POLYS) {
::MessageBox(0, "3DS Import Failed: model was too large (max 16000 polys)", "ERROR", MB_OK);
delete [] mesh_verts;
return FALSE;
}
// get materials:
nmatls = loader.GetMaterialCount();
for (int i = 0; i < nmatls; i++) {
LMaterial& matl = loader.GetMaterial(i);
LMap& tex_diff = matl.GetTextureMap1();
LMap& tex_spec = matl.GetSpecularMap();
LMap& tex_bump = matl.GetBumpMap();
LMap& tex_glow = matl.GetTextureMap2();
Material* material = new Material;
strncpy(material->name, matl.GetName().c_str(), Material::NAMELEN);
material->Ka.Set( matl.GetAmbientColor().r,
matl.GetAmbientColor().g,
matl.GetAmbientColor().b );
material->ambient_value = 1.0f;
material->ambient_color = material->Ka.ToColor();
material->Kd.Set( matl.GetDiffuseColor().r,
matl.GetDiffuseColor().g,
matl.GetDiffuseColor().b );
material->diffuse_value = 1.0f;
material->diffuse_color = material->Kd.ToColor();
material->Ks.Set( matl.GetSpecularColor().r,
matl.GetSpecularColor().g,
matl.GetSpecularColor().b );
material->specular_value = 1.0f;
material->specular_color = material->Ks.ToColor();
material->power = matl.GetShininess() * 100;
if (tex_diff.mapName[0])
LoadTexture(tex_diff.mapName, material->tex_diffuse);
if (tex_spec.mapName[0])
LoadTexture(tex_spec.mapName, material->tex_specular);
if (tex_bump.mapName[0])
LoadTexture(tex_bump.mapName, material->tex_bumpmap);
if (tex_glow.mapName[0])
LoadTexture(tex_glow.mapName, material->tex_emissive);
model->GetMaterials().append(material);
}
Surface* surface = new Surface;
model->GetSurfaces().append(surface);
surface->CreateVerts(nverts);
surface->CreatePolys(npolys);
VertexSet* vset = surface->GetVertexSet();
Poly* polys = surface->GetPolys();
float radius = 0;
int v = 0;
for (int m = 0; m < nmeshs && v < nverts; m++) {
LMesh& mesh = loader.GetMesh(m);
// read vertex set:
for (int i = 0; i < mesh.GetVertexCount() && v < nverts; i++) {
vset->loc[v].x = mesh.GetVertex(i).x;
vset->loc[v].y = mesh.GetVertex(i).z;
vset->loc[v].z = mesh.GetVertex(i).y;
vset->nrm[v].x = mesh.GetNormal(i).x;
vset->nrm[v].y = mesh.GetNormal(i).z;
vset->nrm[v].z = mesh.GetNormal(i).y;
vset->tu[v] = mesh.GetUV(i).x;
vset->tv[v] = mesh.GetUV(i).y;
float d = vset->loc[v].length();
if (d > radius)
radius = d;
v++;
}
}
if (radius < 16) {
model->ScaleBy(256.0f / radius);
radius = 256.0f;
}
int n = 0;
for (int m = 0; m < nmeshs && n < npolys; m++) {
LMesh& mesh = loader.GetMesh(m);
// read polys:
int mesh_tris = mesh.GetTriangleCount();
for (int i = 0; i < mesh_tris && n < npolys; i++) {
Poly* p = surface->GetPolys() + n++;
const LTriangle& tri = mesh.GetTriangle(i);
LTriangle2 tri2 = mesh.GetTriangle2(i);
p->nverts = 3;
p->verts[0] = tri.a + mesh_verts[m];
p->verts[1] = tri.b + mesh_verts[m];
p->verts[2] = tri.c + mesh_verts[m];
if (p->verts[0] > nverts || p->verts[1] > nverts || p->verts[2] > nverts) {
p->verts[0] = 0;
p->verts[1] = 1;
p->verts[2] = 2;
}
if (tri2.vertexNormals[0] == tri2.vertexNormals[1] &&
tri2.vertexNormals[0] == tri2.vertexNormals[2])
p->flatness = 1.0f;
else
p->flatness = 0.0f;
p->vertex_set = vset;
p->material = model->GetMaterials()[ tri2.materialId ];
p->sortval = tri2.materialId;
p->plane = Plane(vset->loc[ p->verts[0] ],
vset->loc[ p->verts[2] ],
vset->loc[ p->verts[1] ]);
surface->AddIndices(p->nverts);
}
}
// sort the polys by material index:
qsort((void*) polys, npolys, sizeof(Poly), mcomp);
// then assign them to cohesive segments:
Segment* segment = 0;
for (int n = 0; n < npolys; n++) {
if (segment && segment->material == polys[n].material) {
segment->npolys++;
}
else {
segment = 0;
}
if (!segment) {
segment = new Segment;
segment->npolys = 1;
segment->polys = &polys[n];
segment->material = segment->polys->material;
surface->GetSegments().append(segment);
}
}
*pnverts = nverts;
*pnpolys = npolys;
*pradius = radius;
model->Normalize();
return true;
}
return false;
}
// +--------------------------------------------------------------------+
bool
ModelFile3DS::Save(Model* m)
{
if (m) {
ModelFile::Save(m);
FILE* f = fopen(filename, "w");
if (!f) {
::MessageBox(0, "3DS Export Failed: Magic could not open the file for writing", "ERROR", MB_OK);
return false;
}
fclose(f);
return true;
}
return false;
}
// +--------------------------------------------------------------------+
|