2
0

GLS.FileGL2.pas 8.0 KB

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