modelviewer: rewrite camera handling to be more correct

This commit is contained in:
Adam
2016-11-27 20:08:14 -05:00
parent 5f0ce62b6b
commit 3508cd4a33
2 changed files with 262 additions and 178 deletions

View File

@@ -1,64 +1,120 @@
/*
* Copyright (c) 2016, 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Adam <Adam@sigterm.info>
* 4. Neither the name of the Adam <Adam@sigterm.info> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Adam <Adam@sigterm.info> ''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 Adam <Adam@sigterm.info> 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; package net.runelite.modelviewer;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse; import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import static org.lwjgl.opengl.GL11.glRotatef; import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glTranslatef; import static org.lwjgl.opengl.GL11.glTranslatef;
public class Camera { public class Camera
public static float moveSpeed = 0.05f; {
private static final float MAX_X = 89;
private static float maxLook = 85; public float moveSpeed = 0.05f;
private static float mouseSensitivity = 0.05f; private float mouseSensitivity = 0.05f;
private static Vector3f pos; private Vector3f pos = new Vector3f(0, 0, 0);
private static Vector3f rotation; private Vector3f rotation = new Vector3f(0, 0, 0);
public static void create() { public void apply()
pos = new Vector3f(0, 0, 0); {
rotation = new Vector3f(0, 0, 0); GL11.glMatrixMode(GL11.GL_MODELVIEW);
} GL11.glLoadIdentity();
public static void apply() {
if(rotation.y / 360 > 1) {
rotation.y -= 360;
} else if(rotation.y / 360 < -1) {
rotation.y += 360;
}
// glLoadIdentity();
glRotatef(rotation.x, 1, 0, 0); glRotatef(rotation.x, 1, 0, 0);
glRotatef(rotation.y, 0, 1, 0); glRotatef(rotation.y, 0, 1, 0);
glRotatef(rotation.z, 0, 0, 1); glRotatef(rotation.z, 0, 0, 1);
glTranslatef(-pos.x, -pos.y, -pos.z); glTranslatef(-pos.x, -pos.y, -pos.z);
GL11.glPopMatrix();
} }
public static void acceptInput(float delta) { public void acceptInput(float delta)
{
acceptInputRotate(delta); acceptInputRotate(delta);
acceptInputGrab(); acceptInputGrab();
acceptInputMove(delta); acceptInputMove(delta);
} }
public static void acceptInputRotate(float delta) { public void acceptInputRotate(float delta)
if(Mouse.isGrabbed()) { {
if (Mouse.isGrabbed())
{
float mouseDX = Mouse.getDX(); float mouseDX = Mouse.getDX();
float mouseDY = -Mouse.getDY(); float mouseDY = -Mouse.getDY();
rotation.y += mouseDX * mouseSensitivity * delta; rotation.y += mouseDX * mouseSensitivity * delta;
rotation.x += mouseDY * mouseSensitivity * delta; rotation.x += mouseDY * mouseSensitivity * delta;
rotation.x = Math.max(-maxLook, Math.min(maxLook, rotation.x));
rotation.y %= 360.0f;
rotation.x %= 360.0f;
// bound y between (-180, 180]
if (rotation.y > 180.0f)
{
rotation.y = -360.0f + rotation.y;
}
if (rotation.y <= -180.0f)
{
rotation.y = 360.0f + rotation.y;
}
// cap x to prevent from flipping upsidedown
if (rotation.x < -MAX_X)
{
rotation.x = -MAX_X;
}
if (rotation.x > MAX_X)
{
rotation.x = MAX_X;
}
} }
} }
public static void acceptInputGrab() { public void acceptInputGrab()
if(Mouse.isInsideWindow() && Mouse.isButtonDown(0)) { {
if (Mouse.isInsideWindow() && Mouse.isButtonDown(0))
{
Mouse.setGrabbed(true); Mouse.setGrabbed(true);
} }
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
{
Mouse.setGrabbed(false); Mouse.setGrabbed(false);
} }
} }
public static void acceptInputMove(float delta) { public void acceptInputMove(float delta)
{
boolean keyUp = Keyboard.isKeyDown(Keyboard.KEY_W); boolean keyUp = Keyboard.isKeyDown(Keyboard.KEY_W);
boolean keyDown = Keyboard.isKeyDown(Keyboard.KEY_S); boolean keyDown = Keyboard.isKeyDown(Keyboard.KEY_S);
boolean keyRight = Keyboard.isKeyDown(Keyboard.KEY_D); boolean keyRight = Keyboard.isKeyDown(Keyboard.KEY_D);
@@ -68,151 +124,185 @@ public class Camera {
boolean keyFlyUp = Keyboard.isKeyDown(Keyboard.KEY_SPACE); boolean keyFlyUp = Keyboard.isKeyDown(Keyboard.KEY_SPACE);
boolean keyFlyDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT); boolean keyFlyDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT);
float speed; float speed;
if(keyFast) { if (keyFast)
{
speed = moveSpeed * 5; speed = moveSpeed * 5;
} }
else if(keySlow) { else if (keySlow)
{
speed = moveSpeed / 2; speed = moveSpeed / 2;
} }
else { else
{
speed = moveSpeed; speed = moveSpeed;
} }
speed *= delta; speed *= delta;
if(keyFlyUp) { // sin(0) = 0
pos.y += speed; // cos(0) = 1
if (keyFlyUp)
{
pos.y += Math.cos(Math.toRadians(rotation.x)) * speed;
pos.z -= Math.cos(Math.toRadians(rotation.y)) * Math.sin(Math.toRadians(rotation.x)) * speed;
pos.x += Math.sin(Math.toRadians(rotation.x)) * Math.sin(Math.toRadians(rotation.y)) * speed;
} }
if(keyFlyDown) { if (keyFlyDown)
pos.y -= speed; {
pos.y -= Math.cos(Math.toRadians(rotation.x)) * speed;
pos.z += Math.cos(Math.toRadians(rotation.y)) * Math.sin(Math.toRadians(rotation.x)) * speed;
pos.x -= Math.sin(Math.toRadians(rotation.x)) * Math.sin(Math.toRadians(rotation.y)) * speed;
} }
if(keyDown) { if (keyDown)
{
pos.x -= Math.sin(Math.toRadians(rotation.y)) * speed; pos.x -= Math.sin(Math.toRadians(rotation.y)) * speed;
pos.z += Math.cos(Math.toRadians(rotation.y)) * speed; pos.z += Math.cos(Math.toRadians(rotation.x)) * Math.cos(Math.toRadians(rotation.y)) * speed;
pos.y += Math.sin(Math.toRadians(rotation.x)) * speed;
} }
if(keyUp) { if (keyUp)
{
pos.x += Math.sin(Math.toRadians(rotation.y)) * speed; pos.x += Math.sin(Math.toRadians(rotation.y)) * speed;
pos.z -= Math.cos(Math.toRadians(rotation.y)) * speed; pos.z -= Math.cos(Math.toRadians(rotation.x)) * Math.cos(Math.toRadians(rotation.y)) * speed;
pos.y -= Math.sin(Math.toRadians(rotation.x)) * speed;
} }
if(keyLeft) {
if (keyLeft)
{
pos.x += Math.sin(Math.toRadians(rotation.y - 90)) * speed; pos.x += Math.sin(Math.toRadians(rotation.y - 90)) * speed;
pos.z -= Math.cos(Math.toRadians(rotation.y - 90)) * speed; pos.z -= Math.cos(Math.toRadians(rotation.y - 90)) * speed;
} }
if(keyRight) { if (keyRight)
{
pos.x += Math.sin(Math.toRadians(rotation.y + 90)) * speed; pos.x += Math.sin(Math.toRadians(rotation.y + 90)) * speed;
pos.z -= Math.cos(Math.toRadians(rotation.y + 90)) * speed; pos.z -= Math.cos(Math.toRadians(rotation.y + 90)) * speed;
} }
} }
public static void setSpeed(float speed) { public void setSpeed(float speed)
{
moveSpeed = speed; moveSpeed = speed;
} }
public static void setPos(Vector3f pos) { public void setPos(Vector3f pos)
Camera.pos = pos; {
this.pos = pos;
} }
public static Vector3f getPos() { public Vector3f getPos()
{
return pos; return pos;
} }
public static void setX(float x) { public void setX(float x)
{
pos.x = x; pos.x = x;
} }
public static float getX() { public float getX()
{
return pos.x; return pos.x;
} }
public static void addToX(float x) { public void addToX(float x)
{
pos.x += x; pos.x += x;
} }
public static void setY(float y) { public void setY(float y)
{
pos.y = y; pos.y = y;
} }
public static float getY() { public float getY()
{
return pos.y; return pos.y;
} }
public static void addToY(float y) { public void addToY(float y)
{
pos.y += y; pos.y += y;
} }
public static void setZ(float z) { public void setZ(float z)
{
pos.z = z; pos.z = z;
} }
public static float getZ() { public float getZ()
{
return pos.z; return pos.z;
} }
public static void addToZ(float z) { public void addToZ(float z)
{
pos.z += z; pos.z += z;
} }
public static void setRotation(Vector3f rotation) { public void setRotation(Vector3f rotation)
Camera.rotation = rotation; {
this.rotation = rotation;
} }
public static Vector3f getRotation() { public Vector3f getRotation()
{
return rotation; return rotation;
} }
public static void setRotationX(float x) { public void setRotationX(float x)
{
rotation.x = x; rotation.x = x;
} }
public static float getRotationX() { public float getRotationX()
{
return rotation.x; return rotation.x;
} }
public static void addToRotationX(float x) { public void addToRotationX(float x)
{
rotation.x += x; rotation.x += x;
} }
public static void setRotationY(float y) { public void setRotationY(float y)
{
rotation.y = y; rotation.y = y;
} }
public static float getRotationY() { public float getRotationY()
{
return rotation.y; return rotation.y;
} }
public static void addToRotationY(float y) { public void addToRotationY(float y)
{
rotation.y += y; rotation.y += y;
} }
public static void setRotationZ(float z) { public void setRotationZ(float z)
{
rotation.z = z; rotation.z = z;
} }
public static float getRotationZ() { public float getRotationZ()
{
return rotation.z; return rotation.z;
} }
public static void addToRotationZ(float z) { public void addToRotationZ(float z)
{
rotation.z += z; rotation.z += z;
} }
public static void setMaxLook(float maxLook) { public void setMouseSensitivity(float mouseSensitivity)
Camera.maxLook = maxLook; {
this.mouseSensitivity = mouseSensitivity;
} }
public static float getMaxLook() { public float getMouseSensitivity()
return maxLook; {
}
public static void setMouseSensitivity(float mouseSensitivity) {
Camera.mouseSensitivity = mouseSensitivity;
}
public static float getMouseSensitivity() {
return mouseSensitivity; return mouseSensitivity;
} }
} }

View File

@@ -37,8 +37,6 @@ import net.runelite.cache.definitions.loaders.ModelLoader;
import org.lwjgl.opengl.Display; import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glTranslatef;
public class ModelViewer public class ModelViewer
{ {
@@ -71,9 +69,7 @@ public class ModelViewer
long last = 0; long last = 0;
// rotate and step back to view Camera camera = new Camera();
glRotatef(-90, 1, 0, 0);
glTranslatef(0, 60.0f, 0);
while (!Display.isCloseRequested()) while (!Display.isCloseRequested())
{ {
@@ -125,10 +121,8 @@ public class ModelViewer
long delta = System.currentTimeMillis() - last; long delta = System.currentTimeMillis() - last;
last = System.currentTimeMillis(); last = System.currentTimeMillis();
Camera.create(); camera.acceptInput(delta);
Camera.acceptInput(delta); camera.apply();
Camera.apply();
} }
Display.destroy(); Display.destroy();