goat3d

view exporters/maxgoat/src/maxgoat.cc @ 5:fca2ea844875

added rudimentary visual studio project
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 19 Aug 2013 05:18:08 +0300
parents
children 1f94a2107c64
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <windows.h>
5 #include <shlobj.h>
6 #include "max.h"
7 #include "impexp.h" // SceneExport
8 #include "iparamb2.h" // ClassDesc2
9 #include "plugapi.h"
10 #include "IGame.h"
11 #include "IGameExport.h"
12 #include "IConversionmanager.h"
15 #pragma comment (lib, "core.lib")
16 #pragma comment (lib, "geom.lib")
17 #pragma comment (lib, "gfx.lib")
18 #pragma comment (lib, "mesh.lib")
19 #pragma comment (lib, "maxutil.lib")
20 #pragma comment (lib, "maxscrpt.lib")
21 #pragma comment (lib, "paramblk2.lib")
22 #pragma comment (lib, "msxml2.lib")
23 #pragma comment (lib, "igame.lib")
24 #pragma comment (lib, "comctl32.lib")
27 static FILE *logfile;
28 static HINSTANCE hinst;
30 class GoatExporter : public SceneExport {
31 public:
32 IGameScene *igame;
34 int ExtCount();
35 const TCHAR *Ext(int n);
36 const TCHAR *LongDesc();
37 const TCHAR *ShortDesc();
38 const TCHAR *AuthorName();
39 const TCHAR *CopyrightMessage();
40 const TCHAR *OtherMessage1();
41 const TCHAR *OtherMessage2();
42 unsigned int Version();
43 void ShowAbout(HWND win);
45 int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0);
47 bool export_materials(FILE *fp);
48 };
51 int GoatExporter::ExtCount()
52 {
53 return 1;
54 }
56 const TCHAR *GoatExporter::Ext(int n)
57 {
58 return L"txt";
59 }
61 const TCHAR *GoatExporter::LongDesc()
62 {
63 return L"Goat3D scene file";
64 }
66 const TCHAR *GoatExporter::ShortDesc()
67 {
68 return L"Goat3D";
69 }
71 const TCHAR *GoatExporter::AuthorName()
72 {
73 return L"John Tsiombikas";
74 }
76 const TCHAR *GoatExporter::CopyrightMessage()
77 {
78 return L"Copyright 2013 (C) John Tsiombikas - GNU General Public License v3, see COPYING for details.";
79 }
81 const TCHAR *GoatExporter::OtherMessage1()
82 {
83 return L"other1";
84 }
86 const TCHAR *GoatExporter::OtherMessage2()
87 {
88 return L"other2";
89 }
91 unsigned int GoatExporter::Version()
92 {
93 return 10;
94 }
96 void GoatExporter::ShowAbout(HWND win)
97 {
98 MessageBoxA(win, "Goat3D exporter plugin", "About this plugin", 0);
99 }
101 int GoatExporter::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface,
102 BOOL non_interactive, DWORD opt)
103 {
104 char fname[512];
105 wcstombs(fname, name, sizeof fname - 1);
107 FILE *fp = fopen(fname, "wb");
108 if(!fp) {
109 fprintf(logfile, "failed to open %s for writting: %s", fname, strerror(errno));
110 return IMPEXP_FAIL;
111 }
113 if(!(igame = GetIGameInterface())) {
114 fprintf(logfile, "failed to get the igame interface\n");
115 fclose(fp);
116 return IMPEXP_FAIL;
117 }
118 IGameConversionManager *cm = GetConversionManager();
119 cm->SetCoordSystem(IGameConversionManager::IGAME_OGL);
120 igame->InitialiseIGame();
121 igame->SetStaticFrame(0);
123 export_materials(fp);
125 fclose(fp);
127 return IMPEXP_SUCCESS;
128 }
130 bool GoatExporter::export_materials(FILE *fp)
131 {
132 IGameProperty *prop;
134 int num_mtl = igame->GetRootMaterialCount();
135 fprintf(fp, "number of materials: %d\n", num_mtl);
137 for(int i=0; i<num_mtl; i++) {
138 IGameMaterial *mtl = igame->GetRootMaterial(i);
139 if(mtl) {
140 Point3 diffuse(1, 1, 1);
141 Point3 specular(0, 0, 0);
142 float shin = 1.0, sstr = 1.0;
143 char name[512] = "unnamed";
145 const MCHAR *wname = mtl->GetMaterialName();
146 if(wname) {
147 wcstombs(name, wname, sizeof name - 1);
148 }
150 if((prop = mtl->GetDiffuseData())) {
151 prop->GetPropertyValue(diffuse);
152 }
153 if((prop = mtl->GetSpecularData())) {
154 prop->GetPropertyValue(specular);
155 }
156 if((prop = mtl->GetSpecularLevelData())) {
157 prop->GetPropertyValue(sstr);
158 }
159 if((prop = mtl->GetGlossinessData())) {
160 prop->GetPropertyValue(shin);
161 }
163 fprintf(fp, "Material %d (%s):\n", i, name);
164 fprintf(fp, " diffuse: %f %f %f\n", diffuse[0], diffuse[1], diffuse[2]);
165 fprintf(fp, " specular: %f %f %f\n", specular[0] * sstr, specular[1] * sstr, specular[2] * sstr);
166 fprintf(fp, " shininess: %f\n", shin * 100.0);
168 for(int j=0; j<mtl->GetNumberOfTextureMaps(); j++) {
169 IGameTextureMap *tex = mtl->GetIGameTextureMap(j);
170 const MCHAR *wfname = tex->GetBitmapFileName();
171 if(wfname) {
172 char fname[512];
173 wcstombs(fname, wfname, sizeof fname - 1);
174 fprintf(fp, " texture%d: %s\n", j, fname);
175 }
176 }
177 }
178 }
180 return true;
181 }
183 // ------------------------------------------
185 class GoatClassDesc : public ClassDesc2 {
186 public:
187 int IsPublic() { return TRUE; }
188 void *Create(BOOL loading = FALSE) { return new GoatExporter; }
189 const TCHAR *ClassName() { return L"GoatExporter"; }
190 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
191 Class_ID ClassID() { return Class_ID(0x77050f0d, 0x7d4c5ab5); }
192 const TCHAR *Category() { return L"Mutant Stargoat"; }
194 const TCHAR *InternalName() { return L"GoatExporter"; }
195 HINSTANCE HInstance() { return hinst; }
196 };
198 static GoatClassDesc class_desc;
200 BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved)
201 {
202 if(reason == DLL_PROCESS_ATTACH) {
203 hinst = inst_handle;
204 DisableThreadLibraryCalls(hinst);
205 }
206 return TRUE;
207 }
209 extern "C" {
211 __declspec(dllexport) const TCHAR *LibDescription()
212 {
213 return L"test exporter";
214 }
216 __declspec(dllexport) int LibNumberClasses()
217 {
218 return 1;
219 }
221 __declspec(dllexport) ClassDesc *LibClassDesc(int i)
222 {
223 return i == 0 ? &class_desc : 0;
224 }
226 __declspec(dllexport) ULONG LibVersion()
227 {
228 return Get3DSMAXVersion();
229 }
231 __declspec(dllexport) int LibInitialize()
232 {
233 static char path[1024];
235 SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path);
236 strcat(path, "/testexp.log");
238 if((logfile = fopen(path, "w"))) {
239 setvbuf(logfile, 0, _IONBF, 0);
240 }
241 return TRUE;
242 }
244 __declspec(dllexport) int LibShutdown()
245 {
246 if(logfile) {
247 fclose(logfile);
248 logfile = 0;
249 }
250 return TRUE;
251 }
253 } // extern "C"