VisClient/org/hfbk/vis/Viewpoint.java

Go to the documentation of this file.
00001 /*
00002  * Created on 14.04.2008
00003  *
00004  */
00005 package org.hfbk.vis;
00006 
00007 import org.dronus.al.ALUtil;
00008 import org.dronus.gl.Buffers;
00009 import org.dronus.gl.GLUtil;
00010 import org.lwjgl.openal.AL;
00011 import org.lwjgl.openal.AL10;
00012 import org.lwjgl.opengl.GL11;
00013 import org.lwjgl.util.glu.*;
00014 import org.lwjgl.util.vector.Vector3f;
00015 import org.lwjgl.util.vector.Vector4f;
00016 
00023 public class Viewpoint extends Vector3f implements Cloneable{
00024 
00025         public static final float DEFAULT_FOV = .5f;
00026         
00027         public float angle=0, elevation=0, roll=0, fov=DEFAULT_FOV;
00028         
00029         Viewpoint last=this; //store our last view to detect movement
00030 
00034         boolean moving=false;
00035         
00036         public Viewpoint(){}
00037         
00041         public Viewpoint(Vector3f position, float angle, float elevation) {
00042                 set(position);
00043                 this.angle=angle;
00044                 this.elevation=elevation;
00045         }
00046         
00052         void forward(float speed){
00053                 x+=(float)(-Math.sin(angle)*speed);
00054                 z+=(float)(-Math.cos(angle)*speed);
00055         }
00056         
00062         public void strafe(float speed) {
00063                 x+=(float)(Math.cos(angle)*speed);
00064                 z+=(float)(-Math.sin(angle)*speed);
00065         }
00066         
00067         
00068         
00069         
00070         Vector3f delta=new Vector3f();
00071         
00078         void render(float dt){
00079                 
00080                 moving =!last.equals(this); //if we differ from last vp we are moving
00081                 Vector3f.sub(this, last, delta);
00082                 last=this.clone(); //save current vp for next compare  
00083 
00084                 //clamp elevation to prevent loopings
00085                 elevation=(float)Math.min(elevation,Math.PI/2-.1f);
00086                 elevation=(float)Math.max(elevation,-Math.PI/2+.1f);
00087                 
00088                 //calculate look at vector
00089                 double r=Math.cos(elevation);
00090                 Vector4f p=new Vector4f(
00091                                 (float)(-Math.sin(angle)*r),
00092                                 (float)(Math.sin(elevation)),                           
00093                                 (float)(-Math.cos(angle)*r), 
00094                                 1
00095                 );              
00096                 
00097                 //calculate up vector
00098                 double q=Math.sin(roll);
00099                 Vector4f up=new Vector4f(
00100                                 (float)(-Math.cos(angle)*q),
00101                                 (float)(Math.cos(roll)),                                
00102                                 (float)(Math.sin(angle)*q), 
00103                                 1
00104                                 );              
00105                 
00106                 //apply all transforms
00107                 GLUtil.fov=fov;
00108                 GL11.glLoadIdentity();          
00109                 GLU.gluLookAt(x, y, z, x+p.x,y+p.y ,z+p.z, up.x,up.y,up.z);
00110                 
00111                 //apply audio transforms
00112                 ALUtil.enable(Prefs.current.sound);
00113                 if (AL.isCreated() && Prefs.current.sound){
00114                         
00115                         AL10.alListener(AL10.AL_POSITION, Buffers.buffer(this));
00116                     
00117                     Vector3f vel=new Vector3f(delta); //calculate speed from position delta (units/second)                  
00118                     vel.scale(1/dt);
00119                     AL10.alListener(AL10.AL_VELOCITY, Buffers.buffer(vel));
00120                     
00121                     ALUtil.setListenerOrientation(new Vector3f(p), new Vector3f(up));
00122                 }
00123         }
00124 
00130         public void set(Viewpoint vp) {
00131                 super.set(vp);
00132                 elevation=vp.elevation; angle=vp.angle;
00133         }
00134         
00138         public Viewpoint clone() {
00139                 try {
00140                         return (Viewpoint)super.clone();
00141                 } catch (CloneNotSupportedException e) {
00142                         return null;
00143                 }
00144         }
00145 
00150         public boolean equals(Object other) {
00151                 if (other instanceof Viewpoint){
00152                         Viewpoint o=(Viewpoint)other;
00153                         return (x==o.x && y==o.y && z==o.z && angle==o.angle  && elevation==o.elevation);
00154                 }else 
00155                         return false; //what is not a viewpoint can't be equal to us
00156         }
00157         
00158 }

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1