GLFileLWO.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. unit GLFileLWO;
  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. GLVectorFileObjects,
  13. GLVectorLists,
  14. FileLWObjects;
  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: TMeshObject);
  23. procedure AddPols(Pols: TLWPols; Mesh: TMeshObject);
  24. procedure AddVMap(VMap: TLWVMap; Mesh: TMeshObject);
  25. public
  26. procedure LoadFromStream(aStream: TStream); override;
  27. end;
  28. //============================================
  29. implementation
  30. //============================================
  31. uses
  32. GLVectorGeometry,
  33. GLTexture,
  34. GLMaterial,
  35. GLVectorTypes;
  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: TMeshObject;
  59. Pnts: TLWPnts;
  60. begin
  61. // Add mesh
  62. Mesh := TMeshObject.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: TMeshObject);
  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: TMeshObject);
  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)} if PolsType = POLS_TYPE_CURV then
  194. begin
  195. {Todo: CURV Pols import}
  196. end
  197. else
  198. {// nurbs patch pols type chunk} if PolsType = POLS_TYPE_PTCH then
  199. begin
  200. {Todo: NURBS Patch Pols import}
  201. end
  202. else
  203. {// metaball pols type chunk} if PolsType = POLS_TYPE_MBAL then
  204. begin
  205. {Todo: MetaBall type Pols import}
  206. end
  207. else
  208. {// bone pols type chunk} if PolsType = POLS_TYPE_BONE then
  209. begin
  210. {Todo: Bone Pols import}
  211. end;
  212. SetLength(TagPolys, 0);
  213. end;
  214. end;
  215. procedure TGLLWOVectorFile.AddSurf(Surf: TLWSurf; LWO: TLWObjectFile);
  216. var
  217. matLib: TGLMaterialLibrary;
  218. libMat: TGLLibMaterial;
  219. tex2Mat: TGLLibMaterial;
  220. colr: TColr12;
  221. FloatParm, tran, refl: TF4;
  222. WordParm: TU2;
  223. StrParm: string;
  224. Idx: integer;
  225. begin
  226. {DONE: implement surface inheritance}
  227. if GetOwner is TGLBaseMesh then
  228. begin
  229. matLib := TGLBaseMesh(GetOwner).MaterialLibrary;
  230. if Assigned(matLib) then
  231. begin
  232. libMat := matLib.Materials.GetLibMaterialByName(Surf.Name);
  233. if not Assigned(libMat) then
  234. begin
  235. libMat := matLib.Materials.Add;
  236. libMat.Name := Surf.Name;
  237. with libMat.Material.FrontProperties do
  238. begin
  239. tran := Surf.FloatParam[ID_TRAN];
  240. if tran <> 0 then
  241. libMat.Material.BlendingMode := bmTransparency;
  242. colr := Surf.Vec3Param[ID_COLR];
  243. // Ambient.Color := VectorMake(colr[0],colr[1],colr[2],1);
  244. Ambient.Color := VectorMake(0, 0, 0, 1);
  245. (* Diffuse *)
  246. FloatParm := Surf.FloatParam[ID_DIFF];
  247. Diffuse.Color := VectorMake(colr[0] * FloatParm, colr[1] * FloatParm, colr[2] * FloatParm, tran);
  248. (* Luminosity -> Emission *)
  249. FloatParm := Surf.FloatParam[ID_LUMI];
  250. Emission.Color := VectorMake(colr[0] * FloatParm, colr[1] * FloatParm, colr[2] * FloatParm, 1);
  251. (* Specularity *)
  252. FloatParm := Surf.FloatParam[ID_SPEC];
  253. Specular.Color := VectorMake(colr[0] * FloatParm, colr[1] * FloatParm, colr[2] * FloatParm, 1);
  254. (* Glossiness -> Shininess *)
  255. FloatParm := Surf.FloatParam[ID_GLOS];
  256. Shininess := Round(Power(2, 7 * FloatParm));
  257. (* Polygon sidedness *)
  258. WordParm := Surf.WordParam[ID_SIDE];
  259. if (WordParm and SIDE_BACK) = SIDE_BACK then
  260. AssignTo(libMat.Material.BackProperties);
  261. (* Reflection settings *)
  262. refl := Surf.FloatParam[ID_REFL];
  263. if refl > 0 then
  264. begin
  265. // Check the reflection options
  266. WordParm := Surf.WordParam[ID_RFOP];
  267. if WordParm > RFOP_RAYTRACEANDBACKDROP then
  268. begin
  269. WordParm := Surf.VXParam[ID_RIMG];
  270. Idx := Surf.RootChunks.FindChunk(@FindClipByClipIndex, @WordParm);
  271. if Idx <> -1 then
  272. begin
  273. StrParm := string(PAnsiChar(TLWClip(Surf.RootChunks[Idx]).ParamAddr[ID_STIL]));
  274. StrParm := GetContentDir.FindContent(ToDosPath(StrParm));
  275. if FileExists(StrParm) then
  276. try
  277. if (not libMat.Material.Texture.Enabled) then
  278. tex2Mat := libMat
  279. else
  280. tex2Mat := matLib.Materials.Add;
  281. with tex2Mat do
  282. begin
  283. Material.Texture.Image.LoadFromFile(StrParm);
  284. Material.Texture.Disabled := False;
  285. with Material.Texture do
  286. begin
  287. MappingMode := tmmCubeMapReflection;
  288. if refl < 100 then
  289. TextureMode := tmBlend
  290. else
  291. TextureMode := tmDecal;
  292. end;
  293. end;
  294. libMat.Texture2Name := 'REFL_' + ExtractFileName(StrParm);
  295. except
  296. on E: ETexture do
  297. begin
  298. if not Self.Owner.IgnoreMissingTextures then
  299. raise;
  300. end;
  301. end;
  302. end;
  303. end;
  304. end;
  305. end;
  306. end;
  307. end;
  308. end;
  309. end;
  310. procedure TGLLWOVectorFile.AddVMap(VMap: TLWVMap; Mesh: TMeshObject);
  311. var
  312. i: integer;
  313. begin
  314. with VMap, Mesh do
  315. begin
  316. // texture coords
  317. if VMapType = VMAP_TYPE_TXUV then
  318. begin
  319. for i := 0 to ValueCount - 1 do
  320. TexCoords.Items[Value[i].vert] := AffineVectorMake(Value[i].values[0], Value[i].values[1], 0);
  321. end
  322. else
  323. {// vertex weight map} if VMapType = VMAP_TYPE_WGHT then
  324. begin
  325. {Todo: WeightMap import}
  326. end
  327. else
  328. {// vertex morph (relative)} if VMapType = VMAP_TYPE_MORF then
  329. begin
  330. {Todo: Morph target (relative) import}
  331. end
  332. else
  333. {// vertex morph (absolute)} if VMapType = VMAP_TYPE_SPOT then
  334. begin
  335. {Todo: Morph target (absolute) import}
  336. end;
  337. end;
  338. end;
  339. procedure TGLLWOVectorFile.LoadFromStream(aStream: TStream);
  340. var
  341. Ind: Integer;
  342. begin
  343. FLWO := TLWObjectFile.Create;
  344. with FLWO do
  345. try
  346. LoadFromStream(aStream);
  347. // Add Surfaces to material list
  348. Ind := Chunks.FindChunk(@FindChunkById, @ID_SURF, 0);
  349. while Ind <> -1 do
  350. begin
  351. AddSurf(TLWSurf(Chunks[Ind]), FLWO);
  352. Ind := Chunks.FindChunk(@FindChunkById, @ID_SURF, Ind + 1);
  353. end;
  354. // Lw layer
  355. Ind := Chunks.FindChunk(@FindChunkById, @ID_LAYR, 0);
  356. while Ind <> -1 do
  357. begin
  358. AddLayr(TLWLayr(Chunks[Ind]), FLWO);
  359. Ind := Chunks.FindChunk(@FindChunkById, @ID_LAYR, Ind + 1);
  360. end;
  361. finally
  362. FreeAndNil(FLWO);
  363. end;
  364. end;
  365. //------------------------------------------------------------
  366. initialization
  367. //------------------------------------------------------------
  368. RegisterVectorFileFormat('lwo', 'Lightwave3D object file (6.0 or above)', TGLLWOVectorFile);
  369. finalization
  370. end.