source: trunk/src/camera.cpp @ 1353

Last change on this file since 1353 was 1353, checked in by sam, 8 years ago

orbital: play with the camera.

  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstring>
16#include <cstdlib>
17
18#include "core.h"
19
20#if defined _WIN32 || defined _XBOX
21#   define strcasecmp _stricmp
22#endif
23
24namespace lol
25{
26
27Camera::Camera(vec3 const &position, vec3 const &target, vec3 const &up)
28  : m_target(target),
29    m_up(up)
30{
31    m_gamegroup = GAMEGROUP_BEFORE;
32    m_drawgroup = DRAWGROUP_CAMERA;
33
34    /* Create a default perspective */
35    SetPerspective(45.f, 800.f, 600.f, -1000.f, 1000.f);
36    SetPosition(position);
37}
38
39Camera::~Camera()
40{
41}
42
43void Camera::SetPosition(vec3 const &pos)
44{
45    m_position = pos;
46}
47
48void Camera::SetRotation(quat const &rot)
49{
50    m_rotation = rot;
51}
52
53void Camera::SetOrtho(float width, float height, float near, float far)
54{
55    m_proj_matrix = mat4::ortho(width, height, near, far);
56}
57
58void Camera::SetPerspective(float fov, float width, float height,
59                            float near, float far)
60{
61    m_proj_matrix = mat4::perspective(fov, width, height, near, far);
62}
63
64mat4 const &Camera::GetViewMatrix()
65{
66    return m_view_matrix;
67}
68
69mat4 const &Camera::GetProjMatrix()
70{
71    return m_proj_matrix;
72}
73
74void Camera::TickGame(float seconds)
75{
76    WorldEntity::TickGame(seconds);
77
78    /* Hackish keyboard support */
79    float updown = 2.f * (Input::GetButtonState('w')
80                           + Input::GetButtonState('z')
81                           - Input::GetButtonState('s'));
82    float rightleft = 2.f * (Input::GetButtonState('d')
83                              - Input::GetButtonState('q')
84                              - Input::GetButtonState('a'));
85    float pgupdown = 2.f * (Input::GetButtonState('r')
86                             - Input::GetButtonState('f'));
87
88    /* Hackish stick support */
89    static Stick *stick = NULL;
90    if (!stick)
91        stick = Input::TrackStick();
92    if (stick && stick->GetAxisCount() >= 2)
93    {
94        rightleft += 2.f * stick->GetAxis(0) * std::abs(stick->GetAxis(0));
95        updown += -2.f * stick->GetAxis(1) * std::abs(stick->GetAxis(1));
96    }
97
98    m_position += vec3(rightleft, pgupdown, -updown) * 200.f * seconds;
99    m_target += vec3(rightleft, 0, -updown) * 200.f * seconds;
100
101    m_view_matrix = mat4::lookat(m_position, m_target, m_up)
102                  * mat4(m_rotation);
103}
104
105void Camera::TickDraw(float seconds)
106{
107    WorldEntity::TickDraw(seconds);
108
109    Scene::GetDefault()->SetViewMatrix(m_view_matrix);
110    Scene::GetDefault()->SetProjMatrix(m_proj_matrix);
111}
112
113} /* namespace lol */
114
Note: See TracBrowser for help on using the repository browser.