GLFileOCT.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. unit GLFileOCT;
  5. (*
  6. Support-code to load OCT Files into TGLFreeForm-Components in GLScene.
  7. (OCT being the format output from FSRad, http://www.fluidstudios.com/fsrad.html).
  8. *)
  9. interface
  10. {$I GLScene.inc}
  11. uses
  12. System.Classes,
  13. System.SysUtils,
  14. Vcl.Graphics,
  15. GLTexture,
  16. GLMaterial,
  17. GLGraphics,
  18. GLCrossPlatform,
  19. GLState,
  20. GLS.Utils,
  21. GLTextureFormat,
  22. GLVectorFileObjects,
  23. GLVectorGeometry,
  24. GLApplicationFileIO,
  25. FileOCT;
  26. type
  27. // The OCT vector file (FSRad output).
  28. TGLOCTGLVectorFile = class(TGLVectorFile)
  29. public
  30. class function Capabilities: TGLDataFileCapabilities; override;
  31. procedure LoadFromStream(aStream: TStream); override;
  32. end;
  33. var
  34. vGLFileOCTLightmapBrightness: single = 1; // Scaling factor, 1.0 = unchanged
  35. vGLFileOCTLightmapGammaCorrection: single = 1; // Scaling factor, 1.0 = unchanged
  36. vGLFileOCTAllocateMaterials: boolean = True; // Flag to avoid loading materials (useful for IDE Ext or scene editors)
  37. // ------------------------------------------------------------------
  38. implementation
  39. // ------------------------------------------------------------------
  40. // ------------------
  41. // ------------------ TGLOCTGLVectorFile ------------------
  42. // ------------------
  43. class function TGLOCTGLVectorFile.Capabilities: TGLDataFileCapabilities;
  44. begin
  45. Result := [dfcRead];
  46. end;
  47. procedure TGLOCTGLVectorFile.LoadFromStream(aStream: TStream);
  48. var
  49. i, y, n: integer;
  50. oct: TOCTFile;
  51. octFace: POCTFace;
  52. octLightmap: POCTLightmap;
  53. mo: TMeshObject;
  54. fg: TFGVertexIndexList;
  55. lightmapLib: TGLMaterialLibrary;
  56. lightmapBmp: TBitmap;
  57. libMat: TGLLibMaterial;
  58. begin
  59. oct := TOCTFile.Create(aStream);
  60. try
  61. mo := TMeshObject.CreateOwned(Owner.MeshObjects);
  62. mo.Mode := momFaceGroups;
  63. lightmapLib := Owner.LightmapLibrary;
  64. if (Assigned(lightmapLib)) and (vGLFileOCTAllocateMaterials) then
  65. begin
  66. // import lightmaps
  67. n := oct.Header.numLightmaps;
  68. lightmapBmp := TBitmap.Create;
  69. try
  70. lightmapBmp.PixelFormat := pf24bit;
  71. lightmapBmp.Width := 128;
  72. lightmapBmp.Height := 128;
  73. for i := 0 to n - 1 do
  74. begin
  75. octLightmap := @oct.Lightmaps[i];
  76. // Brightness correction
  77. if vGLFileOCTLightmapBrightness <> 1.0 then
  78. BrightenRGBArray(@octLightmap.map,
  79. lightmapBmp.Width * lightmapBmp.Height,
  80. vGLFileOCTLightmapBrightness);
  81. // Gamma correction
  82. if vGLFileOCTLightmapGammaCorrection <> 1.0 then
  83. GammaCorrectRGBArray(@octLightmap.map,
  84. lightmapBmp.Width * lightmapBmp.Height,
  85. vGLFileOCTLightmapGammaCorrection);
  86. // convert RAW RGB to BMP
  87. for y := 0 to 127 do
  88. Move(octLightmap.map[y * 128 * 3], lightmapBmp.ScanLine[127 - y]^, 128 * 3);
  89. // spawn lightmap
  90. libMat := lightmapLib.AddTextureMaterial(IntToStr(i), lightmapBmp);
  91. with libMat.Material.Texture do
  92. begin
  93. MinFilter := miLinear;
  94. TextureWrap := twNone;
  95. TextureFormat := tfRGB;
  96. end;
  97. end;
  98. finally
  99. lightmapBmp.Free;
  100. end;
  101. end;
  102. // import geometry
  103. n := oct.Header.numVerts;
  104. mo.Vertices.AdjustCapacityToAtLeast(n);
  105. mo.TexCoords.AdjustCapacityToAtLeast(n);
  106. mo.LightMapTexCoords.AdjustCapacityToAtLeast(n);
  107. for i := 0 to n - 1 do
  108. with oct.Vertices[i] do
  109. begin
  110. mo.Vertices.Add(pos.X, pos.Y, pos.Z);
  111. mo.TexCoords.Add(tv.s, tv.t);
  112. mo.LightMapTexCoords.Add(lv.s, lv.t);
  113. end;
  114. // import faces
  115. n := oct.Header.numFaces;
  116. for i := 0 to n - 1 do
  117. begin
  118. octFace := @oct.Faces[i];
  119. fg := TFGVertexIndexList.CreateOwned(mo.FaceGroups);
  120. fg.Mode := fgmmTriangleFan;
  121. fg.VertexIndices.AddSerie(octFace.start, 1, octFace.num);
  122. if (Assigned(lightmapLib)) and (vGLFileOCTAllocateMaterials) then
  123. fg.LightMapIndex := octFace.lid;
  124. end;
  125. finally
  126. oct.Free;
  127. end;
  128. end;
  129. // ------------------------------------------------------------------
  130. initialization
  131. // ------------------------------------------------------------------
  132. RegisterVectorFileFormat('oct', 'FSRad OCT files', TGLOCTGLVectorFile);
  133. end.