|
| 1 | +{$DEFINE WL_MODEL_INCLUDED} |
| 2 | +{$INCLUDE_ONCE WaspLib/osrs.simba} |
| 3 | + |
| 4 | +type |
| 5 | + TModelFace = object |
| 6 | + Vertices: TIntegerArray; |
| 7 | + Material: Int32; |
| 8 | + end; |
| 9 | + |
| 10 | + TModel = object |
| 11 | + Vertices: TVector3Array; |
| 12 | + Faces: array of TModelFace; |
| 13 | + Materials: TColorArray; |
| 14 | + Alphas: TByteArray; |
| 15 | + end; |
| 16 | + |
| 17 | +procedure TModel.LoadMTL(fileName: string); |
| 18 | +var |
| 19 | + strings: TLazStringList; |
| 20 | + line: string; |
| 21 | + parts: TStringArray; |
| 22 | + color: TColorRGB; |
| 23 | + i: Integer; |
| 24 | +begin |
| 25 | + strings := TLazStringList.Create(); |
| 26 | + |
| 27 | + try |
| 28 | + strings.LoadFromFile(fileName); |
| 29 | + for i := 0 to strings.Count - 1 do |
| 30 | + begin |
| 31 | + line := strings.Strings[i].Trim(); |
| 32 | + if (line = '') or (line[1] = '#') then Continue; |
| 33 | + |
| 34 | + if line.StartsWith('Kd ', False) then |
| 35 | + begin |
| 36 | + parts := line.Split(' ', True); |
| 37 | + if Length(parts) >= 4 then |
| 38 | + begin |
| 39 | + color.R := Round(StrToFloat(parts[1]) * 255); |
| 40 | + color.G := Round(StrToFloat(parts[2]) * 255); |
| 41 | + color.B := Round(StrToFloat(parts[3]) * 255); |
| 42 | + |
| 43 | + Self.Materials += color.ToColor(); |
| 44 | + Self.Alphas += $FF; |
| 45 | + end; |
| 46 | + end; |
| 47 | + |
| 48 | + if line.StartsWith('d ', False) then |
| 49 | + begin |
| 50 | + parts := line.Split(' ', True); |
| 51 | + if Length(parts) = 2 then |
| 52 | + Self.Alphas[High(Self.Alphas)] := Round(StrToFloat(parts[1], 1.0) * 255); |
| 53 | + end; |
| 54 | + end; |
| 55 | + finally |
| 56 | + strings.Free(); |
| 57 | + end; |
| 58 | +end; |
| 59 | + |
| 60 | +procedure TModel.LoadOBJ(fileName: String); |
| 61 | +var |
| 62 | + strings: TLazStringList; |
| 63 | + parts: TStringArray; |
| 64 | + line: String; |
| 65 | + vertex: TVector3; |
| 66 | + face: TModelFace; |
| 67 | + i, j: Integer; |
| 68 | + idx: Integer = -1; |
| 69 | +begin |
| 70 | + strings := TLazStringList.Create(); |
| 71 | + try |
| 72 | + strings.LoadFromFile(fileName); |
| 73 | + for i := 0 to strings.Count-1 do |
| 74 | + begin |
| 75 | + line := strings.Strings[i].Trim(); |
| 76 | + if (line = '') or (line[1] = '#') then Continue; |
| 77 | + |
| 78 | + if line.StartsWith('usemtl ', False) then //which material to use line |
| 79 | + begin |
| 80 | + idx := line.ExtractInteger(-1); |
| 81 | + Continue; |
| 82 | + end; |
| 83 | + |
| 84 | + if line.StartsWith('v ', False) then //vertices line |
| 85 | + begin |
| 86 | + parts := line.Split(' ', False); |
| 87 | + if Length(parts) >= 4 then |
| 88 | + begin |
| 89 | + vertex.X := StrToFloat(parts[1])*4/128; |
| 90 | + vertex.Y := StrToFloat(parts[3])*4/128; |
| 91 | + vertex.Z := StrToFloat(parts[2])*4/128; |
| 92 | + Self.Vertices += vertex; |
| 93 | + end; |
| 94 | + Continue; |
| 95 | + end; |
| 96 | + |
| 97 | + if line.StartsWith('f ', False) then //face line |
| 98 | + begin |
| 99 | + parts := line.Split(' ', True); |
| 100 | + if Length(parts) >= 4 then |
| 101 | + begin |
| 102 | + face := new TModelFace(); |
| 103 | + for j := 0 to 2 do |
| 104 | + face.Vertices += StrToInt64(parts[j+1].Split('/')[0], 0) - 1; |
| 105 | + |
| 106 | + if InRange(idx, 0, High(Self.Materials)) then |
| 107 | + face.Material := idx; |
| 108 | + Self.Faces += face; |
| 109 | + end; |
| 110 | + end; |
| 111 | + end; |
| 112 | + finally |
| 113 | + strings.Free(); |
| 114 | + end; |
| 115 | +end; |
| 116 | + |
| 117 | + |
| 118 | +function TModel.Contruct(objFile: String; mtlFile: String = ''): TModel; static; |
| 119 | +begin |
| 120 | + if mtlFile <> '' then |
| 121 | + Result.LoadMTL(mtlFile); |
| 122 | + Result.LoadOBJ(objFile); |
| 123 | +end; |
| 124 | + |
| 125 | +function TModel.Contruct(size: TVector3): TModel; static; overload; |
| 126 | +var |
| 127 | + face: TModelFace; |
| 128 | +begin |
| 129 | + size.X := 2 * size.X; |
| 130 | + size.Y := 2 * size.Y; |
| 131 | + |
| 132 | + Result.Vertices := [ |
| 133 | + [-size.X,-size.Y,size.Z], [size.X,-size.Y,size.Z], [size.X,size.Y,size.Z], [-size.X,size.Y,size.Z], //top quad |
| 134 | + [-size.X,-size.Y,0], [size.X,-size.Y,0], [size.X,size.Y,0], [-size.X,size.Y,0] //btm quad |
| 135 | + ]; |
| 136 | + |
| 137 | + //top face |
| 138 | + face := new TModelFace(); |
| 139 | + face.Vertices := [0,1,2,3]; |
| 140 | + face.Material := 0; |
| 141 | + Result.Faces += face; |
| 142 | + |
| 143 | + //bottom face |
| 144 | + face := new TModelFace(); |
| 145 | + face.Vertices := [4,5,6,7]; |
| 146 | + face.Material := 0; |
| 147 | + Result.Faces += face; |
| 148 | + |
| 149 | + //front face |
| 150 | + face := new TModelFace(); |
| 151 | + face.Vertices := [0,1,5,4]; |
| 152 | + face.Material := 0; |
| 153 | + Result.Faces += face; |
| 154 | + |
| 155 | + //right face |
| 156 | + face := new TModelFace(); |
| 157 | + face.Vertices := [1,2,6,5]; |
| 158 | + face.Material := 0; |
| 159 | + Result.Faces += face; |
| 160 | + |
| 161 | + //back face |
| 162 | + face := new TModelFace(); |
| 163 | + face.Vertices := [2,3,7,6]; |
| 164 | + face.Material := 0; |
| 165 | + Result.Faces += face; |
| 166 | + |
| 167 | + //left face |
| 168 | + face := new TModelFace(); |
| 169 | + face.Vertices := [3,0,4,7]; |
| 170 | + face.Material := 0; |
| 171 | + Result.Faces += face; |
| 172 | +end; |
| 173 | + |
| 174 | + |
| 175 | +function TModel.Project(minimapCoord: TVector3; rotation, radians: Single): TPointArray; |
| 176 | +begin |
| 177 | + with minimapCoord do |
| 178 | + Result := Projection.Run(Self.Vertices.Offset([X,Y,Z]).Rotate(rotation,X,Y), radians); |
| 179 | +end; |
| 180 | + |
| 181 | + |
| 182 | +procedure TModel.DrawModel(img: TImage; projection: TPointArray); |
| 183 | +var |
| 184 | + i, j: Integer; |
| 185 | + polygon: TPolygon; |
| 186 | +begin |
| 187 | + if Self.Materials <> [] then |
| 188 | + begin |
| 189 | + for i := High(Self.Faces) downto 0 do |
| 190 | + begin |
| 191 | + for j := 0 to High(Self.Faces[i].Vertices) do |
| 192 | + polygon += projection[Self.Faces[i].Vertices[j]]; |
| 193 | + |
| 194 | + img.DrawAlpha := Self.Alphas[Self.Faces[i].Material]; |
| 195 | + img.DrawColor := Self.Materials[Self.Faces[i].Material]; |
| 196 | + img.DrawPolygonFilled(polygon); |
| 197 | + polygon := []; |
| 198 | + end; |
| 199 | + Exit; |
| 200 | + end; |
| 201 | + |
| 202 | + for i := High(Self.Faces) downto 0 do |
| 203 | + begin |
| 204 | + for j := 0 to High(Self.Faces[i].Vertices) do |
| 205 | + polygon += projection[Self.Faces[i].Vertices[j]]; |
| 206 | + img.DrawPolygon(polygon); |
| 207 | + polygon := []; |
| 208 | + end; |
| 209 | +end; |
0 commit comments