PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/CS/migrated/branches/R0_16/libs/csparser/impexp/iv.cpp

#
C++ | 1473 lines | 1019 code | 146 blank | 308 comment | 578 complexity | 7f28c593c0d6e85ca8d0141657ddaecd MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0
  1. /*
  2. Crystal Space 3d format converter
  3. Based on IVCON - converts various 3D graphics file
  4. Author: John Burkardt - used with permission
  5. CS adaption and conversion to C++ classes Bruce Williams
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free
  16. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include "cssysdef.h"
  19. #include "csparser/impexp.h"
  20. // converter.cpp: implementation of the converter class.
  21. //
  22. //////////////////////////////////////////////////////////////////////
  23. /******************************************************************************/
  24. int converter::iv_read ( FILE *filein ) {
  25. /******************************************************************************/
  26. /*
  27. Purpose:
  28. IV_READ reads graphics information from an Inventor file.
  29. Modified:
  30. 20 October 1998
  31. Author:
  32. John Burkardt
  33. */
  34. int count;
  35. int i;
  36. int icolor = 0;
  37. int icface;
  38. int ihi;
  39. int inormface;
  40. int inum_face;
  41. int ivert = 0;
  42. int iword;
  43. int ix;
  44. int ixyz;
  45. int iy;
  46. int iz;
  47. int jval;
  48. int level;
  49. char material_binding[80];
  50. char normal_binding[80];
  51. int nbase;
  52. char *next;
  53. int nlbrack;
  54. int nrbrack;
  55. int nu;
  56. int null_index;
  57. int num_line2;
  58. int num_face2;
  59. int num_normal_temp;
  60. int nv;
  61. int result;
  62. float rval;
  63. int width;
  64. char word[MAX_INCHARS];
  65. char word1[MAX_INCHARS];
  66. char wordm1[MAX_INCHARS];
  67. icface = 0;
  68. inormface = 0;
  69. inum_face = 0;
  70. ix = 0;
  71. ixyz = 0;
  72. iy = 0;
  73. iz = 0;
  74. jval = 0;
  75. level = 0;
  76. strcpy ( levnam[0], "Top" );
  77. nbase = 0;
  78. nlbrack = 0;
  79. nrbrack = 0;
  80. num_face2 = 0;
  81. num_line2 = 0;
  82. num_normal_temp = 0;
  83. rval = 0.0;
  84. strcpy ( word, " " );
  85. strcpy ( wordm1, " " );
  86. /*
  87. Read the next line of text from the input file.
  88. */
  89. while ( TRUE ) {
  90. if ( fgets ( input, MAX_INCHARS, filein ) == NULL ) {
  91. break;
  92. }
  93. num_text = num_text + 1;
  94. next = input;
  95. iword = 0;
  96. /*
  97. Remove all commas from the line, so we can use SSCANF to read
  98. numeric items.
  99. */
  100. i = 0;
  101. while ( input[i] != '\0' ) {
  102. if ( input[i] == ',' ) {
  103. input[i] = ' ';
  104. }
  105. i++;
  106. }
  107. /*
  108. Force brackets and braces to be buffered by spaces.
  109. */
  110. i = 0;
  111. while ( input[i] != '\0' ) {
  112. i++;
  113. }
  114. null_index = i;
  115. i = 0;
  116. while ( input[i] != '\0' && i < MAX_INCHARS ) {
  117. if ( input[i] == '[' || input[i] == ']' ||
  118. input[i] == '{' || input[i] == '}' ) {
  119. result = char_pad ( &i, &null_index, input, MAX_INCHARS );
  120. if ( result == ERROR ) {
  121. break;
  122. }
  123. }
  124. else {
  125. i++;
  126. }
  127. }
  128. /*
  129. Read a word from the line.
  130. */
  131. while ( TRUE ) {
  132. strcpy ( wordm1, word );
  133. strcpy ( word, " " );
  134. count = sscanf ( next, "%s%n", word, &width );
  135. next = next + width;
  136. if ( count <= 0 ) {
  137. break;
  138. }
  139. iword = iword + 1;
  140. if ( iword == 1 ) {
  141. strcpy ( word1, word );
  142. }
  143. /*
  144. The first line of the file must be the header.
  145. */
  146. if ( num_text == 1 ) {
  147. if ( leqi ( word1, "#Inventor" ) != TRUE ) {
  148. fprintf ( logfile, "\n" );
  149. fprintf ( logfile, "IV_READ - Fatal error!\n" );
  150. fprintf ( logfile, " The input file has a bad header.\n" );
  151. return ERROR;
  152. }
  153. else {
  154. num_comment = num_comment + 1;
  155. }
  156. break;
  157. }
  158. /*
  159. A comment begins anywhere with '#'.
  160. Skip the rest of the line.
  161. */
  162. if ( word[1] == '#' ) {
  163. num_comment = num_comment + 1;
  164. break;
  165. }
  166. /*
  167. If the word is a curly or square bracket, count it.
  168. If the word is a left bracket, the previous word is the name of a node.
  169. */
  170. if ( strcmp ( word, "{" ) == 0 || strcmp ( word, "[" ) == 0 ) {
  171. nlbrack = nlbrack + 1;
  172. level = nlbrack - nrbrack;
  173. strcpy ( levnam[level], wordm1 );
  174. if ( debug == TRUE ) {
  175. fprintf ( logfile, "Level: %s\n", wordm1 );
  176. }
  177. }
  178. else if ( strcmp ( word, "}" ) == 0 || strcmp ( word, "]" ) == 0 ) {
  179. nrbrack = nrbrack + 1;
  180. if ( nlbrack < nrbrack ) {
  181. fprintf ( logfile, "\n" );
  182. fprintf ( logfile, "IV_READ - Fatal error!\n" );
  183. fprintf ( logfile, " Extraneous right bracket on line %d.\n", num_text );
  184. fprintf ( logfile, " Currently processing field %s\n.", levnam[level] );
  185. return ERROR;
  186. }
  187. }
  188. /*
  189. BASECOLOR
  190. */
  191. if ( leqi ( levnam[level], "BASECOLOR" ) == TRUE ) {
  192. if ( strcmp ( word, "{" ) == 0 ) {
  193. }
  194. else if ( strcmp ( word, "}" ) == 0 ) {
  195. level = nlbrack - nrbrack;
  196. }
  197. else if ( leqi ( word, "RGB" ) == TRUE ) {
  198. }
  199. else {
  200. num_bad = num_bad + 1;
  201. fprintf ( logfile, "Bad data %s\n", word );
  202. }
  203. }
  204. /*
  205. COORDINATE3
  206. */
  207. else if ( leqi ( levnam[level], "COORDINATE3" ) == TRUE ) {
  208. if ( strcmp ( word, "{" ) == 0 ) {
  209. }
  210. else if ( strcmp ( word, "}" ) == 0 ) {
  211. level = nlbrack - nrbrack;
  212. }
  213. else if ( leqi ( word, "POINT" ) == TRUE ) {
  214. }
  215. else {
  216. num_bad = num_bad + 1;
  217. fprintf ( logfile, "COORDINATE3: Bad data %s\n", word );
  218. }
  219. }
  220. /*
  221. COORDINATE4
  222. */
  223. else if ( leqi ( levnam[level], "COORDINATE4" ) == TRUE ) {
  224. if ( strcmp ( word, "{" ) == 0 ) {
  225. }
  226. else if ( strcmp ( word, "}" ) == 0 ) {
  227. level = nlbrack - nrbrack;
  228. }
  229. else if ( leqi ( word, "POINT" ) == TRUE ) {
  230. }
  231. else {
  232. num_bad = num_bad + 1;
  233. fprintf ( logfile, "COORDINATE4: Bad data %s\n", word );
  234. }
  235. }
  236. /*
  237. COORDINDEX
  238. */
  239. else if ( leqi ( levnam[level], "COORDINDEX" ) == TRUE ) {
  240. if ( strcmp ( word, "[" ) == 0 ) {
  241. ivert = 0;
  242. }
  243. else if ( strcmp ( word, "]" ) == 0 ) {
  244. level = nlbrack - nrbrack;
  245. }
  246. /*
  247. (indexedlineset) COORDINDEX
  248. */
  249. else if ( leqi ( levnam[level-1], "INDEXEDLINESET" ) == TRUE ) {
  250. count = sscanf ( word, "%d%n", &jval, &width );
  251. if ( count > 0 ) {
  252. if ( jval < -1 ) {
  253. num_bad = num_bad + 1;
  254. }
  255. else {
  256. if ( num_line < MAX_LINE ) {
  257. if ( jval != -1 ) {
  258. jval = jval + nbase;
  259. }
  260. line_dex[num_line] = jval;
  261. }
  262. num_line = num_line + 1;
  263. }
  264. }
  265. else {
  266. num_bad = num_bad + 1;
  267. }
  268. }
  269. /*
  270. (indexedfaceset) COORDINDEX
  271. Warning: If the list of indices is not terminated with a final -1, then
  272. the last face won't get counted.
  273. */
  274. else if ( leqi ( levnam[level-1], "INDEXEDFACESET" ) == TRUE ) {
  275. count = sscanf ( word, "%d%n", &jval, &width );
  276. if ( count > 0 ) {
  277. if ( jval == -1 ) {
  278. ivert = 0;
  279. num_face = num_face + 1;
  280. }
  281. else {
  282. if ( ivert == 0 ) {
  283. if ( num_face < MAX_FACE ) {
  284. face_order[num_face] = 0;
  285. }
  286. }
  287. if ( num_face < MAX_FACE ) {
  288. face_order[num_face] = face_order[num_face] + 1;
  289. face[ivert][num_face] = jval + nbase;
  290. ivert = ivert + 1;
  291. }
  292. }
  293. }
  294. }
  295. /*
  296. (indexednurbssurface) COORDINDEX
  297. */
  298. else if ( leqi ( levnam[level-1], "INDEXEDNURBSSURFACE" ) == TRUE ) {
  299. }
  300. /*
  301. (indexedtrianglestripset) COORDINDEX
  302. First three coordinate indices I1, I2, I3 define a triangle.
  303. Next triangle is defined by I2, I3, I4 (actually, I4, I3, I2
  304. to stay with same counterclockwise sense).
  305. Next triangle is defined by I3, I4, I5 ( do not need to reverse
  306. odd numbered triangles) and so on.
  307. List is terminated with -1.
  308. */
  309. else if ( leqi ( levnam[level-1], "INDEXEDTRIANGLESTRIPSET" ) == TRUE ) {
  310. count = sscanf ( word, "%d%n", &jval, &width );
  311. if ( count > 0 ) {
  312. if ( jval == -1 ) {
  313. ivert = 0;
  314. }
  315. else {
  316. ix = iy;
  317. iy = iz;
  318. iz = jval + nbase;
  319. if ( ivert == 0 ) {
  320. if ( num_face < MAX_FACE ) {
  321. face[ivert][num_face] = jval + nbase;
  322. face_order[num_face] = 3;
  323. }
  324. }
  325. else if ( ivert == 1 ) {
  326. if ( num_face < MAX_FACE ) {
  327. face[ivert][num_face] = jval + nbase;
  328. }
  329. }
  330. else if ( ivert == 2 ) {
  331. if ( num_face < MAX_FACE ) {
  332. face[ivert][num_face] = jval + nbase;
  333. }
  334. num_face = num_face + 1;
  335. }
  336. else {
  337. if ( num_face < MAX_FACE ) {
  338. face_order[num_face] = 3;
  339. if ( ( ivert % 2 ) == 0 ) {
  340. face[0][num_face] = ix;
  341. face[1][num_face] = iy;
  342. face[2][num_face] = iz;
  343. }
  344. else {
  345. face[0][num_face] = iz;
  346. face[1][num_face] = iy;
  347. face[2][num_face] = ix;
  348. }
  349. }
  350. num_face = num_face + 1;
  351. }
  352. ivert = ivert + 1;
  353. /*
  354. Very very tentative guess as to how indices into the normal
  355. vector array are set up...
  356. */
  357. if ( num_face < MAX_FACE && ivert > 2 ) {
  358. for ( i = 0; i < 3; i++ ) {
  359. face_normal[i][num_face] = normal_temp[i][ix];
  360. }
  361. }
  362. }
  363. }
  364. }
  365. }
  366. /*
  367. INDEXEDFACESET
  368. */
  369. else if ( leqi ( levnam[level], "INDEXEDFACESET" ) == TRUE ) {
  370. if ( strcmp ( word, "{" ) == 0 ) {
  371. }
  372. else if ( strcmp ( word, "}" ) == 0 ) {
  373. level = nlbrack - nrbrack;
  374. }
  375. else if ( leqi ( word, "COORDINDEX" ) == TRUE ) {
  376. ivert = 0;
  377. }
  378. else if ( leqi ( word, "MATERIALINDEX" ) == TRUE ) {
  379. }
  380. else if ( leqi ( word, "NORMALINDEX" ) == TRUE ) {
  381. }
  382. else if ( leqi ( word, "TEXTURECOORDINDEX" ) == TRUE ) {
  383. }
  384. else {
  385. num_bad = num_bad + 1;
  386. fprintf ( logfile, "Bad data %s\n", word );
  387. }
  388. }
  389. /*
  390. INDEXEDLINESET
  391. */
  392. else if ( leqi ( levnam[level], "INDEXEDLINESET" ) == TRUE ) {
  393. if ( strcmp ( word, "{" ) == 0 ) {
  394. }
  395. else if ( strcmp ( word, "}" ) == 0 ) {
  396. level = nlbrack - nrbrack;
  397. }
  398. else if ( leqi ( word, "COORDINDEX" ) == TRUE ) {
  399. }
  400. else if ( leqi ( word, "MATERIALINDEX" ) == TRUE ) {
  401. }
  402. else {
  403. num_bad = num_bad + 1;
  404. fprintf ( logfile, "Bad data %s\n", word );
  405. }
  406. }
  407. /*
  408. INDEXEDNURBSSURFACE
  409. */
  410. else if ( leqi ( levnam[level], "INDEXEDNURBSSURFACE" ) == TRUE ) {
  411. if ( strcmp ( word, "{" ) == 0 ) {
  412. }
  413. else if ( strcmp ( word, "}" ) == 0 ) {
  414. level = nlbrack - nrbrack;
  415. }
  416. else if ( leqi ( word, "NUMUCONTROLPOINTS") == TRUE ) {
  417. count = sscanf ( word, "%d%n", &jval, &width );
  418. if ( count > 0 ) {
  419. nu = jval;
  420. }
  421. else {
  422. nu = 0;
  423. num_bad = num_bad + 1;
  424. fprintf ( logfile, "Bad data %s\n", word );
  425. }
  426. }
  427. else if ( leqi ( word, "NUMVCONTROLPOINTS" ) == TRUE ) {
  428. count = sscanf ( word, "%d%n", &jval, &width );
  429. if ( count > 0 ) {
  430. nv = jval;
  431. }
  432. else {
  433. nv = 0;
  434. num_bad = num_bad + 1;
  435. }
  436. }
  437. else if ( leqi ( word, "COORDINDEX" ) == TRUE ) {
  438. }
  439. else if ( leqi ( word, "UKNOTVECTOR" ) == TRUE ) {
  440. }
  441. else if ( leqi ( word, "VKNOTVECTOR" ) == TRUE ) {
  442. }
  443. else {
  444. num_bad = num_bad + 1;
  445. fprintf ( logfile, "Bad data %s\n", word );
  446. }
  447. }
  448. /*
  449. INDEXEDTRIANGLESTRIPSET
  450. */
  451. else if ( leqi ( levnam[level], "INDEXEDTRIANGLESTRIPSET" ) == TRUE ) {
  452. if ( strcmp ( word, "{" ) == 0 ) {
  453. }
  454. else if ( strcmp ( word, "}" ) == 0 ) {
  455. level = nlbrack - nrbrack;
  456. }
  457. else if ( leqi ( word, "VERTEXPROPERTY" ) == TRUE ) {
  458. count = sscanf ( next, "%s%n", word, &width );
  459. next = next + width;
  460. }
  461. else if ( leqi ( word, "COORDINDEX" ) == TRUE ) {
  462. ivert = 0;
  463. }
  464. else if ( leqi ( word, "NORMALINDEX" ) == TRUE ) {
  465. count = sscanf ( next, "%s%n", word, &width );
  466. next = next + width;
  467. }
  468. else {
  469. num_bad = num_bad + 1;
  470. fprintf ( logfile, "Bad data %s\n", word );
  471. }
  472. }
  473. /*
  474. INFO
  475. */
  476. else if ( leqi ( levnam[level], "INFO" ) == TRUE ) {
  477. if ( strcmp ( word, "{" ) == 0 ) {
  478. }
  479. else if ( strcmp ( word, "}" ) == 0 ) {
  480. level = nlbrack - nrbrack;
  481. }
  482. else if ( leqi ( word, "STRING" ) == TRUE ) {
  483. }
  484. else if ( strcmp ( word, "\"" ) == 0) {
  485. }
  486. else {
  487. }
  488. }
  489. /*
  490. LIGHTMODEL
  491. Read, but ignore.
  492. */
  493. else if ( leqi ( levnam[level], "LIGHTMODEL" ) == TRUE ) {
  494. if ( strcmp ( word, "{" ) == 0 ) {
  495. }
  496. else if ( strcmp ( word, "}" ) == 0 ) {
  497. level = nlbrack - nrbrack;
  498. }
  499. else if ( leqi ( word, "model" ) == TRUE ) {
  500. }
  501. else {
  502. }
  503. }
  504. /*
  505. MATERIAL
  506. Read, but ignore.
  507. */
  508. else if ( leqi ( levnam[level],"MATERIAL" ) == TRUE ) {
  509. if ( strcmp ( word, "{" ) == 0 ) {
  510. }
  511. else if ( strcmp ( word, "}" ) == 0 ) {
  512. level = nlbrack - nrbrack;
  513. }
  514. else if ( leqi ( word, "AMBIENTCOLOR" ) == TRUE ) {
  515. }
  516. else if ( leqi ( word, "EMISSIVECOLOR" ) == TRUE ) {
  517. }
  518. else if ( leqi ( word, "DIFFUSECOLOR" ) == TRUE ) {
  519. }
  520. else if ( leqi ( word, "SHININESS" ) == TRUE ) {
  521. }
  522. else if ( leqi ( word, "SPECULARCOLOR" ) == TRUE ) {
  523. }
  524. else if ( leqi ( word, "TRANSPARENCY" ) == TRUE ) {
  525. }
  526. else {
  527. }
  528. }
  529. /*
  530. MATERIALBINDING
  531. Read, but ignore
  532. */
  533. else if ( leqi ( levnam[level], "MATERIALBINDING" ) == TRUE ) {
  534. if ( strcmp ( word, "{" ) == 0 ) {
  535. }
  536. else if ( strcmp ( word, "}" ) == 0 ) {
  537. level = nlbrack - nrbrack;
  538. }
  539. else if ( leqi ( word, "VALUE" ) == TRUE ) {
  540. count = sscanf ( next, "%s%n", material_binding, &width );
  541. next = next + width;
  542. }
  543. else {
  544. count = sscanf ( next, "%f%n", &rval, &width );
  545. next = next + width;
  546. if ( count > 0 ) {
  547. }
  548. else {
  549. num_bad = num_bad + 1;
  550. fprintf ( logfile, "Bad data %s\n", word );
  551. }
  552. }
  553. }
  554. /*
  555. MATERIALINDEX
  556. */
  557. else if ( leqi ( levnam[level], "MATERIALINDEX" ) == TRUE ) {
  558. if ( strcmp ( word, "[" ) == 0 ) {
  559. ivert = 0;
  560. }
  561. else if ( strcmp ( word, "]" ) == 0 ) {
  562. level = nlbrack - nrbrack;
  563. }
  564. /*
  565. (indexedfaceset) MATERIALINDEX
  566. */
  567. else if ( leqi ( levnam[level-1], "INDEXEDFACESET" ) == TRUE ) {
  568. count = sscanf ( word, "%d%n", &jval, &width );
  569. if ( count > 0 ) {
  570. if ( jval == -1 ) {
  571. ivert = 0;
  572. num_face2 = num_face2 + 1;
  573. }
  574. else {
  575. if ( num_face2 < MAX_FACE ) {
  576. if ( jval != -1 ) {
  577. jval = jval + nbase;
  578. }
  579. face_mat[ivert][num_face2] = jval;
  580. ivert = ivert + 1;
  581. }
  582. }
  583. }
  584. else {
  585. num_bad = num_bad + 1;
  586. fprintf ( logfile, "Bad data %s\n", word );
  587. }
  588. }
  589. /*
  590. (indexedlineset) MATERIALINDEX
  591. */
  592. else if ( leqi ( levnam[level-1], "INDEXEDLINESET" ) == TRUE ) {
  593. count = sscanf ( word, "%d%n", &jval, &width );
  594. if ( count > 0 ) {
  595. if ( num_line2 < MAX_LINE ) {
  596. if ( jval != -1 ) {
  597. jval = jval + nbase;
  598. }
  599. line_mat[num_line2] = jval;
  600. num_line2 = num_line2 + 1;
  601. }
  602. }
  603. else {
  604. num_bad = num_bad + 1;
  605. fprintf ( logfile, "Bad data %s\n", word );
  606. }
  607. }
  608. else {
  609. count = sscanf ( word, "%d%n", &jval, &width );
  610. if ( count > 0 ) {
  611. }
  612. else {
  613. num_bad = num_bad + 1;
  614. fprintf ( logfile, "Bad data %s\n", word );
  615. }
  616. }
  617. }
  618. /*
  619. NORMAL
  620. The field "VECTOR" may be followed by three numbers,
  621. (handled here), or by a square bracket, and sets of three numbers.
  622. */
  623. else if ( leqi ( levnam[level], "NORMAL" ) == TRUE ) {
  624. /*
  625. (vertexproperty) NORMAL
  626. */
  627. if ( leqi ( levnam[level-1], "VERTEXPROPERTY" ) == TRUE ) {
  628. if ( strcmp ( word, "[" ) == 0 ) {
  629. ixyz = 0;
  630. }
  631. else if ( strcmp ( word, "]" ) == 0 ) {
  632. level = nlbrack - nrbrack;
  633. }
  634. else {
  635. count = sscanf ( word, "%f%n", &rval, &width );
  636. if ( count > 0 ) {
  637. if ( inormface < MAX_FACE ) {
  638. face_normal[ixyz][inormface] = rval;
  639. }
  640. ixyz = ixyz + 1;
  641. if ( ixyz > 2 ) {
  642. ixyz = 0;
  643. inormface = inormface + 1;
  644. }
  645. }
  646. }
  647. }
  648. /*
  649. (anythingelse) NORMAL
  650. */
  651. else {
  652. if ( strcmp ( word, "{" ) == 0 ) {
  653. ixyz = 0;
  654. }
  655. else if ( strcmp ( word, "}" ) == 0 ) {
  656. level = nlbrack - nrbrack;
  657. }
  658. else if ( leqi ( word, "VECTOR" ) == TRUE ) {
  659. }
  660. else {
  661. count = sscanf ( word, "%f%n", &rval, &width );
  662. if ( count > 0 ) {
  663. /* COMMENTED OUT
  664. if ( nfnorm < MAX_FACE ) {
  665. normal[ixyz][nfnorm] = rval;
  666. }
  667. */
  668. ixyz = ixyz + 1;
  669. if ( ixyz > 2 ) {
  670. ixyz = 0;
  671. }
  672. }
  673. else {
  674. num_bad = num_bad + 1;
  675. fprintf ( logfile, "Bad data %s\n", word );
  676. }
  677. }
  678. }
  679. }
  680. /*
  681. NORMALBINDING
  682. Read, but ignore
  683. */
  684. else if ( leqi ( levnam[level], "NORMALBINDING" ) == TRUE ) {
  685. if ( strcmp ( word, "{" ) == 0 ) {
  686. }
  687. else if ( strcmp ( word, "}" ) == 0 ) {
  688. level = nlbrack - nrbrack;
  689. }
  690. else if ( leqi ( word, "VALUE" ) == TRUE ) {
  691. count = sscanf ( next, "%s%n", normal_binding, &width );
  692. next = next + width;
  693. }
  694. else {
  695. count = sscanf ( word, "%f%n", &rval, &width );
  696. if ( count > 0 ) {
  697. }
  698. else {
  699. num_bad = num_bad + 1;
  700. fprintf ( logfile, "Bad data %s\n", word );
  701. }
  702. }
  703. }
  704. /*
  705. NORMALINDEX
  706. */
  707. else if ( leqi ( levnam[level], "NORMALINDEX" ) == TRUE ) {
  708. /*
  709. (indexedtrianglestripset) NORMALINDEX
  710. */
  711. if ( leqi ( levnam[level-1], "INDEXEDTRIANGLESTRIPSET" ) == TRUE ) {
  712. count = sscanf ( word, "%d%n", &jval, &width );
  713. if ( count > 0 ) {
  714. }
  715. else if ( strcmp ( word, "[" ) == 0 ) {
  716. }
  717. else if ( strcmp ( word, "]" ) == 0 ) {
  718. }
  719. }
  720. /*
  721. (anythingelse) NORMALINDEX
  722. */
  723. else {
  724. if ( strcmp ( word, "[" ) == 0 ) {
  725. ivert = 0;
  726. }
  727. else if ( strcmp ( word, "]" ) == 0 ) {
  728. level = nlbrack - nrbrack;
  729. }
  730. else {
  731. count = sscanf ( word, "%d%n", &jval, &width );
  732. if ( count > 0 ) {
  733. if ( jval == -1 ) {
  734. ivert = 0;
  735. inum_face = inum_face + 1;
  736. }
  737. else {
  738. if ( inum_face < MAX_FACE ) {
  739. for ( i = 0; i < 3; i++ ){
  740. vertex_normal[i][ivert][inum_face] = normal_temp[i][jval];
  741. }
  742. ivert = ivert + 1;
  743. }
  744. }
  745. }
  746. else {
  747. num_bad = num_bad + 1;
  748. fprintf ( logfile, "Bad data %s\n", word );
  749. }
  750. }
  751. }
  752. }
  753. /*
  754. POINT
  755. */
  756. else if ( leqi ( levnam[level], "POINT" ) == TRUE ) {
  757. if ( strcmp ( word, "[" ) == 0 ) {
  758. ixyz = 0;
  759. }
  760. else if ( strcmp ( word, "]" ) == 0 ) {
  761. level = nlbrack - nrbrack;
  762. }
  763. else {
  764. count = sscanf ( word, "%f%n", &rval, &width );
  765. if ( count > 0 ) {
  766. if ( num_cor3 < MAX_COR3 ) {
  767. cor3[ixyz][num_cor3] = rval;
  768. }
  769. ixyz = ixyz + 1;
  770. if ( ixyz == 3 ) {
  771. ixyz = 0;
  772. num_cor3 = num_cor3 + 1;
  773. continue;
  774. }
  775. }
  776. else {
  777. num_bad = num_bad + 1;
  778. break;
  779. }
  780. }
  781. }
  782. /*
  783. RGB
  784. */
  785. else if ( leqi ( levnam[level],"RGB" ) == TRUE ) {
  786. /*
  787. (basecolor) RGB
  788. */
  789. if ( leqi ( levnam[level-1], "BASECOLOR" ) == TRUE ) {
  790. if ( strcmp ( word, "[" ) == 0 ) {
  791. icolor = 0;
  792. }
  793. else if ( strcmp ( word, "]" ) == 0 ) {
  794. level = nlbrack - nrbrack;
  795. }
  796. else {
  797. count = sscanf ( word, "%f%n", &rval, &width );
  798. if ( count > 0 ) {
  799. rgbcolor[icolor][num_color] = rval;
  800. icolor = icolor + 1;
  801. if ( icolor == 3 ) {
  802. icolor = 0;
  803. num_color = num_color + 1;
  804. }
  805. }
  806. else {
  807. num_bad = num_bad + 1;
  808. fprintf ( logfile, "Bad data %s\n", word );
  809. }
  810. }
  811. }
  812. /*
  813. (anythingelse RGB)
  814. */
  815. else {
  816. fprintf ( logfile, "HALSBAND DES TODES!\n" );
  817. if ( strcmp ( word, "[" ) == 0 ) {
  818. icolor = 0;
  819. ivert = 0;
  820. }
  821. else if ( strcmp ( word, "]" ) == 0 ) {
  822. level = nlbrack - nrbrack;
  823. }
  824. else {
  825. count = sscanf ( word, "%f%n", &rval, &width );
  826. if ( count > 0 ) {
  827. if ( icface < MAX_FACE ) {
  828. vertex_rgb[icolor][ivert][icface] = rval;
  829. icolor = icolor + 1;
  830. if ( icolor == 3 ) {
  831. icolor = 0;
  832. num_color = num_color + 1;
  833. ivert = ivert + 1;
  834. if ( ivert == face_order[icface] ) {
  835. ivert = 0;
  836. icface = icface + 1;
  837. }
  838. }
  839. }
  840. }
  841. else {
  842. num_bad = num_bad + 1;
  843. fprintf ( logfile, "Bad data %s\n", word );
  844. }
  845. }
  846. }
  847. }
  848. /*
  849. SEPARATOR
  850. */
  851. else if ( leqi ( levnam[level], "SEPARATOR" ) == TRUE ) {
  852. if ( strcmp ( word, "{" ) == 0 ) {
  853. }
  854. else if ( strcmp ( word, "}" ) == 0 ) {
  855. level = nlbrack - nrbrack;
  856. }
  857. else {
  858. }
  859. }
  860. /*
  861. SHAPEHINTS
  862. Read, but ignore.
  863. */
  864. else if ( leqi ( levnam[level], "SHAPEHINTS" ) == TRUE ) {
  865. if ( strcmp ( word, "{" ) == 0 ) {
  866. }
  867. else if ( strcmp ( word, "}" ) == 0 ) {
  868. level = nlbrack - nrbrack;
  869. }
  870. else if ( leqi ( word, "CREASEANGLE" ) == TRUE ) {
  871. count = sscanf ( next, "%f%n", &rval, &width );
  872. next = next + width;
  873. if ( count <= 0 ) {
  874. num_bad = num_bad + 1;
  875. fprintf ( logfile, "Bad data %s\n", word );
  876. }
  877. }
  878. else if ( leqi ( word, "FACETYPE" ) == TRUE ) {
  879. count = sscanf ( next, "%s%n", word, &width );
  880. next = next + width;
  881. }
  882. else if ( leqi ( word, "SHAPETYPE" ) == TRUE ) {
  883. count = sscanf ( next, "%s%n", word, &width );
  884. next = next + width;
  885. }
  886. else if ( leqi ( word, "VERTEXORDERING" ) == TRUE ) {
  887. count = sscanf ( next, "%s%n", word, &width );
  888. next = next + width;
  889. }
  890. else {
  891. num_bad = num_bad + 1;
  892. fprintf ( logfile, "Bad data %s\n", word );
  893. }
  894. }
  895. /*
  896. TEXTURECOORDINDEX
  897. Read but ignore.
  898. */
  899. else if ( leqi ( levnam[level], "TEXTURECOORDINDEX" ) == TRUE ) {
  900. if ( strcmp ( word, "[" ) == 0 ) {
  901. }
  902. else if ( strcmp ( word, "]" ) == 0 ) {
  903. level = nlbrack - nrbrack;
  904. }
  905. else {
  906. count = sscanf ( word, "%d%n", &jval, &width );
  907. if ( count > 0 ) {
  908. }
  909. else {
  910. num_bad = num_bad + 1;
  911. fprintf ( logfile, "Bad data %s\n", word );
  912. }
  913. }
  914. }
  915. /*
  916. UKNOTVECTOR
  917. */
  918. else if ( leqi ( levnam[level], "UKNOTVECTOR" ) == TRUE ) {
  919. if ( strcmp ( word, "[" ) == 0 ) {
  920. continue;
  921. }
  922. else if ( strcmp ( word, "]" ) == 0 ) {
  923. level = nlbrack - nrbrack;
  924. continue;
  925. }
  926. else {
  927. count = sscanf ( word, "%d%n", &jval, &width );
  928. }
  929. }
  930. /*
  931. VECTOR
  932. */
  933. else if ( leqi ( levnam[level], "VECTOR" ) == TRUE ) {
  934. if ( strcmp ( word, "[" ) == 0 ) {
  935. }
  936. else if ( strcmp ( word, "]" ) == 0 ) {
  937. level = nlbrack - nrbrack;
  938. }
  939. /*
  940. (normal) VECTOR
  941. */
  942. else if ( leqi ( levnam[level-1], "NORMAL" ) == TRUE ) {
  943. count = sscanf ( word, "%f%n", &rval, &width );
  944. if ( count > 0 ) {
  945. if ( num_normal_temp < MAX_ORDER * MAX_FACE ) {
  946. normal_temp[ixyz][num_normal_temp] = rval;
  947. ixyz = ixyz + 1;
  948. if ( ixyz == 3 ) {
  949. ixyz = 0;
  950. num_normal_temp = num_normal_temp + 1;
  951. }
  952. }
  953. }
  954. else {
  955. num_bad = num_bad + 1;
  956. fprintf ( logfile, "NORMAL VECTOR: bad data %s\n", word );
  957. }
  958. }
  959. }
  960. /*
  961. (vertexproperty) VERTEX
  962. */
  963. else if ( leqi ( levnam[level], "VERTEX" ) == TRUE ) {
  964. if ( leqi ( levnam[level-1], "VERTEXPROPERTY" ) == TRUE ) {
  965. if ( strcmp ( word, "[" ) == 0 ) {
  966. ixyz = 0;
  967. nbase = num_cor3;
  968. }
  969. else if ( strcmp ( word, "]" ) == 0 ) {
  970. level = nlbrack - nrbrack;
  971. }
  972. else {
  973. count = sscanf ( word, "%f%n", &rval, &width );
  974. if ( count > 0 ) {
  975. if ( num_cor3 < MAX_COR3 ) {
  976. cor3[ixyz][num_cor3] = rval;
  977. }
  978. ixyz = ixyz + 1;
  979. if ( ixyz == 3 ) {
  980. ixyz = 0;
  981. num_cor3 = num_cor3 + 1;
  982. }
  983. }
  984. else {
  985. num_bad = num_bad + 1;
  986. fprintf ( logfile, "Bad data %s\n", word );
  987. }
  988. }
  989. }
  990. }
  991. /*
  992. (indexedtrianglestripset) VERTEXPROPERTY
  993. */
  994. else if ( leqi ( levnam[level], "VERTEXPROPERTY" ) == TRUE ) {
  995. if ( strcmp ( word, "{" ) == 0 ) {
  996. }
  997. else if ( strcmp ( word, "}" ) == 0 ) {
  998. level = nlbrack - nrbrack;
  999. }
  1000. else if ( leqi ( word, "VERTEX" ) == TRUE ) {
  1001. }
  1002. else if ( leqi ( word, "NORMAL" ) == TRUE ) {
  1003. ixyz = 0;
  1004. }
  1005. else if ( leqi ( word, "MATERIALBINDING" ) == TRUE ) {
  1006. count = sscanf ( next, "%s%n", word, &width );
  1007. next = next + width;
  1008. }
  1009. else if ( leqi ( word, "NORMALBINDING" ) == TRUE ) {
  1010. count = sscanf ( next, "%s%n", word, &width );
  1011. next = next + width;
  1012. }
  1013. else {
  1014. num_bad = num_bad + 1;
  1015. fprintf ( logfile, "Bad data %s\n", word );
  1016. }
  1017. }
  1018. /*
  1019. VKNOTVECTOR
  1020. */
  1021. else if ( leqi ( levnam[level], "VKNOTVECTOR" ) == TRUE ) {
  1022. if ( strcmp ( word, "[" ) == 0 ) {
  1023. continue;
  1024. }
  1025. else if ( strcmp ( word, "]" ) == 0 ) {
  1026. level = nlbrack - nrbrack;
  1027. continue;
  1028. }
  1029. else {
  1030. count = sscanf ( word, "%d%n", &jval, &width );
  1031. }
  1032. }
  1033. /*
  1034. Any other word:
  1035. */
  1036. else {
  1037. }
  1038. }
  1039. /*
  1040. End of information on current line.
  1041. */
  1042. }
  1043. /*
  1044. Check the "materials" defining a line.
  1045. If COORDINDEX is -1, so should be the MATERIALINDEX.
  1046. If COORDINDEX is not -1, then the MATERIALINDEX shouldn"t be either.
  1047. */
  1048. ihi = MIN ( num_line, MAX_LINE );
  1049. for ( i = 0; i < ihi; i++ ) {
  1050. if ( line_dex[i] == -1 ) {
  1051. line_mat[i] = -1;
  1052. }
  1053. else if ( line_mat[i] == -1 ) {
  1054. line_mat[i] = 0;
  1055. }
  1056. }
  1057. return SUCCESS;
  1058. }
  1059. /******************************************************************************/
  1060. int converter::iv_write ( FILE *fileout ) {
  1061. /******************************************************************************/
  1062. /*
  1063. Purpose:
  1064. IV_WRITE writes graphics information to an Inventor file.
  1065. Modified:
  1066. 19 November 1998
  1067. Author:
  1068. John Burkardt
  1069. */
  1070. int icor3;
  1071. int iface;
  1072. int ivert;
  1073. int j;
  1074. int length;
  1075. int num_text;
  1076. num_text = 0;
  1077. fprintf ( fileout, "#Inventor V2.0 ascii\n" );
  1078. fprintf ( fileout, "\n" );
  1079. fprintf ( fileout, "Separator {\n" );
  1080. fprintf ( fileout, " Info {\n" );
  1081. fprintf ( fileout, " string \"%s geconverternewated by IVCON.\"\n", fileout_name );
  1082. fprintf ( fileout, " string \"Original data in file %s.\"\n", filein_name );
  1083. fprintf ( fileout, " }\n" );
  1084. fprintf ( fileout, " Separator {\n" );
  1085. num_text = num_text + 8;
  1086. /*
  1087. LightModel:
  1088. BASE_COLOR ignores light sources, and uses only diffuse color
  1089. and transparency. Even without normal vector information,
  1090. the object will show up. However, you won't get shadow
  1091. and lighting effects.
  1092. PHONG uses the Phong lighting model, accounting for light sources
  1093. and surface orientation. This is the default. I believe
  1094. you need accurate normal vector information in order for this
  1095. option to produce nice pictures.
  1096. DEPTH ignores light sources, and calculates lighting based on
  1097. the location of the object within the near and far planes
  1098. of the current camera's view volume.
  1099. */
  1100. fprintf ( fileout, " LightModel {\n" );
  1101. fprintf ( fileout, " model PHONG\n" );
  1102. fprintf ( fileout, " }\n" );
  1103. num_text = num_text + 3;
  1104. /*
  1105. Material
  1106. */
  1107. fprintf ( fileout, " Material {\n" );
  1108. fprintf ( fileout, " ambientColor 0.2 0.2 0.2\n" );
  1109. fprintf ( fileout, " diffuseColor 0.8 0.8 0.8\n" );
  1110. fprintf ( fileout, " emissiveColor 0.0 0.0 0.0\n" );
  1111. fprintf ( fileout, " specularColor 0.0 0.0 0.0\n" );
  1112. fprintf ( fileout, " shininess 0.2\n" );
  1113. fprintf ( fileout, " transparency 0.0\n" );
  1114. fprintf ( fileout, " }\n" );
  1115. num_text = num_text + 8;
  1116. /*
  1117. MaterialBinding
  1118. */
  1119. fprintf ( fileout, " MaterialBinding {\n" );
  1120. fprintf ( fileout, " value PER_VERTEX_INDEXED\n" );
  1121. fprintf ( fileout, " }\n" );
  1122. num_text = num_text + 3;
  1123. /*
  1124. NormalBinding
  1125. PER_VERTEX promises that we will write a list of normal vectors
  1126. in a particular order, namely, the normal vectors for the vertices
  1127. of the first face, then the second face, and so on.
  1128. PER_VERTEX_INDEXED promises that we will write a list of normal vectors,
  1129. and then, as part of the IndexedFaceSet, we will give a list of
  1130. indices referencing this normal vector list.
  1131. */
  1132. fprintf ( fileout, " NormalBinding {\n" );
  1133. fprintf ( fileout, " value PER_VERTEX_INDEXED\n" );
  1134. fprintf ( fileout, " }\n" );
  1135. num_text = num_text + 3;
  1136. /*
  1137. ShapeHints
  1138. */
  1139. fprintf ( fileout, " ShapeHints {\n" );
  1140. fprintf ( fileout, " vertexOrdering COUNTERCLOCKWISE\n" );
  1141. fprintf ( fileout, " shapeType UNKNOWN_SHAPE_TYPE\n" );
  1142. fprintf ( fileout, " faceType CONVEX\n" );
  1143. fprintf ( fileout, " creaseAngle 6.28319\n" );
  1144. fprintf ( fileout, " }\n" );
  1145. num_text = num_text + 6;
  1146. /*
  1147. Point coordinates.
  1148. */
  1149. fprintf ( fileout, " Coordinate3 {\n" );
  1150. fprintf ( fileout, " point [\n" );
  1151. num_text = num_text + 2;
  1152. for ( j = 0; j < num_cor3; j++ ) {
  1153. fprintf ( fileout, " %f %f %f,\n", cor3[0][j], cor3[1][j], cor3[2][j] );
  1154. num_text = num_text + 1;
  1155. }
  1156. fprintf ( fileout, " ]\n" );
  1157. fprintf ( fileout, " }\n" );
  1158. num_text = num_text + 2;
  1159. /*
  1160. BaseColor.
  1161. */
  1162. if ( num_color > 0 ) {
  1163. fprintf ( fileout, " BaseColor {\n" );
  1164. fprintf ( fileout, " rgb [\n" );
  1165. num_text = num_text + 2;
  1166. for ( j = 0; j < num_color; j++ ) {
  1167. fprintf ( fileout, " %f %f %f,\n", rgbcolor[0][j], rgbcolor[1][j],
  1168. rgbcolor[2][j] );
  1169. num_text = num_text + 1;
  1170. }
  1171. fprintf ( fileout, " ]\n" );
  1172. fprintf ( fileout, " }\n" );
  1173. num_text = num_text + 2;
  1174. }
  1175. /*
  1176. Normal vectors.
  1177. Use the normal vectors associated with nodes.
  1178. */
  1179. if ( num_face > 0 ) {
  1180. fprintf ( fileout, " Normal { \n" );
  1181. fprintf ( fileout, " vector [\n" );
  1182. num_text = num_text + 2;
  1183. for ( icor3 = 0; icor3 < num_cor3; icor3++ ) {
  1184. fprintf ( fileout, " %f %f %f,\n",
  1185. cor3_normal[0][icor3],
  1186. cor3_normal[1][icor3],
  1187. cor3_normal[2][icor3] );
  1188. num_text = num_text + 1;
  1189. }
  1190. fprintf ( fileout, " ]\n" );
  1191. fprintf ( fileout, " }\n" );
  1192. num_text = num_text + 2;
  1193. }
  1194. /*
  1195. IndexedLineSet
  1196. */
  1197. if ( num_line > 0 ) {
  1198. fprintf ( fileout, " IndexedLineSet {\n" );
  1199. /*
  1200. IndexedLineSet coordIndex
  1201. */
  1202. fprintf ( fileout, " coordIndex [\n" );
  1203. num_text = num_text + 2;
  1204. length = 0;
  1205. for ( j = 0; j < num_line; j++ ) {
  1206. if ( length == 0 ) {
  1207. fprintf ( fileout, " " );
  1208. }
  1209. fprintf ( fileout, " %d,", line_dex[j] );
  1210. length = length + 1;
  1211. if ( line_dex[j] == -1 || length >= 10 || j == num_line-1 ) {
  1212. fprintf ( fileout, "\n" );
  1213. num_text = num_text + 1;
  1214. length = 0;
  1215. }
  1216. }
  1217. fprintf ( fileout, " ]\n" );
  1218. num_text = num_text + 1;
  1219. /*
  1220. IndexedLineSet materialIndex.
  1221. */
  1222. fprintf ( fileout, " materialIndex [\n" );
  1223. num_text = num_text + 1;
  1224. length = 0;
  1225. for ( j = 0; j < num_line; j++ ) {
  1226. if ( length == 0 ) {
  1227. fprintf ( fileout, " " );
  1228. }
  1229. fprintf ( fileout, " %d,", line_mat[j] );
  1230. length = length + 1;
  1231. if ( line_mat[j] == -1 || length >= 10 || j == num_line-1 ) {
  1232. fprintf ( fileout, "\n" );
  1233. num_text = num_text + 1;
  1234. length = 0;
  1235. }
  1236. }
  1237. fprintf ( fileout, " ]\n" );
  1238. fprintf ( fileout, " }\n" );
  1239. num_text = num_text + 2;
  1240. }
  1241. /*
  1242. IndexedFaceSet.
  1243. */
  1244. if ( num_face > 0 ) {
  1245. fprintf ( fileout, " IndexedFaceSet {\n" );
  1246. fprintf ( fileout, " coordIndex [\n" );
  1247. num_text = num_text + 2;
  1248. for ( iface = 0; iface < num_face; iface++ ) {
  1249. fprintf ( fileout, " " );
  1250. for ( ivert = 0; ivert < face_order[iface]; ivert++ ) {
  1251. fprintf ( fileout, " %d,", face[ivert][iface] );
  1252. }
  1253. fprintf ( fileout, " -1,\n" );
  1254. num_text = num_text + 1;
  1255. }
  1256. fprintf ( fileout, " ]\n" );
  1257. num_text = num_text + 1;
  1258. /*
  1259. IndexedFaceSet materialIndex
  1260. */
  1261. fprintf ( fileout, " materialIndex [\n" );
  1262. num_text = num_text + 1;
  1263. for ( iface = 0; iface < num_face; iface++ ) {
  1264. fprintf ( fileout, " " );
  1265. for ( ivert = 0; ivert < face_order[iface]; ivert++ ) {
  1266. fprintf ( fileout, " %d,", face_mat[ivert][iface] );
  1267. }
  1268. fprintf ( fileout, " -1,\n" );
  1269. num_text = num_text + 1;
  1270. }
  1271. fprintf ( fileout, " ]\n" );
  1272. num_text = num_text + 1;
  1273. /*
  1274. IndexedFaceSet normalIndex
  1275. */
  1276. fprintf ( fileout, " normalIndex [\n" );
  1277. num_text = num_text + 1;
  1278. for ( iface = 0; iface < num_face; iface++ ) {
  1279. fprintf ( fileout, " " );
  1280. for ( ivert = 0; ivert < face_order[iface]; ivert++ ) {
  1281. fprintf ( fileout, " %d,", face[ivert][iface] );
  1282. }
  1283. fprintf ( fileout, " -1,\n" );
  1284. num_text = num_text + 1;
  1285. }
  1286. fprintf ( fileout, " ]\n" );
  1287. fprintf ( fileout, " }\n" );
  1288. num_text = num_text + 2;
  1289. }
  1290. /*
  1291. Close up the Separator nodes.
  1292. */
  1293. fprintf ( fileout, " }\n" );
  1294. fprintf ( fileout, "}\n" );
  1295. num_text = num_text + 2;
  1296. /*
  1297. Report.
  1298. */
  1299. fprintf ( logfile, "\n" );
  1300. fprintf ( logfile, "IV_WRITE - Wrote %d text lines;\n", num_text );
  1301. return SUCCESS;
  1302. }