goat3d

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/exporters/maxgoat/src/maxgoat.cc	Mon Aug 19 05:18:08 2013 +0300
     1.3 @@ -0,0 +1,253 @@
     1.4 +#include <stdio.h>
     1.5 +#include <string.h>
     1.6 +#include <errno.h>
     1.7 +#include <windows.h>
     1.8 +#include <shlobj.h>
     1.9 +#include "max.h"
    1.10 +#include "impexp.h"		// SceneExport
    1.11 +#include "iparamb2.h"	// ClassDesc2
    1.12 +#include "plugapi.h"
    1.13 +#include "IGame.h"
    1.14 +#include "IGameExport.h"
    1.15 +#include "IConversionmanager.h"
    1.16 +
    1.17 +
    1.18 +#pragma comment (lib, "core.lib")
    1.19 +#pragma comment (lib, "geom.lib")
    1.20 +#pragma comment (lib, "gfx.lib")
    1.21 +#pragma comment (lib, "mesh.lib")
    1.22 +#pragma comment (lib, "maxutil.lib")
    1.23 +#pragma comment (lib, "maxscrpt.lib")
    1.24 +#pragma comment (lib, "paramblk2.lib")
    1.25 +#pragma comment (lib, "msxml2.lib")
    1.26 +#pragma comment (lib, "igame.lib")
    1.27 +#pragma comment (lib, "comctl32.lib")
    1.28 +
    1.29 +
    1.30 +static FILE *logfile;
    1.31 +static HINSTANCE hinst;
    1.32 +
    1.33 +class GoatExporter : public SceneExport {
    1.34 +public:
    1.35 +	IGameScene *igame;
    1.36 +
    1.37 +	int ExtCount();
    1.38 +	const TCHAR *Ext(int n);
    1.39 +	const TCHAR *LongDesc();
    1.40 +	const TCHAR *ShortDesc();
    1.41 +	const TCHAR *AuthorName();
    1.42 +	const TCHAR *CopyrightMessage();
    1.43 +	const TCHAR *OtherMessage1();
    1.44 +	const TCHAR *OtherMessage2();
    1.45 +	unsigned int Version();
    1.46 +	void ShowAbout(HWND win);
    1.47 +
    1.48 +	int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0);
    1.49 +
    1.50 +	bool export_materials(FILE *fp);
    1.51 +};
    1.52 +
    1.53 +
    1.54 +int GoatExporter::ExtCount()
    1.55 +{
    1.56 +	return 1;
    1.57 +}
    1.58 +
    1.59 +const TCHAR *GoatExporter::Ext(int n)
    1.60 +{
    1.61 +	return L"txt";
    1.62 +}
    1.63 +
    1.64 +const TCHAR *GoatExporter::LongDesc()
    1.65 +{
    1.66 +	return L"Goat3D scene file";
    1.67 +}
    1.68 +
    1.69 +const TCHAR *GoatExporter::ShortDesc()
    1.70 +{
    1.71 +	return L"Goat3D";
    1.72 +}
    1.73 +
    1.74 +const TCHAR *GoatExporter::AuthorName()
    1.75 +{
    1.76 +	return L"John Tsiombikas";
    1.77 +}
    1.78 +
    1.79 +const TCHAR *GoatExporter::CopyrightMessage()
    1.80 +{
    1.81 +	return L"Copyright 2013 (C) John Tsiombikas - GNU General Public License v3, see COPYING for details.";
    1.82 +}
    1.83 +
    1.84 +const TCHAR *GoatExporter::OtherMessage1()
    1.85 +{
    1.86 +	return L"other1";
    1.87 +}
    1.88 +
    1.89 +const TCHAR *GoatExporter::OtherMessage2()
    1.90 +{
    1.91 +	return L"other2";
    1.92 +}
    1.93 +
    1.94 +unsigned int GoatExporter::Version()
    1.95 +{
    1.96 +	return 10;
    1.97 +}
    1.98 +
    1.99 +void GoatExporter::ShowAbout(HWND win)
   1.100 +{
   1.101 +	MessageBoxA(win, "Goat3D exporter plugin", "About this plugin", 0);
   1.102 +}
   1.103 +
   1.104 +int GoatExporter::DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface,
   1.105 +		BOOL non_interactive, DWORD opt)
   1.106 +{
   1.107 +	char fname[512];
   1.108 +	wcstombs(fname, name, sizeof fname - 1);
   1.109 +
   1.110 +	FILE *fp = fopen(fname, "wb");
   1.111 +	if(!fp) {
   1.112 +		fprintf(logfile, "failed to open %s for writting: %s", fname, strerror(errno));
   1.113 +		return IMPEXP_FAIL;
   1.114 +	}
   1.115 +
   1.116 +	if(!(igame = GetIGameInterface())) {
   1.117 +		fprintf(logfile, "failed to get the igame interface\n");
   1.118 +		fclose(fp);
   1.119 +		return IMPEXP_FAIL;
   1.120 +	}
   1.121 +	IGameConversionManager *cm = GetConversionManager();
   1.122 +	cm->SetCoordSystem(IGameConversionManager::IGAME_OGL);
   1.123 +	igame->InitialiseIGame();
   1.124 +	igame->SetStaticFrame(0);
   1.125 +
   1.126 +	export_materials(fp);
   1.127 +
   1.128 +	fclose(fp);
   1.129 +
   1.130 +	return IMPEXP_SUCCESS;
   1.131 +}
   1.132 +
   1.133 +bool GoatExporter::export_materials(FILE *fp)
   1.134 +{
   1.135 +	IGameProperty *prop;
   1.136 +
   1.137 +	int num_mtl = igame->GetRootMaterialCount();
   1.138 +	fprintf(fp, "number of materials: %d\n", num_mtl);
   1.139 +
   1.140 +	for(int i=0; i<num_mtl; i++) {
   1.141 +		IGameMaterial *mtl = igame->GetRootMaterial(i);
   1.142 +		if(mtl) {
   1.143 +			Point3 diffuse(1, 1, 1);
   1.144 +			Point3 specular(0, 0, 0);
   1.145 +			float shin = 1.0, sstr = 1.0;
   1.146 +			char name[512] = "unnamed";
   1.147 +
   1.148 +			const MCHAR *wname = mtl->GetMaterialName();
   1.149 +			if(wname) {
   1.150 +				wcstombs(name, wname, sizeof name - 1);
   1.151 +			}
   1.152 +
   1.153 +			if((prop = mtl->GetDiffuseData())) {
   1.154 +				prop->GetPropertyValue(diffuse);
   1.155 +			}
   1.156 +			if((prop = mtl->GetSpecularData())) {
   1.157 +				prop->GetPropertyValue(specular);
   1.158 +			}
   1.159 +			if((prop = mtl->GetSpecularLevelData())) {
   1.160 +				prop->GetPropertyValue(sstr);
   1.161 +			}
   1.162 +			if((prop = mtl->GetGlossinessData())) {
   1.163 +				prop->GetPropertyValue(shin);
   1.164 +			}
   1.165 +
   1.166 +			fprintf(fp, "Material %d (%s):\n", i, name);
   1.167 +			fprintf(fp, "  diffuse: %f %f %f\n", diffuse[0], diffuse[1], diffuse[2]);
   1.168 +			fprintf(fp, "  specular: %f %f %f\n", specular[0] * sstr, specular[1] * sstr, specular[2] * sstr);
   1.169 +			fprintf(fp, "  shininess: %f\n", shin * 100.0);
   1.170 +
   1.171 +			for(int j=0; j<mtl->GetNumberOfTextureMaps(); j++) {
   1.172 +				IGameTextureMap *tex = mtl->GetIGameTextureMap(j);
   1.173 +				const MCHAR *wfname = tex->GetBitmapFileName();
   1.174 +				if(wfname) {
   1.175 +					char fname[512];
   1.176 +					wcstombs(fname, wfname, sizeof fname - 1);
   1.177 +					fprintf(fp, "  texture%d: %s\n", j, fname);
   1.178 +				}
   1.179 +			}
   1.180 +		}
   1.181 +	}
   1.182 +
   1.183 +	return true;
   1.184 +}
   1.185 +
   1.186 +// ------------------------------------------
   1.187 +
   1.188 +class GoatClassDesc : public ClassDesc2 {
   1.189 +public:
   1.190 +	int IsPublic() { return TRUE; }
   1.191 +	void *Create(BOOL loading = FALSE) { return new GoatExporter; }
   1.192 +	const TCHAR *ClassName() { return L"GoatExporter"; }
   1.193 +	SClass_ID SuperClassID() { return SCENE_EXPORT_CLASS_ID; }
   1.194 +	Class_ID ClassID() { return Class_ID(0x77050f0d, 0x7d4c5ab5); }
   1.195 +	const TCHAR *Category() { return L"Mutant Stargoat"; }
   1.196 +
   1.197 +	const TCHAR *InternalName() { return L"GoatExporter"; }
   1.198 +	HINSTANCE HInstance() { return hinst; }
   1.199 +};
   1.200 +
   1.201 +static GoatClassDesc class_desc;
   1.202 +
   1.203 +BOOL WINAPI DllMain(HINSTANCE inst_handle, ULONG reason, void *reserved)
   1.204 +{
   1.205 +	if(reason == DLL_PROCESS_ATTACH) {
   1.206 +		hinst = inst_handle;
   1.207 +		DisableThreadLibraryCalls(hinst);
   1.208 +	}
   1.209 +	return TRUE;
   1.210 +}
   1.211 +
   1.212 +extern "C" {
   1.213 +
   1.214 +__declspec(dllexport) const TCHAR *LibDescription()
   1.215 +{
   1.216 +	return L"test exporter";
   1.217 +}
   1.218 +
   1.219 +__declspec(dllexport) int LibNumberClasses()
   1.220 +{
   1.221 +	return 1;
   1.222 +}
   1.223 +
   1.224 +__declspec(dllexport) ClassDesc *LibClassDesc(int i)
   1.225 +{
   1.226 +	return i == 0 ? &class_desc : 0;
   1.227 +}
   1.228 +
   1.229 +__declspec(dllexport) ULONG LibVersion()
   1.230 +{
   1.231 +	return Get3DSMAXVersion();
   1.232 +}
   1.233 +
   1.234 +__declspec(dllexport) int LibInitialize()
   1.235 +{
   1.236 +	static char path[1024];
   1.237 +
   1.238 +	SHGetFolderPathA(0, CSIDL_PERSONAL, 0, 0, path);
   1.239 +	strcat(path, "/testexp.log");
   1.240 +
   1.241 +	if((logfile = fopen(path, "w"))) {
   1.242 +		setvbuf(logfile, 0, _IONBF, 0);
   1.243 +	}
   1.244 +	return TRUE;
   1.245 +}
   1.246 +
   1.247 +__declspec(dllexport) int LibShutdown()
   1.248 +{
   1.249 +	if(logfile) {
   1.250 +		fclose(logfile);
   1.251 +		logfile = 0;
   1.252 +	}
   1.253 +	return TRUE;
   1.254 +}
   1.255 +
   1.256 +}	// extern "C"
   1.257 \ No newline at end of file