00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <math.h>
00026 #include <time.h>
00027 #include "MayaDisplayDevice.h"
00028 #include "Matrix4.h"
00029 #include "DispCmds.h"
00030 #include "Inform.h"
00031 #include "utilities.h"
00032 #include "config.h"
00033
00034 #define DASH_LENGTH 0.02
00035
00036
00037 MayaDisplayDevice::MayaDisplayDevice(void)
00038 : FileRenderer("Maya", "Maya (ASCII)", "vmdscene.ma", "true") { }
00039
00040
00041 MayaDisplayDevice::~MayaDisplayDevice(void) { }
00042
00043
00044 void MayaDisplayDevice::beginrepgeomgroup(const char *s) {
00045 fprintf(outfile, "// g %s\n", s);
00046 }
00047
00048
00049 void MayaDisplayDevice::comment(const char *s) {
00050 fprintf(outfile, "// %s\n", s);
00051 }
00052
00053
00054 void MayaDisplayDevice::point(float * spdata) {
00055 float vec[3];
00056
00057 (transMat.top()).multpoint3d(spdata, vec);
00058
00059
00060 fprintf(outfile, "// v %5f %5f %5f\n", vec[0], vec[1], -vec[2]);
00061 fprintf(outfile, "// p -1\n");
00062 }
00063
00064
00065
00066 void MayaDisplayDevice::sphere(float * spdata) {
00067 float vec[3];
00068 float radius;
00069
00070
00071 (transMat.top()).multpoint3d(spdata, vec);
00072 radius = scale_radius(spdata[3]);
00073
00074
00075 fprintf(outfile, "createNode transform -n \"vmd%d\";\n", objnameindex);
00076 fprintf(outfile, " setAttr \".s\" -type \"double3\" %f %f %f;\n", radius, radius, radius);
00077 fprintf(outfile, " setAttr \".t\" -type \"double3\" %f %f %f;\n", vec[0], vec[1], vec[2]);
00078 fprintf(outfile, "parent -s -nc -r -add \"|VMDNurbSphere|nurbsSphereShape1\" \"vmd%d\";\n", objnameindex);
00079
00080 char strbuf[1024];
00081 sprintf(strbuf, "|vmd%d|nurbsSphereShape1", objnameindex);
00082 write_cindexmaterial(strbuf, colorIndex, materialIndex);
00083
00084
00085 objnameindex++;
00086 }
00087
00088
00089
00090 void MayaDisplayDevice::line(float *a, float*b) {
00091 int i, j, test;
00092 float dirvec[3], unitdirvec[3];
00093 float from[3], to[3], tmp1[3], tmp2[3];
00094
00095 if (lineStyle == ::SOLIDLINE) {
00096
00097 (transMat.top()).multpoint3d(a, from);
00098 (transMat.top()).multpoint3d(b, to);
00099
00100
00101 fprintf(outfile, "// XXX lines not supported yet\n");
00102 fprintf(outfile, "// v %5f %5f %5f\n", from[0], from[1], -from[2]);
00103 fprintf(outfile, "// v %5f %5f %5f\n", to[0], to[1], -to[2]);
00104 fprintf(outfile, "// l -1 -2\n");
00105 } else if (lineStyle == ::DASHEDLINE) {
00106
00107 (transMat.top()).multpoint3d(a, tmp1);
00108 (transMat.top()).multpoint3d(b, tmp2);
00109
00110
00111 vec_sub(dirvec, tmp2, tmp1);
00112 vec_copy(unitdirvec, dirvec);
00113 vec_normalize(unitdirvec);
00114 test = 1;
00115 i = 0;
00116 while (test == 1) {
00117 for (j=0; j<3; j++) {
00118 from[j] = (float) (tmp1[j] + (2*i )*DASH_LENGTH*unitdirvec[j]);
00119 to[j] = (float) (tmp1[j] + (2*i + 1)*DASH_LENGTH*unitdirvec[j]);
00120 }
00121 if (fabsf(tmp1[0] - to[0]) >= fabsf(dirvec[0])) {
00122 vec_copy(to, tmp2);
00123 test = 0;
00124 }
00125
00126
00127 fprintf(outfile, "// XXX lines not supported yet\n");
00128 fprintf(outfile, "// v %5f %5f %5f\n", from[0], from[1], -from[2]);
00129 fprintf(outfile, "// v %5f %5f %5f\n", to[0], to[1], -to[2]);
00130 fprintf(outfile, "// l -1 -2\n");
00131 i++;
00132 }
00133 } else {
00134 msgErr << "MayaDisplayDevice: Unknown line style "
00135 << lineStyle << sendmsg;
00136 }
00137 }
00138
00139
00140
00141 void MayaDisplayDevice::triangle(const float *v1, const float *v2, const float *v3,
00142 const float *n1, const float *n2, const float *n3) {
00143 float a[3], b[3], c[3];
00144 float norm1[3], norm2[3], norm3[3];
00145
00146
00147 (transMat.top()).multpoint3d(v1, a);
00148 (transMat.top()).multpoint3d(v2, b);
00149 (transMat.top()).multpoint3d(v3, c);
00150
00151
00152 (transMat.top()).multnorm3d(n1, norm1);
00153 (transMat.top()).multnorm3d(n2, norm2);
00154 (transMat.top()).multnorm3d(n3, norm3);
00155
00156
00157 fprintf(outfile, "createNode transform -n \"vmd%d\";\n", objnameindex);
00158 fprintf(outfile, "createNode mesh -n \"vmd%dShape\" -p \"vmd%d\";\n", objnameindex, objnameindex);
00159 fprintf(outfile, " setAttr -k off \".v\";\n");
00160 fprintf(outfile, " setAttr -s 1 \".iog[0].og\";\n");
00161 fprintf(outfile, " setAttr \".iog[0].og[0].gcl\" -type \"componentList\" 1 \"f[0]\";\n");
00162 fprintf(outfile, " setAttr \".uvst[0].uvsn\" -type \"string\" \"map1\";\n");
00163 fprintf(outfile, " setAttr \".cuvs\" -type \"string\" \"map1\";\n");
00164 fprintf(outfile, " setAttr \".dcc\" -type \"string\" \"Ambient+Diffuse\";\n");
00165 fprintf(outfile, " setAttr -s 3 \".vt[0:2]\" \n");
00166 fprintf(outfile, " %f %f %f\n", a[0], a[1], a[2]);
00167 fprintf(outfile, " %f %f %f\n", b[0], b[1], b[2]);
00168 fprintf(outfile, " %f %f %f ;\n", c[0], c[1], c[2]);
00169 fprintf(outfile, " setAttr -s 3 \".ed[0:2]\" \n");
00170 fprintf(outfile, " 0 1 0\n");
00171 fprintf(outfile, " 1 2 0\n");
00172 fprintf(outfile, " 2 0 0 ;\n");
00173 fprintf(outfile, " setAttr -s 3 \".n[0:2]\" -type \"float3\" \n");
00174 fprintf(outfile, " %f %f %f\n", norm1[0], norm1[1], norm1[2]);
00175 fprintf(outfile, " %f %f %f\n", norm2[0], norm2[1], norm2[2]);
00176 fprintf(outfile, " %f %f %f ;\n", norm3[0], norm3[1], norm3[2]);
00177 fprintf(outfile, " setAttr -s 1 \".fc[0]\" -type \"polyFaces\"\n");
00178 fprintf(outfile, " f 3 0 1 2 ;\n");
00179 fprintf(outfile, " setAttr \".cd\" -type \"dataPolyComponent\" Index_Data Edge 0 ;\n");
00180 fprintf(outfile, " setAttr \".cvd\" -type \"dataPolyComponent\" Index_Data Vertex 0 ;\n");
00181
00182 char strbuf[1024];
00183 sprintf(strbuf, "|vmd%d|vmd%dShape", objnameindex, objnameindex);
00184 write_cindexmaterial(strbuf, colorIndex, materialIndex);
00185
00186
00187 objnameindex++;
00188 }
00189
00190 #if 0
00191 void MayaDisplayDevice::tricolor(const float *v1, const float *v2, const float *v3,
00192 const float *n1, const float *n2, const float *n3,
00193 const float *c1, const float *c2, const float *c3) {
00194 float a[3], b[3], c[3];
00195 float norm1[3], norm2[3], norm3[3];
00196
00197
00198 (transMat.top()).multpoint3d(v1, a);
00199 (transMat.top()).multpoint3d(v2, b);
00200 (transMat.top()).multpoint3d(v3, c);
00201
00202
00203 (transMat.top()).multnorm3d(n1, norm1);
00204 (transMat.top()).multnorm3d(n2, norm2);
00205 (transMat.top()).multnorm3d(n3, norm3);
00206
00207
00208 fprintf(outfile, "createNode transform -n \"vmd%d\";\n", objnameindex);
00209 fprintf(outfile, "createNode mesh -n \"vmd%dShape\" -p \"vmd%d\";\n", objnameindex, objnameindex);
00210 fprintf(outfile, " setAttr -k off \".v\";\n");
00211 fprintf(outfile, " setAttr -s 1 \".iog[0].og\";\n");
00212 fprintf(outfile, " setAttr \".iog[0].og[0].gcl\" -type \"componentList\" 1 \"f[0]\";\n");
00213 fprintf(outfile, " setAttr \".uvst[0].uvsn\" -type \"string\" \"map1\";\n");
00214 fprintf(outfile, " setAttr \".cuvs\" -type \"string\" \"map1\";\n");
00215
00216 fprintf(outfile, " setAttr \".dcol\" yes;\n");
00217 fprintf(outfile, " setAttr \".dcc\" -type \"string\" \"Ambient+Diffuse\";\n");
00218
00219 fprintf(outfile, " setAttr \".ccls\" -type \"string\" \"colorSet1\";\n");
00220 fprintf(outfile, " setAttr \".clst[0].clsn\" -type \"string\" \"colorSet1\";\n");
00221
00222 #if 0
00223 fprintf(outfile, " setAttr -s 3 \".clst[0].clsp[0:2]\" -type \"colorSet1\" \n");
00224 fprintf(outfile, " %f %f %f 1.0\n", c1[0], c1[1], c1[2]);
00225 fprintf(outfile, " %f %f %f 1.0\n", c2[0], c2[1], c2[2]);
00226 fprintf(outfile, " %f %f %f 1.0 ;\n", c3[0], c3[1], c3[2]);
00227 #elif 0
00228 fprintf(outfile, " setAttr -s 3 \".vclr[0].vfcl\";\n");
00229 fprintf(outfile, " setAttr \".vclr[0].vfcl[0].frgb\" -type \"float3\" %f %f %f ;\n", c1[0], c1[1], c1[2]);
00230 fprintf(outfile, " setAttr \".vclr[0].vfcl[1].frgb\" -type \"float3\" %f %f %f ;\n", c2[0], c2[1], c2[2]);
00231 fprintf(outfile, " setAttr \".vclr[0].vfcl[2].frgb\" -type \"float3\" %f %f %f ;\n", c3[0], c3[1], c3[2]);
00232 #elif 0
00233 fprintf(outfile, " setAttr \".vclr[0].vrgb\" -type \"float3\" ");
00234 fprintf(outfile, " %f %f %f ;\n", c1[0], c1[1], c1[2]);
00235 fprintf(outfile, " setAttr \".vclr[1].vrgb\" -type \"float3\" ");
00236 fprintf(outfile, " %f %f %f ;\n", c2[0], c2[1], c2[2]);
00237 fprintf(outfile, " setAttr \".vclr[2].vrgb\" -type \"float3\" ");
00238 fprintf(outfile, " %f %f %f ;\n", c3[0], c3[1], c3[2]);
00239 #endif
00240
00241 fprintf(outfile, " setAttr -s 3 \".vt[0:2]\" \n");
00242 fprintf(outfile, " %f %f %f\n", a[0], a[1], a[2]);
00243 fprintf(outfile, " %f %f %f\n", b[0], b[1], b[2]);
00244 fprintf(outfile, " %f %f %f ;\n", c[0], c[1], c[2]);
00245
00246 #if 1
00247 fprintf(outfile, " setAttr -s 3 \".clr[0:2]\" \n");
00248 fprintf(outfile, " %f %f %f 1\n", c1[0], c1[1], c1[2]);
00249 fprintf(outfile, " %f %f %f 1\n", c2[0], c2[1], c2[2]);
00250 fprintf(outfile, " %f %f %f 1 ;\n", c3[0], c3[1], c3[2]);
00251 #endif
00252
00253 fprintf(outfile, " setAttr -s 3 \".ed[0:2]\" \n");
00254 fprintf(outfile, " 0 1 0\n");
00255 fprintf(outfile, " 1 2 0\n");
00256 fprintf(outfile, " 2 0 0 ;\n");
00257 fprintf(outfile, " setAttr -s 3 \".n[0:2]\" -type \"float3\" \n");
00258 fprintf(outfile, " %f %f %f\n", norm1[0], norm1[1], norm1[2]);
00259 fprintf(outfile, " %f %f %f\n", norm2[0], norm2[1], norm2[2]);
00260 fprintf(outfile, " %f %f %f ;\n", norm3[0], norm3[1], norm3[2]);
00261 fprintf(outfile, " setAttr -s 1 \".fc[0]\" -type \"polyFaces\"\n");
00262 fprintf(outfile, " f 3 0 1 2 ;\n");
00263 fprintf(outfile, " setAttr \".cd\" -type \"dataPolyComponent\" Index_Data Edge 0 ;\n");
00264 fprintf(outfile, " setAttr \".cvd\" -type \"dataPolyComponent\" Index_Data Vertex 0 ;\n");
00265
00266 #if 0
00267 fprintf(outfile, "createNode polyColorPerVertex -n \"polyColorPerVertex%d\";\n", objnameindex);
00268 fprintf(outfile, " setAttr \".uopa\" yes;\n");
00269 fprintf(outfile, " setAttr -s 3 \".vclr\";\n");
00270 fprintf(outfile, " setAttr -s 1 \".vclr[0].vfcl\";\n");
00271 fprintf(outfile, " setAttr \".vclr[0].vfcl[0].frgb\" -type \"float3\" %f %f %f ;\n", c1[0], c1[1], c1[2]);
00272 fprintf(outfile, " setAttr \".vclr[1].vfcl[0].frgb\" -type \"float3\" %f %f %f ;\n", c2[0], c2[1], c2[2]);
00273 fprintf(outfile, " setAttr \".vclr[1].vfcl[0].frgb\" -type \"float3\" %f %f %f ;\n", c3[0], c3[1], c3[2]);
00274 fprintf(outfile, " setAttr \".cn\" -type \"string\" \"colorSet1\";\n");
00275 fprintf(outfile, " setAttr \".clam\" no;\n");
00276
00277
00278 #endif
00279
00280
00281 char strbuf[1024];
00282 sprintf(strbuf, "|vmd%d|vmd%dShape", objnameindex, objnameindex);
00283 write_cindexmaterial(strbuf, colorIndex, materialIndex);
00284
00285
00286 objnameindex++;
00287 }
00288 #endif
00289
00290 int MayaDisplayDevice::open_file(const char *filename) {
00291 if (isOpened) {
00292 close_file();
00293 }
00294 if ((outfile = fopen(filename, "w")) == NULL) {
00295 msgErr << "Could not open file " << filename
00296 << " in current directory for writing!" << sendmsg;
00297 return FALSE;
00298 }
00299 my_filename = stringdup(filename);
00300 isOpened = TRUE;
00301 reset_state();
00302 objnameindex = 0;
00303 oldColorIndex = -1;
00304 oldMaterialIndex = -1;
00305 oldMaterialState = -1;
00306 return TRUE;
00307 }
00308
00309 void MayaDisplayDevice::close_file(void) {
00310 if (outfile) {
00311 fclose(outfile);
00312 outfile = NULL;
00313 }
00314 delete [] my_filename;
00315 my_filename = NULL;
00316 isOpened = FALSE;
00317 }
00318
00319 void MayaDisplayDevice::write_header(void) {
00320 fprintf(outfile, "//Maya ASCII 2010 scene\n");
00321 fprintf(outfile, "//Codeset: UTF-8\n");
00322 fprintf(outfile, "requires maya \"2010\";\n");
00323 fprintf(outfile, "currentUnit -l centimeter -a degree -t film;\n");
00324 fprintf(outfile, "fileInfo \"application\" \"vmd\";\n");
00325 fprintf(outfile, "fileInfo \"product\" \"VMD %s\";\n", VMDVERSION);
00326 fprintf(outfile, "fileInfo \"version\" \"%s\";\n", VMD_ARCH);
00327
00328 write_material_block();
00329
00330 fprintf(outfile, "// VMD template objects for instancing/copying geometry\n");
00331 fprintf(outfile, "createNode transform -n \"VMDNurbSphere\";\n");
00332
00333 fprintf(outfile, " setAttr \".v\" no;\n");
00334
00335 fprintf(outfile, "createNode nurbsSurface -n \"nurbsSphereShape1\" -p \"VMDNurbSphere\";\n");
00336 fprintf(outfile, " setAttr -k off \".v\";\n");
00337 fprintf(outfile, " setAttr \".vir\" yes;\n");
00338 fprintf(outfile, " setAttr \".vif\" yes;\n");
00339 fprintf(outfile, " setAttr \".tw\" yes;\n");
00340 fprintf(outfile, " setAttr \".covm[0]\" 0 1 1;\n");
00341 fprintf(outfile, " setAttr \".cdvm[0]\" 0 1 1;\n");
00342 fprintf(outfile, " setAttr \".dvu\" 0;\n");
00343 fprintf(outfile, " setAttr \".dvv\" 0;\n");
00344 fprintf(outfile, " setAttr \".cpr\" 3;\n");
00345 fprintf(outfile, " setAttr \".cps\" 3;\n");
00346 fprintf(outfile, " setAttr \".nufa\" 4.5;\n");
00347 fprintf(outfile, " setAttr \".nvfa\" 4.5;\n");
00348
00349 fprintf(outfile, "createNode makeNurbSphere -n \"makeNurbSphere1\";\n");
00350 fprintf(outfile, " setAttr \".ax\" -type \"double3\" 0 1 0;\n");
00351 fprintf(outfile, " setAttr \".r\" 1.0;\n");
00352
00353 fprintf(outfile, "connectAttr \"makeNurbSphere1.os\" \"nurbsSphereShape1.cr\";\n");
00354 fprintf(outfile, "connectAttr \"|VMDNurbSphere|nurbsSphereShape1.iog\" \":initialShadingGroup.dsm\" -na;\n");
00355
00356 fprintf(outfile, "// End of template objects\n");
00357 }
00358
00359 void MayaDisplayDevice::write_material_block(void) {
00360 int n;
00361
00362
00363 for (n=BEGREGCLRS; n < (BEGREGCLRS + REGCLRS + MAPCLRS); n++) {
00364 fprintf(outfile, "createNode blinn -n \"vmd_mat_cindex_%d\";\n", n);
00365 fprintf(outfile, " setAttr \".c\" -type \"float3\" %f %f %f;\n",
00366 matData[n][0], matData[n][1], matData[n][2]);
00367 fprintf(outfile, "createNode shadingEngine -n \"vmd_mat_cindex_%d_SG\";\n", n);
00368 fprintf(outfile, " setAttr \".ihi\" 0;\n");
00369 fprintf(outfile, " setAttr \".ro\" yes;\n");
00370 fprintf(outfile, "createNode materialInfo -n \"vmd_mat_info_cindex_%d\";\n", n);
00371 fprintf(outfile, "connectAttr \"vmd_mat_cindex_%d.oc\" \"vmd_mat_cindex_%d_SG.ss\";\n", n, n);
00372 fprintf(outfile, "connectAttr \"vmd_mat_cindex_%d_SG.msg\" \"vmd_mat_info_cindex_%d.sg\";\n", n, n);
00373 fprintf(outfile, "connectAttr \"vmd_mat_cindex_%d.msg\" \"vmd_mat_info_cindex_%d.m\";\n", n, n);
00374 fprintf(outfile, "\n");
00375 }
00376
00377
00378 for (n=BEGREGCLRS; n < (BEGREGCLRS + REGCLRS + MAPCLRS); n++) {
00379 fprintf(outfile, "createNode lambert -n \"vmd_nomat_cindex_%d\";\n", n);
00380 fprintf(outfile, " setAttr \".c\" -type \"float3\" %f %f %f;\n",
00381 matData[n][0], matData[n][1], matData[n][2]);
00382 fprintf(outfile, "createNode shadingEngine -n \"vmd_nomat_cindex_%d_SG\";\n", n);
00383 fprintf(outfile, " setAttr \".ihi\" 0;\n");
00384 fprintf(outfile, " setAttr \".ro\" yes;\n");
00385 fprintf(outfile, "createNode materialInfo -n \"vmd_nomat_info_cindex_%d\";\n", n);
00386 fprintf(outfile, "connectAttr \"vmd_nomat_cindex_%d.oc\" \"vmd_nomat_cindex_%d_SG.ss\";\n", n, n);
00387 fprintf(outfile, "connectAttr \"vmd_nomat_cindex_%d_SG.msg\" \"vmd_nomat_info_cindex_%d.sg\";\n", n, n);
00388 fprintf(outfile, "connectAttr \"vmd_nomat_cindex_%d.msg\" \"vmd_nomat_info_cindex_%d.m\";\n", n, n);
00389 fprintf(outfile, "\n");
00390 }
00391 }
00392
00393 void MayaDisplayDevice::write_cindexmaterial(const char *mayaobjstr, int cindex, int material) {
00394
00395 if (materials_on) {
00396 fprintf(outfile, "connectAttr \"%s.iog\" \"vmd_mat_cindex_%d_SG.dsm\" -na;\n", mayaobjstr, cindex);
00397 } else {
00398 fprintf(outfile, "connectAttr \"%s.iog\" \"vmd_nomat_cindex_%d_SG.dsm\" -na;\n", mayaobjstr, cindex);
00399 }
00400 oldMaterialIndex = material;
00401 oldColorIndex = cindex;
00402 oldMaterialState = materials_on;
00403 }
00404
00405 void MayaDisplayDevice::write_colormaterial(float *rgb, int material) {
00406
00407
00408 }
00409
00410 void MayaDisplayDevice::write_trailer (void) {
00411 msgWarn << "Materials are not exported to Maya files.\n";
00412 }
00413