Skip to content

Commit 1f0b92f

Browse files
committed
feat(TModel): read notes
TModel is an interace for 3D models. It supports a simple model and material file loader as well as simple TCuboid creation. It's not in use for anything yet but the idea is that you cuold pick in the future to use the default which is the cuboid everyone is used to or actually load the object/npc model.
1 parent 49a7f1c commit 1f0b92f

File tree

6 files changed

+214
-5
lines changed

6 files changed

+214
-5
lines changed

osrs.simba

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Summary: It includes this file until the current file is reached.
117117
{$IFNDEF WL_MM2MS_INCLUDED} {$INCLUDE_ONCE osrs/projection/mm2ms.simba}
118118
{$IFNDEF WL_CAMERA_INCLUDED} {$INCLUDE_ONCE osrs/projection/camera.simba}
119119
{$IFNDEF WL_PROJECTION_INCLUDED} {$INCLUDE_ONCE osrs/projection/projection.simba}
120+
{$IFNDEF WL_MODEL_INCLUDED} {$INCLUDE_ONCE osrs/projection/model.simba}
120121

121122
{$IFNDEF WL_ANTIBAN_INCLUDED} {$INCLUDE_ONCE osrs/antiban/antiban.simba}
122123

@@ -171,7 +172,7 @@ Summary: It includes this file until the current file is reached.
171172
{$IFNDEF WL_SETUP_INCLUDED} {$INCLUDE_ONCE osrs/interfaces/setup.simba}
172173
{$IFNDEF WL_OVERRIDES_INCLUDED} {$INCLUDE_ONCE osrs/overrides.simba}
173174

174-
{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}
175+
{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}
175176
{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}
176177
{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}
177178
{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}{$ENDIF}

osrs/overrides.simba

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ begin
4545
{$IFNDEF WL_TEST}
4646
if Result then
4747
begin
48-
if not MainScreen.HighestPitch then
48+
if not MainScreen.HighestPitch and (RSCamera.Pitch = RSCamera.MAX_PITCH) then
4949
MainScreen.SetHighestPitch();
5050

5151
if not Options.MaxBrightness then

osrs/projection/camera.simba

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ begin
128128
y1 := Sar(y * Self.Cosines[Self.Yaw] - x * Self.Sines[Self.Yaw], 16);
129129
z1 := Sar(y1 * Self.Cosines[Self.Pitch] + z * Self.Sines[Self.Pitch], 16);
130130

131-
WriteLn z1;
132131
if z1 = 0 then z1 := 1;
133132

134133
x1 := Sar(x * Self.Cosines[Self.Yaw] + y * Self.Sines[Self.Yaw], 16);

osrs/projection/model.simba

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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;

osrs/projection/projection.simba

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ begin
101101
case Self.System of
102102
EProjector.CAMERA:
103103
begin
104-
WriteLn 'HERE';
105104
RSCamera.Update(Options.GetZoomLevel(True), roll);
106105
Result := RSCamera.Run(arr);
107106
end;

utils/math/geometry.simba

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
# Geometry
33
Methods and functions related to geometry.
44
*)
5-
65
{$DEFINE WL_GEOMETRY_INCLUDED}
6+
{$INCLUDE_ONCE WaspLib/utils.simba}
7+
78
{$loadlib Plugins/wasp-plugins/libtpaex/libtpaex}
89

910
{$R-}

0 commit comments

Comments
 (0)