source: trunk/src/camera.cpp @ 1375

Last change on this file since 1375 was 1375, checked in by touky, 9 years ago

Added some convenient functions in camera.

  • Property svn:keywords set to Id
File size: 2.9 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
64void Camera::SetTarget(vec3 const &pos)
65{
66    m_target = pos;
67}
68
69vec3 Camera::GetTarget()
70{
71    return m_target;
72}
73
74mat4 const &Camera::GetViewMatrix()
75{
76    return m_view_matrix;
77}
78
79mat4 const &Camera::GetProjMatrix()
80{
81    return m_proj_matrix;
82}
83
84void Camera::TickGame(float seconds)
85{
86    WorldEntity::TickGame(seconds);
87
88    /* Hackish keyboard support */
89    float updown = 2.f * (Input::GetButtonState('w')
90                           + Input::GetButtonState('z')
91                           - Input::GetButtonState('s'));
92    float rightleft = 2.f * (Input::GetButtonState('d')
93                              - Input::GetButtonState('q')
94                              - Input::GetButtonState('a'));
95    float pgupdown = 2.f * (Input::GetButtonState('r')
96                             - Input::GetButtonState('f'));
97
98#if 0
99    /* Hackish stick support */
100    static Stick *stick = NULL;
101    if (!stick)
102        stick = Input::TrackStick();
103    if (stick && stick->GetAxisCount() >= 2)
104    {
105        rightleft += 2.f * stick->GetAxis(0) * std::abs(stick->GetAxis(0));
106        updown += -2.f * stick->GetAxis(1) * std::abs(stick->GetAxis(1));
107    }
108#endif
109
110    m_position += vec3(rightleft, pgupdown, -updown) * 200.f * seconds;
111    m_target += vec3(rightleft, 0, -updown) * 200.f * seconds;
112
113    m_view_matrix = mat4::lookat(m_position, m_target, m_up)
114                  * mat4(m_rotation);
115}
116
117void Camera::TickDraw(float seconds)
118{
119    WorldEntity::TickDraw(seconds);
120
121    Scene::GetDefault()->SetViewMatrix(m_view_matrix);
122    Scene::GetDefault()->SetProjMatrix(m_proj_matrix);
123}
124
125} /* namespace lol */
126
Note: See TracBrowser for help on using the repository browser.