GLFile3DPDF.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. {
  5. 3D PDF converter of GLScene's models
  6. }
  7. unit GLFile3DPDF;
  8. interface
  9. uses
  10. WinApi.Windows,
  11. WinApi.ShellAPI,
  12. System.Classes,
  13. System.SysUtils,
  14. System.StrUtils,
  15. GLVectorTypes,
  16. GLPersistentClasses,
  17. GLVectorGeometry,
  18. GLVectorLists,
  19. GLVectorFileObjects,
  20. GLApplicationFileIO,
  21. GLUtils;
  22. type
  23. { The IDTF vector file (Intermediate Data Text File).
  24. Used for converting to IDTF -> U3D -> 3D PDF}
  25. TGLIDTFVectorFile = class(TGLVectorFile)
  26. private
  27. procedure BuildNormals(m: TMeshObject);
  28. public
  29. class function Capabilities: TGLDataFileCapabilities; override;
  30. procedure SaveToStream(aStream: TStream); override;
  31. end;
  32. { The U3D vector file (using IDTF and U3DConverter).}
  33. TGLU3DVectorFile = class(TGLIDTFVectorFile)
  34. public
  35. class function Capabilities: TGLDataFileCapabilities; override;
  36. procedure SaveToStream(aStream: TStream); override;
  37. end;
  38. var
  39. // global variable for accing the IDTF->U3D converter
  40. IDTFConverterFileName: string;
  41. //=========================================================
  42. implementation
  43. //=========================================================
  44. const
  45. ConstIDTFTemplate =
  46. ' FILE_FORMAT "IDTF" ' + #13#10 +
  47. ' FORMAT_VERSION 100 ' + #13#10 +
  48. ' ' + #13#10 +
  49. ' NODE "MODEL" { ' + #13#10 +
  50. ' NODE_NAME "VcgMesh01" ' + #13#10 +
  51. ' PARENT_LIST { ' + #13#10 +
  52. ' PARENT_COUNT 1 ' + #13#10 +
  53. ' PARENT 0 { ' + #13#10 +
  54. ' PARENT_NAME "<NULL>" ' + #13#10 +
  55. ' PARENT_TM { ' + #13#10 +
  56. ' 1.000000 0.000000 0.000000 0.000000 ' + #13#10 +
  57. ' 0.000000 1.000000 0.000000 0.000000 ' + #13#10 +
  58. ' 0.000000 0.000000 1.000000 0.000000 ' + #13#10 +
  59. ' 0.000000 0.000000 0.000000 1.000000 ' + #13#10 +
  60. ' } ' + #13#10 +
  61. ' } ' + #13#10 +
  62. ' } ' + #13#10 +
  63. ' RESOURCE_NAME "MyVcgMesh01" ' + #13#10 +
  64. ' } ' + #13#10 +
  65. ' ' + #13#10 +
  66. ' RESOURCE_LIST "MODEL" { ' + #13#10 +
  67. ' RESOURCE_COUNT 1 ' + #13#10 +
  68. ' RESOURCE 0 { ' + #13#10 +
  69. ' RESOURCE_NAME "MyVcgMesh01" ' + #13#10 +
  70. ' MODEL_TYPE "MESH" ' + #13#10 +
  71. ' MESH { ' + #13#10 +
  72. ' FACE_COUNT %d ' + #13#10 +
  73. ' MODEL_POSITION_COUNT %d ' + #13#10 +
  74. ' MODEL_NORMAL_COUNT %d ' + #13#10 +
  75. ' MODEL_DIFFUSE_COLOR_COUNT 0 ' + #13#10 +
  76. ' MODEL_SPECULAR_COLOR_COUNT 0 ' + #13#10 +
  77. ' MODEL_TEXTURE_COORD_COUNT 0 ' + #13#10 +
  78. ' MODEL_BONE_COUNT 0 ' + #13#10 +
  79. ' MODEL_SHADING_COUNT 1 ' + #13#10 +
  80. ' MODEL_SHADING_DESCRIPTION_LIST { ' + #13#10 +
  81. ' SHADING_DESCRIPTION 0 { ' + #13#10 +
  82. ' TEXTURE_LAYER_COUNT 0 ' + #13#10 +
  83. ' SHADER_ID 0 ' + #13#10 +
  84. ' } ' + #13#10 +
  85. ' } ' + #13#10 +
  86. ' MESH_FACE_POSITION_LIST { ' + #13#10 +
  87. ' %s ' + #13#10 +
  88. ' } ' + #13#10 +
  89. ' MESH_FACE_NORMAL_LIST { ' + #13#10 +
  90. ' %s ' + #13#10 +
  91. ' } ' + #13#10 +
  92. ' MESH_FACE_SHADING_LIST { ' + #13#10 +
  93. ' %s ' + #13#10 +
  94. ' } ' + #13#10 +
  95. ' MODEL_POSITION_LIST { ' + #13#10 +
  96. ' %s ' + #13#10 +
  97. ' } ' + #13#10 +
  98. ' MODEL_NORMAL_LIST { ' + #13#10 +
  99. ' %s ' + #13#10 +
  100. ' } ' + #13#10 +
  101. ' } ' + #13#10 +
  102. ' } ' + #13#10 +
  103. ' } ';
  104. var
  105. USFormat: TFormatSettings;
  106. // helper functions
  107. function SingleToStr(const AValue: Single): string; inline;
  108. begin
  109. if AValue = 0.0 then
  110. begin
  111. Result:= '0';
  112. Exit;
  113. end
  114. else
  115. // Limit to maxint
  116. if AValue > (MaxInt - 1) then
  117. Result:= '2147483647'
  118. else
  119. if AValue < -(MaxInt - 1) then
  120. Result:= '2147483647';
  121. Result:= FloatToStrF(AValue, ffFixed, 8, 6, USFormat);
  122. end;
  123. function GetTempPath: string;
  124. var
  125. Len: Integer;
  126. begin
  127. SetLastError(ERROR_SUCCESS);
  128. // get memory for the buffer retaining the temp path (plus null-termination)
  129. SetLength(Result, MAX_PATH);
  130. Len := Winapi.Windows.GetTempPath(MAX_PATH, PChar(Result));
  131. if Len <> 0 then
  132. begin
  133. Len := GetLongPathName(PChar(Result), nil, 0);
  134. GetLongPathName(PChar(Result), PChar(Result), Len);
  135. SetLength(Result, Len - 1);
  136. end
  137. else
  138. Result := '';
  139. end;
  140. procedure ExecProgramAndWait(const AProcessName, AParams: string);
  141. var
  142. startUpInfo : TStartupInfo;
  143. ProcessInfo : TProcessInformation;
  144. exeCmd : string;
  145. ExitCode: cardinal;
  146. begin
  147. // Concat in the parameters
  148. exeCmd := AProcessName + ' ' + AParams;
  149. // Initialise the StartUpInfo record, which handles the creation of
  150. // a new main window for a process
  151. FillChar(startUpInfo, SizeOf(startUpInfo), 0);
  152. StartUpInfo.cb := SizeOf( StartUpInfo );
  153. StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
  154. StartUpInfo.wShowWindow := SW_HIDE;
  155. // Spawn the process out.
  156. if CreateProcess(nil, PChar(exeCmd), nil, nil, false,
  157. CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil,
  158. PChar(ExtractFilePath(AProcessName)), startUpInfo, ProcessInfo) then
  159. begin
  160. // close the thread handle as soon as it is no longer needed
  161. CloseHandle(ProcessInfo.hThread);
  162. // Wait for the process to finish.
  163. if not WaitForSingleObject(ProcessInfo.hProcess, INFINITE) = WAIT_OBJECT_0 then
  164. raise Exception.CreateFmt('U3DConverter: %s', [SysErrorMessage(GetLastError)]);
  165. // finish process
  166. if GetExitCodeProcess(ProcessInfo.hProcess, ExitCode) then
  167. raise Exception.CreateFmt('U3DConverter failed with exitcode $%x', [ExitCode]);
  168. CloseHandle(ProcessInfo.hProcess);
  169. end
  170. else
  171. begin
  172. // create process failure
  173. raise Exception.CreateFmt('U3DConverter: %s', [SysErrorMessage(GetLastError)]);
  174. end;
  175. end;
  176. { TGLIDTFVectorFile }
  177. class function TGLIDTFVectorFile.Capabilities: TGLDataFileCapabilities;
  178. begin
  179. Result := [dfcWrite];
  180. end;
  181. // build normals
  182. procedure TGLIDTFVectorFile.BuildNormals(m: TMeshObject);
  183. var
  184. i, j: Integer;
  185. v1, v2, v3, v4, n: TAffineVector;
  186. begin
  187. for i := 0 to m.vertices.count - 1 do
  188. m.Normals.Add(0, 0, 0);
  189. for i := 0 to m.FaceGroups.count - 1 do
  190. if m.FaceGroups[i] is TFGVertexIndexList then
  191. with m.FaceGroups[i] as TFGVertexIndexList do
  192. case mode of
  193. fgmmTriangles:
  194. begin
  195. for j := 0 to (VertexIndices.count div 3) - 1 do
  196. begin
  197. v1 := m.vertices[VertexIndices[j * 3]];
  198. v2 := m.vertices[VertexIndices[j * 3 + 1]];
  199. v3 := m.vertices[VertexIndices[j * 3 + 2]];
  200. n := CalcPlaneNormal(v1, v2, v3);
  201. m.Normals.items[VertexIndices[j * 3]] :=
  202. VectorAdd(m.Normals.items[VertexIndices[j * 3]], n);
  203. m.Normals.items[VertexIndices[j * 3 + 1]] :=
  204. VectorAdd(m.Normals.items[VertexIndices[j * 3 + 1]], n);
  205. m.Normals.items[VertexIndices[j * 3 + 2]] :=
  206. VectorAdd(m.Normals.items[VertexIndices[j * 3 + 2]], n);
  207. end;
  208. end;
  209. fgmmQuads:
  210. begin
  211. for j := 0 to (VertexIndices.count div 4) - 1 do
  212. begin
  213. v1 := m.vertices[VertexIndices[j * 4]];
  214. v2 := m.vertices[VertexIndices[j * 4 + 1]];
  215. v3 := m.vertices[VertexIndices[j * 4 + 2]];
  216. v4 := m.vertices[VertexIndices[j * 4 + 3]];
  217. n := CalcPlaneNormal(v1, v2, v3);
  218. m.Normals.items[VertexIndices[j * 4]] :=
  219. VectorAdd(m.Normals.items[VertexIndices[j * 4]], n);
  220. m.Normals.items[VertexIndices[j * 4 + 1]] :=
  221. VectorAdd(m.Normals.items[VertexIndices[j * 4 + 1]], n);
  222. m.Normals.items[VertexIndices[j * 4 + 2]] :=
  223. VectorAdd(m.Normals.items[VertexIndices[j * 4 + 2]], n);
  224. m.Normals.items[VertexIndices[j * 4 + 3]] :=
  225. VectorAdd(m.Normals.items[VertexIndices[j * 4 + 3]], n);
  226. end;
  227. end;
  228. end;
  229. for i := 0 to m.Normals.count - 1 do
  230. m.Normals.items[i] := VectorNormalize(m.Normals.items[i]);
  231. end;
  232. procedure TGLIDTFVectorFile.SaveToStream(aStream: TStream);
  233. var
  234. S: String;
  235. Mesh: TMeshObject;
  236. FaceCount, ModelPositionCount, NormalCount, I, J: Integer;
  237. FacePositionList, NormalList, ModelPositionList, ModelNormalList, FaceShadingList: String;
  238. Lines: TStringList;
  239. Indicies: TIntegerList;
  240. function FormatVector(const AVector: TAffineVector): string;
  241. begin
  242. Result:= SingleToStr(AVector.X) + ' ' + SingleToStr(AVector.Y) + ' ' + SingleToStr(AVector.Z);
  243. end;
  244. begin
  245. // ++ todo: save more than one mesh
  246. Mesh:= Owner.MeshObjects[0];
  247. // BuildNormals(Mesh);
  248. // count
  249. NormalList:= '';
  250. ModelNormalList:= '';
  251. // faces
  252. // MESH_FACE_POSITION_LIST
  253. FaceCount:= 0;
  254. FacePositionList:= '';
  255. for i := 0 to Mesh.FaceGroups.count - 1 do
  256. if Mesh.FaceGroups[i] is TFGVertexIndexList then
  257. with Mesh.FaceGroups[i] as TFGVertexIndexList do
  258. begin
  259. // face indicies
  260. Indicies:= TFGVertexIndexList(Mesh.FaceGroups[i]).VertexIndices;
  261. Inc(FaceCount, Indicies.Count div 3);
  262. J:= 0;
  263. while J < Indicies.Count do
  264. begin
  265. FacePositionList:= FacePositionList + ' ' + Format('%d %d %d', [Indicies[J +2], Indicies[J +1], Indicies[J]]) + #13#10;
  266. inc(J, 3);
  267. end;
  268. end;
  269. FacePositionList:= Trim(FacePositionList);
  270. FaceShadingList:= '';
  271. for i := 1 to FaceCount do
  272. FaceShadingList:= FaceShadingList + ' 0' + #13#10;
  273. FaceShadingList:= Trim(FaceShadingList);
  274. // verticies
  275. ModelPositionList:= '';
  276. ModelPositionCount:= Mesh.Vertices.Count;
  277. for I:= 0 to Pred(Mesh.Vertices.Count) do
  278. ModelPositionList:= ModelPositionList + ' ' + FormatVector(Mesh.Vertices[I]) + #13#10;
  279. ModelPositionList:= Trim(ModelPositionList);
  280. // points
  281. // normals
  282. // FMeshObject.BuildNormals(FMeshObject.Vertices., momFaceGroups); ++
  283. NormalCount:= Mesh.Normals.Count;
  284. NormalList:= '';
  285. for I:= 0 to Pred(Mesh.Normals.Count) do
  286. NormalList:= NormalList + ' ' + FormatVector(Mesh.Normals[I]) + #13#10;
  287. NormalList:= Trim(NormalList);
  288. // build the IDTF file
  289. S:= Format(ConstIDTFTemplate, [FaceCount, ModelPositionCount, NormalCount,
  290. FacePositionList, NormalList, FaceShadingList, ModelPositionList, ModelNormalList]);
  291. ;
  292. Lines:= TStringList.Create;
  293. try
  294. Lines.Text:= S;
  295. Lines.SaveToStream(aStream, TEncoding.ASCII);
  296. finally
  297. Lines.Free;
  298. end;
  299. end;
  300. { TGLU3DVectorFile }
  301. class function TGLU3DVectorFile.Capabilities: TGLDataFileCapabilities;
  302. begin
  303. Result := [dfcWrite];
  304. end;
  305. procedure TGLU3DVectorFile.SaveToStream(aStream: TStream);
  306. var
  307. TempStream: TStream;
  308. TempInFile, TempOutFile, Params: String;
  309. begin
  310. TempInFile:= IncludeTrailingPathDelimiter(GetTempPath) + 'GLObject.idtf';
  311. // save as temp .idtf file
  312. TempStream:= TFileStream.Create(TempInFile, fmCreate);
  313. try
  314. inherited SaveToStream(TempStream);
  315. finally
  316. TempStream.Free;
  317. end;
  318. // convert IDTF to U3D using U3DConverter
  319. TempOutFile:= ChangeFileExt(TempInFile, '.u3d');
  320. Params:= Format('-i "%s" -o "%s"', [TempInFile, TempOutFile]);
  321. ExecProgramAndWait(IDTFConverterFileName, Params);
  322. // copy U3D file to stream
  323. TempStream:= TFileStream.Create(TempOutFile, fmOpenRead or fmShareDenyWrite);
  324. try
  325. AStream.CopyFrom(TempStream, 0);
  326. finally
  327. TempStream.Free;
  328. end;
  329. DeleteFile(TempInFile);
  330. DeleteFile(TempOutFile);
  331. end;
  332. // ------------------------------------------------------------------
  333. initialization
  334. // ------------------------------------------------------------------
  335. USFormat:= TFormatSettings.Create('en_us');
  336. IDTFConverterFileName:= IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + 'IDTFConverter.exe';
  337. // register formats
  338. RegisterVectorFileFormat('idtf', 'Intermediate Data Text File', TGLIDTFVectorFile);
  339. RegisterVectorFileFormat('u3d', 'Universal 3D', TGLU3DVectorFile);
  340. end.