GLS.FileGL2.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // The graphics engine GLScene https://github.com/glscene
  3. //
  4. unit GLS.FileGL2;
  5. (* Vector file object loading of Ghoul2 model and animation files *)
  6. interface
  7. uses
  8. System.Classes,
  9. System.SysUtils,
  10. GLS.VectorTypes,
  11. GLS.VectorGeometry,
  12. GLS.PersistentClasses,
  13. GLS.VectorFileObjects,
  14. GLS.ApplicationFileIO,
  15. GLS.Material,
  16. GLS.VectorLists,
  17. Formats.GL2;
  18. type
  19. TGLMVectorFile = class(TGLVectorFile)
  20. public
  21. class function Capabilities: TGLDataFileCapabilities; override;
  22. procedure LoadFromStream(aStream: TStream); override;
  23. end;
  24. TGLAVectorFile = class(TGLVectorFile)
  25. public
  26. class function Capabilities: TGLDataFileCapabilities; override;
  27. procedure LoadFromStream(aStream: TStream); override;
  28. end;
  29. var
  30. vGhoul2LevelOfDetail: Integer = 0; // Highest detail level by default
  31. vGhoul2MaxBonesPerVertex: Integer = 4; // Ghoul2 supports up to 4 bones
  32. // per vertex. Use this global
  33. // variable to set a different limit.
  34. // ------------------------------------------------------------------
  35. implementation
  36. // ------------------------------------------------------------------
  37. // ------------------
  38. // ------------------ TGLMVectorFile ------------------
  39. // ------------------
  40. class function TGLMVectorFile.Capabilities: TGLDataFileCapabilities;
  41. begin
  42. Result := [dfcRead];
  43. end;
  44. procedure TGLMVectorFile.LoadFromStream(aStream: TStream);
  45. var
  46. GLMFile: TFileGLM;
  47. i, j, k, s, c, d: Integer;
  48. mesh: TGLSkeletonMeshObject;
  49. fg: TFGVertexIndexList;
  50. VertOfs: Integer;
  51. shader: string;
  52. vec2: Tvector2f;
  53. numweights, boneref, boneid: Integer;
  54. boneweight, weighttot: Single;
  55. NumSurfVert: Integer;
  56. procedure AllocateMaterial(meshname, shader: string);
  57. var
  58. LibMat: TGLLibMaterial;
  59. begin
  60. if Assigned(Owner.MaterialLibrary) then
  61. with Owner.MaterialLibrary do
  62. begin
  63. if Assigned(Materials.GetLibMaterialByName(meshname)) then
  64. exit;
  65. LibMat := Materials.Add;
  66. LibMat.name := meshname;
  67. LibMat.Material.Texture.Disabled := False;
  68. end;
  69. end;
  70. begin
  71. GLMFile := TFileGLM.Create;
  72. GLMFile.LoadFromStream(aStream);
  73. try
  74. // Need a way to import all levels of detail, but this global
  75. // variable will do for now.
  76. d := vGhoul2LevelOfDetail;
  77. if d >= Length(GLMFile.LODs) then
  78. exit;
  79. for s := 0 to Length(GLMFile.SurfaceHeirachy) - 1 do
  80. begin
  81. mesh := TGLSkeletonMeshObject.CreateOwned(Owner.MeshObjects);
  82. mesh.Mode := momFaceGroups;
  83. mesh.name := trim(GLMFile.SurfaceHeirachy[s].name);
  84. shader := trim(GLMFile.SurfaceHeirachy[s].shader);
  85. AllocateMaterial(mesh.name, shader);
  86. // Set size of VerticesBonesWeights
  87. NumSurfVert := 0;
  88. for c := 0 to GLMFile.SurfaceHeirachy[s].numChildren - 1 do
  89. begin
  90. i := GLMFile.SurfaceHeirachy[s].childIndices[c] - 1;
  91. NumSurfVert := NumSurfVert + GLMFile.LODs[d].Surfaces[i].SurfaceHeader.numVerts;
  92. end;
  93. mesh.BonesPerVertex := vGhoul2MaxBonesPerVertex;
  94. mesh.VerticeBoneWeightCount := NumSurfVert;
  95. for c := 0 to GLMFile.SurfaceHeirachy[s].numChildren - 1 do
  96. begin
  97. i := GLMFile.SurfaceHeirachy[s].childIndices[c] - 1;
  98. with GLMFile.LODs[d].Surfaces[i] do
  99. begin
  100. VertOfs := mesh.Vertices.Count;
  101. for j := 0 to Length(Vertices) - 1 do
  102. begin
  103. // Add vertices and normals
  104. mesh.Vertices.Add(Vertices[j].vertex);
  105. mesh.Normals.Add(Vertices[j].normal);
  106. // Fix and then add the Texture coords
  107. vec2 := TexCoords[j];
  108. vec2.Y := 1 - vec2.Y; // reverse the v coordinate
  109. mesh.TexCoords.Add(vec2);
  110. // Add weighted bones
  111. numweights := G2_GetVertWeights(Vertices[j]);
  112. weighttot := 0;
  113. for k := 0 to mesh.BonesPerVertex - 1 do
  114. if k < numweights then
  115. begin
  116. boneref := G2_GetVertBoneIndex(Vertices[j], k);
  117. boneid := BoneReferences[boneref];
  118. boneweight := G2_GetVertBoneWeight(Vertices[j], k, weighttot, numweights);
  119. mesh.VerticesBonesWeights^[mesh.Vertices.Count - 1]^[k].boneid := boneid;
  120. mesh.VerticesBonesWeights^[mesh.Vertices.Count - 1]^[k].Weight := boneweight;
  121. end
  122. else
  123. begin
  124. mesh.VerticesBonesWeights^[mesh.Vertices.Count - 1]^[k].boneid := 0;
  125. mesh.VerticesBonesWeights^[mesh.Vertices.Count - 1]^[k].Weight := 0;
  126. end;
  127. end;
  128. fg := TFGVertexIndexList.CreateOwned(mesh.FaceGroups);
  129. fg.Mode := fgmmTriangles;
  130. fg.MaterialName := mesh.name;
  131. for j := 0 to Length(Triangles) - 1 do
  132. begin
  133. // The faces need to be wound in the opposite direction so...
  134. fg.VertexIndices.Add(Triangles[j].indices[0] + VertOfs);
  135. fg.VertexIndices.Add(Triangles[j].indices[2] + VertOfs);
  136. fg.VertexIndices.Add(Triangles[j].indices[1] + VertOfs);
  137. end;
  138. end;
  139. end;
  140. end;
  141. finally
  142. GLMFile.Free;
  143. end;
  144. end;
  145. // ------------------
  146. // ------------------ TGLAVectorFile ------------------
  147. // ------------------
  148. class function TGLAVectorFile.Capabilities: TGLDataFileCapabilities;
  149. begin
  150. Result := [dfcRead];
  151. end;
  152. procedure TGLAVectorFile.LoadFromStream(aStream: TStream);
  153. var
  154. GLAFile: TFileGLA;
  155. i, j: Integer;
  156. frame: TGLSkeletonFrame;
  157. CompBone: TGLACompQuatBone;
  158. quat: TQuaternion;
  159. pos: TAffineVector;
  160. basepose: TGLSkeletonFrame;
  161. bonelist: TGLIntegerList;
  162. bone: TGLSkeletonBone;
  163. begin
  164. GLAFile := TFileGLA.Create;
  165. GLAFile.LoadFromStream(aStream);
  166. try
  167. if not(Owner is TGLActor) then
  168. exit;
  169. TGLActor(Owner).Reference := aarSkeleton;
  170. bonelist := TGLIntegerList.Create;
  171. for i := 0 to GLAFile.AnimHeader.numBones - 1 do
  172. bonelist.Add(i);
  173. while bonelist.Count > 0 do
  174. begin
  175. if GLAFile.Skeleton[bonelist[0]].parent = -1 then
  176. bone := TGLSkeletonBone.CreateOwned(Owner.Skeleton.RootBones)
  177. else
  178. begin
  179. bone := Owner.Skeleton.RootBones.BoneByID(GLAFile.Skeleton[bonelist[0]].parent);
  180. if Assigned(bone) then
  181. bone := TGLSkeletonBone.CreateOwned(bone)
  182. end;
  183. if Assigned(bone) then
  184. begin
  185. bone.name := GLAFile.Skeleton[bonelist[0]].name;
  186. bone.boneid := bonelist[0];
  187. end
  188. else
  189. bonelist.Add(bonelist[0]);
  190. bonelist.Delete(0);
  191. end;
  192. bonelist.Free;
  193. // Build the base pose
  194. basepose := TGLSkeletonFrame.CreateOwned(TGLActor(Owner).Skeleton.Frames);
  195. basepose.name := 'basepose';
  196. basepose.TransformMode := sftQuaternion;
  197. basepose.Position.AddNulls(GLAFile.AnimHeader.numBones);
  198. basepose.Quaternion.AddNulls(GLAFile.AnimHeader.numBones);
  199. // Load animation data
  200. for i := 0 to GLAFile.AnimHeader.numFrames - 1 do
  201. begin
  202. // Creates the frame
  203. frame := TGLSkeletonFrame.CreateOwned(TGLActor(Owner).Skeleton.Frames);
  204. frame.name := 'Frame' + IntToStr(i);
  205. frame.TransformMode := sftQuaternion;
  206. for j := 0 to GLAFile.AnimHeader.numBones - 1 do
  207. begin
  208. // Uncompress bone and add to the frame
  209. CompBone := GLAFile.GetCompressedMatrix(i, j);
  210. quat := QuaternionMake([CompBone[1] - 32726, CompBone[2] - 32726, CompBone[3] - 32726],
  211. CompBone[0] - 32726);
  212. pos := AffineVectorMake(CompBone[4] / 64 - 512, CompBone[5] / 64 - 512,
  213. CompBone[6] / 64 - 512);
  214. frame.Quaternion.Add(quat);
  215. frame.Position.Add(pos);
  216. end;
  217. end;
  218. Owner.Skeleton.RootBones.PrepareGlobalMatrices;
  219. for i := 0 to Owner.MeshObjects.Count - 1 do
  220. TGLSkeletonMeshObject(Owner.MeshObjects[i]).PrepareBoneMatrixInvertedMeshes;
  221. finally
  222. GLAFile.Free;
  223. end;
  224. end;
  225. // ------------------------------------------------------------------
  226. // ------------------------------------------------------------------
  227. // ------------------------------------------------------------------
  228. initialization
  229. // ------------------------------------------------------------------
  230. // ------------------------------------------------------------------
  231. // ------------------------------------------------------------------
  232. RegisterVectorFileFormat('glm', 'Ghoul2 (GLM) model files', TGLMVectorFile);
  233. RegisterVectorFileFormat('glx', 'Ghoul2 (GLX) model files', TGLMVectorFile);
  234. RegisterVectorFileFormat('gla', 'Ghoul2 (GLA) animation files', TGLAVectorFile);
  235. end.