model-viewer: fix some of the orientation issues

This commit is contained in:
Adam
2017-05-11 17:09:48 -04:00
parent cce60dddab
commit c24f6b5eb9
7 changed files with 340 additions and 47 deletions

View File

@@ -0,0 +1,80 @@
/*
* 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.modelviewer;
public class LocationKey
{
private final int id;
private final int type;
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

@@ -0,0 +1,123 @@
/*
* 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.modelviewer;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import net.runelite.cache.definitions.ModelDefinition;
import net.runelite.cache.definitions.ObjectDefinition;
import net.runelite.cache.definitions.loaders.ModelLoader;
import net.runelite.cache.region.Location;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ModelManager
{
private static final Logger logger = LoggerFactory.getLogger(ModelManager.class);
private static Map<LocationKey, ModelDefinition> models = new HashMap<>();
public static ModelDefinition getModel(int id, ObjectDefinition object, Location location)
{
LocationKey key;
if (location != null)
{
key = new LocationKey(id, location.getType(), location.getOrientation());
}
else
{
key = new LocationKey(id, -1, -1);
}
ModelDefinition md = models.get(key);
if (md != null)
{
return md;
}
try
{
byte[] b = Files.readAllBytes(new File("models/" + id + ".model").toPath());
ModelLoader loader = new ModelLoader();
md = loader.load(id, b);
rotate(md, object, location);
models.put(key, md);
return md;
}
catch (IOException ex)
{
logger.warn(null, ex);
return null;
}
}
// this logic is from method3697 in 140
private static void rotate(ModelDefinition md, ObjectDefinition object, Location location)
{
if (object.getObjectTypes() == null)
{
boolean isRotate = object.isRotated();
if (location.getType() == 2 && location.getOrientation() > 3)
{
isRotate = !isRotate;
}
if (isRotate)
{
md.method1493();
}
}
else
{
boolean isRotate = object.isRotated() ^ location.getOrientation() > 3;
if (isRotate)
{
md.method1493();
}
}
switch (location.getOrientation())
{
case 1:
md.rotate1();
break;
case 2:
md.rotate2();
break;
case 3:
md.rotate3();
break;
}
}
}

View File

@@ -27,13 +27,11 @@ package net.runelite.modelviewer;
import com.google.gson.Gson;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -45,7 +43,6 @@ import net.runelite.cache.definitions.ObjectDefinition;
import net.runelite.cache.definitions.OverlayDefinition;
import net.runelite.cache.definitions.TextureDefinition;
import net.runelite.cache.definitions.UnderlayDefinition;
import net.runelite.cache.definitions.loaders.ModelLoader;
import net.runelite.cache.models.Vector3f;
import net.runelite.cache.models.VertexNormal;
import net.runelite.cache.region.Location;
@@ -78,7 +75,6 @@ public class ModelViewer
private static final int NUM_OVERLAYS = 174;
private static final int NUM_TEXTURES = 61;
private static final int NUM_OBJECTS = 28598;
private static final int NUM_MODELS = 31247;
/**
* size of a tile in local coordinates
@@ -91,7 +87,6 @@ public class ModelViewer
private static Map<Integer, Texture> textures = new HashMap<>();
private static ObjectDefinition[] objects = new ObjectDefinition[NUM_OBJECTS];
private static ModelDefinition[] models = new ModelDefinition[NUM_MODELS];
public static void main(String[] args) throws Exception
{
@@ -124,7 +119,7 @@ public class ModelViewer
// render model
String model = cmd.getOptionValue("model");
ModelDefinition md = getModel(Integer.parseInt(model));
ModelDefinition md = ModelManager.getModel(Integer.parseInt(model), null, null);
models.add(md);
}
if (cmd.hasOption("npc"))
@@ -138,7 +133,7 @@ public class ModelViewer
for (int model : npcdef.models)
{
ModelDefinition md = getModel(model);
ModelDefinition md = ModelManager.getModel(model, null, null);
models.add(md);
}
}
@@ -153,7 +148,7 @@ public class ModelViewer
for (int model : objdef.getObjectModels())
{
ModelDefinition md = getModel(model);
ModelDefinition md = ModelManager.getModel(model, null, null);
models.add(md);
}
}
@@ -301,6 +296,9 @@ public class ModelViewer
Texture texture = getTexture(textureId);
assert texture != null;
if (md.faceTextureUCoordinates == null || md.faceTextureVCoordinates == null)
md.computeTextureUVCoordinates();
u = md.faceTextureUCoordinates[i];
v = md.faceTextureVCoordinates[i];
@@ -529,7 +527,6 @@ public class ModelViewer
int regionY = objectPos.getY() - region.getBaseY();
int height = -region.getTileHeight(objectPos.getZ(), regionX, regionY) / HEIGHT_MOD;
//byte overlayRotation = region.getOverlayRotation(objectPos.getZ(), regionX, regionY);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
// TILE_SCALE/2 to draw the object from the center of the tile it is on
@@ -537,14 +534,11 @@ public class ModelViewer
for (int i = 0; i < object.getObjectModels().length; ++i)
{
ModelDefinition md = getModel(object.getObjectModels()[i]);
ModelDefinition md = ModelManager.getModel(object.getObjectModels()[i], object, location);
if (object.getObjectTypes() != null)
if (object.getObjectTypes() != null && object.getObjectTypes()[i] != location.getType())
{
if (object.getObjectTypes()[i] != location.getType())
{
continue;
}
continue;
}
drawModel(md, object.getRecolorToFind(), object.getRecolorToReplace());
@@ -606,31 +600,6 @@ public class ModelViewer
}
}
private static ModelDefinition getModel(int id)
{
ModelDefinition md = models[id];
if (md != null)
{
return md;
}
try
{
byte[] b = Files.readAllBytes(new File("models/" + id + ".model").toPath());
ModelLoader loader = new ModelLoader();
md = loader.load(id, b);
models[id] = md;
return md;
}
catch (IOException ex)
{
logger.warn(null, ex);
return null;
}
}
private static Texture getTexture(int id)
{
Texture texture = textures.get(id);