goat3d

diff exporters/maxgoat/src/maxgoat.cc @ 25:d0260d80ae09

adding the nodes interface, and continuing the max plugin
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 28 Sep 2013 19:12:50 +0300
parents 1f94a2107c64
children 4deb0b12fe14
line diff
     1.1 --- a/exporters/maxgoat/src/maxgoat.cc	Sat Sep 28 06:32:00 2013 +0300
     1.2 +++ b/exporters/maxgoat/src/maxgoat.cc	Sat Sep 28 19:12:50 2013 +0300
     1.3 @@ -10,6 +10,7 @@
     1.4  #include "IGame.h"
     1.5  #include "IGameExport.h"
     1.6  #include "IConversionmanager.h"
     1.7 +#include "goat3d.h"
     1.8  #include "config.h"
     1.9  
    1.10  
    1.11 @@ -48,7 +49,9 @@
    1.12  
    1.13  	int DoExport(const MCHAR *name, ExpInterface *eiface, Interface *iface, BOOL silent = FALSE, DWORD opt = 0);
    1.14  
    1.15 -	bool export_materials(FILE *fp);
    1.16 +	void export_materials(goat3d *goat);
    1.17 +	void export_meshes(goat3d *goat);
    1.18 +	void process_node(goat3d *goat, IGameNode *maxnode);
    1.19  };
    1.20  
    1.21  
    1.22 @@ -108,15 +111,8 @@
    1.23  	char fname[512];
    1.24  	wcstombs(fname, name, sizeof fname - 1);
    1.25  
    1.26 -	FILE *fp = fopen(fname, "wb");
    1.27 -	if(!fp) {
    1.28 -		fprintf(logfile, "failed to open %s for writting: %s", fname, strerror(errno));
    1.29 -		return IMPEXP_FAIL;
    1.30 -	}
    1.31 -
    1.32  	if(!(igame = GetIGameInterface())) {
    1.33  		fprintf(logfile, "failed to get the igame interface\n");
    1.34 -		fclose(fp);
    1.35  		return IMPEXP_FAIL;
    1.36  	}
    1.37  	IGameConversionManager *cm = GetConversionManager();
    1.38 @@ -124,64 +120,128 @@
    1.39  	igame->InitialiseIGame();
    1.40  	igame->SetStaticFrame(0);
    1.41  
    1.42 -	export_materials(fp);
    1.43 +	goat3d *goat = goat3d_create();
    1.44  
    1.45 -	fclose(fp);
    1.46 +	export_materials(goat);
    1.47 +	export_meshes(goat);
    1.48  
    1.49 +	if(goat3d_save(goat, fname) == -1) {
    1.50 +		goat3d_free(goat);
    1.51 +		return IMPEXP_FAIL;
    1.52 +	}
    1.53 +
    1.54 +	// process all nodes
    1.55 +	for(int i=0; i<igame->GetTopLevelNodeCount(); i++) {
    1.56 +		IGameNode *node = igame->GetTopLevelNode(i);
    1.57 +		process_node(goat, node);
    1.58 +	}
    1.59 +
    1.60 +	goat3d_free(goat);
    1.61  	return IMPEXP_SUCCESS;
    1.62  }
    1.63  
    1.64 -bool GoatExporter::export_materials(FILE *fp)
    1.65 +static const char *max_string(const MCHAR *wstr)
    1.66 +{
    1.67 +	if(!wstr) return 0;
    1.68 +	static char str[512];
    1.69 +	wcstombs(str, wstr, sizeof str - 1);
    1.70 +	return str;
    1.71 +}
    1.72 +
    1.73 +void GoatExporter::export_materials(goat3d *goat)
    1.74  {
    1.75  	IGameProperty *prop;
    1.76  
    1.77  	int num_mtl = igame->GetRootMaterialCount();
    1.78 -	fprintf(fp, "number of materials: %d\n", num_mtl);
    1.79 +	for(int i=0; i<num_mtl; i++) {
    1.80 +		IGameMaterial *maxmtl = igame->GetRootMaterial(i);
    1.81 +		if(maxmtl) {
    1.82 +			goat3d_material *mtl = goat3d_create_mtl();
    1.83  
    1.84 -	for(int i=0; i<num_mtl; i++) {
    1.85 -		IGameMaterial *mtl = igame->GetRootMaterial(i);
    1.86 -		if(mtl) {
    1.87 -			Point3 diffuse(1, 1, 1);
    1.88 -			Point3 specular(0, 0, 0);
    1.89 -			float shin = 1.0, sstr = 1.0;
    1.90 -			char name[512] = "unnamed";
    1.91 -
    1.92 -			const MCHAR *wname = mtl->GetMaterialName();
    1.93 -			if(wname) {
    1.94 -				wcstombs(name, wname, sizeof name - 1);
    1.95 +			const char *name = max_string(maxmtl->GetMaterialName());
    1.96 +			if(name) {
    1.97 +				goat3d_set_mtl_name(mtl, name);
    1.98  			}
    1.99  
   1.100 -			if((prop = mtl->GetDiffuseData())) {
   1.101 +			// diffuse
   1.102 +			if((prop = maxmtl->GetDiffuseData())) {
   1.103 +				Point3 diffuse(1, 1, 1);
   1.104  				prop->GetPropertyValue(diffuse);
   1.105 +				goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_DIFFUSE, diffuse[0],
   1.106 +					diffuse[1], diffuse[2]);
   1.107  			}
   1.108 -			if((prop = mtl->GetSpecularData())) {
   1.109 +			// specular
   1.110 +			if((prop = maxmtl->GetSpecularData())) {
   1.111 +				Point3 specular(0, 0, 0);
   1.112  				prop->GetPropertyValue(specular);
   1.113 +
   1.114 +				float sstr = 1.0;
   1.115 +				if((prop = maxmtl->GetSpecularLevelData())) {
   1.116 +					prop->GetPropertyValue(sstr);
   1.117 +				}
   1.118 +				goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_SPECULAR, specular[0] * sstr,
   1.119 +					specular[1] * sstr, specular[2] * sstr);
   1.120  			}
   1.121 -			if((prop = mtl->GetSpecularLevelData())) {
   1.122 -				prop->GetPropertyValue(sstr);
   1.123 -			}
   1.124 -			if((prop = mtl->GetGlossinessData())) {
   1.125 +			// shininess
   1.126 +			if((prop = maxmtl->GetGlossinessData())) {
   1.127 +				float shin;
   1.128  				prop->GetPropertyValue(shin);
   1.129 +				goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_SHININESS, shin * 100.0);
   1.130  			}
   1.131  
   1.132 -			fprintf(fp, "Material %d (%s):\n", i, name);
   1.133 -			fprintf(fp, "  diffuse: %f %f %f\n", diffuse[0], diffuse[1], diffuse[2]);
   1.134 -			fprintf(fp, "  specular: %f %f %f\n", specular[0] * sstr, specular[1] * sstr, specular[2] * sstr);
   1.135 -			fprintf(fp, "  shininess: %f\n", shin * 100.0);
   1.136 +			// textures
   1.137 +			for(int j=0; j<maxmtl->GetNumberOfTextureMaps(); j++) {
   1.138 +				IGameTextureMap *tex = maxmtl->GetIGameTextureMap(j);
   1.139  
   1.140 -			for(int j=0; j<mtl->GetNumberOfTextureMaps(); j++) {
   1.141 -				IGameTextureMap *tex = mtl->GetIGameTextureMap(j);
   1.142 -				const MCHAR *wfname = tex->GetBitmapFileName();
   1.143 -				if(wfname) {
   1.144 -					char fname[512];
   1.145 -					wcstombs(fname, wfname, sizeof fname - 1);
   1.146 -					fprintf(fp, "  texture%d: %s\n", j, fname);
   1.147 +				const char *fname = max_string(tex->GetBitmapFileName());
   1.148 +				if(!fname) {
   1.149 +					continue;
   1.150 +				}
   1.151 +
   1.152 +				int slot = tex->GetStdMapSlot();
   1.153 +				switch(slot) {
   1.154 +				case ID_DI:	// diffuse
   1.155 +					goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_DIFFUSE, fname);
   1.156 +					break;
   1.157 +
   1.158 +				case ID_SP:
   1.159 +				case ID_SS:
   1.160 +					goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SPECULAR, fname);
   1.161 +					break;
   1.162 +
   1.163 +				case ID_SH:
   1.164 +					goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SHININESS, fname);
   1.165 +					break;
   1.166 +
   1.167 +				case ID_BU:
   1.168 +					goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_NORMAL, fname);
   1.169 +					break;
   1.170 +
   1.171 +				case ID_RL:
   1.172 +					goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_REFLECTION, fname);
   1.173 +					break;
   1.174 +
   1.175 +				case ID_RR:
   1.176 +					goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_TRANSMISSION, fname);
   1.177 +					break;
   1.178 +
   1.179 +				default:
   1.180 +					break;
   1.181  				}
   1.182  			}
   1.183 +
   1.184 +			goat3d_add_mtl(goat, mtl);
   1.185  		}
   1.186  	}
   1.187 +}
   1.188  
   1.189 -	return true;
   1.190 +void GoatExporter::export_meshes(goat3d *goat)
   1.191 +{
   1.192 +	Tab<IGameNode*> meshes = igame->GetIGameNodeByType(IGameObject::IGAME_MESH);
   1.193 +
   1.194 +	for(int i=0; i<meshes.Count(); i++) {
   1.195 +		const char *name = max_string(meshes[i]->GetName());
   1.196 +	}
   1.197  }
   1.198  
   1.199  // ------------------------------------------