-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.java
More file actions
140 lines (108 loc) · 4.43 KB
/
Copy pathCamera.java
File metadata and controls
140 lines (108 loc) · 4.43 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
128
129
130
131
132
133
134
135
136
137
138
139
140
package org.opengame.engine.camera;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.java.Log;
import org.joml.Matrix4f;
import org.joml.Matrix4x3f;
import org.joml.Vector3f;
import org.joml.Vector4f;
import org.lwjgl.system.MemoryUtil;
import org.opengame.engine.Engine;
import org.opengame.engine.object.MaterialObject;
import org.opengame.engine.render.CameraUtils;
import java.nio.FloatBuffer;
import static org.lwjgl.bgfx.BGFX.bgfx_set_view_transform;
/**
* Base camera class
*/
@Log
public class Camera extends MaterialObject {
private final Matrix4x3f view = new Matrix4x3f();
private final FloatBuffer viewBuffer;
private final Matrix4f projection = new Matrix4f();
private final FloatBuffer projectionBuffer;
protected Vector3f direction;
protected Vector3f right;
protected Vector3f left;
protected Vector3f up;
private float nearPlane = 0.1f;
private float farPlane = 100f;
@Setter
@Getter
private boolean debugMode = false;
public Camera() {
viewBuffer = MemoryUtil.memAllocFloat(16);
projectionBuffer = MemoryUtil.memAllocFloat(16);
direction = new Vector3f(0, 0, -1);
right = new Vector3f(1, 0, 0);
left = new Vector3f(1, 0, 0);
up = new Vector3f(0, 1, 0);
setPerspective(35, Engine.getScreenWidth(), Engine.getScreenHeight(),
nearPlane, nearPlane);
}
public void setPerspective(float fov, int screenWidth, int screenHeight, float nearPlane, float farPlane) {
CameraUtils.perspective(fov, screenWidth, screenHeight, nearPlane, farPlane, projection);
setViewProjection();
}
public void setViewProjection() {
view.identity();
var eye = new Vector3f(getPosition());
eye.add(direction);
CameraUtils.lookAt(getPosition(), eye, view);
bgfx_set_view_transform(0, view.get4x4(viewBuffer), projection.get(projectionBuffer));
if (debugMode) {
log.info("Current pos: " + getPosition());
log.info("Current rotation: " + getRotation());
}
}
@Override
public void setPosition(Vector3f position) {
super.setPosition(position);
setViewProjection();
}
@Override
public void setRotation(Vector3f rotation) {
super.setRotation(rotation);
rotateLeftRight(rotation.x);
rotateUpDown(rotation.y);
setViewProjection();
}
protected void rotateUpDown(float delta) {
var normalizedDirection = direction.normalize();
var directionNoY = new Vector3f(normalizedDirection.x, 0, normalizedDirection.z).normalize();
var currentAngleDegrees = Math.toDegrees(Math.acos(directionNoY.dot(direction)));
if (normalizedDirection.y < 0.0f) {
currentAngleDegrees = -currentAngleDegrees;
}
var newAngleDegrees = currentAngleDegrees + delta;
if (newAngleDegrees < -85.0f || newAngleDegrees > 85.0f) return;
var rotationAxis = new Vector3f(normalizedDirection).cross(up).normalize();
var rotationMatrix = new Matrix4f().rotate((float) Math.toRadians(delta), rotationAxis);
var rotatedDirection = new Vector4f(normalizedDirection, 0).mul(rotationMatrix).normalize();
direction = new Vector3f(rotatedDirection.x, rotatedDirection.y, rotatedDirection.z);
}
protected void rotateLeftRight(float delta) {
var normalizedDirection = direction.normalize();
var rotationMatrix = new Matrix4f().rotate((float) Math.toRadians(delta), up);
var rotatedDirection = new Vector4f(normalizedDirection, 0).mul(rotationMatrix);
direction = new Vector3f(rotatedDirection.x, rotatedDirection.y, rotatedDirection.z);
var rotatedRight = new Vector4f(right.normalize(), 0).mul(rotationMatrix);
right = new Vector3f(rotatedRight.x, rotatedRight.y, rotatedRight.z);
}
public void moveForward(float offset) {
getPosition().add(direction.x * offset, direction.y * offset, direction.z * offset);
setViewProjection();
}
public void moveRight(float offset) {
getPosition().add(right.x * offset, right.y * offset, right.z * offset);
setViewProjection();
}
public void moveUp(float offset) {
getPosition().add(up.x * offset, up.y * offset, up.z * offset);
setViewProjection();
}
public void dispose() {
MemoryUtil.memFree(viewBuffer);
MemoryUtil.memFree(projectionBuffer);
}
}