GLS.FileLWO.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. //
  2. // The multimedia graphics platform GLScene https://github.com/glscene
  3. //
  4. unit GLS.FileLWO;
  5. (* Support-code to load Lightwave LWO Files (v6.0+, partial support).*)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. System.Classes,
  10. System.SysUtils,
  11. System.Math,
  12. GLS.VectorFileObjects,
  13. GLS.VectorLists,
  14. Formats.LWO;
  15. type
  16. TGLLWOVectorFile = class(TGLVectorFile)
  17. private
  18. FLWO: TLWObjectFile;
  19. FPnts: TLWPnts;
  20. procedure AddLayr(Layr: TLWLayr; LWO: TLWObjectFile);
  21. procedure AddSurf(Surf: TLWSurf; LWO: TLWObjectFile);
  22. procedure AddPnts(Pnts: TLWPnts; Mesh: TGLMeshObject);
  23. procedure AddPols(Pols: TLWPols; Mesh: TGLMeshObject);
  24. procedure AddVMap(VMap: TLWVMap; Mesh: TGLMeshObject);
  25. public
  26. procedure LoadFromStream(aStream: TStream); override;
  27. end;
  28. //============================================
  29. implementation
  30. //============================================
  31. uses
  32. GLS.VectorGeometry,
  33. GLS.Texture,
  34. GLS.Material,
  35. GLS.VectorTypes;
  36. type
  37. PVector3f = ^TVector3f;
  38. function CalcTriNorm(v1, v2, v3: TVec12): TVector3f;
  39. var
  40. e1, e2: TVector3f;
  41. begin
  42. e1 := VectorSubtract(PVector3f(@v2)^, PVector3f(@v1)^);
  43. e2 := VectorSubtract(PVector3f(@v3)^, PVector3f(@v1)^);
  44. VectorCrossProduct(e1, e2, result);
  45. result := VectorNormalize(result);
  46. end;
  47. type
  48. TNormBuffer = record
  49. count, lasttag: TU2;
  50. end;
  51. TNormBufferDynArray = array of TNormBuffer;
  52. //-----------------------------------------
  53. // TGLLWOVectorFile
  54. //-----------------------------------------
  55. procedure TGLLWOVectorFile.AddLayr(Layr: TLWLayr; LWO: TLWObjectFile);
  56. var
  57. Idx: Integer;
  58. Mesh: TGLMeshObject;
  59. Pnts: TLWPnts;
  60. begin
  61. // Add mesh
  62. Mesh := TGLMeshObject.CreateOwned(Owner.MeshObjects);
  63. with Mesh do
  64. begin
  65. Name := Layr.Name;
  66. Mode := momFaceGroups;
  67. // pnts
  68. Idx := Layr.Items.FindChunk(@FindChunkById, @ID_PNTS);
  69. Pnts := TLWPnts(Layr.Items[Idx]);
  70. if Idx <> -1 then
  71. AddPnts(Pnts, Mesh);
  72. // vertex maps
  73. Idx := TLWPnts(Layr.Items[Idx]).Items.FindChunk(@FindChunkById, @ID_VMAP);
  74. while Idx <> -1 do
  75. begin
  76. AddVMap(TLWVMap(Pnts.Items[Idx]), Mesh);
  77. Idx := Pnts.Items.FindChunk(@FindChunkById, @ID_VMAP, Idx + 1);
  78. end;
  79. // Polygons
  80. Idx := Layr.Items.FindChunk(@FindChunkById, @ID_POLS);
  81. while Idx <> -1 do
  82. begin
  83. AddPols(TLWPols(Layr.Items[Idx]), Mesh);
  84. Idx := Layr.Items.FindChunk(@FindChunkById, @ID_POLS, Idx + 1);
  85. end;
  86. // Normals.Normalize;
  87. end;
  88. FPnts := nil;
  89. end;
  90. procedure TGLLWOVectorFile.AddPnts(Pnts: TLWPnts; Mesh: TGLMeshObject);
  91. var
  92. i: Integer;
  93. begin
  94. FPnts := Pnts;
  95. with Mesh do
  96. begin
  97. Vertices.Capacity := Pnts.PntsCount;
  98. TexCoords.Capacity := Pnts.PntsCount;
  99. TexCoords.AddNulls(Pnts.PntsCount);
  100. for i := 0 to Pnts.PntsCount - 1 do
  101. Vertices.Add(PAffineVector(@Pnts.Pnts[i])^);
  102. end;
  103. end;
  104. procedure TGLLWOVectorFile.AddPols(Pols: TLWPols; Mesh: TGLMeshObject);
  105. var
  106. Idx: Integer;
  107. i, j, k, PolyIdx, NormIdx: Integer;
  108. TagPolys: TU2DynArray;
  109. FaceGrp: TFGVertexNormalTexIndexList;
  110. VertPolys: TU2DynArray;
  111. begin
  112. SetLength(VertPolys, 0);
  113. with Pols do
  114. begin
  115. // face type pols chunk
  116. if PolsType = POLS_TYPE_FACE then
  117. begin
  118. Idx := Items.FindChunk(@FindChunkById, @ID_PTAG);
  119. while Idx <> -1 do
  120. begin
  121. with TLWPTag(Items[Idx]) do
  122. begin
  123. if MapType = PTAG_TYPE_SURF then
  124. begin
  125. // for each tag
  126. for i := 0 to TagCount - 1 do
  127. begin
  128. // get polygons using this tag
  129. if GetPolsByTag(Tags[i], TagPolys) > 0 then
  130. begin
  131. // make the facegroup and set the material name
  132. FaceGrp := TFGVertexNormalTexIndexList.CreateOwned(Mesh.FaceGroups);
  133. FaceGrp.MaterialName := FLWO.SurfaceByTag[Tags[i]].Name;
  134. FaceGrp.Mode := fgmmTriangles;
  135. // for each polygon in the current surface Tags[i]
  136. for j := 0 to Length(TagPolys) - 1 do
  137. begin
  138. PolyIdx := PolsByIndex[TagPolys[j]];
  139. // triple 2,3 and 4 point ngons
  140. case Indices[PolyIdx] of
  141. 2:
  142. begin
  143. // triangle line segment
  144. NormIdx := Mesh.Normals.Add(PVector3f(@PolsInfo[TagPolys[j]].vnorms[0])^);
  145. FaceGrp.Add(Indices[PolyIdx + 1], NormIdx, Indices[PolyIdx + 1]);
  146. NormIdx := Mesh.Normals.Add(PVector3f(@PolsInfo[TagPolys[j]].vnorms[1])^);
  147. FaceGrp.Add(Indices[PolyIdx + 2], NormIdx, Indices[PolyIdx + 1]);
  148. NormIdx := Mesh.Normals.Add(PVector3f(@PolsInfo[TagPolys[j]].vnorms[0])^);
  149. FaceGrp.Add(Indices[PolyIdx + 1], NormIdx, Indices[PolyIdx + 1]);
  150. end;
  151. 3: for k := 1 to 3 do
  152. begin
  153. NormIdx := Mesh.Normals.Add(PVector3f(@PolsInfo[TagPolys[j]].vnorms[k - 1])^);
  154. FaceGrp.Add(Indices[PolyIdx + k], NormIdx, Indices[PolyIdx + 1]);
  155. end;
  156. 4:
  157. begin
  158. // triangle A
  159. NormIdx := Mesh.Normals.Add(PVector3f(@PolsInfo[TagPolys[j]].vnorms[0])^);
  160. FaceGrp.Add(Indices[PolyIdx + 1], NormIdx, Indices[PolyIdx + 1]);
  161. NormIdx := Mesh.Normals.Add(PVector3f(@PolsInfo[TagPolys[j]].vnorms[1])^);
  162. FaceGrp.Add(Indices[PolyIdx + 2], NormIdx, Indices[PolyIdx + 1]);
  163. NormIdx := Mesh.Normals.Add(PVector3f(@PolsInfo[TagPolys[j]].vnorms[2])^);
  164. FaceGrp.Add(Indices[PolyIdx + 3], NormIdx, Indices[PolyIdx + 1]);
  165. // triangle B
  166. NormIdx := Mesh.Normals.Add(PVector3f(@PolsInfo[TagPolys[j]].vnorms[0])^);
  167. FaceGrp.Add(Indices[PolyIdx + 1], NormIdx, Indices[PolyIdx + 1]);
  168. NormIdx := Mesh.Normals.Add(PVector3f(@PolsInfo[TagPolys[j]].vnorms[2])^);
  169. FaceGrp.Add(Indices[PolyIdx + 3], NormIdx, Indices[PolyIdx + 1]);
  170. NormIdx := Mesh.Normals.Add(PVector3f(@PolsInfo[TagPolys[j]].vnorms[3])^);
  171. FaceGrp.Add(Indices[PolyIdx + 4], NormIdx, Indices[PolyIdx + 1]);
  172. end;
  173. end;
  174. end;
  175. SetLength(TagPolys, 0);
  176. end;
  177. end;
  178. end
  179. else if MapType = PTAG_TYPE_PART then
  180. begin
  181. // Todo: PTag PART
  182. end
  183. else
  184. if MapType = PTAG_TYPE_SMGP then
  185. begin
  186. // Todo: PTag Smooth Group
  187. end;
  188. Idx := Items.FindChunk(@FindChunkById, @ID_PTAG, Idx + 1);
  189. end;
  190. end;
  191. end
  192. else
  193. // curv type pols chunk (catmull-rom splines)
  194. if PolsType = POLS_TYPE_CURV then
  195. begin
  196. // Todo: CURV Pols import
  197. end
  198. else
  199. // nurbs patch pols type chunk
  200. if PolsType = POLS_TYPE_PTCH then
  201. begin
  202. // Todo: NURBS Patch Pols import
  203. end
  204. else
  205. // // metaball pols type chunk
  206. if PolsType = POLS_TYPE_MBAL then
  207. begin
  208. // Todo: MetaBall type Pols import
  209. end
  210. else
  211. (* // bone pols type chunk *)
  212. if PolsType = POLS_TYPE_BONE then
  213. begin
  214. // Todo: Bone Pols import
  215. end;
  216. SetLength(TagPolys, 0);
  217. end;
  218. end;
  219. procedure TGLLWOVectorFile.AddSurf(Surf: TLWSurf; LWO: TLWObjectFile);
  220. var
  221. matLib: TGLMaterialLibrary;
  222. libMat: TGLLibMaterial;
  223. tex2Mat: TGLLibMaterial;
  224. colr: TColr12;
  225. FloatParm, tran, refl: TF4;
  226. WordParm: TU2;
  227. StrParm: string;
  228. Idx: integer;
  229. begin
  230. {DONE: implement surface inheritance}
  231. if GetOwner is TGLBaseMesh then
  232. begin
  233. matLib := TGLBaseMesh(GetOwner).MaterialLibrary;
  234. if Assigned(matLib) then
  235. begin
  236. libMat := matLib.Materials.GetLibMaterialByName(Surf.Name);
  237. if not Assigned(libMat) then
  238. begin
  239. libMat := matLib.Materials.Add;
  240. libMat.Name := Surf.Name;
  241. with libMat.Material.FrontProperties do
  242. begin
  243. tran := Surf.FloatParam[ID_TRAN];
  244. if tran <> 0 then
  245. libMat.Material.BlendingMode := bmTransparency;
  246. colr := Surf.Vec3Param[ID_COLR];
  247. // Ambient.Color := VectorMake(colr[0],colr[1],colr[2],1);
  248. Ambient.Color := VectorMake(0, 0, 0, 1);
  249. (* Diffuse *)
  250. FloatParm := Surf.FloatParam[ID_DIFF];
  251. Diffuse.Color := VectorMake(colr[0] * FloatParm, colr[1] * FloatParm, colr[2] * FloatParm, tran);
  252. (* Luminosity -> Emission *)
  253. FloatParm := Surf.FloatParam[ID_LUMI];
  254. Emission.Color := VectorMake(colr[0] * FloatParm, colr[1] * FloatParm, colr[2] * FloatParm, 1);
  255. (* Specularity *)
  256. FloatParm := Surf.FloatParam[ID_SPEC];
  257. Specular.Color := VectorMake(colr[0] * FloatParm, colr[1] * FloatParm, colr[2] * FloatParm, 1);
  258. (* Glossiness -> Shininess *)
  259. FloatParm := Surf.FloatParam[ID_GLOS];
  260. Shininess := Round(Power(2, 7 * FloatParm));
  261. (* Polygon sidedness *)
  262. WordParm := Surf.WordParam[ID_SIDE];
  263. if (WordParm and SIDE_BACK) = SIDE_BACK then
  264. AssignTo(libMat.Material.BackProperties);
  265. (* Reflection settings *)
  266. refl := Surf.FloatParam[ID_REFL];
  267. if refl > 0 then
  268. begin
  269. // Check the reflection options
  270. WordParm := Surf.WordParam[ID_RFOP];
  271. if WordParm > RFOP_RAYTRACEANDBACKDROP then
  272. begin
  273. WordParm := Surf.VXParam[ID_RIMG];
  274. Idx := Surf.RootChunks.FindChunk(@FindClipByClipIndex, @WordParm);
  275. if Idx <> -1 then
  276. begin
  277. StrParm := string(PAnsiChar(TLWClip(Surf.RootChunks[Idx]).ParamAddr[ID_STIL]));
  278. StrParm := GetContentDir.FindContent(ToDosPath(StrParm));
  279. if FileExists(StrParm) then
  280. try
  281. if (not libMat.Material.Texture.Enabled) then
  282. tex2Mat := libMat
  283. else
  284. tex2Mat := matLib.Materials.Add;
  285. with tex2Mat do
  286. begin
  287. Material.Texture.Image.LoadFromFile(StrParm);
  288. Material.Texture.Disabled := False;
  289. with Material.Texture do
  290. begin
  291. MappingMode := tmmCubeMapReflection;
  292. if refl < 100 then
  293. TextureMode := tmBlend
  294. else
  295. TextureMode := tmDecal;
  296. end;
  297. end;
  298. libMat.Texture2Name := 'REFL_' + ExtractFileName(StrParm);
  299. except
  300. on E: ETexture do
  301. begin
  302. if not Self.Owner.IgnoreMissingTextures then
  303. raise;
  304. end;
  305. end;
  306. end;
  307. end;
  308. end;
  309. end;
  310. end;
  311. end;
  312. end;
  313. end;
  314. procedure TGLLWOVectorFile.AddVMap(VMap: TLWVMap; Mesh: TGLMeshObject);
  315. var
  316. i: integer;
  317. begin
  318. with VMap, Mesh do
  319. begin
  320. // texture coords
  321. if VMapType = VMAP_TYPE_TXUV then
  322. begin
  323. for i := 0 to ValueCount - 1 do
  324. TexCoords.Items[Value[i].vert] := AffineVectorMake(Value[i].values[0], Value[i].values[1], 0);
  325. end
  326. else
  327. {// vertex weight map} if VMapType = VMAP_TYPE_WGHT then
  328. begin
  329. {Todo: WeightMap import}
  330. end
  331. else
  332. {// vertex morph (relative)} if VMapType = VMAP_TYPE_MORF then
  333. begin
  334. {Todo: Morph target (relative) import}
  335. end
  336. else
  337. {// vertex morph (absolute)} if VMapType = VMAP_TYPE_SPOT then
  338. begin
  339. {Todo: Morph target (absolute) import}
  340. end;
  341. end;
  342. end;
  343. procedure TGLLWOVectorFile.LoadFromStream(aStream: TStream);
  344. var
  345. Ind: Integer;
  346. begin
  347. FLWO := TLWObjectFile.Create;
  348. with FLWO do
  349. try
  350. LoadFromStream(aStream);
  351. // Add Surfaces to material list
  352. Ind := Chunks.FindChunk(@FindChunkById, @ID_SURF, 0);
  353. while Ind <> -1 do
  354. begin
  355. AddSurf(TLWSurf(Chunks[Ind]), FLWO);
  356. Ind := Chunks.FindChunk(@FindChunkById, @ID_SURF, Ind + 1);
  357. end;
  358. // Lw layer
  359. Ind := Chunks.FindChunk(@FindChunkById, @ID_LAYR, 0);
  360. while Ind <> -1 do
  361. begin
  362. AddLayr(TLWLayr(Chunks[Ind]), FLWO);
  363. Ind := Chunks.FindChunk(@FindChunkById, @ID_LAYR, Ind + 1);
  364. end;
  365. finally
  366. FreeAndNil(FLWO);
  367. end;
  368. end;
  369. //------------------------------------------------------------
  370. initialization
  371. //------------------------------------------------------------
  372. RegisterVectorFileFormat('lwo', 'Lightwave3D object file (6.0 or above)', TGLLWOVectorFile);
  373. finalization
  374. end.