source: trunk/src/camera.cpp @ 2216

Last change on this file since 2216 was 2216, checked in by touky, 7 years ago

New year copyright update.

  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 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://www.wtfpl.net/ 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#   undef near
23#   undef far
24#endif
25
26namespace lol
27{
28
29Camera::Camera(vec3 const &position, vec3 const &target, vec3 const &up)
30  : m_target(target),
31    m_up(up)
32{
33    m_gamegroup = GAMEGROUP_BEFORE;
34    m_drawgroup = DRAWGROUP_CAMERA;
35
36    /* Create a default perspective */
37    SetPerspective(45.f, 800.f, 600.f, -1000.f, 1000.f);
38    SetPosition(position);
39}
40
41Camera::~Camera()
42{
43}
44
45void Camera::SetPosition(vec3 const &pos)
46{
47    m_position = pos;
48}
49
50void Camera::SetRotation(quat const &rot)
51{
52    m_rotation = rot;
53}
54
55void Camera::SetOrtho(float width, float height, float near, float far)
56{
57    m_proj_matrix = mat4::ortho(width, height, near, far);
58}
59
60void Camera::SetPerspective(float fov, float width, float height,
61                            float near, float far)
62{
63    m_proj_matrix = mat4::perspective(fov, width, height, near, far);
64}
65
66void Camera::SetTarget(vec3 const &pos)
67{
68    m_target = pos;
69}
70
71vec3 Camera::GetTarget()
72{
73    return m_target;
74}
75vec3 Camera::GetPosition()
76{
77    return m_position;
78}
79
80mat4 const &Camera::GetViewMatrix()
81{
82    return m_view_matrix;
83}
84
85mat4 const &Camera::GetProjMatrix()
86{
87    return m_proj_matrix;
88}
89
90void Camera::TickGame(float seconds)
91{
92    WorldEntity::TickGame(seconds);
93
94#if 0
95    /* Hackish keyboard support */
96    float updown = 2.f * (Input::GetButtonState('w')
97                           + Input::GetButtonState('z')
98                           - Input::GetButtonState('s'));
99    float rightleft = 2.f * (Input::GetButtonState('d')
100                              - Input::GetButtonState('q')
101                              - Input::GetButtonState('a'));
102    float pgupdown = 2.f * (Input::GetButtonState('r')
103                             - Input::GetButtonState('f'));
104
105    /* Hackish stick support */
106    static Stick *stick = NULL;
107    if (!stick)
108        stick = Input::TrackStick();
109    if (stick && stick->GetAxisCount() >= 2)
110    {
111        rightleft += 2.f * stick->GetAxis(0) * std::abs(stick->GetAxis(0));
112        updown += -2.f * stick->GetAxis(1) * std::abs(stick->GetAxis(1));
113    }
114
115    m_position += vec3(rightleft, pgupdown, -updown) * 200.f * seconds;
116    m_target += vec3(rightleft, 0, -updown) * 200.f * seconds;
117
118#endif
119    m_view_matrix = mat4::lookat(m_position, m_target, m_up)
120                  * mat4(m_rotation);
121}
122
123void Camera::TickDraw(float seconds)
124{
125    WorldEntity::TickDraw(seconds);
126
127    Scene::GetDefault()->SetViewMatrix(m_view_matrix);
128    Scene::GetDefault()->SetProjMatrix(m_proj_matrix);
129}
130
131} /* namespace lol */
132
Note: See TracBrowser for help on using the repository browser.