-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.java
More file actions
127 lines (105 loc) · 3.38 KB
/
Camera.java
File metadata and controls
127 lines (105 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import gmaths.*;
import java.awt.event.*;
import com.jogamp.opengl.awt.GLCanvas;
public class Camera {
public enum CameraType {X, Z};
public enum Movement {NO_MOVEMENT, LEFT, RIGHT, UP, DOWN, FORWARD, BACK};
private static final float DEFAULT_RADIUS = 25;
public static final Vec3 DEFAULT_POSITION = new Vec3(0,0,25);
public static final Vec3 DEFAULT_POSITION_2 = new Vec3(25,0,0);
public static final Vec3 DEFAULT_TARGET = new Vec3(0,0,0);
public static final Vec3 DEFAULT_UP = new Vec3(0,1,0);
public final float YAW = -90f;
public final float PITCH = 0f;
public final float KEYBOARD_SPEED = 0.2f;
public final float MOUSE_SPEED = 1.0f;
private Vec3 position;
private Vec3 target;
private Vec3 up;
private Vec3 worldUp;
private Vec3 front;
private Vec3 right;
private float yaw;
private float pitch;
private Mat4 perspective;
public Camera(Vec3 position, Vec3 target, Vec3 up) {
setupCamera(position, target, up);
}
private void setupCamera(Vec3 position, Vec3 target, Vec3 up) {
this.position = new Vec3(position);
this.target = new Vec3(target);
this.up = new Vec3(up);
front = Vec3.subtract(target, position);
front.normalize();
up.normalize();
calculateYawPitch(front);
worldUp = new Vec3(up);
updateCameraVectors();
}
public Vec3 getPosition() {
return new Vec3(position);
}
public void setPosition(Vec3 p) {
setupCamera(p, target, up);
}
public void setTarget(Vec3 t) {
setupCamera(position, t, up);
}
public void setCamera(CameraType c) {
switch (c) {
case X : setupCamera(DEFAULT_POSITION, DEFAULT_TARGET, DEFAULT_UP) ; break;
case Z : setupCamera(DEFAULT_POSITION_2, DEFAULT_TARGET, DEFAULT_UP); break;
}
}
private void calculateYawPitch(Vec3 v) {
yaw = (float)Math.atan2(v.z,v.x);
pitch = (float)Math.asin(v.y);
}
public Mat4 getViewMatrix() {
target = Vec3.add(position, front);
return Mat4Transform.lookAt(position, target, up);
}
public void setPerspectiveMatrix(Mat4 m) {
perspective = m;
}
public Mat4 getPerspectiveMatrix() {
return perspective;
}
public void keyboardInput(Movement movement) {
switch (movement) {
case NO_MOVEMENT: break;
case LEFT: position.add(Vec3.multiply(right, -KEYBOARD_SPEED)); break;
case RIGHT: position.add(Vec3.multiply(right, KEYBOARD_SPEED)); break;
case UP: position.add(Vec3.multiply(up, KEYBOARD_SPEED)); break;
case DOWN: position.add(Vec3.multiply(up, -KEYBOARD_SPEED)); break;
case FORWARD: position.add(Vec3.multiply(front, KEYBOARD_SPEED)); break;
case BACK: position.add(Vec3.multiply(front, -KEYBOARD_SPEED)); break;
}
}
public void updateYawPitch(float y, float p) {
yaw += y;
pitch += p;
if (pitch > 89) pitch = 89;
else if (pitch < -89) pitch = -89;
updateFront();
updateCameraVectors();
}
private void updateFront() {
double cy, cp, sy, sp;
cy = Math.cos(yaw);
sy = Math.sin(yaw);
cp = Math.cos(pitch);
sp = Math.sin(pitch);
front.x = (float)(cy*cp);
front.y = (float)(sp);
front.z = (float)(sy*cp);
front.normalize();
target = Vec3.add(position,front);
}
private void updateCameraVectors() {
right = Vec3.crossProduct(front, worldUp);
right.normalize();
up = Vec3.crossProduct(right, front);
up.normalize();
}
}