guiScene.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. Minetest
  3. Copyright (C) 2020 Jean-Patrick Guerrero <jeanpatrick.guerrero@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 2.1 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 "guiScene.h"
  17. #include <SViewFrustum.h>
  18. #include <IAnimatedMeshSceneNode.h>
  19. #include "porting.h"
  20. GUIScene::GUIScene(gui::IGUIEnvironment *env, scene::ISceneManager *smgr,
  21. gui::IGUIElement *parent, core::recti rect, s32 id)
  22. : IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rect)
  23. {
  24. m_driver = env->getVideoDriver();
  25. m_smgr = smgr->createNewSceneManager(false);
  26. m_cam = m_smgr->addCameraSceneNode(0, v3f(0.f, 0.f, -100.f), v3f(0.f));
  27. m_cam->setFOV(30.f * core::DEGTORAD);
  28. m_smgr->getParameters()->setAttribute(scene::ALLOW_ZWRITE_ON_TRANSPARENT, true);
  29. }
  30. GUIScene::~GUIScene()
  31. {
  32. setMesh(nullptr);
  33. m_smgr->drop();
  34. }
  35. scene::IAnimatedMeshSceneNode *GUIScene::setMesh(scene::IAnimatedMesh *mesh)
  36. {
  37. if (m_mesh) {
  38. m_mesh->remove();
  39. m_mesh = nullptr;
  40. }
  41. if (!mesh)
  42. return nullptr;
  43. m_mesh = m_smgr->addAnimatedMeshSceneNode(mesh);
  44. m_mesh->setPosition(-m_mesh->getBoundingBox().getCenter());
  45. m_mesh->animateJoints();
  46. return m_mesh;
  47. }
  48. void GUIScene::setTexture(u32 idx, video::ITexture *texture)
  49. {
  50. video::SMaterial &material = m_mesh->getMaterial(idx);
  51. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  52. material.MaterialTypeParam = 0.5f;
  53. material.TextureLayers[0].Texture = texture;
  54. material.Lighting = false;
  55. material.FogEnable = true;
  56. material.TextureLayers[0].MinFilter = video::ETMINF_NEAREST_MIPMAP_NEAREST;
  57. material.TextureLayers[0].MagFilter = video::ETMAGF_NEAREST;
  58. material.BackfaceCulling = false;
  59. material.ZWriteEnable = video::EZW_AUTO;
  60. }
  61. void GUIScene::draw()
  62. {
  63. m_driver->clearBuffers(video::ECBF_DEPTH);
  64. // Control rotation speed based on time
  65. u64 new_time = porting::getTimeMs();
  66. u64 dtime_ms = 0;
  67. if (m_last_time != 0)
  68. dtime_ms = porting::getDeltaMs(m_last_time, new_time);
  69. m_last_time = new_time;
  70. core::rect<s32> oldViewPort = m_driver->getViewPort();
  71. m_driver->setViewPort(getAbsoluteClippingRect());
  72. if (m_bgcolor != 0) {
  73. core::recti borderRect =
  74. Environment->getRootGUIElement()->getAbsoluteClippingRect();
  75. Environment->getSkin()->draw3DSunkenPane(
  76. this, m_bgcolor, false, true, borderRect, 0);
  77. }
  78. core::dimension2d<s32> size = getAbsoluteClippingRect().getSize();
  79. m_smgr->getActiveCamera()->setAspectRatio((f32)size.Width / (f32)size.Height);
  80. if (!m_target) {
  81. updateCamera(m_smgr->addEmptySceneNode());
  82. rotateCamera(v3f(0.f));
  83. m_cam->bindTargetAndRotation(true);
  84. }
  85. cameraLoop();
  86. // Continuous rotation
  87. if (m_inf_rot)
  88. rotateCamera(v3f(0.f, -0.03f * (float)dtime_ms, 0.f));
  89. m_smgr->drawAll();
  90. if (m_initial_rotation && m_mesh) {
  91. rotateCamera(v3f(m_custom_rot.X, m_custom_rot.Y, 0.f));
  92. calcOptimalDistance();
  93. m_initial_rotation = false;
  94. }
  95. m_driver->setViewPort(oldViewPort);
  96. }
  97. bool GUIScene::OnEvent(const SEvent &event)
  98. {
  99. if (m_mouse_ctrl && event.EventType == EET_MOUSE_INPUT_EVENT) {
  100. if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
  101. m_last_pos = v2f((f32)event.MouseInput.X, (f32)event.MouseInput.Y);
  102. return true;
  103. } else if (event.MouseInput.Event == EMIE_MOUSE_MOVED) {
  104. if (event.MouseInput.isLeftPressed()) {
  105. m_curr_pos = v2f((f32)event.MouseInput.X, (f32)event.MouseInput.Y);
  106. rotateCamera(v3f(
  107. m_last_pos.Y - m_curr_pos.Y,
  108. m_curr_pos.X - m_last_pos.X, 0.f));
  109. m_last_pos = m_curr_pos;
  110. return true;
  111. }
  112. }
  113. }
  114. return gui::IGUIElement::OnEvent(event);
  115. }
  116. void GUIScene::setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES> &styles)
  117. {
  118. StyleSpec::State state = StyleSpec::STATE_DEFAULT;
  119. StyleSpec style = StyleSpec::getStyleFromStatePropagation(styles, state);
  120. setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  121. setBackgroundColor(style.getColor(StyleSpec::BGCOLOR, m_bgcolor));
  122. }
  123. /**
  124. * Sets the frame loop range for the mesh
  125. */
  126. void GUIScene::setFrameLoop(s32 begin, s32 end)
  127. {
  128. if (m_mesh->getStartFrame() != begin || m_mesh->getEndFrame() != end)
  129. m_mesh->setFrameLoop(begin, end);
  130. }
  131. /**
  132. * Sets the animation speed (FPS) for the mesh
  133. */
  134. void GUIScene::setAnimationSpeed(f32 speed)
  135. {
  136. m_mesh->setAnimationSpeed(speed);
  137. }
  138. /* Camera control functions */
  139. inline void GUIScene::calcOptimalDistance()
  140. {
  141. core::aabbox3df box = m_mesh->getBoundingBox();
  142. f32 width = box.MaxEdge.X - box.MinEdge.X;
  143. f32 height = box.MaxEdge.Y - box.MinEdge.Y;
  144. f32 depth = box.MaxEdge.Z - box.MinEdge.Z;
  145. f32 max_width = width > depth ? width : depth;
  146. const scene::SViewFrustum *f = m_cam->getViewFrustum();
  147. f32 cam_far = m_cam->getFarValue();
  148. f32 far_width = core::line3df(f->getFarLeftUp(), f->getFarRightUp()).getLength();
  149. f32 far_height = core::line3df(f->getFarLeftUp(), f->getFarLeftDown()).getLength();
  150. core::recti rect = getAbsolutePosition();
  151. f32 zoomX = rect.getWidth() / max_width;
  152. f32 zoomY = rect.getHeight() / height;
  153. f32 dist;
  154. if (zoomX < zoomY)
  155. dist = (max_width / (far_width / cam_far)) + (0.5f * max_width);
  156. else
  157. dist = (height / (far_height / cam_far)) + (0.5f * max_width);
  158. m_cam_distance = dist;
  159. m_update_cam = true;
  160. }
  161. void GUIScene::updateCamera(scene::ISceneNode *target)
  162. {
  163. m_target = target;
  164. updateTargetPos();
  165. m_last_target_pos = m_target_pos;
  166. updateCameraPos();
  167. m_update_cam = true;
  168. }
  169. void GUIScene::updateTargetPos()
  170. {
  171. m_last_target_pos = m_target_pos;
  172. m_target->updateAbsolutePosition();
  173. m_target_pos = m_target->getAbsolutePosition();
  174. }
  175. void GUIScene::setCameraRotation(v3f rot)
  176. {
  177. correctBounds(rot);
  178. core::matrix4 mat;
  179. mat.setRotationDegrees(rot);
  180. m_cam_pos = v3f(0.f, 0.f, m_cam_distance);
  181. mat.rotateVect(m_cam_pos);
  182. m_cam_pos += m_target_pos;
  183. m_cam->setPosition(m_cam_pos);
  184. m_update_cam = false;
  185. }
  186. bool GUIScene::correctBounds(v3f &rot)
  187. {
  188. const float ROTATION_MAX_1 = 60.0f;
  189. const float ROTATION_MAX_2 = 300.0f;
  190. // Limit and correct the rotation when needed
  191. if (rot.X < 90.f) {
  192. if (rot.X > ROTATION_MAX_1) {
  193. rot.X = ROTATION_MAX_1;
  194. return true;
  195. }
  196. } else if (rot.X < ROTATION_MAX_2) {
  197. rot.X = ROTATION_MAX_2;
  198. return true;
  199. }
  200. // Not modified
  201. return false;
  202. }
  203. void GUIScene::cameraLoop()
  204. {
  205. updateCameraPos();
  206. updateTargetPos();
  207. if (m_target_pos != m_last_target_pos)
  208. m_update_cam = true;
  209. if (m_update_cam) {
  210. m_cam_pos = m_target_pos + (m_cam_pos - m_target_pos).normalize() * m_cam_distance;
  211. v3f rot = getCameraRotation();
  212. if (correctBounds(rot))
  213. setCameraRotation(rot);
  214. m_cam->setPosition(m_cam_pos);
  215. m_cam->setTarget(m_target_pos);
  216. m_update_cam = false;
  217. }
  218. }