source: trunk/src/camera.cpp @ 1350

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

core: add methods to set the camera's view matrix.

  • Property svn:keywords set to Id
File size: 2.7 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::SetOrtho(float width, float height, float near, float far)
49{
50    m_proj_matrix = mat4::ortho(width, height, near, far);
51}
52
53void Camera::SetPerspective(float fov, float width, float height,
54                            float near, float far)
55{
56    m_proj_matrix = mat4::perspective(fov, width, height, near, far);
57}
58
59mat4 const &Camera::GetViewMatrix()
60{
61    return m_view_matrix;
62}
63
64mat4 const &Camera::GetProjMatrix()
65{
66    return m_proj_matrix;
67}
68
69void Camera::TickGame(float seconds)
70{
71    WorldEntity::TickGame(seconds);
72
73    /* Hackish keyboard support */
74    float updown = 2.f * (Input::GetButtonState('w')
75                           + Input::GetButtonState('z')
76                           - Input::GetButtonState('s'));
77    float rightleft = 2.f * (Input::GetButtonState('d')
78                              - Input::GetButtonState('q')
79                              - Input::GetButtonState('a'));
80    float pgupdown = 2.f * (Input::GetButtonState('r')
81                             - Input::GetButtonState('f'));
82
83    /* Hackish stick support */
84    static Stick *stick = NULL;
85    if (!stick)
86        stick = Input::TrackStick();
87    if (stick && stick->GetAxisCount() >= 2)
88    {
89        rightleft += 2.f * stick->GetAxis(0) * std::abs(stick->GetAxis(0));
90        updown += -2.f * stick->GetAxis(1) * std::abs(stick->GetAxis(1));
91    }
92
93    m_position += vec3(rightleft, pgupdown, -updown) * 200.f * seconds;
94    m_target += vec3(rightleft, 0, -updown) * 200.f * seconds;
95
96    m_view_matrix = mat4::lookat(m_position, m_target, m_up);
97}
98
99void Camera::TickDraw(float seconds)
100{
101    WorldEntity::TickDraw(seconds);
102
103    Scene::GetDefault()->SetViewMatrix(m_view_matrix);
104    Scene::GetDefault()->SetProjMatrix(m_proj_matrix);
105}
106
107} /* namespace lol */
108
Note: See TracBrowser for help on using the repository browser.