PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/renderer/camera.cpp

https://bitbucket.org/vivkin/gam3b00bs/
C++ | 59 lines | 47 code | 12 blank | 0 comment | 3 complexity | 9e93228841d7f8666836c6d3f45db8ef MD5 | raw file
 1#include "camera.h"
 2
 3void camera_look_at(camera_t &camera, const D3DXVECTOR3 &position, const D3DXVECTOR3 &center, const D3DXVECTOR3 &up)
 4{
 5	D3DXMatrixLookAtLH(&camera.view, &position, &center, &up);
 6}
 7
 8void camera_perspective(camera_t &camera, float fov, float aspect, float znear, float zfar)
 9{
10	D3DXMatrixPerspectiveFovLH(&camera.projection, fov, aspect, znear, zfar);
11}
12
13void camera_update(camera_t &camera)
14{
15	if( camera.phi < 0.1f )
16		camera.phi = 0.1f;
17
18	if( camera.phi > 3.14f / 3.0f )
19		camera.phi =  3.14f / 3.0f;
20
21	camera.position.x = camera.target.x + sin(camera.angle) * camera.radius;
22	camera.position.z = camera.target.z + cos(camera.angle) * camera.radius;
23	camera.position.y = sin(camera.phi) * camera.radius;
24
25	D3DXVECTOR3 up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
26	D3DXMatrixLookAtLH(&camera.view, &camera.position, &camera.target, &up);
27	D3DXMatrixPerspectiveFovLH(&camera.projection, camera.fov, camera.aspect, camera.znear, camera.zfar);
28}
29
30void camera_rotate(camera_t &camera, float delta)
31{
32	camera.angle += delta;
33	camera_update(camera);
34}
35
36void camera_move(camera_t &camera, float dx, float dz)
37{
38	D3DXVECTOR3 fwd = camera.position - camera.target;
39	fwd.y = 0;
40	D3DXVECTOR3 side = D3DXVECTOR3(fwd.z, 0.0f, -fwd.x);
41	D3DXVec3Normalize(&fwd, &fwd);
42	D3DXVec3Normalize(&side, &side);
43
44	camera.position += D3DXVECTOR3(fwd.x * dz, 0.0f, fwd.z * dz);
45	camera.target += D3DXVECTOR3(fwd.x * dz, 0.0f, fwd.z * dz);
46
47	camera.position += D3DXVECTOR3(side.x * dx, 0.0f, side.z * dx);
48	camera.target += D3DXVECTOR3(side.x * dx, 0.0f, side.z * dx);
49
50	camera_update(camera);
51}
52
53void camera_zoom(camera_t &camera, float delta)
54{
55	camera.radius += delta;
56	if(camera.radius < 1.0f)
57		camera.radius = 1.0f;
58	camera_update(camera);
59}