modelviewer: fix most of rotation logic

Also add alpha support
This commit is contained in:
Adam
2018-04-25 21:10:09 -04:00
parent de7c495c4d
commit a0033320c5
4 changed files with 52 additions and 71 deletions

View File

@@ -70,6 +70,12 @@
<version>2.5</version> <version>2.5</version>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>

View File

@@ -24,57 +24,12 @@
*/ */
package net.runelite.modelviewer; package net.runelite.modelviewer;
import lombok.Value;
@Value
public class LocationKey public class LocationKey
{ {
private final int id; private final int id;
private final int type; private final int type;
private final int orientation; private final int orientation;
public LocationKey(int id, int type, int orientation)
{
this.id = id;
this.type = type;
this.orientation = orientation;
}
@Override
public int hashCode()
{
int hash = 5;
hash = 97 * hash + this.id;
hash = 97 * hash + this.type;
hash = 97 * hash + this.orientation;
return hash;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final LocationKey other = (LocationKey) obj;
if (this.id != other.id)
{
return false;
}
if (this.type != other.type)
{
return false;
}
if (this.orientation != other.orientation)
{
return false;
}
return true;
}
} }

View File

@@ -46,9 +46,11 @@ public class ModelManager
{ {
LocationKey key; LocationKey key;
int rot = location.getOrientation();
if (location != null) if (location != null)
{ {
key = new LocationKey(id, location.getType(), location.getOrientation()); key = new LocationKey(id, location.getType(), rot);
} }
else else
{ {
@@ -70,7 +72,8 @@ public class ModelManager
if (object != null && location != null) if (object != null && location != null)
{ {
rotate(md, object, location); rotate(md, object, location, rot);
md.computeNormals();
} }
models.put(key, md); models.put(key, md);
@@ -84,13 +87,13 @@ public class ModelManager
} }
// this logic is from method3697 in 140 // this logic is from method3697 in 140
private static void rotate(ModelDefinition md, ObjectDefinition object, Location location) private static void rotate(ModelDefinition md, ObjectDefinition object, Location location, int rot)
{ {
if (object.getObjectTypes() == null) if (object.getObjectTypes() == null)
{ {
boolean isRotate = object.isRotated(); boolean isRotate = object.isRotated();
if (location.getType() == 2 && location.getOrientation() > 3) if (location.getType() == 2 && rot > 3)
{ {
isRotate = !isRotate; isRotate = !isRotate;
} }
@@ -102,7 +105,7 @@ public class ModelManager
} }
else else
{ {
boolean isRotate = object.isRotated() ^ location.getOrientation() > 3; boolean isRotate = object.isRotated() ^ rot > 3;
if (isRotate) if (isRotate)
{ {
@@ -110,7 +113,7 @@ public class ModelManager
} }
} }
switch (location.getOrientation()) switch (rot & 3)
{ {
case 1: case 1:
md.rotate1(); md.rotate1();

View File

@@ -225,6 +225,10 @@ public class ModelViewer
GL11.glCullFace(GL11.GL_BACK); GL11.glCullFace(GL11.GL_BACK);
GL11.glEnable(GL11.GL_CULL_FACE); GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
long last = 0; long last = 0;
Camera camera = new Camera(); Camera camera = new Camera();
@@ -297,6 +301,11 @@ public class ModelViewer
nB.y = -nB.y; nB.y = -nB.y;
nC.y = -nC.y; nC.y = -nC.y;
// and z
nA.z = -nA.z;
nB.z = -nB.z;
nC.z = -nC.z;
int vertexAx = md.vertexPositionsX[vertexA]; int vertexAx = md.vertexPositionsX[vertexA];
int vertexAy = md.vertexPositionsY[vertexA]; int vertexAy = md.vertexPositionsY[vertexA];
int vertexAz = md.vertexPositionsZ[vertexA]; int vertexAz = md.vertexPositionsZ[vertexA];
@@ -310,6 +319,12 @@ public class ModelViewer
int vertexCz = md.vertexPositionsZ[vertexC]; int vertexCz = md.vertexPositionsZ[vertexC];
short textureId = md.faceTextures != null ? md.faceTextures[i] : -1; short textureId = md.faceTextures != null ? md.faceTextures[i] : -1;
int alpha = md.faceAlphas != null ? (md.faceAlphas[i] & 0xFF) : 255;
if (alpha == 0)
{
alpha = 255;
}
Color color; Color color;
float[] u = null; float[] u = null;
@@ -359,34 +374,32 @@ public class ModelViewer
float rf = (float) color.getRed() / 255f; float rf = (float) color.getRed() / 255f;
float gf = (float) color.getGreen() / 255f; float gf = (float) color.getGreen() / 255f;
float bf = (float) color.getBlue() / 255f; float bf = (float) color.getBlue() / 255f;
float af = (float) alpha / 255f;
GL11.glBegin(GL11.GL_TRIANGLES); GL11.glBegin(GL11.GL_TRIANGLES);
GL11.glColor3f(rf, gf, bf); GL11.glColor4f(rf, gf, bf, af);
// With GL11.GL_CCW we have to draw A -> C -> B when
// inverting y instead of A -> B -> C, or else with cull
// face will cull the wrong side
GL11.glNormal3f(nA.x, nA.y, nA.z); GL11.glNormal3f(nA.x, nA.y, nA.z);
if (textureId != -1) if (textureId != -1)
{ {
GL11.glTexCoord2f(u[0], v[0]); GL11.glTexCoord2f(u[0], v[0]);
} }
GL11.glVertex3i(vertexAx, -vertexAy, vertexAz); GL11.glVertex3i(vertexAx, -vertexAy, -vertexAz);
GL11.glNormal3f(nC.x, nC.y, nC.z);
if (textureId != -1)
{
GL11.glTexCoord2f(u[2], v[2]);
}
GL11.glVertex3i(vertexCx, -vertexCy, vertexCz);
GL11.glNormal3f(nB.x, nB.y, nB.z); GL11.glNormal3f(nB.x, nB.y, nB.z);
if (textureId != -1) if (textureId != -1)
{ {
GL11.glTexCoord2f(u[1], v[1]); GL11.glTexCoord2f(u[1], v[1]);
} }
GL11.glVertex3i(vertexBx, -vertexBy, vertexBz); GL11.glVertex3i(vertexBx, -vertexBy, -vertexBz);
GL11.glNormal3f(nC.x, nC.y, nC.z);
if (textureId != -1)
{
GL11.glTexCoord2f(u[2], v[2]);
}
GL11.glVertex3i(vertexCx, -vertexCy, -vertexCz);
GL11.glEnd(); GL11.glEnd();
@@ -556,15 +569,19 @@ public class ModelViewer
int height = -region.getTileHeight(objectPos.getZ(), regionX, regionY) / HEIGHT_MOD; int height = -region.getTileHeight(objectPos.getZ(), regionX, regionY) / HEIGHT_MOD;
GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glMatrixMode(GL11.GL_MODELVIEW);
// TILE_SCALE/2 to draw the object from the center of the tile it is on // TILE_SCALE/2 to draw the object from the center of the tile it is on
GL11.glTranslatef(regionX * TILE_SCALE + (TILE_SCALE / 2), height, -regionY * TILE_SCALE - (TILE_SCALE / 2)); GL11.glTranslatef(regionX * TILE_SCALE + (TILE_SCALE / 2), height * (location.getPosition().getZ() + 1), -regionY * TILE_SCALE - (TILE_SCALE / 2));
for (int i = 0; i < object.getObjectModels().length; ++i) for (int i = 0; i < object.getObjectModels().length; ++i)
{ {
if (object.getObjectTypes() != null && object.getObjectTypes()[i] != location.getType())
{
continue;
}
ModelDefinition md = ModelManager.getModel(object.getObjectModels()[i], object, location); ModelDefinition md = ModelManager.getModel(object.getObjectModels()[i], object, location);
if (object.getObjectTypes() != null && object.getObjectTypes()[i] != location.getType()) if (md == null)
{ {
continue; continue;
} }
@@ -572,7 +589,7 @@ public class ModelViewer
drawModel(md, object.getRecolorToFind(), object.getRecolorToReplace()); drawModel(md, object.getRecolorToFind(), object.getRecolorToReplace());
} }
GL11.glTranslatef(-regionX * TILE_SCALE - (TILE_SCALE / 2), -height, regionY * TILE_SCALE + (TILE_SCALE / 2)); GL11.glTranslatef(-regionX * TILE_SCALE - (TILE_SCALE / 2), -(height * (location.getPosition().getZ() + 1)), regionY * TILE_SCALE + (TILE_SCALE / 2));
GL11.glPopMatrix(); GL11.glPopMatrix();
} }
} }