Author: paperwing
Date: 2011-06-03 16:45:42 -0700 (Fri, 03 Jun 2011)
New Revision: 25637

Added:
   csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/SimpleCamera.java
Modified:
   csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/MouseMonitor.java
   csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/TestGraphics.java
   csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/Vector3.java
Log:
camera roll, first-person, and camera orbit methods implemented

Modified: 
csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/MouseMonitor.java
===================================================================
--- 
csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/MouseMonitor.java   
    2011-06-03 23:39:57 UTC (rev 25636)
+++ 
csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/MouseMonitor.java   
    2011-06-03 23:45:42 UTC (rev 25637)
@@ -1,3 +1,5 @@
+import java.awt.AWTException;
+import java.awt.Robot;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
@@ -65,6 +67,7 @@
        
        @Override
        public void mouseDragged(MouseEvent event) {
+               mouseMoved(event);
        }
 
        @Override
@@ -78,6 +81,7 @@
                        prevY = currentY;
                        ignoreNext = false;
                }
+               
        }
 
        @Override
@@ -128,6 +132,16 @@
                wheelChange = 0;
                prevX = currentX;
                prevY = currentY;
+               
+               Robot r;
+               try {
+                       r = new Robot();
+                       // r.mouseMove(800, 500);
+               } catch (AWTException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+               
        }
        
 

Added: 
csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/SimpleCamera.java
===================================================================
--- 
csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/SimpleCamera.java   
                            (rev 0)
+++ 
csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/SimpleCamera.java   
    2011-06-03 23:45:42 UTC (rev 25637)
@@ -0,0 +1,271 @@
+import javax.swing.text.Position;
+
+
+public class SimpleCamera {
+       private Vector3 direction;
+       private Vector3 up;
+       private Vector3 left;
+       private Vector3 position;
+       private Vector3 target;
+       
+       private double distance;
+       private double moveSpeed;
+       private double turnSpeed;
+       private double orbitSpeed;
+       private double rollSpeed;
+       
+       public SimpleCamera() {
+               this(new Vector3(0, 0, 10), new Vector3(0, 0, 0), new 
Vector3(0, 1, 0));
+       }
+       
+       public SimpleCamera(Vector3 position, Vector3 target, Vector3 up) {
+               this(position, target, up, 0.01, 0.002, 0.002, 0.1);
+       }
+       
+       public SimpleCamera(Vector3 position, Vector3 target, Vector3 up, 
double moveSpeed, double turnSpeed, double orbitSpeed, double rollSpeed) {
+               this.position = new Vector3(position);
+               this.target = new Vector3(target);
+               this.up = new Vector3(up);
+               this.up.normalizeLocal();
+               
+               direction = target.subtract(position);
+               System.out.println("direction before normalization: " + 
direction);
+               direction.normalizeLocal();
+               System.out.println("direction after normalization: " + 
direction);
+               left = up.cross(direction);
+               left.normalizeLocal();
+               
+               distance = position.distance(target);
+               
+               this.moveSpeed = moveSpeed;
+               this.turnSpeed = turnSpeed;
+               this.orbitSpeed = orbitSpeed;
+               this.rollSpeed = rollSpeed;
+       }
+       
+       public void setSpeed(double moveSpeed, double turnSpeed, double 
orbitSpeed, double rollSpeed) {
+               this.moveSpeed = moveSpeed;
+               this.turnSpeed = turnSpeed;
+               this.orbitSpeed = orbitSpeed;
+               this.rollSpeed = rollSpeed;
+       }
+       
+       public Vector3 getPosition() {
+               return position;
+       }
+       
+       public Vector3 getTarget() {
+               return target;
+       }
+       
+       public Vector3 getUp() {
+               return up;
+       }
+       
+       public Vector3 getLeft() {
+               return left;
+       }
+       
+       public Vector3 getDirection() {
+               return direction;
+       }
+       
+       public void moveLeft() {
+               move(left, moveSpeed);
+       }
+       
+       public void moveRight() {
+               move(left, -moveSpeed);
+       }
+       
+       public void moveForward() {
+               move(direction, moveSpeed);
+       }
+       
+       public void moveBackward() {
+               move(direction, -moveSpeed);
+       }
+       
+       public void moveUp() {
+               move(up, moveSpeed);
+       }
+       
+       public void moveDown() {
+               move(up, -moveSpeed);
+       }
+       
+       public void moveTo(Vector3 position) {
+               this.position.set(position);
+               
+               Vector3 newTarget = direction.multiply(distance);
+               newTarget.addLocal(this.position);
+               
+               target.set(newTarget);
+       }
+       
+       public void moveTo(double x, double y, double z) {
+               this.position.set(x, y, z);
+               
+               Vector3 newTarget = direction.multiply(distance);
+               newTarget.addLocal(this.position);
+               
+               target.set(newTarget);
+       }
+       
+       public void turnLeft(double multiplier) {
+               turnHorizontal(multiplier * turnSpeed);
+       }
+       
+       public void turnRight(double multiplier) {
+               turnHorizontal(multiplier * -turnSpeed);
+       }
+       
+       public void turnUp(double multiplier) {
+               turnVertical(multiplier * turnSpeed);
+       }
+       
+       public void turnDown(double multiplier) {
+               turnVertical(multiplier * -turnSpeed);
+       }
+       
+       public void turnLeft() {
+               turnHorizontal(turnSpeed);
+       }
+       
+       public void turnRight() {
+               turnHorizontal(-turnSpeed);
+       }
+       
+       public void turnUp() {
+               turnVertical(turnSpeed);
+       }
+       
+       public void turnDown() {
+               turnVertical(-turnSpeed);
+       }
+       
+       public void orbitLeft(double multiplier) {
+               orbitHorizontal(multiplier * orbitSpeed);
+       }
+       
+       public void orbitRight(double multiplier) {
+               orbitHorizontal(multiplier * -orbitSpeed);
+       }
+       
+       public void orbitUp(double multiplier) {
+               orbitVertical(multiplier * orbitSpeed);
+       }
+       
+       public void orbitDown(double multiplier) {
+               orbitVertical(multiplier * -orbitSpeed);
+       }
+       
+       public void orbitLeft() {
+               orbitHorizontal(orbitSpeed);
+       }
+       
+       public void orbitRight() {
+               orbitHorizontal(-orbitSpeed);
+       }
+       
+       public void orbitUp() {
+               orbitVertical(orbitSpeed);
+       }
+       
+       public void orbitDown() {
+               orbitVertical(-orbitSpeed);
+       }
+       
+       public void rollClockwise(double multiplier) {
+               roll(multiplier * rollSpeed);
+       }
+       
+       public void rollCounterClockwise(double multiplier) {
+               roll(multiplier * -rollSpeed);
+       }
+       
+       public void rollClockwise() {
+               roll(rollSpeed);
+       }
+       
+       public void rollCounterClockwise() {
+               roll(-rollSpeed);
+       }
+       
+       private void move(Vector3 direction, double multiplier) {
+               Vector3 offset = new Vector3(direction.multiply(multiplier));
+               
+               target.addLocal(offset);
+               
+               Vector3 newPosition = this.direction.multiply(-distance);
+               newPosition.addLocal(target);
+               
+               position.set(newPosition);
+       }
+       
+       private void turnHorizontal(double angle) {
+               direction = direction.rotate(up, angle);
+               direction.normalizeLocal();
+               
+               Vector3 newTarget = direction.multiply(distance);
+               newTarget.addLocal(position);
+               target.set(newTarget);
+               
+               left = left.projectNormal(direction);
+               left.normalizeLocal();
+               
+               // TODO: Check if this line is needed to maintain up, 
direction, and left are perpendicular
+               // up.set(direction.cross(left));
+       }
+       
+       private void turnVertical(double angle) {
+               direction = direction.rotate(left, angle);
+               direction.normalizeLocal();
+               
+               Vector3 newTarget = direction.multiply(distance);
+               newTarget.addLocal(position);
+               target.set(newTarget);
+               
+               up = up.projectNormal(direction);
+               up.normalizeLocal();
+               
+               // TODO: Check if this line is needed to maintain up, 
direction, and left are perpendicular
+               // left.set(up.cross(direction));
+       }
+       
+       private void orbitHorizontal(double angle) {
+               Vector3 newPosition = direction.multiply(-distance);
+               newPosition = newPosition.rotate(up, angle);
+               newPosition.addLocal(target);
+               
+               position.set(newPosition);
+               
+               direction.set(target.subtract(position));
+               direction.normalizeLocal();
+               
+               left = left.projectNormal(direction);
+               left.normalizeLocal();
+       }
+       
+       private void orbitVertical(double angle) {
+               Vector3 newPosition = direction.multiply(-distance);
+               newPosition = newPosition.rotate(left, angle);
+               newPosition.addLocal(target);
+               
+               position.set(newPosition);
+               
+               direction.set(target.subtract(position));
+               direction.normalizeLocal();
+               
+               up = up.projectNormal(direction);
+               up.normalizeLocal();
+       }
+       
+       private void roll(double angle) {
+               up = up.rotate(direction, angle);
+               up.normalizeLocal();
+               
+               left = up.cross(direction);
+               left.normalizeLocal();
+       }       
+}


Property changes on: 
csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/SimpleCamera.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Modified: 
csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/TestGraphics.java
===================================================================
--- 
csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/TestGraphics.java   
    2011-06-03 23:39:57 UTC (rev 25636)
+++ 
csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/TestGraphics.java   
    2011-06-03 23:45:42 UTC (rev 25637)
@@ -1,5 +1,6 @@
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
+import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
 import java.awt.event.MouseWheelListener;
@@ -66,15 +67,23 @@
        private int nodeSeed = 556;
        private int edgeSeed = 556;
        
-       private KeyboardMonitor keys = new KeyboardMonitor();
-       private MouseMonitor mouse = new MouseMonitor();
+       private KeyboardMonitor keys;
+       private MouseMonitor mouse;
+       private SimpleCamera camera;
        
-       
+       /*
        private Vector3 camera = new Vector3(0, 0, 0);
        private Vector3 direction = new Vector3(0, 0, -1.0);
        private Vector3 up = new Vector3(0, 1.0, 0);
        private Vector3 left = new Vector3(-1.0, 0, 0);
-
+       */
+       
+       public TestGraphics() {
+               keys = new KeyboardMonitor();
+               mouse = new MouseMonitor();
+               camera = new SimpleCamera(new Vector3(0, 0, 2), new Vector3(0, 
0, 0), new Vector3(0, 1, 0), 0.04, 0.003, 0.01, 0.01);
+       }
+       
        public KeyListener getKeyListener() {
                return keys;
        }
@@ -146,20 +155,22 @@
                gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
                gl.glLoadIdentity();
                
-               Vector3 current = new Vector3(0, 0, -1);
-               Vector3 normal = direction.cross(current);
+               Vector3 position = camera.getPosition();
+               Vector3 target = camera.getTarget();
+               Vector3 up = camera.getUp();
                
+               // System.out.println(position + " " + target + " " + up);
+               
                GLU glu = new GLU();
-               glu.gluLookAt(camera.x(), camera.y(), camera.z(), 
-                               camera.x() + direction.x(), camera.y() + 
direction.y(), camera.z() + direction.z(), 
+               glu.gluLookAt(position.x(), position.y(), position.z(),
+                               target.x(), target.y(), target.z(),
                                up.x(), up.y(), up.z());
                
-               
                // gl.glRotated(direction.angle(current) * 180 / Math.PI, 
normal.x(), normal.y(), normal.z());
                // gl.glTranslated(-camera.x(), -camera.y(), -camera.z());
                
-               float[] position = { -4.0f, 4.0f, 6.0f, 1.0f };
-               gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, 
FloatBuffer.wrap(position));
+               float[] lightPosition = { -4.0f, 4.0f, 6.0f, 1.0f };
+               gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, 
FloatBuffer.wrap(lightPosition));
                
                
                gl.glColor3f(0.6f, 0.6f, 0.6f);
@@ -188,111 +199,102 @@
                                                + frameRate);
                        }
                        
-                       if (pressed.contains(KeyEvent.VK_Z)) {
-                               camera.set(0.0, 0.0, 0.0);
-                       }
-                       
-                       if (pressed.contains(KeyEvent.VK_X)) {
-                               camera.set(0.5, 0.0, 0.0);
-                       }
-                       
                        if (pressed.contains(KeyEvent.VK_C)) {
-                               // camera = new Vector3(0, 0, 0);
-                               direction.set(0, 0, -1.0);
-                               up.set(0, 1.0, 0);
-                               left.set(-1.0, 0, 0);
+                               camera.setSpeed(0.04, 0.003, 0.01, 0.1);
+                               camera.moveTo(0, 0, 2);
                        }
                        
-                       if (pressed.contains(KeyEvent.VK_V)) {
-                               direction.set(0.0, -0.2, -1.0);
-                               System.out.println("rotation angle: " + 
direction.angle(new Vector3(0, 0, -1)) * 180 / Math.PI);
+                       if (pressed.contains(KeyEvent.VK_SPACE)) {
+                               System.out.println("====");
+                               System.out.print("direction: " + 
camera.getDirection());
+                               System.out.print(", left: " + camera.getLeft());
+                               System.out.println(", up: " + camera.getUp());
+                               System.out.print("position: " + 
camera.getPosition());
+                               System.out.println(", target: " + 
camera.getTarget());
+                               System.out.println("====");
                        }
                        
-                       if (pressed.contains(KeyEvent.VK_B)) {
-                               direction.set(-0.2, 0.0, -1.0);
-                               System.out.println("rotation angle: " + 
direction.angle(new Vector3(0, 0, -1)) * 180 / Math.PI);
+                       if (held.contains(KeyEvent.VK_Z)) {
+                               camera.rollClockwise();
                        }
                        
-                       if (pressed.contains(KeyEvent.VK_N)) {
-                               System.out.println(camera.angle(up));
+                       if (held.contains(KeyEvent.VK_X)) {
+                               camera.rollCounterClockwise();
                        }
                        
-                       double turnSpeed = 0.01;
+                       if (held.contains(KeyEvent.VK_SHIFT)) {
                        
-                       if (held.contains(KeyEvent.VK_LEFT)) {
-                               direction = direction.rotate(up, turnSpeed);
-                               direction.normalizeLocal();
-                               left = left.projectNormal(direction);
-                               left.normalizeLocal();
+                               if (held.contains(KeyEvent.VK_LEFT)) {
+                                       camera.orbitLeft();
+                               }
                                
-                               up.set(direction.cross(left));
-                       }
-                       
-                       if (held.contains(KeyEvent.VK_RIGHT)) {
-                               direction = direction.rotate(up, -turnSpeed);
-                               direction.normalizeLocal();
-                               left = left.projectNormal(direction);
-                               left.normalizeLocal();
+                               if (held.contains(KeyEvent.VK_RIGHT)) {
+                                       camera.orbitRight();
+                               }
                                
-                               up.set(direction.cross(left));
-                       }
+                               if (held.contains(KeyEvent.VK_UP)) {
+                                       camera.orbitUp();
+                               }
+                               
+                               if (held.contains(KeyEvent.VK_DOWN)) {
+                                       camera.orbitDown();
+                               }
+                               
+                       } else {
                        
-                       if (released.contains(KeyEvent.VK_LEFT) || 
released.contains(KeyEvent.VK_RIGHT)) {
-                               System.out.println("direction: " + direction);
-                               System.out.println("left: " + left);
-                               System.out.println("up: " + up);
-                       }
-                       
-                       if (held.contains(KeyEvent.VK_UP)) {
-                               direction = direction.rotate(left, turnSpeed);
-                               direction.normalizeLocal();
-                               up = up.projectNormal(direction);
-                               up.normalizeLocal();
+                               /*
+                               if (held.contains(KeyEvent.VK_LEFT)) {
+                                       camera.turnLeft();
+                               }
                                
-                               left.set(up.cross(direction));
-                       }
+                               if (held.contains(KeyEvent.VK_RIGHT)) {
+                                       camera.turnRight();
+                               }
+                               
+                               if (held.contains(KeyEvent.VK_UP)) {
+                                       camera.turnUp();
+                               }
+                               
+                               if (held.contains(KeyEvent.VK_DOWN)) {
+                                       camera.turnDown();
+                               }
+                               */
                        
-                       if (held.contains(KeyEvent.VK_DOWN)) {
-                               direction = direction.rotate(left, -turnSpeed);
-                               direction.normalizeLocal();
-                               up = up.projectNormal(direction);
-                               up.normalizeLocal();
-                               
-                               left.set(up.cross(direction));
                        }
                        
-                       double translateSpeed = 0.02;
-                       
                        if (held.contains(KeyEvent.VK_W)) {
-                               camera.addLocal(0, 0, -translateSpeed);
+                               camera.moveForward();
                        }
                        
                        if (held.contains(KeyEvent.VK_S)) {
-                               camera.addLocal(0, 0, translateSpeed);
+                               camera.moveBackward();
                        }
                        
                        if (held.contains(KeyEvent.VK_A)) {
-                               camera.addLocal(-translateSpeed, 0, 0);
+                               camera.moveLeft();
                        }
                        
                        if (held.contains(KeyEvent.VK_D)) {
-                               camera.addLocal(translateSpeed, 0, 0);
+                               camera.moveRight();
                        }
                        
                        if (held.contains(KeyEvent.VK_Q)) {
-                               camera.addLocal(0, translateSpeed, 0);
+                               camera.moveDown();
                        }
                        
                        if (held.contains(KeyEvent.VK_E)) {
-                               camera.addLocal(0, -translateSpeed, 0);
+                               camera.moveUp();
                        }
                        
                        keys.update();
                }
                
-               if (mouse.hasNew()) {
-                       // System.out.println("Mouse keys down: " + 
mouse.getHeld());
-                       // System.out.println("Mouse scroll change: " + 
mouse.dWheel());
+               if (mouse.hasMoved()) {
+                       if (mouse.getHeld().contains(MouseEvent.BUTTON1)) {
+                               camera.turnRight(mouse.dX());
+                               camera.turnUp(mouse.dY());
+                       }
+                       
                        mouse.update();
                }
        }

Modified: 
csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/Vector3.java
===================================================================
--- csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/Vector3.java    
2011-06-03 23:39:57 UTC (rev 25636)
+++ csplugins/trunk/toronto/paperwing/TestLibrary/src/test/java/Vector3.java    
2011-06-03 23:45:42 UTC (rev 25637)
@@ -1,6 +1,3 @@
-
-
-
 public class Vector3 {
        private double x;
        private double y;
@@ -15,6 +12,12 @@
                this.z = z;
        }
        
+       public Vector3(Vector3 other) {
+               x = other.x;
+               y = other.y;
+               z = other.z;
+       }
+       
        public double x() {
                return x;
        }
@@ -109,6 +112,11 @@
                }
        }
        
+       // Obtain distance between position vectors
+       public double distance(Vector3 other) {
+               return Math.sqrt(Math.pow(x - other.x, 2) + Math.pow(y - 
other.y, 2) + Math.pow(z - other.z, 2));
+       }
+       
        // Project the vector onto the plane passing through the origin, 
perpendicular to the given normal
        public Vector3 projectNormal(Vector3 normal) {
                return subtract(normal.multiply(this.dot(normal)));

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to