summaryrefslogtreecommitdiff
path: root/src/extend/camera.c
blob: dd77172728ee12a77ce7031709f27b49ccd72364 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "../shaders/common.h"
#include "../core/device.h"
#include "camera.h"

typedef enum {
	CursorType_Arrow = 0,
	CursorType_Hand = 1,
	CursorType_Eye = 2
}CursorType;

// A unity editor style camera
typedef struct Camera {
	Transform transform;
	float fov, aspect, near, far;
	/*matrix*/
	Mat4 cached_view_matrix; /*or WorldToCameraMatrix*/
	Mat4 cached_proj_matrix;
	bool is_viewdirty, is_projdirty;
	/*operations*/
	float zoom_speed;
	Vec2 rotate_sensitivity;
	Vec2 move_sensitivity;
	Euler euler;
	bool speedup;
	float speedupv;
	/*events*/
	bool look_around;
	Vec2 mouse_prev;
	bool move_around;
	/*window*/
	wog_Window* wnd;
	CursorType cursor;
} Camera;

Camera* camera_create(wog_Window* wnd, CameraConfig* config) {
	Camera* cam = ssrM_new(Camera);
	
	cam->is_viewdirty = cam->is_projdirty = TRUE;

	// camera local = world
	cam->transform.parent = NULL ;
	cam->transform.localposition = config->position;
	cam->transform.localscale = vec3_make(1, 1, 1);
	quat_fromeuler(&config->euler, &cam->transform.localrotation);

	cam->near = config->near; 
	cam->far = config->far;
	cam->aspect = config->aspect;
	cam->fov = config->fov;
	cam->zoom_speed = config->zoom_speed;
	cam->speedup = FALSE;
	cam->speedupv = 10;

	cam->look_around = FALSE;
	cam->move_around = FALSE;
	cam->rotate_sensitivity = config->rotate_sensitivity;
	cam->move_sensitivity = config->move_sensitivity;
	cam->euler = config->euler;
	cam->wnd = wnd;

	return cam;
}

void camera_destroy(Camera* cam) {
	if(cam == NULL) return ;
	ssrM_free(cam);
}

void camera_getmatrix(Camera* cam, Mat4* view, Mat4* proj) {
	camera_getviewmatrix(cam, view);
	camera_getprojmatrix(cam, proj);
}

void camera_getviewmatrix(Camera* cam, Mat4* out) {
	if (!cam->is_viewdirty) {
		if (out)*out = cam->cached_view_matrix;
		return;
	}
	transform_getinvmatrixnoscale(&cam->transform, &cam->cached_view_matrix);
	if(out) *out = cam->cached_view_matrix;
	cam->is_viewdirty = FALSE;
}

void camera_getprojmatrix(Camera* cam, Mat4* out) {
	if (!cam->is_projdirty) {
		if (out)*out = cam->cached_proj_matrix;
		return;
	}
	mat4_setperspective(cam->fov, cam->aspect, cam->near, cam->far, &cam->cached_proj_matrix);
	if(out) *out = cam->cached_proj_matrix;
	cam->is_projdirty = FALSE;
}

static void _onwheelscroll(Camera* cam, int wheel, float dt) {
	Quat rot; transform_getrotation(&cam->transform, &rot);
	Vec3 forward = {0,0,-1};
	quat_applytovec3(&rot, &forward, &forward);
	vec3_scale(&forward, cam->zoom_speed * wheel * dt * (cam->speedup ? cam->speedupv : 1), &forward);
	vec3_plus(&forward, &cam->transform.localposition, &cam->transform.localposition);
	cam->is_viewdirty = TRUE;
}

static void _onlookaround(Camera* cam,float dt) {
	if(!cam->look_around) return;
	static Quat rot;
	static Euler angle;
	int x, y; 
	wog_getMouse(cam->wnd, &x, &y);
	float dx = cam->mouse_prev.x - x, dy = y - cam->mouse_prev.y;
	angle.x = dy * cam->rotate_sensitivity.y * dt * (cam->speedup ? cam->speedupv : 1);
	angle.y = dx * cam->rotate_sensitivity.x * dt * (cam->speedup ? cam->speedupv : 1);
	cam->euler.pitch -= angle.x;
	cam->euler.yaw += angle.y;
	//printf("%f    %f\n", cam->euler.pitch, cam->euler.yaw);
	quat_fromeuler(&cam->euler, &cam->transform.localrotation);
	cam->mouse_prev.x = x; cam->mouse_prev.y = y;
	cam->is_viewdirty = TRUE;
}

static void _onmovearound(Camera* cam, float dt) {
	if(!cam->move_around) return ;	
	int x, y;
	wog_getMouse(cam->wnd, &x, &y);
	Vec3 dd = { cam->mouse_prev.x - x, y - cam->mouse_prev.y , 0};
	dd.x *= cam->move_sensitivity.x * dt * (cam->speedup ? cam->speedupv : 1);
	dd.y *= cam->move_sensitivity.y * dt * (cam->speedup ? cam->speedupv : 1);
	quat_applytovec3(&cam->transform.localrotation, &dd, &dd);
	//printf("%f   %f   %f\n", dd.x, dd.y, dd.z);
	vec3_plus(&cam->transform.localposition, &dd, &cam->transform.localposition);
	cam->mouse_prev.x = x; cam->mouse_prev.y = y;
	cam->is_viewdirty = TRUE;
}

void camera_onevent(Camera* cam, wog_Event* e, float dt) {
	if(e == NULL) return ;
	if (e->type == WOG_EMOUSEWHEEL) {//zoom in\zoom out
		_onwheelscroll(cam, e->wheel, dt);
	}
	else if (e->type == WOG_EMOUSEBUTTONDOWN) {
		if (!cam->look_around && e->button == WOG_MOUSE_RBUTTON) {
			cam->look_around = TRUE;
			cam->mouse_prev.x = e->pos.x; 
			cam->mouse_prev.y = e->pos.y;
			if (cam->cursor != CursorType_Eye)
			{
				wog_setcursorImage(cam->wnd, "OrbitView.ico");
				cam->cursor = CursorType_Eye;
			}
		}
		if (!cam->move_around && e->button == WOG_MOUSE_MIDDLE) {
			cam->move_around = TRUE; 
			cam->mouse_prev.x = e->pos.x;
			cam->mouse_prev.y = e->pos.y;
			if (cam->cursor != CursorType_Hand)
			{
				wog_setcursorImage(cam->wnd, "PanView.ico");
				cam->cursor = CursorType_Hand;
			}
		}
	}
	else if (e->type == WOG_EMOUSEBUTTONUP) {
		if (e->button == WOG_MOUSE_RBUTTON)
			cam->look_around = FALSE;
		if (e->button == WOG_MOUSE_MIDDLE)
			cam->move_around = FALSE;
		if (cam->cursor != CursorType_Arrow)
		{
			wog_setcursor(cam->wnd, IDC_ARROW);
			cam->cursor = CursorType_Arrow;
		}
	}
	else if (e->type == WOG_EKEYDOWN) {
		if (!cam->speedup && e->key == VK_SHIFT)
			cam->speedup = TRUE;
	}
	else if (e->type == WOG_EKEYUP) {
		if (e->key == VK_SHIFT)
			cam->speedup = FALSE;
	}
}

void camera_onupdate(Camera* cam, float dt) {
	if (cam->look_around)
		_onlookaround(cam, dt);
	if (cam->move_around)
		_onmovearound(cam, dt);
}

void camera_ondraw(Camera* cam) {
	/*set vp matrix*/
	ssr_matrixmode(MATRIX_PROJECTION);
	camera_getprojmatrix(cam, NULL);
	ssr_loadmatrix(&cam->cached_proj_matrix);
	ssr_matrixmode(MATRIX_VIEW);
	camera_getviewmatrix(cam, NULL);
	ssr_loadmatrix(&cam->cached_view_matrix);
	ssr_matrixmode(MATRIX_MODEL);
	/*set builtin variables*/
	_proj_params.x = cam->near;
	_proj_params.y = cam->far;
	_proj_params.z = cam->fov;
	_proj_params.w = cam->aspect;
}