-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.jai
143 lines (118 loc) · 5.22 KB
/
camera.jai
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
141
142
143
// TODO: Make use of the orientation quaternion. At least Quaternions provide
// this feature of easily accumulating rotations. But we don't use it yet...
#import "Basic";
#import "Math";
Camera :: struct {
// #as using base := BaseEntity.{
// VirtualUpdateOnKeys = (baseEntity : *BaseEntity, frameTime : float) {
// using cast(*Camera)baseEntity;
// if KeyPressed(KeyboardButton.RIGHT) RotateAroundUp(cast(*Camera)baseEntity, -(2.0*PI)/128.0);
// if KeyPressed(KeyboardButton.LEFT) RotateAroundUp(cast(*Camera)baseEntity, (2.0*PI)/128.0);
// if KeyPressed(KeyboardButton.UP) RotateAroundSide(cast(*Camera)baseEntity, (2.0*PI)/128.0);
// if KeyPressed(KeyboardButton.DOWN) RotateAroundSide(cast(*Camera)baseEntity, -(2.0*PI)/128.0);
// if KeyPressed(KeyboardButton.D) MoveSide(cast(*Camera)baseEntity, 1000.0*frameTime);
// if KeyPressed(KeyboardButton.A) MoveSide(cast(*Camera)baseEntity, -1000.0*frameTime);
// if KeyPressed(KeyboardButton.W) MoveForward(cast(*Camera)baseEntity, 1000.0*frameTime);
// if KeyPressed(KeyboardButton.S) MoveForward(cast(*Camera)baseEntity, -1000.0*frameTime);
// }
// };
type : Type = Camera;
center := Vector3.{ 0.0, 0.0, 0.0 };
up := Vector3.{ 0.0, 0.0, 1.0 };
forward : Vector3;
orientation : Quaternion;
fov := 90.0;
}
// FollowCamera :: struct {
// #as using camera : Camera;
// type = FollowCamera;
// // base = BaseEntity.{
// // VirtualUpdateOnKeys = null
// // };
// target : *Entity = null;
// distance := 5.0;
// }
// CreateFollowCamera :: (camera : Camera, target : *Entity, distance : float) -> FollowCamera {
// result : FollowCamera;
// result.distance = distance;
// result.position = camera.position;
// result.center = target.position;
// result.up = camera.up;
// result.fov = camera.fov;
// print("target.pos: %\n", target.position);
// result.forward = normalize(target.position - result.position);
// SetOrientation(*result);
// result.target = target;
// return result;
// }
// UpdateFollowCamera :: (using followCamera : *Entity) {
// pos = target.position + distance*(-forward);
// center = target.pos;
// }
CreateCamera :: (position : Vector3, camera : Camera) -> Entity(Camera) {
result : Entity(Camera);
result.instance = camera;
result.position = position;
result.instance.forward = normalize(result.instance.center - result.position);
SetOrientation(*result);
return result;
}
CameraInputHandler :: (camera : *BaseEntity) {
frameTime := getFrameTime();
cam := cast(*Entity(Camera))camera;
// Keyboard
if KeyPressed(KeyboardButton.RIGHT) RotateAroundUp(cam, -(2.0*PI)/128.0);
if KeyPressed(KeyboardButton.LEFT) RotateAroundUp(cam, (2.0*PI)/128.0);
if KeyPressed(KeyboardButton.UP) RotateAroundSide(cam, (2.0*PI)/128.0);
if KeyPressed(KeyboardButton.DOWN) RotateAroundSide(cam, -(2.0*PI)/128.0);
if KeyPressed(KeyboardButton.D) MoveSide(cam, 2000.0*frameTime);
if KeyPressed(KeyboardButton.A) MoveSide(cam, -2000.0*frameTime);
if KeyPressed(KeyboardButton.W) MoveForward(cam, 2000.0*frameTime);
if KeyPressed(KeyboardButton.S) MoveForward(cam, -2000.0*frameTime);
// Mouse
if MouseRightButtonDown() {
SDL_ShowCursor(0); // TODO: Should be SDL_ENABLE -> Can we fix this in SDL2 module of jai?
UpdateCameraOnMouse(cam, GetMouseDX(), GetMouseDY());
}
if MouseRightButtonUp() {
SDL_ShowCursor(1);
}
}
SetOrientation :: (using camera : *Entity) {
qUp : Quaternion;
qRight : Quaternion;
set_from_axis_and_angle(*qUp, instance.up, 0.0);
right := normalize(cross(instance.forward, instance.up));
set_from_axis_and_angle(*qRight, right, 0.0);
instance.orientation = qUp*qRight;
}
CreateView :: (using camera : Entity(Camera)) -> Matrix4 {
result := LookAt(position, instance.center, instance.up);
return result;
}
MoveSide :: (using camera : *Entity(Camera), distance : float) {
right := normalize(cross(instance.forward, instance.up));
position += distance * right;
instance.center = position + instance.forward;
}
MoveForward :: (using camera : *Entity, distance : float) {
position += distance * instance.forward;
instance.center = position + instance.forward;
}
RotateAroundUp :: (using camera : *Entity(Camera), angle : float) {
refUpQ : Quaternion;
set_from_axis_and_angle(*refUpQ, .{0.0, 0.0, 1.0}, angle);
rotate(*instance.forward, refUpQ);
rotate(*instance.up, refUpQ);
instance.center = position + instance.forward;
instance.orientation = refUpQ * instance.orientation;
}
RotateAroundSide :: (using camera : *Entity(Camera), angle : float) {
refRightQ : Quaternion;
right := normalize(cross(instance.forward, instance.up));
set_from_axis_and_angle(*refRightQ, right, angle);
rotate(*instance.up, refRightQ);
rotate(*instance.forward, refRightQ);
instance.center = position + instance.forward;
instance.orientation = refRightQ * instance.orientation;
}