camera.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 3.0 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "camera.h"
  17. #include "debug.h"
  18. #include "client.h"
  19. #include "map.h"
  20. #include "clientmap.h" // MapDrawControl
  21. #include "player.h"
  22. #include <cmath>
  23. #include "settings.h"
  24. #include "wieldmesh.h"
  25. #include "noise.h" // easeCurve
  26. #include "sound.h"
  27. #include "event.h"
  28. #include "profiler.h"
  29. #include "util/numeric.h"
  30. #include "constants.h"
  31. #include "fontengine.h"
  32. #include "script/scripting_client.h"
  33. #define CAMERA_OFFSET_STEP 200
  34. #include "nodedef.h"
  35. Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,
  36. Client *client):
  37. m_playernode(NULL),
  38. m_headnode(NULL),
  39. m_cameranode(NULL),
  40. m_wieldmgr(NULL),
  41. m_wieldnode(NULL),
  42. m_draw_control(draw_control),
  43. m_client(client),
  44. m_camera_position(0,0,0),
  45. m_camera_direction(0,0,0),
  46. m_camera_offset(0,0,0),
  47. m_aspect(1.0),
  48. m_fov_x(1.0),
  49. m_fov_y(1.0),
  50. m_view_bobbing_anim(0),
  51. m_view_bobbing_state(0),
  52. m_view_bobbing_speed(0),
  53. m_view_bobbing_fall(0),
  54. m_digging_anim(0),
  55. m_digging_button(-1),
  56. m_wield_change_timer(0.125),
  57. m_wield_item_next(),
  58. m_camera_mode(CAMERA_MODE_FIRST)
  59. {
  60. //dstream<<FUNCTION_NAME<<std::endl;
  61. m_driver = smgr->getVideoDriver();
  62. // note: making the camera node a child of the player node
  63. // would lead to unexpected behaviour, so we don't do that.
  64. m_playernode = smgr->addEmptySceneNode(smgr->getRootSceneNode());
  65. m_headnode = smgr->addEmptySceneNode(m_playernode);
  66. m_cameranode = smgr->addCameraSceneNode(smgr->getRootSceneNode());
  67. m_cameranode->bindTargetAndRotation(true);
  68. // This needs to be in its own scene manager. It is drawn after
  69. // all other 3D scene nodes and before the GUI.
  70. m_wieldmgr = smgr->createNewSceneManager();
  71. m_wieldmgr->addCameraSceneNode();
  72. m_wieldnode = new WieldMeshSceneNode(m_wieldmgr->getRootSceneNode(), m_wieldmgr, -1, false);
  73. m_wieldnode->setItem(ItemStack(), m_client);
  74. m_wieldnode->drop(); // m_wieldmgr grabbed it
  75. /* TODO: Add a callback function so these can be updated when a setting
  76. * changes. At this point in time it doesn't matter (e.g. /set
  77. * is documented to change server settings only)
  78. *
  79. * TODO: Local caching of settings is not optimal and should at some stage
  80. * be updated to use a global settings object for getting thse values
  81. * (as opposed to the this local caching). This can be addressed in
  82. * a later release.
  83. */
  84. m_cache_fall_bobbing_amount = g_settings->getFloat("fall_bobbing_amount");
  85. m_cache_view_bobbing_amount = g_settings->getFloat("view_bobbing_amount");
  86. m_cache_fov = g_settings->getFloat("fov");
  87. m_cache_zoom_fov = g_settings->getFloat("zoom_fov");
  88. m_nametags.clear();
  89. }
  90. Camera::~Camera()
  91. {
  92. m_wieldmgr->drop();
  93. }
  94. bool Camera::successfullyCreated(std::string &error_message)
  95. {
  96. if (!m_playernode) {
  97. error_message = "Failed to create the player scene node";
  98. } else if (!m_headnode) {
  99. error_message = "Failed to create the head scene node";
  100. } else if (!m_cameranode) {
  101. error_message = "Failed to create the camera scene node";
  102. } else if (!m_wieldmgr) {
  103. error_message = "Failed to create the wielded item scene manager";
  104. } else if (!m_wieldnode) {
  105. error_message = "Failed to create the wielded item scene node";
  106. } else {
  107. error_message.clear();
  108. }
  109. #ifndef DISABLE_CSM
  110. if (g_settings->getBool("enable_client_modding")) {
  111. m_client->getScript()->on_camera_ready(this);
  112. }
  113. #endif
  114. return error_message.empty();
  115. }
  116. // Returns the fractional part of x
  117. inline f32 my_modf(f32 x)
  118. {
  119. double dummy;
  120. return modf(x, &dummy);
  121. }
  122. void Camera::step(f32 dtime)
  123. {
  124. if(m_view_bobbing_fall > 0)
  125. {
  126. m_view_bobbing_fall -= 3 * dtime;
  127. if(m_view_bobbing_fall <= 0)
  128. m_view_bobbing_fall = -1; // Mark the effect as finished
  129. }
  130. bool was_under_zero = m_wield_change_timer < 0;
  131. m_wield_change_timer = MYMIN(m_wield_change_timer + dtime, 0.125);
  132. if (m_wield_change_timer >= 0 && was_under_zero)
  133. m_wieldnode->setItem(m_wield_item_next, m_client);
  134. if (m_view_bobbing_state != 0)
  135. {
  136. //f32 offset = dtime * m_view_bobbing_speed * 0.035;
  137. f32 offset = dtime * m_view_bobbing_speed * 0.030;
  138. if (m_view_bobbing_state == 2) {
  139. // Animation is getting turned off
  140. if (m_view_bobbing_anim < 0.25) {
  141. m_view_bobbing_anim -= offset;
  142. } else if (m_view_bobbing_anim > 0.75) {
  143. m_view_bobbing_anim += offset;
  144. }
  145. if (m_view_bobbing_anim < 0.5) {
  146. m_view_bobbing_anim += offset;
  147. if (m_view_bobbing_anim > 0.5)
  148. m_view_bobbing_anim = 0.5;
  149. } else {
  150. m_view_bobbing_anim -= offset;
  151. if (m_view_bobbing_anim < 0.5)
  152. m_view_bobbing_anim = 0.5;
  153. }
  154. if (m_view_bobbing_anim <= 0 || m_view_bobbing_anim >= 1 ||
  155. fabs(m_view_bobbing_anim - 0.5) < 0.01) {
  156. m_view_bobbing_anim = 0;
  157. m_view_bobbing_state = 0;
  158. }
  159. }
  160. else {
  161. float was = m_view_bobbing_anim;
  162. m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
  163. bool step = (was == 0 ||
  164. (was < 0.5f && m_view_bobbing_anim >= 0.5f) ||
  165. (was > 0.5f && m_view_bobbing_anim <= 0.5f));
  166. if(step) {
  167. MtEvent *e = new SimpleTriggerEvent("ViewBobbingStep");
  168. m_client->event()->put(e);
  169. }
  170. }
  171. }
  172. if (m_digging_button != -1)
  173. {
  174. f32 offset = dtime * 3.5;
  175. float m_digging_anim_was = m_digging_anim;
  176. m_digging_anim += offset;
  177. if (m_digging_anim >= 1)
  178. {
  179. m_digging_anim = 0;
  180. m_digging_button = -1;
  181. }
  182. float lim = 0.15;
  183. if(m_digging_anim_was < lim && m_digging_anim >= lim)
  184. {
  185. if(m_digging_button == 0)
  186. {
  187. MtEvent *e = new SimpleTriggerEvent("CameraPunchLeft");
  188. m_client->event()->put(e);
  189. } else if(m_digging_button == 1) {
  190. MtEvent *e = new SimpleTriggerEvent("CameraPunchRight");
  191. m_client->event()->put(e);
  192. }
  193. }
  194. }
  195. }
  196. void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime,
  197. f32 tool_reload_ratio, ClientEnvironment &c_env)
  198. {
  199. // Get player position
  200. // Smooth the movement when walking up stairs
  201. v3f old_player_position = m_playernode->getPosition();
  202. v3f player_position = player->getPosition();
  203. if (player->isAttached && player->parent)
  204. player_position = player->parent->getPosition();
  205. //if(player->touching_ground && player_position.Y > old_player_position.Y)
  206. if(player->touching_ground &&
  207. player_position.Y > old_player_position.Y)
  208. {
  209. f32 oldy = old_player_position.Y;
  210. f32 newy = player_position.Y;
  211. f32 t = exp(-23*frametime);
  212. player_position.Y = oldy * t + newy * (1-t);
  213. }
  214. // Set player node transformation
  215. m_playernode->setPosition(player_position);
  216. m_playernode->setRotation(v3f(0, -1 * player->getYaw(), 0));
  217. m_playernode->updateAbsolutePosition();
  218. // Get camera tilt timer (hurt animation)
  219. float cameratilt = fabs(fabs(player->hurt_tilt_timer-0.75)-0.75);
  220. // Fall bobbing animation
  221. float fall_bobbing = 0;
  222. if(player->camera_impact >= 1 && m_camera_mode < CAMERA_MODE_THIRD)
  223. {
  224. if(m_view_bobbing_fall == -1) // Effect took place and has finished
  225. player->camera_impact = m_view_bobbing_fall = 0;
  226. else if(m_view_bobbing_fall == 0) // Initialize effect
  227. m_view_bobbing_fall = 1;
  228. // Convert 0 -> 1 to 0 -> 1 -> 0
  229. fall_bobbing = m_view_bobbing_fall < 0.5 ? m_view_bobbing_fall * 2 : -(m_view_bobbing_fall - 0.5) * 2 + 1;
  230. // Smoothen and invert the above
  231. fall_bobbing = sin(fall_bobbing * 0.5 * M_PI) * -1;
  232. // Amplify according to the intensity of the impact
  233. fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
  234. fall_bobbing *= m_cache_fall_bobbing_amount;
  235. }
  236. // Calculate players eye offset for different camera modes
  237. v3f PlayerEyeOffset = player->getEyeOffset();
  238. if (m_camera_mode == CAMERA_MODE_FIRST)
  239. PlayerEyeOffset += player->eye_offset_first;
  240. else
  241. PlayerEyeOffset += player->eye_offset_third;
  242. // Set head node transformation
  243. m_headnode->setPosition(PlayerEyeOffset+v3f(0,cameratilt*-player->hurt_tilt_strength+fall_bobbing,0));
  244. m_headnode->setRotation(v3f(player->getPitch(), 0, cameratilt*player->hurt_tilt_strength));
  245. m_headnode->updateAbsolutePosition();
  246. // Compute relative camera position and target
  247. v3f rel_cam_pos = v3f(0,0,0);
  248. v3f rel_cam_target = v3f(0,0,1);
  249. v3f rel_cam_up = v3f(0,1,0);
  250. if (m_cache_view_bobbing_amount != 0.0f && m_view_bobbing_anim != 0.0f &&
  251. m_camera_mode < CAMERA_MODE_THIRD) {
  252. f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
  253. f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
  254. #if 1
  255. f32 bobknob = 1.2;
  256. f32 bobtmp = sin(pow(bobfrac, bobknob) * M_PI);
  257. //f32 bobtmp2 = cos(pow(bobfrac, bobknob) * M_PI);
  258. v3f bobvec = v3f(
  259. 0.3 * bobdir * sin(bobfrac * M_PI),
  260. -0.28 * bobtmp * bobtmp,
  261. 0.);
  262. //rel_cam_pos += 0.2 * bobvec;
  263. //rel_cam_target += 0.03 * bobvec;
  264. //rel_cam_up.rotateXYBy(0.02 * bobdir * bobtmp * M_PI);
  265. float f = 1.0;
  266. f *= m_cache_view_bobbing_amount;
  267. rel_cam_pos += bobvec * f;
  268. //rel_cam_target += 0.995 * bobvec * f;
  269. rel_cam_target += bobvec * f;
  270. rel_cam_target.Z -= 0.005 * bobvec.Z * f;
  271. //rel_cam_target.X -= 0.005 * bobvec.X * f;
  272. //rel_cam_target.Y -= 0.005 * bobvec.Y * f;
  273. rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * M_PI * f);
  274. #else
  275. f32 angle_deg = 1 * bobdir * sin(bobfrac * M_PI);
  276. f32 angle_rad = angle_deg * M_PI / 180;
  277. f32 r = 0.05;
  278. v3f off = v3f(
  279. r * sin(angle_rad),
  280. r * (cos(angle_rad) - 1),
  281. 0);
  282. rel_cam_pos += off;
  283. //rel_cam_target += off;
  284. rel_cam_up.rotateXYBy(angle_deg);
  285. #endif
  286. }
  287. // Compute absolute camera position and target
  288. m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
  289. m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
  290. v3f abs_cam_up;
  291. m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
  292. // Seperate camera position for calculation
  293. v3f my_cp = m_camera_position;
  294. // Reposition the camera for third person view
  295. if (m_camera_mode > CAMERA_MODE_FIRST)
  296. {
  297. if (m_camera_mode == CAMERA_MODE_THIRD_FRONT)
  298. m_camera_direction *= -1;
  299. my_cp.Y += 2;
  300. // Calculate new position
  301. bool abort = false;
  302. for (int i = BS; i <= BS*2.75; i++)
  303. {
  304. my_cp.X = m_camera_position.X + m_camera_direction.X*-i;
  305. my_cp.Z = m_camera_position.Z + m_camera_direction.Z*-i;
  306. if (i > 12)
  307. my_cp.Y = m_camera_position.Y + (m_camera_direction.Y*-i);
  308. // Prevent camera positioned inside nodes
  309. INodeDefManager *nodemgr = m_client->ndef();
  310. MapNode n = c_env.getClientMap().getNodeNoEx(floatToInt(my_cp, BS));
  311. const ContentFeatures& features = nodemgr->get(n);
  312. if(features.walkable)
  313. {
  314. my_cp.X += m_camera_direction.X*-1*-BS/2;
  315. my_cp.Z += m_camera_direction.Z*-1*-BS/2;
  316. my_cp.Y += m_camera_direction.Y*-1*-BS/2;
  317. abort = true;
  318. break;
  319. }
  320. }
  321. // If node blocks camera position don't move y to heigh
  322. if (abort && my_cp.Y > player_position.Y+BS*2)
  323. my_cp.Y = player_position.Y+BS*2;
  324. }
  325. // Update offset if too far away from the center of the map
  326. m_camera_offset.X += CAMERA_OFFSET_STEP*
  327. (((s16)(my_cp.X/BS) - m_camera_offset.X)/CAMERA_OFFSET_STEP);
  328. m_camera_offset.Y += CAMERA_OFFSET_STEP*
  329. (((s16)(my_cp.Y/BS) - m_camera_offset.Y)/CAMERA_OFFSET_STEP);
  330. m_camera_offset.Z += CAMERA_OFFSET_STEP*
  331. (((s16)(my_cp.Z/BS) - m_camera_offset.Z)/CAMERA_OFFSET_STEP);
  332. // Set camera node transformation
  333. m_cameranode->setPosition(my_cp-intToFloat(m_camera_offset, BS));
  334. m_cameranode->setUpVector(abs_cam_up);
  335. // *100.0 helps in large map coordinates
  336. m_cameranode->setTarget(my_cp-intToFloat(m_camera_offset, BS) + 100 * m_camera_direction);
  337. // update the camera position in third-person mode to render blocks behind player
  338. // and correctly apply liquid post FX.
  339. if (m_camera_mode != CAMERA_MODE_FIRST)
  340. m_camera_position = my_cp;
  341. // Get FOV
  342. f32 fov_degrees;
  343. if (player->getPlayerControl().zoom && m_client->checkLocalPrivilege("zoom")) {
  344. fov_degrees = m_cache_zoom_fov;
  345. } else {
  346. fov_degrees = m_cache_fov;
  347. }
  348. fov_degrees = rangelim(fov_degrees, 7.0, 160.0);
  349. // FOV and aspect ratio
  350. m_aspect = (f32) porting::getWindowSize().X / (f32) porting::getWindowSize().Y;
  351. m_fov_y = fov_degrees * M_PI / 180.0;
  352. // Increase vertical FOV on lower aspect ratios (<16:10)
  353. m_fov_y *= MYMAX(1.0, MYMIN(1.4, sqrt(16./10. / m_aspect)));
  354. m_fov_x = 2 * atan(m_aspect * tan(0.5 * m_fov_y));
  355. m_cameranode->setAspectRatio(m_aspect);
  356. m_cameranode->setFOV(m_fov_y);
  357. float wieldmesh_offset_Y = -35 + player->getPitch() * 0.05;
  358. wieldmesh_offset_Y = rangelim(wieldmesh_offset_Y, -52, -32);
  359. // Position the wielded item
  360. //v3f wield_position = v3f(45, -35, 65);
  361. v3f wield_position = v3f(55, wieldmesh_offset_Y, 65);
  362. //v3f wield_rotation = v3f(-100, 120, -100);
  363. v3f wield_rotation = v3f(-100, 120, -100);
  364. wield_position.Y += fabs(m_wield_change_timer)*320 - 40;
  365. if(m_digging_anim < 0.05 || m_digging_anim > 0.5)
  366. {
  367. f32 frac = 1.0;
  368. if(m_digging_anim > 0.5)
  369. frac = 2.0 * (m_digging_anim - 0.5);
  370. // This value starts from 1 and settles to 0
  371. f32 ratiothing = pow((1.0f - tool_reload_ratio), 0.5f);
  372. //f32 ratiothing2 = pow(ratiothing, 0.5f);
  373. f32 ratiothing2 = (easeCurve(ratiothing*0.5))*2.0;
  374. wield_position.Y -= frac * 25.0 * pow(ratiothing2, 1.7f);
  375. //wield_position.Z += frac * 5.0 * ratiothing2;
  376. wield_position.X -= frac * 35.0 * pow(ratiothing2, 1.1f);
  377. wield_rotation.Y += frac * 70.0 * pow(ratiothing2, 1.4f);
  378. //wield_rotation.X -= frac * 15.0 * pow(ratiothing2, 1.4f);
  379. //wield_rotation.Z += frac * 15.0 * pow(ratiothing2, 1.0f);
  380. }
  381. if (m_digging_button != -1)
  382. {
  383. f32 digfrac = m_digging_anim;
  384. wield_position.X -= 50 * sin(pow(digfrac, 0.8f) * M_PI);
  385. wield_position.Y += 24 * sin(digfrac * 1.8 * M_PI);
  386. wield_position.Z += 25 * 0.5;
  387. // Euler angles are PURE EVIL, so why not use quaternions?
  388. core::quaternion quat_begin(wield_rotation * core::DEGTORAD);
  389. core::quaternion quat_end(v3f(80, 30, 100) * core::DEGTORAD);
  390. core::quaternion quat_slerp;
  391. quat_slerp.slerp(quat_begin, quat_end, sin(digfrac * M_PI));
  392. quat_slerp.toEuler(wield_rotation);
  393. wield_rotation *= core::RADTODEG;
  394. } else {
  395. f32 bobfrac = my_modf(m_view_bobbing_anim);
  396. wield_position.X -= sin(bobfrac*M_PI*2.0) * 3.0;
  397. wield_position.Y += sin(my_modf(bobfrac*2.0)*M_PI) * 3.0;
  398. }
  399. m_wieldnode->setPosition(wield_position);
  400. m_wieldnode->setRotation(wield_rotation);
  401. m_wieldnode->setColor(player->light_color);
  402. // Set render distance
  403. updateViewingRange();
  404. // If the player is walking, swimming, or climbing,
  405. // view bobbing is enabled and free_move is off,
  406. // start (or continue) the view bobbing animation.
  407. v3f speed = player->getSpeed();
  408. const bool movement_XZ = hypot(speed.X, speed.Z) > BS;
  409. const bool movement_Y = fabs(speed.Y) > BS;
  410. const bool walking = movement_XZ && player->touching_ground;
  411. const bool swimming = (movement_XZ || player->swimming_vertical) && player->in_liquid;
  412. const bool climbing = movement_Y && player->is_climbing;
  413. if ((walking || swimming || climbing) &&
  414. (!g_settings->getBool("free_move") || !m_client->checkLocalPrivilege("fly"))) {
  415. // Start animation
  416. m_view_bobbing_state = 1;
  417. m_view_bobbing_speed = MYMIN(speed.getLength(), 70);
  418. }
  419. else if (m_view_bobbing_state == 1)
  420. {
  421. // Stop animation
  422. m_view_bobbing_state = 2;
  423. m_view_bobbing_speed = 60;
  424. }
  425. }
  426. void Camera::updateViewingRange()
  427. {
  428. f32 viewing_range = g_settings->getFloat("viewing_range");
  429. f32 near_plane = g_settings->getFloat("near_plane");
  430. m_draw_control.wanted_range = viewing_range;
  431. m_cameranode->setNearValue(rangelim(near_plane, 0.0f, 0.5f) * BS);
  432. if (m_draw_control.range_all) {
  433. m_cameranode->setFarValue(100000.0);
  434. return;
  435. }
  436. m_cameranode->setFarValue((viewing_range < 2000) ? 2000 * BS : viewing_range * BS);
  437. }
  438. void Camera::setDigging(s32 button)
  439. {
  440. if (m_digging_button == -1)
  441. m_digging_button = button;
  442. }
  443. void Camera::wield(const ItemStack &item)
  444. {
  445. if (item.name != m_wield_item_next.name ||
  446. item.metadata != m_wield_item_next.metadata) {
  447. m_wield_item_next = item;
  448. if (m_wield_change_timer > 0)
  449. m_wield_change_timer = -m_wield_change_timer;
  450. else if (m_wield_change_timer == 0)
  451. m_wield_change_timer = -0.001;
  452. }
  453. }
  454. void Camera::drawWieldedTool(irr::core::matrix4* translation)
  455. {
  456. // Clear Z buffer so that the wielded tool stay in front of world geometry
  457. m_wieldmgr->getVideoDriver()->clearZBuffer();
  458. // Draw the wielded node (in a separate scene manager)
  459. scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
  460. cam->setAspectRatio(m_cameranode->getAspectRatio());
  461. cam->setFOV(72.0*M_PI/180.0);
  462. cam->setNearValue(10);
  463. cam->setFarValue(1000);
  464. if (translation != NULL)
  465. {
  466. irr::core::matrix4 startMatrix = cam->getAbsoluteTransformation();
  467. irr::core::vector3df focusPoint = (cam->getTarget()
  468. - cam->getAbsolutePosition()).setLength(1)
  469. + cam->getAbsolutePosition();
  470. irr::core::vector3df camera_pos =
  471. (startMatrix * *translation).getTranslation();
  472. cam->setPosition(camera_pos);
  473. cam->setTarget(focusPoint);
  474. }
  475. m_wieldmgr->drawAll();
  476. }
  477. void Camera::drawNametags()
  478. {
  479. core::matrix4 trans = m_cameranode->getProjectionMatrix();
  480. trans *= m_cameranode->getViewMatrix();
  481. for (std::list<Nametag *>::const_iterator
  482. i = m_nametags.begin();
  483. i != m_nametags.end(); ++i) {
  484. Nametag *nametag = *i;
  485. if (nametag->nametag_color.getAlpha() == 0) {
  486. // Enforce hiding nametag,
  487. // because if freetype is enabled, a grey
  488. // shadow can remain.
  489. continue;
  490. }
  491. v3f pos = nametag->parent_node->getAbsolutePosition() + v3f(0.0, 1.1 * BS, 0.0);
  492. f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
  493. trans.multiplyWith1x4Matrix(transformed_pos);
  494. if (transformed_pos[3] > 0) {
  495. std::string nametag_colorless = unescape_enriched(nametag->nametag_text);
  496. core::dimension2d<u32> textsize =
  497. g_fontengine->getFont()->getDimension(
  498. utf8_to_wide(nametag_colorless).c_str());
  499. f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
  500. core::reciprocal(transformed_pos[3]);
  501. v2u32 screensize = m_driver->getScreenSize();
  502. v2s32 screen_pos;
  503. screen_pos.X = screensize.X *
  504. (0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2;
  505. screen_pos.Y = screensize.Y *
  506. (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2;
  507. core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
  508. g_fontengine->getFont()->draw(utf8_to_wide(nametag->nametag_text).c_str(),
  509. size + screen_pos, nametag->nametag_color);
  510. }
  511. }
  512. }
  513. Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
  514. std::string nametag_text, video::SColor nametag_color)
  515. {
  516. Nametag *nametag = new Nametag(parent_node, nametag_text, nametag_color);
  517. m_nametags.push_back(nametag);
  518. return nametag;
  519. }
  520. void Camera::removeNametag(Nametag *nametag)
  521. {
  522. m_nametags.remove(nametag);
  523. delete nametag;
  524. }