source: golgotha/src/max_plugin/import.cc

Last change on this file was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 7.9 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include <windows.h>
10#include "crkutil.hh"
11#include "obj3d.hh"
12#include <utilapi.h>
13#include <stdmat.h>
14#include <stdio.h>
15#include "file/file.hh"
16#include "string/string.hh"
17#include "max_object.hh"
18#include "memory/array.hh"
19#include "loaders/dir_load.hh"
20#include "saver_id.hh"
21#include "debug.hh"
22
23#define G3DF_VERSION 3
24
25#define MAXANIMATIONS 2
26#define MAXFRAMES     300
27#define MAXVERT       100
28#define MAXTOTALVERT  (MAXFRAMES*MAXVERT)
29#define MAXCOUNTS     10000
30#define MAXQUAD       150
31
32class m1_import_class : public SceneImport
33{
34protected:
35public:
36  m1_import_class() {}
37  ~m1_import_class() {}
38
39  int ExtCount() { return 1; }
40  const TCHAR * Ext(int n) { return _T("gmod"); }
41
42  const TCHAR * LongDesc() { return _T("Imports Golgotha MODel files"); }
43  const TCHAR * ShortDesc()  { return _T("Golgotha MODel files"); }
44  const TCHAR * AuthorName() { return _T("Oliver Yu"); }
45  const TCHAR * CopyrightMessage() { return _T("Copyright 1998 Crack dot Com"); }
46  const TCHAR * OtherMessage1() { return _T(""); }
47  const TCHAR * OtherMessage2() { return _T(""); }
48  unsigned int Version() { return 10; }
49  void ShowAbout(HWND hWnd) {}
50
51  int DoImport(const TCHAR *name, ImpInterface *im, Interface *gi);
52};
53
54class m1_import_descriptor_class : public ClassDesc
55{
56public:
57  int           IsPublic()     {return 1;}
58  void *        Create(BOOL loading = FALSE) {return new m1_import_class;}
59  const TCHAR * ClassName()    {return _T("Crack Importer");}
60  SClass_ID     SuperClassID() {return SCENE_IMPORT_CLASS_ID;}
61  Class_ID      ClassID()      { return Class_ID(0xa2341, 0); }
62  const TCHAR*  Category()     {return _T("");}
63};
64static m1_import_descriptor_class m1_import_descriptor;
65ClassDesc* GetCrackImportDesc() {return &m1_import_descriptor;}
66
67class m1_loader : public g1_base_object_loader_class
68//{{{
69{
70public:
71  ImpInterface *im;
72  i4_loader_class *loader;
73  i4_file_class *f;
74  TriObject *obj;
75  Mesh *mesh;
76  ImpNode *node;
77  const TCHAR *name;
78
79  w16 quads;
80  char texture_list[800][256];
81
82  int faces;
83
84  m1_loader() : f(0), loader(0), obj(0), mesh(0), node(0) {}
85 
86  ~m1_loader()
87  {
88    if (f) delete f;
89    if (loader) delete loader;
90    if (obj) delete obj;
91    if (node) delete node;
92  }
93 
94  virtual g1_quad_object_class *allocate_object() { return (g1_quad_object_class*)0xfaced; }
95
96  virtual void set_num_vertex(w16 num_vertex)
97  {
98    dbg("Getting Verts\n");
99    mesh->setNumVerts(num_vertex);
100  }
101
102  virtual void set_num_animations(w16 anims)
103  {
104    // no animation for now
105  }
106
107  virtual void create_animation(w16 anim, const i4_const_str &name, w16 frames)
108  {
109    // no animation for now
110  }
111
112  virtual void create_vertex(w16 anim, w16 frame, w16 index, const i4_3d_vector& v)
113  {
114    if (anim==0 && frame==0)
115      mesh->setVert(index,v.x,v.y,v.z);
116  }
117
118  virtual void store_vertex_normal(w16 anim, w16 frame, w16 index, const i4_3d_vector& normal) {}
119
120  virtual void set_num_quads(w16 num_quads)
121  {
122    dbg("Getting Textures & Quad\n");
123    quads = num_quads;
124    faces = 0;
125    mesh->setNumFaces(quads*2);
126  }
127
128  virtual void create_quad(w16 quad, int verts, w16 *ref, w32 flags)
129  {
130    if (ref[3]!=0xffff)
131    {
132      mesh->faces[faces].setVerts(ref[0], ref[1], ref[2]);
133      mesh->faces[faces].setEdgeVisFlags(1,1,0);
134      mesh->faces[faces].setMatID((MtlID)quad+MAX_MATERIAL_OFFSET);
135      faces++;
136     
137      mesh->faces[faces].setVerts(ref[2], ref[3], ref[0]);
138      mesh->faces[faces].setEdgeVisFlags(1,1,0);
139      mesh->faces[faces].setMatID((MtlID)quad+MAX_MATERIAL_OFFSET);
140      faces++;
141    }
142    else if (ref[2]!=0xffff)
143    {
144      mesh->faces[faces].setVerts(ref[0], ref[1], ref[2]);
145      mesh->faces[faces].setEdgeVisFlags(1,1,1);
146      mesh->faces[faces].setMatID((MtlID)quad+MAX_MATERIAL_OFFSET);
147      faces++;
148    }
149  }
150
151  virtual void store_texture_name(w32 quad, const i4_const_str &name)
152  {
153    i4_os_string(name, texture_list[quad+1], sizeof(texture_list[0]));
154  }
155
156  virtual void store_texture_params(w32 quad, i4_float scale, i4_float *u, i4_float *v)
157  {
158    sprintf(&texture_list[quad+1][strlen(texture_list[quad+1])]," %f %f %f %f %f %f %f %f\r\n",
159            u[0], v[0], u[1], v[1], u[2], v[2], u[3], v[3]);
160  }
161
162  virtual void store_quad_normal(w16 quad, const i4_3d_vector& v) {}
163
164  virtual void set_num_mount_points(w16 num_mounts)
165  {
166    dbg("Getting Mounts\n");
167  }
168  virtual void create_mount_point(w32 index, const i4_const_str &name, const i4_3d_vector &off)
169  {
170    // create a point helper
171    HelperObject *help = (HelperObject *)CreateInstance(HELPER_CLASS_ID,
172                                                        Class_ID(POINTHELP_CLASS_ID,0));
173    ImpNode *node = im->CreateNode();
174   
175    if (help && node)
176    {
177      char tmp_name[1024];
178     
179      i4_os_string(name, tmp_name, sizeof(tmp_name));
180      node->SetName(tmp_name);
181      Matrix3 tm = TransMatrix(Point3(off.x,off.y,off.z));
182      node->Reference(help);
183      node->SetTransform(0,tm);
184      im->AddNodeToScene(node);
185     
186      help=0;
187      node=0;
188    }
189   
190    if (help) delete help;
191    if (node) delete node;
192  }
193
194  virtual void set_num_texture_animations(w16 num_textures)
195  {
196    dbg("Getting Texture Animations\n");
197  }
198  virtual void create_texture_animation(w32 index, w16 quad, w16 max_frames,
199                                        i4_float du, i4_float dv, i4_float speed)
200  {
201    // none for now
202  }
203  virtual void create_texture_pan(w32 index, w16 quad,
204                                  i4_float du, i4_float dv, i4_float speed)
205  {
206    // none for now
207  }
208
209  virtual void finish_object()
210  {
211    // set actual number of triangles
212    mesh->setNumFaces(faces, TRUE);
213
214    dbg("Building Mesh\n");
215    mesh->buildNormals();
216    mesh->buildBoundingBox();
217    mesh->InvalidateEdgeList();
218
219    dbg("Creating Node\n");
220    node = im->CreateNode();
221    if (!node) { g1_base_object_loader_class::obj=0; return; }
222
223    Matrix3 tm;
224    tm.IdentityMatrix();
225    node->Reference(obj);
226    node->SetTransform(0,tm);
227
228    dbg("Building Texture List\n");
229    char *buff = texture_list[0], *q=buff;
230    sprintf(buff, "%s\r\n", gmod_sig);
231    while (*q) q++;
232
233    for (int i=0; i<quads; i++)
234    {
235      char *p = texture_list[i+1];
236      while (*p)
237        *q++ = *p++;
238    }
239    *q = 0;
240
241    dbg("String copy\n");
242
243    TSTR tst(buff);
244
245    dbg("Assigning Texture List\n");
246
247    node->GetINode()->SetUserPropBuffer(tst);
248
249    dbg("Adding Node\n");
250    im->AddNodeToScene(node);
251    node->SetName(name);
252
253    im->RedrawViews();
254    unlink();
255  }
256
257  void unlink()
258  {
259    obj = 0;
260    node = 0;
261  }
262
263  int load(const TCHAR *_name, ImpInterface *_im)
264  {
265    im = _im;
266    name = _name;
267
268    dbg("Opening %s\n", name);
269    if (!(f = i4_open(name, I4_READ))) return 0;
270    dbg("Opening Loader %s\n", name);
271    if (!(loader = i4_open_save_file(f))) return 0;
272    dbg("Creating Object\n");
273    if (!(obj = CreateNewTriObject())) return 0;
274    dbg("Getting Mesh\n");
275    if (!(mesh = &obj->Mesh())) return 0;
276
277    return (g1_base_object_loader_class::load(loader)!=0);
278  }
279
280};
281//}}}
282
283int m1_import_class::DoImport(const TCHAR *name, ImpInterface *im, Interface *gi)
284{
285  dbg.restart();
286
287  dbg("Loading [%s]\n", name);
288
289  m1_loader f;
290  return f.load(name,im);
291}
292
293//{{{ Emacs Locals
294// Local Variables:
295// folded-file: t
296// End:
297//}}}
Note: See TracBrowser for help on using the repository browser.