]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/camera.h
Camera: Fix shooting line offsets (#9681)
[dragonfireclient.git] / src / client / camera.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include "irrlichttypes_extrabloated.h"
23 #include "inventory.h"
24 #include "client/tile.h"
25 #include <ICameraSceneNode.h>
26 #include <ISceneNode.h>
27 #include <list>
28
29 class LocalPlayer;
30 struct MapDrawControl;
31 class Client;
32 class WieldMeshSceneNode;
33
34 struct Nametag {
35         Nametag(scene::ISceneNode *a_parent_node,
36                         const std::string &a_nametag_text,
37                         const video::SColor &a_nametag_color,
38                         const v3f &a_nametag_pos):
39                 parent_node(a_parent_node),
40                 nametag_text(a_nametag_text),
41                 nametag_color(a_nametag_color),
42                 nametag_pos(a_nametag_pos)
43         {
44         }
45         scene::ISceneNode *parent_node;
46         std::string nametag_text;
47         video::SColor nametag_color;
48         v3f nametag_pos;
49 };
50
51 enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
52
53 /*
54         Client camera class, manages the player and camera scene nodes, the viewing distance
55         and performs view bobbing etc. It also displays the wielded tool in front of the
56         first-person camera.
57 */
58 class Camera
59 {
60 public:
61         Camera(MapDrawControl &draw_control, Client *client);
62         ~Camera();
63
64         // Get camera scene node.
65         // It has the eye transformation, pitch and view bobbing applied.
66         inline scene::ICameraSceneNode* getCameraNode() const
67         {
68                 return m_cameranode;
69         }
70
71         // Get the camera position (in absolute scene coordinates).
72         // This has view bobbing applied.
73         inline v3f getPosition() const
74         {
75                 return m_camera_position;
76         }
77
78         // Returns the absolute position of the head SceneNode in the world
79         inline v3f getHeadPosition() const
80         {
81                 return m_headnode->getAbsolutePosition();
82         }
83
84         // Get the camera direction (in absolute camera coordinates).
85         // This has view bobbing applied.
86         inline v3f getDirection() const
87         {
88                 return m_camera_direction;
89         }
90
91         // Get the camera offset
92         inline v3s16 getOffset() const
93         {
94                 return m_camera_offset;
95         }
96
97         // Horizontal field of view
98         inline f32 getFovX() const
99         {
100                 return m_fov_x;
101         }
102
103         // Vertical field of view
104         inline f32 getFovY() const
105         {
106                 return m_fov_y;
107         }
108
109         // Get maximum of getFovX() and getFovY()
110         inline f32 getFovMax() const
111         {
112                 return MYMAX(m_fov_x, m_fov_y);
113         }
114
115         // Checks if the constructor was able to create the scene nodes
116         bool successfullyCreated(std::string &error_message);
117
118         // Step the camera: updates the viewing range and view bobbing.
119         void step(f32 dtime);
120
121         // Update the camera from the local player's position.
122         // busytime is used to adjust the viewing range.
123         void update(LocalPlayer* player, f32 frametime, f32 busytime,
124                         f32 tool_reload_ratio);
125
126         // Update render distance
127         void updateViewingRange();
128
129         // Start digging animation
130         // Pass 0 for left click, 1 for right click
131         void setDigging(s32 button);
132
133         // Replace the wielded item mesh
134         void wield(const ItemStack &item);
135
136         // Draw the wielded tool.
137         // This has to happen *after* the main scene is drawn.
138         // Warning: This clears the Z buffer.
139         void drawWieldedTool(irr::core::matrix4* translation=NULL);
140
141         // Toggle the current camera mode
142         void toggleCameraMode() {
143                 if (m_camera_mode == CAMERA_MODE_FIRST)
144                         m_camera_mode = CAMERA_MODE_THIRD;
145                 else if (m_camera_mode == CAMERA_MODE_THIRD)
146                         m_camera_mode = CAMERA_MODE_THIRD_FRONT;
147                 else
148                         m_camera_mode = CAMERA_MODE_FIRST;
149         }
150
151         // Set the current camera mode
152         inline void setCameraMode(CameraMode mode)
153         {
154                 m_camera_mode = mode;
155         }
156
157         //read the current camera mode
158         inline CameraMode getCameraMode()
159         {
160                 return m_camera_mode;
161         }
162
163         Nametag *addNametag(scene::ISceneNode *parent_node,
164                 const std::string &nametag_text, video::SColor nametag_color,
165                 const v3f &pos);
166
167         void removeNametag(Nametag *nametag);
168
169         const std::list<Nametag *> &getNametags() { return m_nametags; }
170
171         void drawNametags();
172
173         inline void addArmInertia(f32 player_yaw);
174
175 private:
176         // Nodes
177         scene::ISceneNode *m_playernode = nullptr;
178         scene::ISceneNode *m_headnode = nullptr;
179         scene::ICameraSceneNode *m_cameranode = nullptr;
180
181         scene::ISceneManager *m_wieldmgr = nullptr;
182         WieldMeshSceneNode *m_wieldnode = nullptr;
183
184         // draw control
185         MapDrawControl& m_draw_control;
186
187         Client *m_client;
188
189         // Absolute camera position
190         v3f m_camera_position;
191         // Absolute camera direction
192         v3f m_camera_direction;
193         // Camera offset
194         v3s16 m_camera_offset;
195
196         v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
197         v2f m_arm_dir;
198         v2f m_cam_vel;
199         v2f m_cam_vel_old;
200         v2f m_last_cam_pos;
201
202         // Field of view and aspect ratio stuff
203         f32 m_aspect = 1.0f;
204         f32 m_fov_x = 1.0f;
205         f32 m_fov_y = 1.0f;
206
207         // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
208         f32 m_view_bobbing_anim = 0.0f;
209         // If 0, view bobbing is off (e.g. player is standing).
210         // If 1, view bobbing is on (player is walking).
211         // If 2, view bobbing is getting switched off.
212         s32 m_view_bobbing_state = 0;
213         // Speed of view bobbing animation
214         f32 m_view_bobbing_speed = 0.0f;
215         // Fall view bobbing
216         f32 m_view_bobbing_fall = 0.0f;
217
218         // Digging animation frame (0 <= m_digging_anim < 1)
219         f32 m_digging_anim = 0.0f;
220         // If -1, no digging animation
221         // If 0, left-click digging animation
222         // If 1, right-click digging animation
223         s32 m_digging_button = -1;
224
225         // Animation when changing wielded item
226         f32 m_wield_change_timer = 0.125f;
227         ItemStack m_wield_item_next;
228
229         CameraMode m_camera_mode = CAMERA_MODE_FIRST;
230
231         f32 m_cache_fall_bobbing_amount;
232         f32 m_cache_view_bobbing_amount;
233         f32 m_cache_fov;
234         bool m_arm_inertia;
235
236         std::list<Nametag *> m_nametags;
237 };