goat3d

view exporters/maxgoat/src/maxgoat.cc @ 10:1f94a2107c64

merged with win32 stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 26 Aug 2013 05:30:40 +0300
parents fca2ea844875
children d0260d80ae09
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"
13 #include "config.h"
16 #pragma comment (lib, "core.lib")
17 #pragma comment (lib, "geom.lib")
18 #pragma comment (lib, "gfx.lib")
19 #pragma comment (lib, "mesh.lib")
20 #pragma comment (lib, "maxutil.lib")
21 #pragma comment (lib, "maxscrpt.lib")
22 #pragma comment (lib, "paramblk2.lib")
23 #pragma comment (lib, "msxml2.lib")
24 #pragma comment (lib, "igame.lib")
25 #pragma comment (lib, "comctl32.lib")
28 #define VERSION(major, minor) \
29 ((major) * 100 + ((minor) < 10 ? (minor) * 10 : (minor)))
31 static FILE *logfile;
32 static HINSTANCE hinst;
34 class GoatExporter : public SceneExport {
35 public:
36 IGameScene *igame;
38 int ExtCount();
39 const TCHAR *Ext(int n);
40 const TCHAR *LongDesc();
41 const TCHAR *ShortDesc();
42 const TCHAR *AuthorName();
43 const TCHAR *CopyrightMessage();
44 const TCHAR *OtherMessage1();
45 const TCHAR *OtherMessage2();
46 unsigned int Version();
47 void ShowAbout(HWND win);
49 int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0);
51 bool export_materials(FILE *fp);
52 };
55 int GoatExporter::ExtCount()
56 {
57 return 1;
58 }
60 const TCHAR *GoatExporter::Ext(int n)
61 {
62 return L"txt";
63 }
65 const TCHAR *GoatExporter::LongDesc()
66 {
67 return L"Goat3D scene file";
68 }
70 const TCHAR *GoatExporter::ShortDesc()
71 {
72 return L"Goat3D";
73 }
75 const TCHAR *GoatExporter::AuthorName()
76 {
77 return L"John Tsiombikas";
78 }
80 const TCHAR *GoatExporter::CopyrightMessage()
81 {
82 return L"Copyright 2013 (C) John Tsiombikas - GNU General Public License v3, see COPYING for details.";
83 }
85 const TCHAR *GoatExporter::OtherMessage1()
86 {
87 return L"other1";
88 }
90 const TCHAR *GoatExporter::OtherMessage2()
91 {
92 return L"other2";
93 }
95 unsigned int GoatExporter::Version()
96 {
97 return VERSION(VER_MAJOR, VER_MINOR);
98 }
100 void GoatExporter::ShowAbout(HWND win)
101 {
102 MessageBoxA(win, "Goat3D exporter plugin", "About this plugin", 0);
103 }
105 int GoatExporter::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface,
106 BOOL non_interactive, DWORD opt)
107 {
108 char fname[512];
109 wcstombs(fname, name, sizeof fname - 1);
111 FILE *fp = fopen(fname, "wb");
112 if(!fp) {
113 fprintf(logfile, "failed to open %s for writting: %s", fname, strerror(errno));
114 return IMPEXP_FAIL;
115 }
117 if(!(igame = GetIGameInterface())) {
118 fprintf(logfile, "failed to get the igame interface\n");
119 fclose(fp);
120 return IMPEXP_FAIL;
121 }
122 IGameConversionManager *cm = GetConversionManager();
123 cm->SetCoordSystem(IGameConversionManager::IGAME_OGL);
124 igame->InitialiseIGame();
125 igame->SetStaticFrame(0);
127 export_materials(fp);
129 fclose(fp);
131 return IMPEXP_SUCCESS;
132 }
134 bool GoatExporter::export_materials(FILE *fp)
135 {
136 IGameProperty *prop;
138 int num_mtl = igame->GetRootMaterialCount();
139 fprintf(fp, "number of materials: %d\n", num_mtl);
141 for(int i=0; i<num_mtl; i++) {
142 IGameMaterial *mtl = igame->GetRootMaterial(i);
143 if(mtl) {
144 Point3 diffuse(1, 1, 1);
145 Point3 specular(0, 0, 0);
146 float shin = 1.0, sstr = 1.0;
147 char name[512] = "unnamed";
149 const MCHAR *wname = mtl->GetMaterialName();
150 if(wname) {
151 wcstombs(name, wname, sizeof name - 1);
152 }
154 if((prop = mtl->GetDiffuseData())) {
155 prop->GetPropertyValue(diffuse);
156 }
157 if((prop = mtl->GetSpecularData())) {
158 prop->GetPropertyValue(specular);
159 }
160 if((prop = mtl->GetSpecularLevelData())) {
161 prop->GetPropertyValue(sstr);
162 }
163 if((prop = mtl->GetGlossinessData())) {
164 prop->GetPropertyValue(shin);
165 }
167 fprintf(fp, "Material %d (%s):\n", i, name);
168 fprintf(fp, " diffuse: %f %f %f\n", diffuse[0], diffuse[1], diffuse[2]);
169 fprintf(fp, " specular: %f %f %f\n", specular[0] * sstr, specular[1] * sstr, specular[2] * sstr);
170 fprintf(fp, " shininess: %f\n", shin * 100.0);
172 for(int j=0; j<mtl->GetNumberOfTextureMaps(); j++) {
173 IGameTextureMap *tex = mtl->GetIGameTextureMap(j);
174 const MCHAR *wfname = tex->GetBitmapFileName();
175 if(wfname) {
176 char fname[512];
177 wcstombs(fname, wfname, sizeof fname - 1);
178 fprintf(fp, " texture%d: %s\n", j, fname);
179 }
180 }
181 }
182 }
184 return true;
185 }
187 // ------------------------------------------
189 class GoatClassDesc : public ClassDesc2 {
190 public:
191 int IsPublic() { return TRUE; }
192 void *Create(BOOL loading = FALSE) { return new GoatExporter; }
193 const TCHAR *ClassName() { return L"GoatExporter"; }
194 SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
195 Class_ID ClassID() { return Class_ID(0x77050f0d, 0x7d4c5ab5); }
196 const TCHAR *Category() { return L"Mutant Stargoat"; }
198 const TCHAR *InternalName() { return L"GoatExporter"; }
199 HINSTANCE HInstance() { return hinst; }
200 };
202 static GoatClassDesc class_desc;
204 BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved)
205 {
206 if(reason == DLL_PROCESS_ATTACH) {
207 hinst = inst_handle;
208 DisableThreadLibraryCalls(hinst);
209 }
210 return TRUE;
211 }
213 extern "C" {
215 __declspec(dllexport) const TCHAR *LibDescription()
216 {
217 return L"test exporter";
218 }
220 __declspec(dllexport) int LibNumberClasses()
221 {
222 return 1;
223 }
225 __declspec(dllexport) ClassDesc *LibClassDesc(int i)
226 {
227 return i == 0 ? &class_desc : 0;
228 }
230 __declspec(dllexport) ULONG LibVersion()
231 {
232 return Get3DSMAXVersion();
233 }
235 __declspec(dllexport) int LibInitialize()
236 {
237 static char path[1024];
239 SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path);
240 strcat(path, "/testexp.log");
242 if((logfile = fopen(path, "w"))) {
243 setvbuf(logfile, 0, _IONBF, 0);
244 }
245 return TRUE;
246 }
248 __declspec(dllexport) int LibShutdown()
249 {
250 if(logfile) {
251 fclose(logfile);
252 logfile = 0;
253 }
254 return TRUE;
255 }
257 } // extern "C"