Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
M3DS.cpp
Go to the documentation of this file.
1 /* Project Magic 2.0
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Magic.exe
6  FILE: ModelFile3DS.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  File loader for 3DStudio MAX 3DS format models
13 */
14 
15 #include "stdafx.h"
16 #include "Magic.h"
17 #include "MagicDoc.h"
18 #include "ModelFile3DS.h"
19 
20 #include "Bitmap.h"
21 #include "Polygon.h"
22 #include "Text.h"
23 #include "List.h"
24 #include "ArrayList.h"
25 
26 // +--------------------------------------------------------------------+
27 
28 struct Chunk
29 {
30  WORD id;
31  DWORD len;
32  BYTE* start;
33 
35 };
36 
37 // +--------------------------------------------------------------------+
38 
39 struct Vec2
40 {
41  float u,v;
42 };
43 
44 struct Triangle
45 {
46  WORD a,b,c;
47 };
48 
49 struct Triangle2
50 {
51  Vec3 vloc[3];
52  Vec3 vnrm[3];
53  Vec2 vtex[3];
55  DWORD mtl_id;
56 };
57 
58 struct TextureMap
59 {
60  char fname[256];
61  float strength;
62  float u_scale;
63  float v_scale;
64  float u_offset;
65  float v_offset;
66  float angle;
67 };
68 
69 // +--------------------------------------------------------------------+
70 
71 class Object3DS
72 {
73 public:
74  Object3DS();
75  virtual ~Object3DS();
76 
77  virtual const char* GetName();
78  virtual bool IsObject(const char* name);
79 
80 protected:
82 };
83 
84 // +--------------------------------------------------------------------+
85 
86 class Material3DS : public Object3DS
87 {
88 public:
89  Material3DS();
90  virtual ~Material3DS();
91 
92  DWORD GetID();
102  float GetShininess();
103  float GetTransparency();
104  DWORD GetShadingType();
105 
106  // this methods should not be used by the "user", they're used internally to fill the class
107  // with valid data when reading from file. If you're about to add an importer for another format you'LL
108  // have to use these methods
109 
110  void SetID(DWORD value);
111  void SetAmbientColor(const ColorValue &color);
112  void SetDiffuseColor(const ColorValue &color);
113  void SetSpecularColor(const ColorValue &color);
114  void SetShininess(float value);
115  void SetTransparency(float value);
116  void SetShadingType(DWORD shading);
117 
118 protected:
119  int id;
129  float shininess;
131  DWORD shading;
132 };
133 
134 // +--------------------------------------------------------------------+
135 
136 class Mesh3DS : public Object3DS
137 {
138 public:
139  Mesh3DS();
140  virtual ~Mesh3DS();
141 
142  void Clear();
143 
144  DWORD GetVertexCount();
145  void SetVertexArraySize(DWORD value);
146  DWORD GetTriangleCount();
147  void SetTriangleArraySize(DWORD value);
148 
149  const Vec3& GetVertex(DWORD index);
150  const Vec3& GetNormal(DWORD index);
151  const Vec2& GetUV(DWORD index);
152  const Vec3& GetTangent(DWORD index);
153  const Vec3& GetBinormal(DWORD index);
154 
155  void SetVertex(const Vec3 &vec, DWORD index);
156  void SetNormal(const Vec3 &vec, DWORD index);
157  void SetUV(const Vec2 &vec, DWORD index);
158  void SetTangent(const Vec3 &vec, DWORD index);
159  void SetBinormal(const Vec3 &vec, DWORD index);
160 
161  const Triangle& GetTriangle(DWORD index);
162  Triangle2 GetTriangle2(DWORD index);
163 
164  Matrix& GetMatrix();
165  void SetMatrix(Matrix& m);
166  void Optimize(int level);
167 
168  DWORD GetMaterial(DWORD index);
169  DWORD AddMaterial(DWORD id);
170  DWORD GetMaterialCount();
171 
172 protected:
173  // the vertices, normals, etc.
179 
180  // triangles
182 
183  // the transformation matrix.
185 
186  // the material ID array
187  ArrayList materials;
188 
189  void CalcNormals(bool useSmoothingGroups);
190  void CalcTextureSpace();
191 
192  void TransformVertices();
193 };
194 
195 
196 // +--------------------------------------------------------------------+
197 // +--------------------------------------------------------------------+
198 // +--------------------------------------------------------------------+
199 
200 ModelFile3DS::ModelFile3DS(const char* fname)
201  : ModelFile(fname)
202 {
203 }
204 
206 {
207 }
208 
209 // +--------------------------------------------------------------------+
210 
211 static int mcomp(const void* a, const void* b)
212 {
213  Poly* pa = (Poly*) a;
214  Poly* pb = (Poly*) b;
215 
216  if (pa->sortval == pb->sortval)
217  return 0;
218 
219  if (pa->sortval < pb->sortval)
220  return 1;
221 
222  return -1;
223 }
224 
225 bool
226 ModelFile3DS::Load(Model* m, double scale)
227 {
228  if (m && scale > 0 && strlen(filename) > 0) {
229  ModelFile::Load(m, scale);
230 
231  FILE* fp = fopen(filename, "rb");
232  fclose(fp);
233 
234  m->Normalize();
235  return true;
236  }
237 
238  return false;
239 }
240 
241 // +--------------------------------------------------------------------+
242 
243 bool
245 {
246  if (m) {
247  ModelFile::Save(m);
248 
249  FILE* f = fopen(filename, "w");
250  if (!f) {
251  ::MessageBox(0, "Export Failed: Magic could not open the file for writing", "ERROR", MB_OK);
252  return false;
253  }
254 
255  fclose(f);
256 
257  return true;
258  }
259 
260  return false;
261 }
262 
263 // +--------------------------------------------------------------------+