| 1 |
{%MainUnit x3dloadinternalmd3.pas} |
| 2 |
{ |
| 3 |
Copyright 2007-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ MD3 surface class. } |
| 18 |
|
| 19 |
type |
| 20 |
TMd3Surface = class |
| 21 |
private |
| 22 |
Shaders: TMd3ShaderList; |
| 23 |
|
| 24 |
{ Read surface from file, and advance position to next surface. } |
| 25 |
procedure Read(Stream: TStream); |
| 26 |
public |
| 27 |
constructor Create; |
| 28 |
destructor Destroy; override; |
| 29 |
|
| 30 |
public |
| 31 |
Name: string; |
| 32 |
|
| 33 |
Vertexes: TMd3VertexList; |
| 34 |
TextureCoords: TMd3TexCoordList; |
| 35 |
Triangles: TMd3TriangleList; |
| 36 |
|
| 37 |
{ Shader name, or '' if not found in file. |
| 38 |
Each surface may use a different "shader" which is really just a way to specify a texture |
| 39 |
(possibly indirectly, if using Skin dictionary). |
| 40 |
See sandyhead in example MD3 in https://forum.castle-engine.io/t/weirdness-with-zeronextsecondspassed-when-used-with-tupdate/775/21 } |
| 41 |
ShaderName: String; |
| 42 |
|
| 43 |
{ Frames within this surface. |
| 44 |
This is always the same as the TObject3DMD3.NumFrames of enclosing |
| 45 |
TObject3DMD3 instance (yes, this assumption is checked when loading |
| 46 |
MD3, as it must be @true for any valid MD3 file). } |
| 47 |
NumFrames: Cardinal; |
| 48 |
|
| 49 |
{ Vertexes array has VertexesInFrameCount * NumFrames items, |
| 50 |
each set of VertexesInFrameCount is for a different animation frame. |
| 51 |
TextureCoords always has VertexesInFrameCount (texture is not |
| 52 |
animated). } |
| 53 |
VertexesInFrameCount: Cardinal; |
| 54 |
end; |
| 55 |
|
| 56 |
TMd3SurfaceList = {$ifdef FPC}specialize{$endif} TObjectList<TMd3Surface>; |
| 57 |
|
| 58 |
constructor TMd3Surface.Create; |
| 59 |
begin |
| 60 |
inherited; |
| 61 |
Vertexes := TMd3VertexList.Create; |
| 62 |
Triangles := TMd3TriangleList.Create; |
| 63 |
TextureCoords := TMd3TexCoordList.Create; |
| 64 |
Shaders := TMd3ShaderList.Create; |
| 65 |
end; |
| 66 |
|
| 67 |
destructor TMd3Surface.Destroy; |
| 68 |
begin |
| 69 |
FreeAndNil(Vertexes); |
| 70 |
FreeAndNil(Triangles); |
| 71 |
FreeAndNil(TextureCoords); |
| 72 |
FreeAndNil(Shaders); |
| 73 |
inherited; |
| 74 |
end; |
| 75 |
|
| 76 |
procedure TMd3Surface.Read(Stream: TStream); |
| 77 |
type |
| 78 |
TMd3FileSurface = record |
| 79 |
Ident: array [0..3] of AnsiChar; |
| 80 |
Name: array [0..Md3MaxQPath - 1] of AnsiChar; |
| 81 |
Flags: Int32; |
| 82 |
NumFrames: Int32; |
| 83 |
NumShaders: Int32; |
| 84 |
NumVerts: Int32; |
| 85 |
NumTriangles: Int32; |
| 86 |
OffsetTriangles: Int32; |
| 87 |
OffsetShaders: Int32; |
| 88 |
OffsetST: Int32; |
| 89 |
OffsetXYZNormal: Int32; |
| 90 |
OffsetEnd: Int32; |
| 91 |
end; |
| 92 |
var |
| 93 |
SurfaceStart: Int64; |
| 94 |
Surface: TMd3FileSurface; |
| 95 |
begin |
| 96 |
SurfaceStart := Stream.Position; |
| 97 |
Stream.ReadBuffer(Surface, SizeOf(Surface)); |
| 98 |
|
| 99 |
if not CompareMem(@Surface.Ident[0], @GoodIdent[1], Length(GoodIdent)) then |
| 100 |
raise EInvalidMD3.CreateFmt('Identifier of MD3 Surface must be "%s"', |
| 101 |
[GoodIdent]); |
| 102 |
|
| 103 |
Name := Surface.Name; |
| 104 |
NumFrames := Surface.NumFrames; |
| 105 |
VertexesInFrameCount := Surface.NumVerts; |
| 106 |
|
| 107 |
(* Tests: |
| 108 |
Writeln('Surface'); |
| 109 |
Writeln(' Name "', Surface.Name, '"'); |
| 110 |
Writeln(' NumFrames "', Surface.NumFrames, '"'); |
| 111 |
Writeln(' NumShaders "', Surface.NumShaders, '"'); |
| 112 |
Writeln(' NumVerts "', Surface.NumVerts, '"'); |
| 113 |
Writeln(' NumTriangles "', Surface.NumTriangles, '"'); |
| 114 |
Writeln(' OffsetTriangles "', Surface.OffsetTriangles, '"'); |
| 115 |
Writeln(' OffsetShaders "', Surface.OffsetShaders, '"'); |
| 116 |
Writeln(' OffsetST "', Surface.OffsetST, '"'); |
| 117 |
Writeln(' OffsetXYZNormal "', Surface.OffsetXYZNormal, '"'); |
| 118 |
Writeln(' OffsetEnd "', Surface.OffsetEnd, '"'); |
| 119 |
*) |
| 120 |
|
| 121 |
Triangles.Count := Surface.NumTriangles; |
| 122 |
if Triangles.Count <> 0 then |
| 123 |
begin |
| 124 |
Stream.Position := SurfaceStart + Surface.OffsetTriangles; |
| 125 |
Stream.ReadBuffer(Triangles.L^, SizeOf(TMd3Triangle) * Triangles.Count); |
| 126 |
end; |
| 127 |
|
| 128 |
{ For animations: we have Surface.NumFrames times the vertexes array. |
| 129 |
For each frame, separate collection of vertices is added. } |
| 130 |
Vertexes.Count := Surface.NumVerts * Surface.NumFrames; |
| 131 |
if Vertexes.Count <> 0 then |
| 132 |
begin |
| 133 |
Stream.Position := SurfaceStart + Surface.OffsetXYZNormal; |
| 134 |
Stream.ReadBuffer(Vertexes.L^, SizeOf(TMd3Vertex) * Vertexes.Count); |
| 135 |
end; |
| 136 |
|
| 137 |
Shaders.Count := Surface.NumShaders; |
| 138 |
if Shaders.Count <> 0 then |
| 139 |
begin |
| 140 |
Stream.Position := SurfaceStart + Surface.OffsetShaders; |
| 141 |
Stream.ReadBuffer(Shaders.L^, SizeOf(TMd3Shader) * Shaders.Count); |
| 142 |
|
| 143 |
{ We only use 1st shader name. |
| 144 |
Interpretation of other shader names is unknown. } |
| 145 |
Assert(Shaders.Count <> 0); |
| 146 |
ShaderName := Shaders[0].Name; |
| 147 |
end; |
| 148 |
|
| 149 |
TextureCoords.Count := VertexesInFrameCount; |
| 150 |
if TextureCoords.Count <> 0 then |
| 151 |
begin |
| 152 |
Stream.Position := SurfaceStart + Surface.OffsetST; |
| 153 |
Stream.ReadBuffer(TextureCoords.L^, SizeOf(TMd3TexCoord) * TextureCoords.Count); |
| 154 |
end; |
| 155 |
|
| 156 |
Stream.Position := SurfaceStart + Surface.OffsetEnd; |
| 157 |
end; |