Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
6.8 KiB

  1. /*******************************************************************************
  2. This program generates a gridded quadrilateral of given dimensions, and writes
  3. the result as a VRML 1.0 or X-file formatted output stream. The resultant
  4. quadrilateral is in the XY plane, going from [-1,-1] to [+1,+1].
  5. *******************************************************************************/
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. void WriteVRML1 (int nrows, int ncols);
  9. void WriteXFILE (int nrows, int ncols);
  10. inline void print (char *string) { fputs (string, stdout); }
  11. /*****************************************************************************
  12. *****************************************************************************/
  13. int main (int argc, char *argv[])
  14. {
  15. enum { VRML, XFILE } filetype = VRML;
  16. int nrows = 0,
  17. ncols = 0;
  18. int argi;
  19. for (argi=1; argi < argc; ++argi)
  20. {
  21. if ((argv[argi][0] == '-') && (tolower(argv[argi][1]) == 'x'))
  22. { filetype = XFILE;
  23. continue;
  24. }
  25. if (nrows == 0)
  26. nrows = atoi (argv[argi]);
  27. else
  28. ncols = atoi (argv[argi]);
  29. }
  30. if (nrows <= 0)
  31. { fputs
  32. ( "quadgrid: Generates VRML1 or X-file gridded quadrilateral\n"
  33. "Usage: quadgrid [-x] <rows> [columns]\n"
  34. "\n"
  35. "If [columns] is omitted, quadgrid uses the number of rows.\n"
  36. "Use the -x option to generate X files.\n\n",
  37. stderr
  38. );
  39. exit (-1);
  40. }
  41. if (ncols == 0)
  42. ncols = nrows;
  43. if (filetype == VRML)
  44. WriteVRML1 (nrows, ncols);
  45. else
  46. WriteXFILE (nrows, ncols);
  47. return 0;
  48. }
  49. /*****************************************************************************
  50. This procedure writes out a grid in VRML 1.0 format.
  51. *****************************************************************************/
  52. void WriteVRML1 (int nrows, int ncols)
  53. {
  54. // VRML 1.0 Header
  55. printf (
  56. "#VRML V1.0 ascii\n\n"
  57. "Separator {\n"
  58. "Info { string "
  59. "\"%d x %d gridded quadrilateral generated by quadgrid.\""
  60. " }\n",
  61. nrows, ncols
  62. );
  63. // Write out the vertex coordinates.
  64. print ("\nCoordinate3 { point [\n");
  65. // N 2N 3N ... (N+1)(M+1)-1 This is the vertex indexing
  66. // : : : : for the generated grid layout.
  67. // 3 (N+1)+3 2(N+1)+3 ... M(N+1)+3 This contains (N+1)(M+1)
  68. // 2 (N+1)+2 2(N+1)+2 ... M(N+1)+2 vertices, NM quadrilaterals,
  69. // 1 (N+1)+1 2(N+1)+1 M(N+1)+1 and 2NM triangles.
  70. // 0 (N+1) 2(N+1) ... M(N+1)
  71. int row, col;
  72. for (col=0; col <= ncols; ++col)
  73. { for (row=0; row <= nrows; ++row)
  74. { printf ("\t% g\t% g\t0,\n",
  75. (((col / double(ncols)) * 2) - 1),
  76. (((row / double(nrows)) * 2) - 1));
  77. }
  78. }
  79. print ("] } # Coordinate3\n");
  80. // Write out the texture coordinates.
  81. print ("\nTextureCoordinate2 { point [\n");
  82. for (col=0; col <= ncols; ++col)
  83. { for (row=0; row <= nrows; ++row)
  84. { printf ("\t%g\t%g,\n",
  85. (col / double (ncols)),
  86. (row / double (nrows)));
  87. }
  88. }
  89. print ("] } # TextureCoordinate2\n");
  90. // Write out the normal vectors.
  91. print
  92. ( "\nNormal { vector [0 0 1] }\n"
  93. "NormalBinding { value OVERALL }\n"
  94. );
  95. // Lay out triangles column by column.
  96. print ("\nIndexedFaceSet { coordIndex [\n");
  97. int left = 0; // Lower Left Vertex Index
  98. int right = 1+nrows; // Lower Right Vertex Index
  99. for (col=0; col < ncols; ++col, ++left, ++right)
  100. { for (row=0; row < nrows; ++row, ++left, ++right)
  101. { printf ("\t%d,\t%d,\t%d,\t-1,\n", left, right, right+1);
  102. printf ("\t%d,\t%d,\t%d,\t-1,\n", left, right+1, left+1);
  103. }
  104. }
  105. // Epilogue
  106. print
  107. ( "] } # IndexedFaceSet\n"
  108. "\n} # Separator\n"
  109. );
  110. }
  111. /*****************************************************************************
  112. This procedure writes out a grid in X-file format.
  113. *****************************************************************************/
  114. void WriteXFILE (int nrows, int ncols)
  115. {
  116. int nverts = (nrows+1) * (ncols+1);
  117. // Header
  118. printf
  119. ( "xof 0302txt 0032\n\n"
  120. "# %d x %d gridded quadrilateral generated by 'quadgrid'\n\n"
  121. "Header { 1;0;1; }\n\n"
  122. "Mesh {\n\n",
  123. nrows, ncols
  124. );
  125. // Write out the vertex coordinates.
  126. print ("# Vertex Coordinates\n\n");
  127. // N 2N 3N ... (N+1)(M+1)-1 This is the vertex indexing
  128. // : : : : for the generated grid layout.
  129. // 3 (N+1)+3 2(N+1)+3 ... M(N+1)+3 This contains (N+1)(M+1)
  130. // 2 (N+1)+2 2(N+1)+2 ... M(N+1)+2 vertices, NM quadrilaterals,
  131. // 1 (N+1)+1 2(N+1)+1 M(N+1)+1 and 2NM triangles.
  132. // 0 (N+1) 2(N+1) ... M(N+1)
  133. printf ("%d;\n", nverts);
  134. int row, col;
  135. for (col=0; col <= ncols; ++col)
  136. {
  137. for (row=0; row <= nrows; ++row)
  138. {
  139. if (col || row) print (",\n");
  140. printf ("% f; % f; 0.0;",
  141. (((col / double(ncols)) * 2) - 1),
  142. (((row / double(nrows)) * 2) - 1));
  143. }
  144. }
  145. print (";\n\n");
  146. // Face Coordinate Indices
  147. printf ("# Faces\n\n%d;\n", 2 * nrows * ncols);
  148. int left = 0; // Lower Left Vertex Index
  149. int right = 1+nrows; // Lower Right Vertex Index
  150. for (col=0; col < ncols; ++col, ++left, ++right)
  151. { for (row=0; row < nrows; ++row, ++left, ++right)
  152. { if (row || col) print (",\n");
  153. printf ("3;%4d,%4d,%4d;,\n", left, right+1, right);
  154. printf ("3;%4d,%4d,%4d;", left, left+1, right+1);
  155. }
  156. }
  157. print (";\n\n");
  158. // Write out the texture coordinates.
  159. printf ("MeshTextureCoords { \n\t%d;\n", nverts);
  160. for (col=0; col <= ncols; ++col)
  161. { for (row=0; row <= nrows; ++row)
  162. { printf ("%s\t%.4f; %.4f;",
  163. ((row || col) ? ",\n" : ""),
  164. (col / double (ncols)),
  165. (row / double (nrows)));
  166. }
  167. }
  168. print (";\n}\n\n");
  169. // Write out default material
  170. print
  171. ( "MeshMaterialList {\n"
  172. " # Diffuse White\n"
  173. " 1;1;0;;\n"
  174. " Material {\n"
  175. " 1.0; 1.0; 1.0; 1.0;;\n"
  176. " 1.0;\n"
  177. " 0.0; 0.0; 0.0;\n"
  178. " 0.0; 0.0; 0.0;\n"
  179. " }\n"
  180. "}\n\n"
  181. );
  182. // Write out the normal vectors.
  183. print ("MeshNormals {\n\t1;\n\t0.0; 0.0; -1.0;;\n\n");
  184. // Face Normal Indices
  185. printf ("\t%d;\n", 2 * nrows * ncols);
  186. int i;
  187. for (i=0; i < (2*nrows*ncols); ++i)
  188. printf ("%s\t3;0,0,0;", (i ? ",\n" : ""));
  189. print (";\n}\n");
  190. // Epilogue
  191. print ("\n}\n");
  192. }