runelite-api: expose higher level getTriangles instead in Model

This commit is contained in:
Adam
2017-08-11 21:08:33 -04:00
parent b5603f11b8
commit 793d8f9b5e
4 changed files with 217 additions and 48 deletions

View File

@@ -24,6 +24,11 @@
*/
package net.runelite.api;
import java.util.ArrayList;
import java.util.List;
import net.runelite.api.model.Triangle;
import net.runelite.api.model.Vertex;
public class Model
{
private final net.runelite.rs.api.Model model;
@@ -33,33 +38,49 @@ public class Model
this.model = model;
}
public int[] getVerticesX()
public List<Triangle> getTriangles()
{
return model.getVerticesX();
}
int[] verticiesX = model.getVerticesX();
int[] verticiesY = model.getVerticesY();
int[] verticiesZ = model.getVerticesZ();
public int[] getVerticesY()
{
return model.getVerticesY();
}
int[] trianglesX = model.getTrianglesX();
int[] trianglesY = model.getTrianglesY();
int[] trianglesZ = model.getTrianglesZ();
public int[] getVerticesZ()
{
return model.getVerticesZ();
}
assert trianglesX.length == trianglesY.length;
assert trianglesY.length == trianglesZ.length;
public int[] getTrianglesX()
{
return model.getTrianglesX();
}
List<Triangle> triangles = new ArrayList<>(trianglesX.length);
public int[] getTrianglesY()
{
return model.getTrianglesY();
}
for (int i = 0; i < trianglesX.length; ++i)
{
int triangleX = trianglesX[i];
int triangleY = trianglesY[i];
int triangleZ = trianglesZ[i];
public int[] getTrianglesZ()
{
return model.getTrianglesZ();
Vertex x = new Vertex(
verticiesX[triangleX],
verticiesY[triangleX],
verticiesZ[triangleX]
);
Vertex y = new Vertex(
verticiesX[triangleY],
verticiesY[triangleY],
verticiesZ[triangleY]
);
Vertex z = new Vertex(
verticiesX[triangleZ],
verticiesY[triangleZ],
verticiesZ[triangleZ]
);
Triangle triangle = new Triangle(x, y, z);
triangles.add(triangle);
}
return triangles;
}
}

View File

@@ -27,6 +27,8 @@ package net.runelite.api;
import java.awt.Polygon;
import java.util.ArrayList;
import java.util.List;
import net.runelite.api.model.Triangle;
import net.runelite.api.model.Vertex;
public class Player extends Actor
{
@@ -60,37 +62,47 @@ public class Player extends Actor
public Polygon[] getPolygons()
{
List<Polygon> polys = new ArrayList<>();
Model model = getModel();
int localX = player.getX();
int localY = player.getY();
// models are orientated north (1024) and there are 2048 angles total
int orientation = (player.getOrientation() + 1024) % 2048;
if (model == null)
{
return null;
}
int[] verticesX = model.getVerticesX().clone();
int[] verticesY = model.getVerticesY();
int[] verticesZ = model.getVerticesZ().clone();
int localX = player.getX();
int localY = player.getY();
int[] trianglesX = model.getTrianglesX();
int[] trianglesY = model.getTrianglesY();
int[] trianglesZ = model.getTrianglesZ();
// models are orientated north (1024) and there are 2048 angles total
int orientation = (player.getOrientation() + 1024) % 2048;
List<Triangle> triangles = model.getTriangles();
if (orientation != 0)
{
setOrientation(model, orientation, verticesX, verticesZ);
triangles = rotate(triangles, orientation);
}
for (int i = 0; i < trianglesX.length; i++)
List<Polygon> polys = new ArrayList<>();
for (Triangle triangle : triangles)
{
Point x = Perspective.worldToCanvas(client, localX - verticesX[trianglesX[i]], localY - verticesZ[trianglesX[i]], -verticesY[trianglesX[i]]);
Point y = Perspective.worldToCanvas(client, localX - verticesX[trianglesY[i]], localY - verticesZ[trianglesY[i]], -verticesY[trianglesY[i]]);
Point z = Perspective.worldToCanvas(client, localX - verticesX[trianglesZ[i]], localY - verticesZ[trianglesZ[i]], -verticesY[trianglesZ[i]]);
Vertex vx = triangle.getA();
Vertex vy = triangle.getB();
Vertex vz = triangle.getC();
Point x = Perspective.worldToCanvas(client,
localX - vx.getX(),
localY - vx.getZ(),
-vx.getY());
Point y = Perspective.worldToCanvas(client,
localX - vy.getX(),
localY - vy.getZ(),
-vy.getY());
Point z = Perspective.worldToCanvas(client,
localX - vz.getX(),
localY - vz.getZ(),
-vz.getY());
int xx[] =
{
@@ -106,18 +118,34 @@ public class Player extends Actor
return polys.toArray(new Polygon[polys.size()]);
}
private void setOrientation(Model model, int orientation, int[] verticesX, int[] verticesZ)
private List<Triangle> rotate(List<Triangle> triangles, int orientation)
{
List<Triangle> rotatedTriangles = new ArrayList<>();
for (Triangle triangle : triangles)
{
Vertex a = triangle.getA();
Vertex b = triangle.getB();
Vertex c = triangle.getC();
Triangle rotatedTriangle = new Triangle(
rotate(a, orientation),
rotate(b, orientation),
rotate(c, orientation)
);
rotatedTriangles.add(rotatedTriangle);
}
return rotatedTriangles;
}
private Vertex rotate(Vertex vertex, int orientation)
{
int sin = Perspective.SINE[orientation];
int cos = Perspective.COSINE[orientation];
int[] originalX = model.getVerticesX();
int[] originalZ = model.getVerticesZ();
for (int i = 0; i < originalX.length; ++i)
{
verticesX[i] = originalX[i] * cos + originalZ[i] * sin >> 16;
verticesZ[i] = originalZ[i] * cos - originalX[i] * sin >> 16;
}
return new Vertex(
vertex.getX() * cos + vertex.getZ() * sin >> 16,
vertex.getY(),
vertex.getZ() * cos - vertex.getX() * sin >> 16
);
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (c) 2017, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.api.model;
public class Triangle
{
private final Vertex a;
private final Vertex b;
private final Vertex c;
public Triangle(Vertex a, Vertex b, Vertex c)
{
this.a = a;
this.b = b;
this.c = c;
}
@Override
public String toString()
{
return "Triangle{" + "a=" + a + ", b=" + b + ", c=" + c + '}';
}
public Vertex getA()
{
return a;
}
public Vertex getB()
{
return b;
}
public Vertex getC()
{
return c;
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (c) 2017, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.api.model;
public class Vertex
{
private final int x;
private final int y;
private final int z;
public Vertex(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
@Override
public String toString()
{
return "Vertex{" + "x=" + x + ", y=" + y + ", z=" + z + '}';
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getZ()
{
return z;
}
}