summaryrefslogtreecommitdiffhomepage
path: root/Magic2/M3DS.cpp
blob: b9d47f3b5316c97999e9ae427d55b528c6bc7e70 (plain)
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
/*  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 "ArrayList.h"

// +--------------------------------------------------------------------+

struct Chunk
{
   WORD  id;
   DWORD len;
   BYTE* start;

   List<Chunk> chunks;
};

// +--------------------------------------------------------------------+

struct Vec2
{
   float u,v;
};

struct Triangle
{
   WORD a,b,c;
};

struct Triangle2
{
   Vec3     vloc[3];
   Vec3     vnrm[3];
   Vec2     vtex[3];
   Vec3     fnrm;
   DWORD    mtl_id;
};

struct TextureMap
{
   char     fname[256];
   float    strength;
   float    u_scale;
   float    v_scale;
   float    u_offset;
   float    v_offset;
   float    angle;
};

// +--------------------------------------------------------------------+

class Object3DS
{
public:
   Object3DS();
   virtual ~Object3DS();

   virtual const char*  GetName();
   virtual bool         IsObject(const char* name);

protected:
    Text    name;
};

// +--------------------------------------------------------------------+

class Material3DS : public Object3DS
{
public:
   Material3DS();
   virtual ~Material3DS();

   DWORD       GetID();
   TextureMap& GetTextureMap1();
   TextureMap& GetTextureMap2();
   TextureMap& GetOpacityMap();
   TextureMap& GetSpecularMap();
   TextureMap& GetBumpMap();
   TextureMap& GetReflectionMap();
   ColorValue  GetAmbientColor();
   ColorValue  GetDiffuseColor();
   ColorValue  GetSpecularColor();
   float       GetShininess();
   float       GetTransparency();
   DWORD       GetShadingType();

   // this methods should not be used by the "user", they're used internally to fill the class
   // with valid data when reading from file. If you're about to add an importer for another format you'LL
   // have to use these methods

   void        SetID(DWORD value);
   void        SetAmbientColor(const ColorValue &color);
   void        SetDiffuseColor(const ColorValue &color);
   void        SetSpecularColor(const ColorValue &color);
   void        SetShininess(float value);
   void        SetTransparency(float value);
   void        SetShadingType(DWORD shading);

protected:
   int         id;
   TextureMap  texMap1;
   TextureMap  texMap2;
   TextureMap  opacMap;
   TextureMap  refTextureMap;
   TextureMap  bumpMap;
   TextureMap  specMap;
   ColorValue  ambient;
   ColorValue  diffuse;
   ColorValue  specular;
   float       shininess;
   float       transparency;
   DWORD       shading;
};

// +--------------------------------------------------------------------+

class Mesh3DS : public Object3DS
{
public:
    Mesh3DS();
    virtual ~Mesh3DS();

    void    Clear();

    DWORD   GetVertexCount();
    void    SetVertexArraySize(DWORD value);
    DWORD   GetTriangleCount();
    void    SetTriangleArraySize(DWORD value);

    const Vec3&   GetVertex(DWORD index);
    const Vec3&   GetNormal(DWORD index);
    const Vec2&   GetUV(DWORD index);
    const Vec3&   GetTangent(DWORD index);
    const Vec3&   GetBinormal(DWORD index);

    void          SetVertex(const Vec3 &vec, DWORD index);
    void          SetNormal(const Vec3 &vec, DWORD index);
    void          SetUV(const Vec2 &vec, DWORD index);
    void          SetTangent(const Vec3 &vec, DWORD index);
    void          SetBinormal(const Vec3 &vec, DWORD index);

    const Triangle&  GetTriangle(DWORD index);
    Triangle2        GetTriangle2(DWORD index);

    Matrix&       GetMatrix();
    void          SetMatrix(Matrix& m);
    void          Optimize(int level);

    DWORD         GetMaterial(DWORD index);
    DWORD         AddMaterial(DWORD id);
    DWORD         GetMaterialCount();

protected:
    // the vertices, normals, etc.
    List<Vec3>       vertices;
    List<Vec3>       normals;
    List<Vec3>       binormals;
    List<Vec3>       tangents;
    List<Vec2>       uv;

    // triangles
    List<Triangle>   triangles;

    // the transformation matrix.
    Matrix           matrix;

    // the material ID array
    ArrayList        materials;

    void CalcNormals(bool useSmoothingGroups);
    void CalcTextureSpace();

    void TransformVertices();
};


// +--------------------------------------------------------------------+
// +--------------------------------------------------------------------+
// +--------------------------------------------------------------------+

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* m, double scale)
{
   if (m && scale > 0 && strlen(filename) > 0) {
      ModelFile::Load(m, scale);

      FILE* fp = fopen(filename, "rb");
      fclose(fp);

      m->Normalize();
      return true;
   }

   return false;
}

// +--------------------------------------------------------------------+

bool
ModelFile3DS::Save(Model* m)
{
   if (m) {
      ModelFile::Save(m);

      FILE* f = fopen(filename, "w");
      if (!f) {
         ::MessageBox(0, "Export Failed: Magic could not open the file for writing", "ERROR", MB_OK);
         return false;
      }

      fclose(f);

      return true;
   }

   return false;
}

// +--------------------------------------------------------------------+