JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
Camera.java
Go to the documentation of this file.
1 package jarmos.visual;
2 
11 public class Camera {
12 
13  private float[] M = new float[16]; // resulted rotation matrix
14 
15  // View position, focus points and view, up and right vectors
16  // view vector pointing from view position to the focus point
17  // up vector perpendicular to the view vector and current horizontal
18  // plane
19  // right vector lies on the current horizontal plane
20  private float[] Position, Center, View, Up, Right;
21 
22  // initialize data
23  public Camera(float px, float py, float pz, float vx, float vy, float vz, float ux, float uy, float uz) {
24  Position = new float[] { px, py, pz };
25  Center = new float[] { vx, vy, vz };
26  Up = new float[] { ux, uy, uz };
27  // calculate View and Right vectors
28  View = MinusVec(Center, Position); // pointing from the looking
29  // point toward the focus point
30  Right = NormVec(CrossVec(Up, View)); // right vector perpendicular
31  // to the the up and view
32  // vectors
33  }
34 
35  public float[] getRotationMatrix() {
36  return M;
37  }
38 
39  // calculate the rotation matrix from the current information
40  // equivalent to gluLookat
41  private void cal_M() {
42  float[] s, u, f;
43  // Calculate M for gluLookat
44  f = NormVec(MinusVec(Center, Position));
45  s = CrossVec(f, NormVec(Up));
46  u = CrossVec(s, f);
47  // Get transformation Matrix
48  M[0] = s[0];
49  M[4] = s[1];
50  M[8] = s[2];
51  M[12] = 0.0f;
52  M[1] = u[0];
53  M[5] = u[1];
54  M[9] = u[2];
55  M[13] = 0.0f;
56  M[2] = -f[0];
57  M[6] = -f[1];
58  M[10] = -f[2];
59  M[14] = 0.0f;
60  M[3] = 0.0f;
61  M[7] = 0.0f;
62  M[11] = 0.0f;
63  M[15] = 1.0f;
64  }
65 
66  // return cross product of two vectors
67  private float[] CrossVec(float[] A, float[] B) {
68  float[] C = new float[3];
69  C[0] = A[1] * B[2] - A[2] * B[1];
70  C[1] = A[2] * B[0] - A[0] * B[2];
71  C[2] = A[0] * B[1] - A[1] * B[0];
72  return C;
73  }
74 
75  // return the subtraction from two vectors
76  private float[] MinusVec(float[] A, float[] B) {
77  float[] C = new float[3];
78  C[0] = A[0] - B[0];
79  C[1] = A[1] - B[1];
80  C[2] = A[2] - B[2];
81  return C;
82  }
83 
84  // quaternion multiplication
85  private float[] MultQuat(float[] A, float[] B) {
86  float[] C = new float[4];
87  C[0] = A[3] * B[0] + A[0] * B[3] + A[1] * B[2] - A[2] * B[1];
88  C[1] = A[3] * B[1] - A[0] * B[2] + A[1] * B[3] + A[2] * B[0];
89  C[2] = A[3] * B[2] + A[0] * B[1] - A[1] * B[0] + A[2] * B[3];
90  C[3] = A[3] * B[3] - A[0] * B[0] - A[1] * B[1] - A[2] * B[2];
91  return C;
92  }
93 
94  // normalize a vector
95  private float[] NormVec(float[] A) {
96  float[] C = new float[3];
97  float length = (float) Math.sqrt(A[0] * A[0] + A[1] * A[1] + A[2] * A[2]);
98  C[0] = A[0] / length;
99  C[1] = A[1] / length;
100  C[2] = A[2] / length;
101  return C;
102  }
103 
104  // rotate current view vector an "angle" degree about an arbitrary
105  // vector [x,y,z]
106  private void RotateCamera(float angle, float x, float y, float z) {
107  float[] temp = new float[4];
108  float[] conjtemp = new float[4];
109  float[] quat_view = new float[4];
110  float[] result = new float[4];
111  // temp is the rotation quaternion
112  float deg = (float) ((angle / 180f) * Math.PI);
113  float sinhtheta = (float) Math.sin(deg / 2f);
114  temp[0] = x * sinhtheta;
115  temp[1] = y * sinhtheta;
116  temp[2] = z * sinhtheta;
117  temp[3] = (float) Math.cos(deg / 2f);
118  // the conjugate rotation quaternion
119  conjtemp[0] = -temp[0];
120  conjtemp[1] = -temp[1];
121  conjtemp[2] = -temp[2];
122  conjtemp[3] = temp[3];
123  // convert view vector into quaternion
124  quat_view[0] = View[0];
125  quat_view[1] = View[1];
126  quat_view[2] = View[2];
127  quat_view[3] = 0.0f;
128  // rotate by quaternion temp'*quat_view*temp
129  result = MultQuat(MultQuat(temp, quat_view), conjtemp);
130  // retrieve the new view vector from the resulted quaternion
131  View[0] = result[0];
132  View[1] = result[1];
133  View[2] = result[2];
134  }
135 
136  // calculate the new position if we rotate rot1 degree about the current
137  // up vectors (yaw)
138  // and rot2 degree about the right vector (pitch)
139  public void SetRotation(float rot1, float rot2) {
140  // rotate rot1 about Up
141  RotateCamera(rot1, Up[0], Up[1], Up[2]);
142  // recalculae Right
143  Right = NormVec(CrossVec(Up, View));
144  // rotate rot2 about Right
145  RotateCamera(rot2, Right[0], Right[1], Right[2]);
146  // recalculate Up
147  Up = NormVec(CrossVec(View, Right));
148  // recalculate Position
149  Position = MinusVec(Center, View);
150  // Get transformation matrix
151  cal_M();
152  }
153 }
Camera(float px, float py, float pz, float vx, float vy, float vz, float ux, float uy, float uz)
Definition: Camera.java:23
float[] getRotationMatrix()
Definition: Camera.java:35
Extracted the Camera class from GLRenderer.
Definition: Camera.java:11
void SetRotation(float rot1, float rot2)
Definition: Camera.java:139