3 Copyright (C) 2020 Jean-Patrick Guerrero <jeanpatrick.guerrero@gmail.com>
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.
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.
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.
22 #include <SViewFrustum.h>
23 #include <IAnimatedMeshSceneNode.h>
24 #include <ILightSceneNode.h>
27 GUIScene::GUIScene(gui::IGUIEnvironment *env, scene::ISceneManager *smgr,
28 gui::IGUIElement *parent, core::recti rect, s32 id)
29 : IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rect)
31 m_driver = env->getVideoDriver();
32 m_smgr = smgr->createNewSceneManager(false);
34 m_cam = m_smgr->addCameraSceneNode(0, v3f(0.f, 0.f, -100.f), v3f(0.f));
35 m_cam->setFOV(30.f * core::DEGTORAD);
37 scene::ILightSceneNode *light = m_smgr->addLightSceneNode(m_cam);
38 light->setRadius(1000.f);
40 m_smgr->getParameters()->setAttribute(scene::ALLOW_ZWRITE_ON_TRANSPARENT, true);
50 scene::IAnimatedMeshSceneNode *GUIScene::setMesh(scene::IAnimatedMesh *mesh)
60 m_mesh = m_smgr->addAnimatedMeshSceneNode(mesh);
61 m_mesh->setPosition(-m_mesh->getBoundingBox().getCenter());
62 m_mesh->animateJoints();
66 void GUIScene::setTexture(u32 idx, video::ITexture *texture)
68 video::SMaterial &material = m_mesh->getMaterial(idx);
69 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
70 material.MaterialTypeParam = 0.5f;
71 material.TextureLayer[0].Texture = texture;
72 material.setFlag(video::EMF_LIGHTING, false);
73 material.setFlag(video::EMF_FOG_ENABLE, true);
74 material.setFlag(video::EMF_BILINEAR_FILTER, false);
75 material.setFlag(video::EMF_BACK_FACE_CULLING, false);
80 // Control rotation speed based on time
81 u64 new_time = porting::getTimeMs();
84 dtime_ms = porting::getDeltaMs(m_last_time, new_time);
85 m_last_time = new_time;
87 core::rect<s32> oldViewPort = m_driver->getViewPort();
88 m_driver->setViewPort(getAbsoluteClippingRect());
89 core::recti borderRect = Environment->getRootGUIElement()->getAbsoluteClippingRect();
92 Environment->getSkin()->draw3DSunkenPane(
93 this, m_bgcolor, false, true, borderRect, 0);
96 core::dimension2d<s32> size = getAbsoluteClippingRect().getSize();
97 m_smgr->getActiveCamera()->setAspectRatio((f32)size.Width / (f32)size.Height);
100 updateCamera(m_smgr->addEmptySceneNode());
101 rotateCamera(v3f(0.f));
102 m_cam->bindTargetAndRotation(true);
107 // Continuous rotation
109 rotateCamera(v3f(0.f, -0.03f * (float)dtime_ms, 0.f));
113 if (m_initial_rotation && m_mesh) {
114 rotateCamera(v3f(m_custom_rot.X, m_custom_rot.Y, 0.f));
115 calcOptimalDistance();
117 m_initial_rotation = false;
120 m_driver->setViewPort(oldViewPort);
123 bool GUIScene::OnEvent(const SEvent &event)
125 if (m_mouse_ctrl && event.EventType == EET_MOUSE_INPUT_EVENT) {
126 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
127 m_last_pos = v2f((f32)event.MouseInput.X, (f32)event.MouseInput.Y);
129 } else if (event.MouseInput.Event == EMIE_MOUSE_MOVED) {
130 if (event.MouseInput.isLeftPressed()) {
131 m_curr_pos = v2f((f32)event.MouseInput.X, (f32)event.MouseInput.Y);
134 m_last_pos.Y - m_curr_pos.Y,
135 m_curr_pos.X - m_last_pos.X, 0.f));
137 m_last_pos = m_curr_pos;
143 return gui::IGUIElement::OnEvent(event);
146 void GUIScene::setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES> &styles)
148 StyleSpec::State state = StyleSpec::STATE_DEFAULT;
149 StyleSpec style = StyleSpec::getStyleFromStatePropagation(styles, state);
151 setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
152 setBackgroundColor(style.getColor(StyleSpec::BGCOLOR, m_bgcolor));
156 * Sets the frame loop range for the mesh
158 void GUIScene::setFrameLoop(s32 begin, s32 end)
160 if (m_mesh->getStartFrame() != begin || m_mesh->getEndFrame() != end)
161 m_mesh->setFrameLoop(begin, end);
164 /* Camera control functions */
166 inline void GUIScene::calcOptimalDistance()
168 core::aabbox3df box = m_mesh->getBoundingBox();
169 f32 width = box.MaxEdge.X - box.MinEdge.X;
170 f32 height = box.MaxEdge.Y - box.MinEdge.Y;
171 f32 depth = box.MaxEdge.Z - box.MinEdge.Z;
172 f32 max_width = width > depth ? width : depth;
174 const scene::SViewFrustum *f = m_cam->getViewFrustum();
175 f32 cam_far = m_cam->getFarValue();
176 f32 far_width = core::line3df(f->getFarLeftUp(), f->getFarRightUp()).getLength();
177 f32 far_height = core::line3df(f->getFarLeftUp(), f->getFarLeftDown()).getLength();
179 core::recti rect = getAbsolutePosition();
180 f32 zoomX = rect.getWidth() / max_width;
181 f32 zoomY = rect.getHeight() / height;
185 dist = (max_width / (far_width / cam_far)) + (0.5f * max_width);
187 dist = (height / (far_height / cam_far)) + (0.5f * max_width);
189 m_cam_distance = dist;
193 void GUIScene::updateCamera(scene::ISceneNode *target)
198 m_last_target_pos = m_target_pos;
204 void GUIScene::updateTargetPos()
206 m_last_target_pos = m_target_pos;
207 m_target->updateAbsolutePosition();
208 m_target_pos = m_target->getAbsolutePosition();
211 void GUIScene::setCameraRotation(v3f rot)
216 mat.setRotationDegrees(rot);
218 m_cam_pos = v3f(0.f, 0.f, m_cam_distance);
219 mat.rotateVect(m_cam_pos);
221 m_cam_pos += m_target_pos;
222 m_cam->setPosition(m_cam_pos);
223 m_update_cam = false;
226 bool GUIScene::correctBounds(v3f &rot)
228 const float ROTATION_MAX_1 = 60.0f;
229 const float ROTATION_MAX_2 = 300.0f;
231 // Limit and correct the rotation when needed
233 if (rot.X > ROTATION_MAX_1) {
234 rot.X = ROTATION_MAX_1;
237 } else if (rot.X < ROTATION_MAX_2) {
238 rot.X = ROTATION_MAX_2;
246 void GUIScene::cameraLoop()
251 if (m_target_pos != m_last_target_pos)
255 m_cam_pos = m_target_pos + (m_cam_pos - m_target_pos).normalize() * m_cam_distance;
257 v3f rot = getCameraRotation();
258 if (correctBounds(rot))
259 setCameraRotation(rot);
261 m_cam->setPosition(m_cam_pos);
262 m_cam->setTarget(m_target_pos);
264 m_update_cam = false;