Merge branch '138-toggle-free-look-with-hotkey' into main/gingo-test
[ryzomcore.git] / ryzom / client / src / interface_v3 / interface_3d_scene.cpp
blob36c0d2aae9a6ba5f7ab8f3244f1fb8157ccb3f61
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010-2018 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 // Copyright (C) 2015-2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 // ----------------------------------------------------------------------------
24 #include "stdpch.h"
26 #include "interface_3d_scene.h"
27 #include "interface_manager.h"
28 #include "character_3d.h"
29 #include "../time_client.h"
30 #include "../entities.h"
32 #include "nel/3d/u_point_light.h"
33 #include "nel/3d/u_particle_system_instance.h"
34 #include "nel/3d/u_animation_set.h"
37 #include "nel/misc/xml_auto_ptr.h"
38 #include "nel/gui/action_handler.h"
40 #include "nel/gui/lua_ihm.h"
42 // ----------------------------------------------------------------------------
43 using namespace std;
44 using namespace NL3D;
45 using namespace NLMISC;
47 // ----------------------------------------------------------------------------
48 // CInterface3DScene
49 // ----------------------------------------------------------------------------
50 CInterface3DScene::CInterface3DScene(const TCtorParam &param)
51 : CInterfaceGroup(param)
53 _Scene = NULL;
54 _AutoAnimSet = NULL;
55 _CurrentCS = 0;
56 _CurrentCamera = 0;
57 _MouseLDown = false;
58 _MouseRDown = false;
59 _UserInteraction = false;
60 _RotYFactor = _RotZFactor = 0.005f;
61 _RotYLimitMin = _RotZLimitMin = -(float)(180.0f / NLMISC::Pi);
62 _RotYLimitMax = _RotZLimitMax = (float)(180.0f / NLMISC::Pi);
63 _DistLimitMin = 0.1f;
64 _DistLimitMax = 15.0f;
65 _DistFactor = 0.005f;
68 // ----------------------------------------------------------------------------
69 CInterface3DScene::~CInterface3DScene()
71 uint i;
73 for (i = 0; i < _Characters.size(); ++i)
74 delete _Characters[i];
75 for (i = 0; i < _IGs.size(); ++i)
76 delete _IGs[i];
77 for (i = 0; i < _Cameras.size(); ++i)
78 delete _Cameras[i];
79 for (i = 0; i < _Lights.size(); ++i)
80 delete _Lights[i];
81 for (i = 0; i < _Shapes.size(); ++i)
82 delete _Shapes[i];
83 for (i = 0; i < _FXs.size(); ++i)
84 delete _FXs[i];
86 NL3D::UDriver *Driver = CViewRenderer::getInstance()->getDriver();
87 if (_Scene != NULL)
88 Driver->deleteScene (_Scene);
90 if (_AutoAnimSet != NULL)
91 Driver->deleteAnimationSet(_AutoAnimSet);
94 // ----------------------------------------------------------------------------
95 CInterface3DCharacter *CInterface3DScene::getCharacter3D(uint index)
97 nlassert(index < _Characters.size());
98 return _Characters[index];
101 // ----------------------------------------------------------------------------
102 CInterface3DCamera *CInterface3DScene::getCamera(uint index)
104 nlassert(index < _Cameras.size());
105 return _Cameras[index];
108 // ----------------------------------------------------------------------------
109 bool CInterface3DScene::parse (xmlNodePtr cur, CInterfaceGroup *parentGroup)
111 CInterfaceManager *pIM = CInterfaceManager::getInstance();
112 if (!CInterfaceElement::parse(cur, parentGroup))
113 return false;
115 CXMLAutoPtr ptr;
116 double value;
118 // Check for user interaction properties
119 ptr = (char*) xmlGetProp( cur, (xmlChar*)"user_interaction" );
120 if (ptr) _UserInteraction = convertBool(ptr);
122 ptr = (char*) xmlGetProp( cur, (xmlChar*)"rotz_limit_min" );
123 if (ptr)
125 fromString((const char*)ptr, value);
126 _RotZLimitMin = (float)(value * (NLMISC::Pi/180.0));
129 ptr = (char*) xmlGetProp( cur, (xmlChar*)"rotz_limit_max" );
130 if (ptr)
132 fromString((const char*)ptr, value);
133 _RotZLimitMax = (float)(value * (NLMISC::Pi/180.0));
136 ptr = (char*) xmlGetProp( cur, (xmlChar*)"rotz_factor" );
137 if (ptr) fromString((const char*)ptr, _RotZFactor);
139 ptr = (char*) xmlGetProp( cur, (xmlChar*)"roty_limit_min" );
140 if (ptr)
142 fromString((const char*)ptr, value);
143 _RotYLimitMin = (float)(value * (NLMISC::Pi/180.0));
146 ptr = (char*) xmlGetProp( cur, (xmlChar*)"roty_limit_max" );
147 if (ptr)
149 fromString((const char*)ptr, value);
150 _RotYLimitMax = (float)(value * (NLMISC::Pi/180.0));
153 ptr = (char*) xmlGetProp( cur, (xmlChar*)"roty_factor" );
154 if (ptr) fromString((const char*)ptr, _RotYFactor);
156 ptr = (char*) xmlGetProp( cur, (xmlChar*)"dist_limit_min" );
157 if (ptr) fromString((const char*)ptr, _DistLimitMin);
159 ptr = (char*) xmlGetProp( cur, (xmlChar*)"dist_limit_max" );
160 if (ptr) fromString((const char*)ptr, _DistLimitMax);
162 ptr = (char*) xmlGetProp( cur, (xmlChar*)"dist_factor" );
163 if (ptr) fromString((const char*)ptr, _DistFactor);
165 // Check right now if this is a reference view
166 ptr = (char*) xmlGetProp( cur, (xmlChar*)"reference" );
167 _Ref3DScene = NULL;
168 if (ptr)
170 CInterfaceElement *pIE = CWidgetManager::getInstance()->getElementFromId(this->getId(), ptr.str());
171 _Ref3DScene = dynamic_cast<CInterface3DScene*>(pIE);
173 if (_Ref3DScene != NULL)
175 ptr = (char*) xmlGetProp( cur, (xmlChar*)"curcam" );
176 if (ptr) setCurrentCamera (ptr.str());
177 return true;
180 NL3D::UDriver *Driver = CViewRenderer::getInstance()->getDriver();
181 nlassert ( Driver != NULL);
183 _Scene = Driver->createScene(true);
185 _Scene->enableLightingSystem(true);
187 CRGBA rgbaTmp;
188 ptr = (char*) xmlGetProp( cur, (xmlChar*)"ambient" );
189 rgbaTmp = CRGBA::Black;
190 if (ptr) rgbaTmp = convertColor(ptr);
191 _Scene->setAmbientGlobal(rgbaTmp);
193 ptr = (char*) xmlGetProp( cur, (xmlChar*)"sun_ambient" );
194 rgbaTmp = CRGBA(50,50,50);
195 if (ptr) rgbaTmp = convertColor(ptr);
196 _Scene->setSunAmbient(rgbaTmp);
198 ptr = (char*) xmlGetProp( cur, (xmlChar*)"sun_diffuse" );
199 rgbaTmp = CRGBA::White;
200 if (ptr) rgbaTmp = convertColor(ptr);
201 _Scene->setSunDiffuse(rgbaTmp);
203 ptr = (char*) xmlGetProp( cur, (xmlChar*)"sun_specular" );
204 rgbaTmp = CRGBA::White;
205 if (ptr) rgbaTmp = convertColor(ptr);
206 _Scene->setSunSpecular(rgbaTmp);
208 CVector v(-1,1,-1);
209 ptr = (char*) xmlGetProp( cur, (xmlChar*)"sun_direction" );
210 if (ptr) v = convertVector(ptr);
211 _Scene->setSunDirection(v);
213 // Read all children
214 // bool ok = true;
215 cur = cur->children;
216 while (cur)
218 // Check that this is a camera node
219 if ( stricmp((char*)cur->name,"character3d") == 0 )
221 CInterface3DCharacter *pCha = new CInterface3DCharacter;
222 if (!pCha->parse(cur,this))
224 delete pCha;
225 nlwarning("character3d not added to scene3d");
227 else
229 _Characters.push_back(pCha);
232 else if ( stricmp((char*)cur->name,"ig") == 0 )
234 CInterface3DIG *pIG = new CInterface3DIG;
235 if (!pIG->parse(cur,this))
237 delete pIG;
238 nlwarning("ig not added to scene3d");
240 else
242 _IGs.push_back(pIG);
245 else if ( stricmp((char*)cur->name,"shape") == 0 )
247 CInterface3DShape *pShp = new CInterface3DShape;
248 if (!pShp->parse(cur,this))
250 delete pShp;
251 nlwarning("shape not added to scene3d");
253 else
255 _Shapes.push_back(pShp);
258 else if ( stricmp((char*)cur->name,"camera") == 0 )
260 CInterface3DCamera *pCam = new CInterface3DCamera;
261 if (!pCam->parse(cur,this))
263 delete pCam;
264 nlwarning("camera not added to scene3d");
266 else
268 _Cameras.push_back(pCam);
271 else if ( stricmp((char*)cur->name,"light") == 0 )
273 CInterface3DLight *pLig = new CInterface3DLight;
274 if (!pLig->parse(cur,this))
276 delete pLig;
277 nlwarning("light not added to scene3d");
279 else
281 _Lights.push_back(pLig);
284 else if ( stricmp((char*)cur->name,"fx") == 0 )
286 CInterface3DFX *pFX = new CInterface3DFX;
287 if (!pFX->parse(cur,this))
289 delete pFX;
290 nlwarning("fx not added to scene3d");
292 else
294 _FXs.push_back(pFX);
297 else if ( stricmp((char*)cur->name,"auto_anim") == 0 )
299 CXMLAutoPtr ptr((const char*)xmlGetProp (cur, (xmlChar*)"name"));
300 string animName;
301 if (ptr)
302 animName = toLowerAscii(CFile::getFilenameWithoutExtension(ptr.str()));
304 if (!animName.empty())
306 if (_AutoAnimSet == NULL)
307 _AutoAnimSet = CViewRenderer::getInstance()->getDriver()->createAnimationSet();
308 uint id = _AutoAnimSet->addAnimation (ptr, animName.c_str ());
309 if (id == UAnimationSet::NotFound)
311 nlwarning ("Can't load automatic animation '%s'", animName.c_str());
314 else
316 nlwarning ("Can't get automatic animation name");
320 cur = cur->next;
323 // if some auto_anim, found, compile and set auto_anim
324 if (_AutoAnimSet != NULL)
326 _AutoAnimSet->build ();
327 _Scene->setAutomaticAnimationSet (_AutoAnimSet);
330 // If no camera create the default one
331 if (_Cameras.empty())
333 CInterface3DCamera *pCam = new CInterface3DCamera;
334 _Cameras.push_back(pCam);
337 _CurrentCamera = 0;
339 // Initialize all camera distance
340 for (uint i = 0; i < _Cameras.size(); ++i)
342 CInterface3DCamera *pCam = _Cameras[i];
343 pCam->setDist ((pCam->getPos() - pCam->getTarget()).norm());
346 // Get the current camera
347 ptr = (char*) xmlGetProp( cur, (xmlChar*)"curcam" );
348 if (ptr) setCurrentCamera(ptr.str());
350 return true;
353 // ----------------------------------------------------------------------------
354 void CInterface3DScene::checkCoords()
356 uint i;
358 for (i = 0; i < _Characters.size(); ++i)
359 _Characters[i]->checkCoords();
360 for (i = 0; i < _IGs.size(); ++i)
361 _IGs[i]->checkCoords();
362 for (i = 0; i < _Cameras.size(); ++i)
363 _Cameras[i]->checkCoords();
364 for (i = 0; i < _Lights.size(); ++i)
365 _Lights[i]->checkCoords();
366 for (i = 0; i < _FXs.size(); ++i)
367 _FXs[i]->checkCoords();
368 for (i = 0; i < _Shapes.size(); ++i)
369 _Shapes[i]->checkCoords();
371 if (_Scene != NULL)
372 _Scene->animate (TimeInSec-FirstTimeInSec);
375 // ----------------------------------------------------------------------------
376 void CInterface3DScene::updateCoords ()
378 CViewBase::updateCoords();
381 // ----------------------------------------------------------------------------
382 void CInterface3DScene::draw ()
384 H_AUTO( RZ_Interface_CInterface3DScene_draw )
386 NL3D::UDriver *Driver = CViewRenderer::getInstance()->getDriver();
388 if ( Driver == NULL)
389 return;
391 // No Op if screen minimized
392 CInterfaceManager *pIM = CInterfaceManager::getInstance();
393 CViewRenderer &rVR = *CViewRenderer::getInstance();
394 if(rVR.isMinimized())
395 return;
397 CInterface3DScene *pDisp = this;
398 // If this is a reference view
399 if (_Ref3DScene != NULL)
401 pDisp = _Ref3DScene;
402 pDisp->setFlareContext(1);
404 else
406 pDisp->setFlareContext(0);
409 // This is not a reference view !
410 if (pDisp->_Scene == NULL)
411 return;
413 CInterface3DCamera *pI3DCam = pDisp->_Cameras[_CurrentCamera];
415 // TEMP TEMP TEMP DISPLAY BACKGROUND
416 //rVR.drawRotFlipBitmap (_RenderLayer, _XReal, _YReal, _WReal, _HReal, 0, false,
417 // rVR.getBlankTextureId(), CRGBA(255,255,255,255) );
418 // TEMP TEMP TEMP
420 rVR.flush();
422 // Viewport and frustrum
423 uint32 wsw, wsh;
425 sint32 oldSciX, oldSciY, oldSciW, oldSciH;
426 makeNewClip (oldSciX, oldSciY, oldSciW, oldSciH);
428 // Display sons only if not total clipped
429 if( rVR.isClipWindowEmpty() )
431 restoreClip (oldSciX, oldSciY, oldSciW, oldSciH);
432 return;
435 sint32 clipx,clipy,clipw,cliph;
436 getClip (clipx, clipy, clipw, cliph);
438 rVR.getScreenSize (wsw, wsh);
439 NL3D::CViewport oldVP = Driver->getViewport();
440 NL3D::CViewport newVP;
441 float vpX = (float) (clipx) / iavoid0(wsw);
442 float vpY = (float) (clipy) / iavoid0(wsh);
443 float vpW = (float) clipw / iavoid0(wsw);
444 float vpH = (float) cliph / iavoid0(wsh);
445 newVP.init(vpX, vpY, vpW, vpH);
446 NL3D::CFrustum oldFrustum = CViewRenderer::getInstance()->getDriver()->getFrustum();
447 NL3D::CFrustum newFrustum;
448 newFrustum.initPerspective (pI3DCam->getFOV() * (float) (NLMISC::Pi / 180), (float) _WReal / iavoid0(_HReal), 0.1f, 100.f);
450 // Ajust frustum when there's clamping on border of screen
451 float xLeft = 0.f;
452 float xRight = 1.f;
453 float yBottom = 0.f;
454 float yTop = 1.f;
456 // We assume that the viewport has dimensions < to those of the screen
457 if ((_XReal+_WReal) > (clipx+clipw)) // right clamp ?
459 xRight = ((clipx+clipw) - _XReal) / (float) _WReal;
461 else if (_XReal < clipx) // left clamp
463 xLeft = (clipx - _XReal) / (float) _WReal;
465 if ((_YReal + _HReal) > (clipy+cliph)) // top clamp ?
467 yTop = ((clipy+cliph) - _YReal) / (float) _HReal;
469 else if (_YReal < clipy) // bottom clamp
471 yBottom = (clipy - _YReal) / (float) _HReal;
474 // adjust frustum
475 float fWidth = newFrustum.Right - newFrustum.Left;
476 float fLeft = newFrustum.Left;
477 newFrustum.Left = fLeft + fWidth * xLeft;
478 newFrustum.Right = fLeft + fWidth * xRight;
479 float fHeight = newFrustum.Top - newFrustum.Bottom;
480 float fBottom = newFrustum.Bottom;
481 newFrustum.Bottom = fBottom + fHeight * yBottom;
482 newFrustum.Top = fBottom + fHeight * yTop;
484 pDisp->_Scene->setViewport(newVP);
485 NL3D::UCamera cam = pDisp->_Scene->getCam();
486 cam.setFrustum(newFrustum);
488 // Rotate the camera position around the target with the rot parameters
489 CVector pos = pI3DCam->getPos() - pI3DCam->getTarget();
490 // float dist = pos.norm();
491 pos.normalize();
492 CMatrix m;
493 m.identity();
494 m.rotateZ(pI3DCam->getRotZ());
495 m.rotateX(pI3DCam->getRotY());
496 pos = m.mulVector(pos);
497 pos = pos * pI3DCam->getDist();
498 pos = pos + pI3DCam->getTarget();
499 cam.lookAt (pos, pI3DCam->getTarget(), pI3DCam->getRoll() * (float) (NLMISC::Pi / 180));
501 uint i;
502 if (!_IGs.empty())
504 for (i = 0; i < _Characters.size(); ++i)
505 _Characters[i]->setClusterSystem (_IGs[_CurrentCS]->getIG());
506 for (i = 0; i < _Shapes.size(); ++i)
507 _Shapes[i]->getShape().setClusterSystem (_IGs[_CurrentCS]->getIG());
508 for (i = 0; i < _FXs.size(); ++i)
509 if (!_FXs[i]->getPS().empty())
510 _FXs[i]->getPS().setClusterSystem (_IGs[_CurrentCS]->getIG());
511 cam.setClusterSystem (_IGs[_CurrentCS]->getIG());
513 else
515 for (i = 0; i < _Characters.size(); ++i)
516 _Characters[i]->setClusterSystem ((UInstanceGroup*)-1);
517 for (i = 0; i < _Shapes.size(); ++i)
519 if (!_Shapes[i]->getShape().empty())
520 _Shapes[i]->getShape().setClusterSystem ((UInstanceGroup*)-1);
522 for (i = 0; i < _FXs.size(); ++i)
523 if (!_FXs[i]->getPS().empty())
524 _FXs[i]->getPS().setClusterSystem ((UInstanceGroup*)-1);
525 cam.setClusterSystem ((UInstanceGroup*)-1);
528 ////////////////////////
529 // Clear the Z-Buffer //
530 ////////////////////////
531 NL3D::CScissor oldScissor = Driver->getScissor();
532 NL3D::CScissor newScissor;
533 newScissor.X = vpX;
534 newScissor.Y = vpY;
535 newScissor.Width = vpW;
536 newScissor.Height = vpH;
537 Driver->setScissor(newScissor);
538 Driver->clearZBuffer();
539 Driver->setScissor(oldScissor);
540 ///////////////////////////////////////////////
542 pDisp->_Scene->render();
544 Driver->setViewport(oldVP);
545 Driver->setFrustum(oldFrustum);
547 // Restore render states
548 CViewRenderer::getInstance()->setRenderStates();
550 restoreClip (oldSciX, oldSciY, oldSciW, oldSciH);
553 // ----------------------------------------------------------------------------
554 bool CInterface3DScene::handleEvent (const NLGUI::CEventDescriptor &event)
556 if (!_UserInteraction)
557 return false;
559 if (!_Active)
560 return false;
561 // if focus is lost then cancel rotation / zoom
562 if (event.getType() == NLGUI::CEventDescriptor::system)
564 const NLGUI::CEventDescriptorSystem &eds = (const NLGUI::CEventDescriptorSystem &) event;
565 if (eds.getEventTypeExtended() == NLGUI::CEventDescriptorSystem::setfocus)
567 const NLGUI::CEventDescriptorSetFocus &edsf = (const NLGUI::CEventDescriptorSetFocus &) eds;
568 if (edsf.hasFocus() == false)
570 _MouseLDown = false;
571 _MouseRDown = false;
572 return true;
576 if (event.getType() == NLGUI::CEventDescriptor::mouse)
578 const NLGUI::CEventDescriptorMouse &eventDesc = (const NLGUI::CEventDescriptorMouse &)event;
579 if ((CWidgetManager::getInstance()->getCapturePointerLeft() != this) &&
580 (CWidgetManager::getInstance()->getCapturePointerRight() != this) &&
581 (!((eventDesc.getX() >= _XReal) &&
582 (eventDesc.getX() < (_XReal + _WReal))&&
583 (eventDesc.getY() > _YReal) &&
584 (eventDesc.getY() <= (_YReal+ _HReal)))))
585 return false;
587 if (eventDesc.getEventTypeExtended() == NLGUI::CEventDescriptorMouse::mouseleftdown)
589 _MouseLDown = true;
590 _MouseLDownX = eventDesc.getX();
591 _MouseLDownY = eventDesc.getY();
592 CInterfaceManager *pIM = CInterfaceManager::getInstance();
593 CWidgetManager::getInstance()->setCapturePointerLeft(this); // Because we are not just a control
594 return true;
596 if (eventDesc.getEventTypeExtended() == NLGUI::CEventDescriptorMouse::mouseleftup)
598 _MouseLDown = false;
599 return true;
601 if (eventDesc.getEventTypeExtended() == NLGUI::CEventDescriptorMouse::mouserightdown)
603 _MouseRDown = true;
604 _MouseRDownX = eventDesc.getX();
605 _MouseRDownY = eventDesc.getY();
606 CInterfaceManager *pIM = CInterfaceManager::getInstance();
607 CWidgetManager::getInstance()->setCapturePointerRight(this); // Because we are not just a control
608 return true;
610 if (eventDesc.getEventTypeExtended() == NLGUI::CEventDescriptorMouse::mouserightup)
612 _MouseRDown = false;
613 return true;
615 if (eventDesc.getEventTypeExtended() == NLGUI::CEventDescriptorMouse::mousemove)
617 if (_MouseLDown)
619 sint32 dx = eventDesc.getX() - _MouseLDownX;
620 sint32 dy = eventDesc.getY() - _MouseLDownY;
621 mouseLMove (dx,dy);
622 _MouseLDownX = eventDesc.getX();
623 _MouseLDownY = eventDesc.getY();
625 if (_MouseRDown)
627 sint32 dx = eventDesc.getX() - _MouseRDownX;
628 sint32 dy = eventDesc.getY() - _MouseRDownY;
629 mouseRMove (dx,dy);
630 _MouseRDownX = eventDesc.getX();
631 _MouseRDownY = eventDesc.getY();
633 return true;
636 return false;
639 // ----------------------------------------------------------------------------
640 void CInterface3DScene::mouseLMove (sint32 dx, sint32 dy)
642 const CInterface3DScene *pI3DS = (_Ref3DScene != NULL) ? _Ref3DScene : this;
643 CInterface3DCamera *pI3DCam = pI3DS->_Cameras[_CurrentCamera];
644 float ang = pI3DCam->getRotY() + ((float)dy)*_RotYFactor;
645 clamp (ang, _RotYLimitMin, _RotYLimitMax);
646 pI3DCam->setRotY (ang);
647 ang = pI3DCam->getRotZ() - ((float)dx)*_RotZFactor;
648 clamp (ang ,_RotZLimitMin, _RotZLimitMax);
649 pI3DCam->setRotZ (ang);
652 // ----------------------------------------------------------------------------
653 void CInterface3DScene::mouseRMove (sint32 /* dx */, sint32 dy)
655 const CInterface3DScene *pI3DS = (_Ref3DScene != NULL) ? _Ref3DScene : this;
656 CInterface3DCamera *pI3DCam = pI3DS->_Cameras[_CurrentCamera];
657 float dist = pI3DCam->getDist() - ((float)dy)*_DistFactor;
658 clamp (dist, _DistLimitMin, _DistLimitMax);
659 pI3DCam->setDist (dist);
662 // ----------------------------------------------------------------------------
663 CInterfaceElement* CInterface3DScene::getElement (const string &id)
665 if (id == getId())
666 return this;
668 string sTmp = id.substr(0, getId().size());
669 //if (sTmp != getId()) return NULL;
671 uint i;
673 for (i = 0; i < _Characters.size(); ++i)
674 if (id == _Characters[i]->getId() || id == toString("character#%d", i))
675 return _Characters[i];
677 for (i = 0; i < _IGs.size(); ++i)
678 if (id == _IGs[i]->getId())
679 return _IGs[i];
681 for (i = 0; i < _Shapes.size(); ++i) {
682 if (id == _Shapes[i]->getId() || id == toString("shape#%d", i))
683 return _Shapes[i];
686 for (i = 0; i < _Cameras.size(); ++i)
687 if (id == _Cameras[i]->getId() || id == toString("camera#%d", i))
688 return _Cameras[i];
690 for (i = 0; i < _Lights.size(); ++i)
691 if (id == _Lights[i]->getId())
692 return _Lights[i];
694 for (i = 0; i < _FXs.size(); ++i)
695 if (id == _FXs[i]->getId())
696 return _FXs[i];
698 return NULL;
701 int CInterface3DScene::luaGetElement(CLuaState &ls)
703 CLuaIHM::checkArgCount(ls, "CInterfaceGroup::find", 1);
704 CLuaIHM::checkArgType(ls, "CInterfaceGroup::find", 1, LUA_TSTRING);
705 std::string id = ls.toString(1);
706 CInterfaceElement* element = getElement(id);
707 if (!element)
709 ls.pushNil();
711 else
713 CLuaIHM::pushUIOnStack(ls, element);
715 return 1;
718 // ----------------------------------------------------------------------------
719 string CInterface3DScene::getCurrentCamera() const
721 const CInterface3DScene *pI3DS = (_Ref3DScene != NULL) ? _Ref3DScene : this;
722 string name = pI3DS->_Cameras[_CurrentCamera]->getId();
723 name = name.substr(name.rfind(':'));
724 return name;
727 // ----------------------------------------------------------------------------
728 void CInterface3DScene::setCurrentCamera (const string &name)
730 CInterfaceManager *pIM = CInterfaceManager::getInstance();
731 CInterface3DScene *pI3DS = (_Ref3DScene != NULL) ? _Ref3DScene : this;
732 CInterfaceElement *pIE = CWidgetManager::getInstance()->getElementFromId(pI3DS->getId(), name);
733 CInterface3DCamera *pI3DCam = dynamic_cast<CInterface3DCamera*>(pIE);
734 if (pI3DCam != NULL)
736 uint i = 0;
737 for (i = 0; i < pI3DS->_Cameras.size(); ++i)
738 if (pI3DS->_Cameras[i] == pI3DCam)
739 break;
740 if (i != pI3DS->_Cameras.size())
741 _CurrentCamera = i;
745 // ----------------------------------------------------------------------------
746 string CInterface3DScene::getCurrentClusterSystem () const
748 const CInterface3DScene *pI3DS = (_Ref3DScene != NULL) ? _Ref3DScene : this;
749 string name = pI3DS->_IGs[_CurrentCS]->getId();
750 name = name.substr(name.rfind(':'));
751 return name;
754 // ----------------------------------------------------------------------------
755 void CInterface3DScene::setCurrentClusterSystem(const string &sCSName)
757 CInterfaceManager *pIM = CInterfaceManager::getInstance();
758 CInterface3DScene *pI3DS = (_Ref3DScene != NULL) ? _Ref3DScene : this;
759 CInterfaceElement *pIE = CWidgetManager::getInstance()->getElementFromId(pI3DS->getId(), sCSName);
760 CInterface3DIG *pI3DIG = dynamic_cast<CInterface3DIG*>(pIE);
761 if (pI3DIG != NULL)
763 uint i = 0;
764 for (i = 0; i < pI3DS->_IGs.size(); ++i)
765 if (pI3DS->_IGs[i] == pI3DIG)
766 break;
767 if (i != pI3DS->_IGs.size())
768 _CurrentCS = i;
772 // ----------------------------------------------------------------------------
773 void CInterface3DScene::remove(NL3D::UInstanceGroup *pIG)
775 uint32 i;
776 for (i = 0; i < _Characters.size(); ++i)
777 _Characters[i]->setClusterSystem ((UInstanceGroup*)NULL);
778 for (i = 0; i < _Shapes.size(); ++i)
779 _Shapes[i]->getShape().setClusterSystem ((UInstanceGroup*)NULL);
780 for (i = 0; i < _FXs.size(); ++i)
781 if (!_FXs[i]->getPS().empty())
782 _FXs[i]->getPS().setClusterSystem ((UInstanceGroup*)NULL);
784 CInterface3DScene *pDisp = this;
785 if (_Ref3DScene != NULL)
786 pDisp = _Ref3DScene;
787 if (pDisp->_Scene == NULL)
788 return;
789 NL3D::UCamera cam = pDisp->_Scene->getCam();
790 cam.setClusterSystem ((UInstanceGroup*)NULL);
792 pIG->removeFromScene(*_Scene);
793 _Scene->deleteInstanceGroup(pIG);
796 // ----------------------------------------------------------------------------
797 // CInterface3DCharacter
798 // ----------------------------------------------------------------------------
800 // ----------------------------------------------------------------------------
801 CInterface3DCharacter::CInterface3DCharacter()
803 _Char3D = NULL;
806 // ----------------------------------------------------------------------------
807 CInterface3DCharacter::~CInterface3DCharacter()
809 delete _Char3D;
812 // ----------------------------------------------------------------------------
813 bool CInterface3DCharacter::parse (xmlNodePtr cur, CInterfaceGroup *parentGroup)
815 if (!CInterfaceElement::parse(cur, parentGroup))
816 return false;
818 CXMLAutoPtr ptr((const char*)xmlGetProp (cur, (xmlChar*)"dblink"));
819 _DBLink.clear();
820 if (ptr) _DBLink = (const char *)ptr;
822 CVector pos(0,0,0), rot(0,0,0);
823 ptr = xmlGetProp (cur, (xmlChar*)"pos");
824 if (ptr) pos = convertVector(ptr);
826 ptr = xmlGetProp (cur, (xmlChar*)"rot");
827 if (ptr) rot = convertVector(ptr);
829 bool copyAnim = false;
830 ptr = xmlGetProp (cur, (xmlChar*)"copy_anim");
831 if (ptr) copyAnim = convertBool(ptr);
833 _Char3D = new CCharacter3D;
834 _Char3D->copyAnimation(copyAnim);
835 _Char3D->init (dynamic_cast<CInterface3DScene*>(parentGroup)->getScene());
836 _Char3D->setPos (pos.x, pos.y, pos.z);
837 _Char3D->setRotEuler ( rot.x * ((float)(NLMISC::Pi / 180)),
838 rot.y * ((float)(NLMISC::Pi / 180)),
839 rot.z * ((float)(NLMISC::Pi / 180)) );
840 checkCoords();
842 return true;
845 // ----------------------------------------------------------------------------
846 void CInterface3DCharacter::checkCoords()
848 if (_Char3D)
850 SCharacter3DSetup c3Ds = _Char3D->getCurrentSetup();
851 if ((_DBLink.empty()) || (_DBLink == "player"))
852 c3Ds.setupFromSERVERDataBase();
853 else if (_DBLink == "target")
855 if (UserEntity != NULL)
857 CEntityCL *selection = EntitiesMngr.entity(UserEntity->selection());
858 if (selection != NULL)
859 c3Ds.setupFromSERVERDataBase(selection->slot());
862 else
863 c3Ds.setupFromDataBase (_DBLink);
864 _Char3D->setup (c3Ds);
865 _Char3D->animate (TimeInSec);
869 // ----------------------------------------------------------------------------
870 void CInterface3DCharacter::setupCharacter3D(sint32 slot)
872 SCharacter3DSetup c3Ds = _Char3D->getCurrentSetup();
873 c3Ds.setupFromSERVERDataBase((uint8)slot);
874 _Char3D->setup(c3Ds);
877 // ----------------------------------------------------------------------------
878 int CInterface3DCharacter::luaSetupCharacter3D(CLuaState &ls)
880 const char *funcName = "setupCharacter3D";
881 CLuaIHM::checkArgCount(ls, funcName, 1);
882 CLuaIHM::checkArgType(ls, funcName, 1, LUA_TNUMBER);
883 setupCharacter3D((sint32) ls.toInteger(1));
884 return 0;
887 // ----------------------------------------------------------------------------
888 int CInterface3DCharacter::luaEnableLOD(CLuaState &ls)
890 const char *funcName = "enableLOD";
891 CLuaIHM::checkArgCount(ls, funcName, 1);
892 CLuaIHM::checkArgType(ls, funcName, 1, LUA_TBOOLEAN);
893 if (!_Char3D->getSkeleton().empty())
895 _Char3D->getSkeleton().enableLOD(ls.toBoolean(1));
897 return 0;
900 // ----------------------------------------------------------------------------
901 void CInterface3DCharacter::setClusterSystem (UInstanceGroup *pIG)
903 if (_Char3D != NULL)
904 _Char3D->setClusterSystem (pIG);
907 // ----------------------------------------------------------------------------
908 float CInterface3DCharacter::getPosX () const
910 if (_Char3D == NULL) return 0.0;
911 float x, y ,z;
912 _Char3D->getPos (x, y, z);
913 return x;
916 // ----------------------------------------------------------------------------
917 float CInterface3DCharacter::getPosY () const
919 if (_Char3D == NULL) return 0.0;
920 float x, y ,z;
921 _Char3D->getPos (x, y, z);
922 return y;
925 // ----------------------------------------------------------------------------
926 float CInterface3DCharacter::getPosZ () const
928 if (_Char3D == NULL) return 0.0;
929 float x, y ,z;
930 _Char3D->getPos (x, y, z);
931 return z;
934 // ----------------------------------------------------------------------------
935 void CInterface3DCharacter::setPosX (float f)
937 if (_Char3D == NULL) return;
938 float x, y ,z;
939 _Char3D->getPos(x, y, z);
940 x = f;
941 _Char3D->setPos(x, y, z);
944 // ----------------------------------------------------------------------------
945 void CInterface3DCharacter::setPosY (float f)
947 if (_Char3D == NULL) return;
948 float x, y ,z;
949 _Char3D->getPos(x, y, z);
950 y = f;
951 _Char3D->setPos(x, y, z);
954 // ----------------------------------------------------------------------------
955 void CInterface3DCharacter::setPosZ (float f)
957 if (_Char3D == NULL) return;
958 float x, y ,z;
959 _Char3D->getPos(x, y, z);
960 z = f;
961 _Char3D->setPos(x, y, z);
964 // ----------------------------------------------------------------------------
965 float CInterface3DCharacter::getRotX () const
967 if (_Char3D == NULL) return 0.0;
968 float x, y ,z;
969 _Char3D->getRotEuler(x, y, z);
970 return x / ((float)(NLMISC::Pi / 180));
973 // ----------------------------------------------------------------------------
974 float CInterface3DCharacter::getRotY () const
976 if (_Char3D == NULL) return 0.0;
977 float x, y ,z;
978 _Char3D->getRotEuler(x, y, z);
979 return y / ((float)(NLMISC::Pi / 180));
982 // ----------------------------------------------------------------------------
983 float CInterface3DCharacter::getRotZ () const
985 if (_Char3D == NULL) return 0.0;
986 float x, y ,z;
987 _Char3D->getRotEuler(x, y, z);
988 return z / ((float)(NLMISC::Pi / 180));
991 // ----------------------------------------------------------------------------
992 void CInterface3DCharacter::setRotX (float f)
994 if (_Char3D == NULL) return;
995 float x, y ,z;
996 _Char3D->getRotEuler(x, y, z);
997 x = f * ((float)(NLMISC::Pi / 180));
998 _Char3D->setRotEuler(x, y, z);
1001 // ----------------------------------------------------------------------------
1002 void CInterface3DCharacter::setRotY (float f)
1004 if (_Char3D == NULL) return;
1005 float x, y ,z;
1006 _Char3D->getRotEuler(x, y, z);
1007 y = f * ((float)(NLMISC::Pi / 180));
1008 _Char3D->setRotEuler(x, y, z);
1011 // ----------------------------------------------------------------------------
1012 void CInterface3DCharacter::setRotZ (float f)
1014 if (_Char3D == NULL) return;
1015 float x, y ,z;
1016 _Char3D->getRotEuler(x, y, z);
1017 z = f * ((float)(NLMISC::Pi / 180));
1018 _Char3D->setRotEuler(x, y, z);
1021 // ----------------------------------------------------------------------------
1022 float CInterface3DCharacter::getHeadX () const
1024 if (_Char3D == NULL) return 0.0;
1025 float x, y ,z;
1026 _Char3D->getHeadPos (x, y, z);
1027 return x;
1030 // ----------------------------------------------------------------------------
1031 float CInterface3DCharacter::getHeadY () const
1033 if (_Char3D == NULL) return 0.0;
1034 float x, y ,z;
1035 _Char3D->getHeadPos (x, y, z);
1036 return y;
1039 // ----------------------------------------------------------------------------
1040 float CInterface3DCharacter::getHeadZ () const
1042 if (_Char3D == NULL) return 0.0;
1043 float x, y ,z;
1044 _Char3D->getHeadPos (x, y, z);
1045 return z;
1048 // ----------------------------------------------------------------------------
1049 void CInterface3DCharacter::setAnim (sint32 anim)
1051 if (_Char3D)
1052 _Char3D->setAnim(anim);
1054 checkCoords();
1057 // ----------------------------------------------------------------------------
1058 void CInterface3DCharacter::setPeople(const std::string & people)
1060 _Char3D->setPeople(EGSPD::CPeople::fromString(people));
1063 // ----------------------------------------------------------------------------
1064 std::string CInterface3DCharacter::getPeople() const
1066 return EGSPD::CPeople::toString(_Char3D->getPeople());
1069 // ----------------------------------------------------------------------------
1070 void CInterface3DCharacter::setSex(bool male)
1072 _Char3D->setSex(male);
1075 // ----------------------------------------------------------------------------
1076 bool CInterface3DCharacter::getSex() const
1078 return _Char3D->getSex();
1082 // ----------------------------------------------------------------------------
1083 // CInterface3DIG
1084 // ----------------------------------------------------------------------------
1086 // ----------------------------------------------------------------------------
1087 CInterface3DIG::~CInterface3DIG()
1089 CInterface3DScene *pI3DS = dynamic_cast<CInterface3DScene*>(_Parent);
1090 nlassert(pI3DS != NULL);
1091 if (_IG)
1093 _IG->removeFromScene(*pI3DS->getScene());
1094 pI3DS->getScene()->deleteInstanceGroup(_IG);
1095 _IG = NULL;
1099 // ----------------------------------------------------------------------------
1100 bool CInterface3DIG::parse (xmlNodePtr cur, CInterfaceGroup *parentGroup)
1102 if (!CInterfaceElement::parse(cur, parentGroup))
1103 return false;
1105 CXMLAutoPtr ptr((const char*)xmlGetProp (cur, (xmlChar*)"pos"));
1106 if (ptr) _Pos = convertVector(ptr);
1108 ptr = xmlGetProp (cur, (xmlChar*)"rot");
1109 if (ptr) _Rot = convertVector(ptr);
1111 ptr = xmlGetProp (cur, (xmlChar*)"name");
1112 if (ptr) _Name = toLowerAscii((const char*)ptr);
1114 _IG = UInstanceGroup::createInstanceGroup(_Name);
1115 if (_IG == NULL)
1116 return true; // Create anyway
1117 _IG->setPos (_Pos);
1118 //_IG->setRot (_Rot);
1119 setRotX (_Rot.x);
1120 setRotY (_Rot.y);
1121 setRotZ (_Rot.z);
1122 _IG->addToScene (*dynamic_cast<CInterface3DScene*>(parentGroup)->getScene(), CViewRenderer::getInstance()->getDriver() );
1123 dynamic_cast<CInterface3DScene*>(parentGroup)->getScene()->setToGlobalInstanceGroup (_IG);
1125 return true;
1128 // ----------------------------------------------------------------------------
1129 float CInterface3DIG::getPosX () const
1131 return _Pos.x;
1134 // ----------------------------------------------------------------------------
1135 float CInterface3DIG::getPosY () const
1137 return _Pos.y;
1140 // ----------------------------------------------------------------------------
1141 float CInterface3DIG::getPosZ () const
1143 return _Pos.z;
1146 // ----------------------------------------------------------------------------
1147 void CInterface3DIG::setPosX (float f)
1149 _Pos.x = f;
1150 if (_IG != NULL) _IG->setPos(_Pos);
1153 // ----------------------------------------------------------------------------
1154 void CInterface3DIG::setPosY (float f)
1156 _Pos.y = f;
1157 if (_IG != NULL) _IG->setPos(_Pos);
1160 // ----------------------------------------------------------------------------
1161 void CInterface3DIG::setPosZ (float f)
1163 _Pos.z = f;
1164 if (_IG != NULL) _IG->setPos(_Pos);
1167 // ----------------------------------------------------------------------------
1168 float CInterface3DIG::getRotX () const
1170 return _Rot.x;
1173 // ----------------------------------------------------------------------------
1174 float CInterface3DIG::getRotY () const
1176 return _Rot.y;
1179 // ----------------------------------------------------------------------------
1180 float CInterface3DIG::getRotZ () const
1182 return _Rot.z;
1185 // ----------------------------------------------------------------------------
1186 void CInterface3DIG::setRotX (float f)
1188 _Rot.x = f;
1189 CMatrix m;
1190 m.identity();
1191 m.setRot (_Rot,CMatrix::XYZ);
1192 CQuat q = m.getRot();
1193 _IG->setRotQuat (q);
1196 // ----------------------------------------------------------------------------
1197 void CInterface3DIG::setRotY (float f)
1199 _Rot.y = f;
1200 CMatrix m;
1201 m.identity();
1202 m.setRot (_Rot,CMatrix::XYZ);
1203 CQuat q = m.getRot();
1204 _IG->setRotQuat (q);
1207 // ----------------------------------------------------------------------------
1208 void CInterface3DIG::setRotZ (float f)
1210 _Rot.z = f;
1211 CMatrix m;
1212 m.identity();
1213 m.setRot (_Rot,CMatrix::XYZ);
1214 CQuat q = m.getRot();
1215 _IG->setRotQuat (q);
1218 // ----------------------------------------------------------------------------
1219 std::string CInterface3DIG::getName() const
1221 return _Name;
1224 // ----------------------------------------------------------------------------
1225 void CInterface3DIG::setName (const std::string &ht)
1227 string lwrname = toLowerAscii(ht);
1228 if (lwrname != _Name)
1230 CInterface3DScene *pI3DS = dynamic_cast<CInterface3DScene*>(_Parent);
1231 nlassert(pI3DS != NULL);
1233 if (_IG != NULL)
1235 pI3DS->remove(_IG);
1236 _IG = NULL;
1239 _Name = lwrname;
1240 _IG = UInstanceGroup::createInstanceGroup(_Name);
1241 if (_IG == NULL) return;
1242 _IG->setPos (_Pos);
1243 _IG->addToScene (*pI3DS->getScene(), CViewRenderer::getInstance()->getDriver() );
1244 pI3DS->getScene()->setToGlobalInstanceGroup (_IG);
1248 // ----------------------------------------------------------------------------
1249 // CInterface3DShape
1250 // ----------------------------------------------------------------------------
1252 // ----------------------------------------------------------------------------
1253 CInterface3DShape::~CInterface3DShape()
1255 CInterface3DScene *pI3DS = dynamic_cast<CInterface3DScene*>(_Parent);
1256 nlassert(pI3DS != NULL);
1257 if (!_Instance.empty())
1258 pI3DS->getScene()->deleteInstance(_Instance);
1259 if (!_Skeleton.empty())
1260 pI3DS->getScene()->deleteSkeleton(_Skeleton);
1261 if (_PlayListManager)
1263 _PlayListManager->deletePlayList(_PlayList);
1264 _PlayList = NULL;
1265 CViewRenderer::getInstance()->getDriver()->deleteAnimationSet(_AnimationSet);
1266 _AnimationSet = NULL;
1267 pI3DS->getScene()->deletePlayListManager(_PlayListManager);
1268 _PlayListManager = NULL;
1272 // ----------------------------------------------------------------------------
1273 bool CInterface3DShape::parse (xmlNodePtr cur, CInterfaceGroup *parentGroup)
1275 if (!CInterfaceElement::parse(cur, parentGroup))
1276 return false;
1278 CXMLAutoPtr ptr((const char*)xmlGetProp (cur, (xmlChar*)"pos"));
1279 if (ptr) _Pos = convertVector(ptr);
1281 ptr = xmlGetProp (cur, (xmlChar*)"rot");
1282 if (ptr) _Rot = convertVector(ptr);
1284 ptr = xmlGetProp (cur, (xmlChar*)"name");
1285 if (ptr) _Name = toLowerAscii((const char*)ptr);
1287 _Instance = dynamic_cast<CInterface3DScene*>(parentGroup)->getScene()->createInstance(_Name);
1288 if (_Instance.empty())
1289 return false;
1291 _Instance.setTransformMode(UTransformable::RotEuler);
1292 _Instance.setPos (_Pos);
1293 _Instance.setRotEuler (_Rot.x, _Rot.y, _Rot.z);
1295 return true;
1299 // ----------------------------------------------------------------------------
1300 void CInterface3DShape::checkCoords()
1302 if (_PlayListManager)
1303 _PlayListManager->animate(TimeInSec);
1307 float CInterface3DShape::getBBoxSizeX () const
1309 CAABBox bbox;
1310 _Instance.getShapeAABBox(bbox);
1312 if (bbox.getCenter() == CVector::Null)
1313 return -0.5f;
1315 return bbox.getMax().x - bbox.getMin().x;
1318 float CInterface3DShape::getBBoxSizeY () const
1320 CAABBox bbox;
1321 _Instance.getShapeAABBox(bbox);
1323 if (bbox.getCenter() == CVector::Null)
1324 return -0.5f;
1326 return bbox.getMax().y - bbox.getMin().y;
1329 float CInterface3DShape::getBBoxSizeZ () const
1331 CAABBox bbox;
1332 _Instance.getShapeAABBox(bbox);
1334 if (bbox.getCenter() == CVector::Null)
1335 return -0.5f;
1337 return bbox.getMax().z - bbox.getMin().z;
1340 // ----------------------------------------------------------------------------
1341 float CInterface3DShape::getPosX () const
1343 return _Pos.x;
1346 // ----------------------------------------------------------------------------
1347 float CInterface3DShape::getPosY () const
1349 return _Pos.y;
1352 // ----------------------------------------------------------------------------
1353 float CInterface3DShape::getPosZ () const
1355 return _Pos.z;
1358 // ----------------------------------------------------------------------------
1359 void CInterface3DShape::setPosX (float f)
1361 _Pos.x = f;
1362 if (!_Instance.empty()) _Instance.setPos(_Pos);
1365 // ----------------------------------------------------------------------------
1366 void CInterface3DShape::setPosY (float f)
1368 _Pos.y = f;
1369 if (!_Instance.empty()) _Instance.setPos(_Pos);
1372 // ----------------------------------------------------------------------------
1373 void CInterface3DShape::setPosZ (float f)
1375 _Pos.z = f;
1376 if (!_Skeleton.empty()) _Skeleton.setPos(_Pos);
1377 if (!_Instance.empty()) _Instance.setPos(_Pos);
1380 // ----------------------------------------------------------------------------
1381 float CInterface3DShape::getRotX () const
1383 return _Rot.x / ((float)(NLMISC::Pi / 180));
1386 // ----------------------------------------------------------------------------
1387 float CInterface3DShape::getRotY () const
1389 return _Rot.y / ((float)(NLMISC::Pi / 180));
1392 // ----------------------------------------------------------------------------
1393 float CInterface3DShape::getRotZ () const
1395 return _Rot.z / ((float)(NLMISC::Pi / 180));
1398 // ----------------------------------------------------------------------------
1399 void CInterface3DShape::setRotX (float f)
1401 _Rot.x = f * ((float)(NLMISC::Pi / 180));
1402 if (!_Instance.empty()) _Instance.setRotEuler (_Rot.x, _Rot.y, _Rot.z);
1405 // ----------------------------------------------------------------------------
1406 void CInterface3DShape::setRotY (float f)
1408 _Rot.y = f * ((float)(NLMISC::Pi / 180));
1409 if (!_Instance.empty()) _Instance.setRotEuler (_Rot.x, _Rot.y, _Rot.z);
1412 // ----------------------------------------------------------------------------
1413 void CInterface3DShape::setRotZ (float f)
1415 _Rot.z = f * ((float)(NLMISC::Pi / 180));
1416 if (!_Instance.empty()) _Instance.setRotEuler (_Rot.x, _Rot.y, _Rot.z);
1419 // ----------------------------------------------------------------------------
1420 std::string CInterface3DShape::getName() const
1422 return _Name;
1425 // ----------------------------------------------------------------------------
1426 void CInterface3DShape::setName (const std::string &ht)
1428 if (ht.empty())
1430 CInterface3DScene *pI3DS = dynamic_cast<CInterface3DScene*>(_Parent);
1431 nlassert(pI3DS != NULL);
1433 if (!_Instance.empty())
1435 pI3DS->getScene()->deleteInstance(_Instance);
1437 return;
1438 _Name.clear();
1441 string lwrname = toLowerAscii(ht);
1442 if (lwrname != _Name)
1444 CInterface3DScene *pI3DS = dynamic_cast<CInterface3DScene*>(_Parent);
1445 nlassert(pI3DS != NULL);
1447 if (!_Instance.empty())
1449 pI3DS->getScene()->deleteInstance(_Instance);
1452 _Name = lwrname;
1453 _Instance = pI3DS->getScene()->createInstance(_Name);
1454 if (_Instance.empty()) return;
1455 _Instance.setTransformMode(UTransformable::RotEuler);
1456 _Instance.setPos (_Pos);
1457 _Instance.setRotEuler (_Rot.x, _Rot.y, _Rot.z);
1461 std::string CInterface3DShape::getTextures() const
1463 return _Textures;
1467 void CInterface3DShape::setTextures(const std::string &textures)
1469 if (textures.empty())
1470 return;
1472 _Textures = textures;
1473 vector<string> texList;
1474 splitString(textures, " ", texList);
1476 for(uint j=0;j<_Instance.getNumMaterials();j++)
1478 sint numStages = _Instance.getMaterial(j).getLastTextureStage() + 1;
1479 for(sint l = 0; l < numStages; l++)
1481 if (_Instance.getMaterial(j).isTextureFile((uint) l))
1483 _Instance.getMaterial(j).setTextureFileName(texList[std::min((int)j, (int)texList.size()-1)], (uint) l);
1489 std::string CInterface3DShape::getSkeleton() const
1491 return _SkeletonName;
1495 void CInterface3DShape::setSkeleton(const std::string &skeleton)
1497 if (skeleton.empty())
1498 return;
1499 _SkeletonName = skeleton;
1501 CInterface3DScene *pI3DS = dynamic_cast<CInterface3DScene*>(_Parent);
1502 nlassert(pI3DS != NULL);
1504 if (!_Skeleton.empty())
1505 pI3DS->getScene()->deleteSkeleton(_Skeleton);
1507 _Skeleton = pI3DS->getScene()->createSkeleton(skeleton);
1508 if (!_Skeleton.empty())
1510 _Skeleton.bindSkin(_Instance);
1511 // _Skeleton.setTransformMode(UTransformable::RotEuler);
1512 // _Skeleton.setPos(_Pos);
1513 // _Skeleton.setRotEuler(_Rot.x, _Rot.y, _Rot.z);
1517 std::string CInterface3DShape::getAnim() const
1519 return _Anim;
1523 void CInterface3DShape::setAnim(const std::string &anim)
1525 CInterface3DScene *pI3DS = dynamic_cast<CInterface3DScene*>(_Parent);
1526 nlassert(pI3DS != NULL);
1528 if (!_PlayListManager)
1529 _PlayListManager = pI3DS->getScene()->createPlayListManager();
1530 if (!_AnimationSet)
1531 _AnimationSet = CViewRenderer::getInstance()->getDriver()->createAnimationSet();
1533 uint idAnim = 0;
1536 idAnim = _AnimationSet->addAnimation( (anim+".anim").c_str(), anim.c_str() );
1538 catch(Exception &)
1540 nlwarning( "CInterface3DShape::setAnim Animation %s not found", anim.c_str() );
1541 return;
1544 _AnimationSet->build();
1545 _PlayList = _PlayListManager->createPlayList( _AnimationSet );
1547 if (!_Skeleton.empty())
1548 _PlayList->registerTransform( _Skeleton );
1549 else
1550 _PlayList->registerTransform( _Instance );
1552 _PlayList->setAnimation(0, idAnim);
1553 _PlayList->setTimeOrigin(0, 0 );
1554 _PlayList->setSpeedFactor( 0, 1 );
1555 _PlayListManager->animate( 0 );
1561 // ----------------------------------------------------------------------------
1562 // CInterface3DCamera
1563 // ----------------------------------------------------------------------------
1565 // ----------------------------------------------------------------------------
1566 bool CInterface3DCamera::parse (xmlNodePtr cur, CInterfaceGroup *parentGroup)
1568 if (!CInterfaceElement::parse(cur, parentGroup))
1569 return false;
1571 CXMLAutoPtr ptr((const char*)xmlGetProp (cur, (xmlChar*)"pos"));
1572 if (ptr) _Pos = convertVector(ptr);
1573 ptr = xmlGetProp (cur, (xmlChar*)"target");
1574 if (ptr) _Target = convertVector(ptr);
1575 ptr = xmlGetProp (cur, (xmlChar*)"fov");
1576 if (ptr) fromString((const char*)ptr, _FOV);
1577 ptr = xmlGetProp (cur, (xmlChar*)"roll");
1578 if (ptr) fromString((const char*)ptr, _Roll);
1580 return true;
1583 // ----------------------------------------------------------------------------
1584 void CInterface3DCamera::reset()
1586 setTgtX(getTgtX());
1587 _Rot = NLMISC::CVector(0,0,0);
1590 // ----------------------------------------------------------------------------
1591 // CInterface3DLight
1592 // ----------------------------------------------------------------------------
1594 // ----------------------------------------------------------------------------
1595 CInterface3DLight::~CInterface3DLight()
1597 CInterface3DScene *pI3DS = dynamic_cast<CInterface3DScene *>(_Parent);
1598 nlassert(pI3DS != NULL);
1599 pI3DS->getScene()->deletePointLight(_Light);
1602 // ----------------------------------------------------------------------------
1603 bool CInterface3DLight::parse (xmlNodePtr cur, CInterfaceGroup *parentGroup)
1605 if (!CInterfaceElement::parse(cur, parentGroup))
1606 return false;
1608 _Light = dynamic_cast<CInterface3DScene*>(parentGroup)->getScene()->createPointLight();
1610 CXMLAutoPtr ptr((const char*)xmlGetProp (cur, (xmlChar*)"pos"));
1611 if (ptr) _Pos = convertVector(ptr);
1612 ptr = xmlGetProp (cur, (xmlChar*)"color");
1613 if (ptr) _Color = convertColor(ptr);
1614 ptr = xmlGetProp (cur, (xmlChar*)"near");
1615 if (ptr) fromString((const char*)ptr, _Near);
1616 ptr = xmlGetProp (cur, (xmlChar*)"far");
1617 if (ptr) fromString((const char*)ptr, _Far);
1619 _Light.setPos(_Pos);
1620 _Light.setAmbient (CRGBA(0,0,0));
1621 _Light.setDiffuse (CRGBA(255,255,255));
1622 _Light.setSpecular (CRGBA(255,255,255));
1623 _Light.setColor (_Color);
1624 _Light.setupAttenuation (_Near, _Far);
1626 return true;
1629 // ----------------------------------------------------------------------------
1630 void CInterface3DLight::setPosX(float f)
1632 _Pos.x = f;
1633 _Light.setPos(_Pos);
1636 // ----------------------------------------------------------------------------
1637 void CInterface3DLight::setPosY(float f)
1639 _Pos.y = f;
1640 _Light.setPos(_Pos);
1643 // ----------------------------------------------------------------------------
1644 void CInterface3DLight::setPosZ(float f)
1646 _Pos.z = f;
1647 _Light.setPos(_Pos);
1650 // ----------------------------------------------------------------------------
1651 void CInterface3DLight::setNear(float f)
1653 _Near = f;
1654 _Light.setupAttenuation (_Near, _Far);
1657 // ----------------------------------------------------------------------------
1658 void CInterface3DLight::setFar(float f)
1660 _Far = f;
1661 _Light.setupAttenuation (_Near, _Far);
1664 // ----------------------------------------------------------------------------
1665 void CInterface3DLight::setColR(sint32 f)
1667 _Color.R = (uint8)f;
1668 _Light.setColor (_Color);
1671 // ----------------------------------------------------------------------------
1672 void CInterface3DLight::setColG(sint32 f)
1674 _Color.G = (uint8)f;
1675 _Light.setColor (_Color);
1678 // ----------------------------------------------------------------------------
1679 void CInterface3DLight::setColB(sint32 f)
1681 _Color.B = (uint8)f;
1682 _Light.setColor (_Color);
1685 // ----------------------------------------------------------------------------
1686 // CInterface3DFX
1687 // ----------------------------------------------------------------------------
1689 // ----------------------------------------------------------------------------
1690 CInterface3DFX::~CInterface3DFX()
1694 // ----------------------------------------------------------------------------
1695 bool CInterface3DFX::parse (xmlNodePtr cur, CInterfaceGroup *parentGroup)
1697 if (!CInterfaceElement::parse(cur, parentGroup))
1698 return false;
1700 CXMLAutoPtr ptr((const char*)xmlGetProp (cur, (xmlChar*)"pos"));
1701 if (ptr) _Pos = convertVector(ptr);
1703 ptr = xmlGetProp (cur, (xmlChar*)"rot");
1704 if (ptr) _Rot = convertVector(ptr);
1706 ptr = xmlGetProp (cur, (xmlChar*)"name");
1707 if (ptr) _Name = toLowerAscii((const char*)ptr);
1709 return true;
1712 // ----------------------------------------------------------------------------
1713 void CInterface3DFX::checkCoords()
1715 if (!_FX.empty())
1717 if (!_FX.isValid())
1719 CInterface3DScene *pI3DS = dynamic_cast<CInterface3DScene*>(_Parent);
1720 nlassert(pI3DS != NULL);
1721 pI3DS->getScene()->deleteInstance(_FX);
1726 // ----------------------------------------------------------------------------
1727 float CInterface3DFX::getPosX () const
1729 return _Pos.x;
1732 // ----------------------------------------------------------------------------
1733 float CInterface3DFX::getPosY () const
1735 return _Pos.y;
1738 // ----------------------------------------------------------------------------
1739 float CInterface3DFX::getPosZ () const
1741 return _Pos.z;
1744 // ----------------------------------------------------------------------------
1745 void CInterface3DFX::setPosX (float f)
1747 _Pos.x = f;
1748 if (!_FX.empty()) _FX.setPos (_Pos);
1751 // ----------------------------------------------------------------------------
1752 void CInterface3DFX::setPosY (float f)
1754 _Pos.y = f;
1755 if (!_FX.empty()) _FX.setPos (_Pos);
1758 // ----------------------------------------------------------------------------
1759 void CInterface3DFX::setPosZ (float f)
1761 _Pos.z = f;
1762 if (!_FX.empty()) _FX.setPos (_Pos);
1765 // ----------------------------------------------------------------------------
1766 float CInterface3DFX::getRotX () const
1768 return _Rot.x / ((float)(NLMISC::Pi / 180));
1771 // ----------------------------------------------------------------------------
1772 float CInterface3DFX::getRotY () const
1774 return _Rot.y / ((float)(NLMISC::Pi / 180));
1777 // ----------------------------------------------------------------------------
1778 float CInterface3DFX::getRotZ () const
1780 return _Rot.z / ((float)(NLMISC::Pi / 180));
1783 // ----------------------------------------------------------------------------
1784 void CInterface3DFX::setRotX (float f)
1786 _Rot.x = f;
1787 if (!_FX.empty()) _FX.setRotEuler(_Rot);
1790 // ----------------------------------------------------------------------------
1791 void CInterface3DFX::setRotY (float f)
1793 _Rot.y = f;
1794 if (!_FX.empty()) _FX.setRotEuler(_Rot);
1797 // ----------------------------------------------------------------------------
1798 void CInterface3DFX::setRotZ (float f)
1800 _Rot.z = f;
1801 if (!_FX.empty()) _FX.setRotEuler(_Rot);
1804 // ----------------------------------------------------------------------------
1805 std::string CInterface3DFX::getName() const
1807 return _Name;
1810 // ----------------------------------------------------------------------------
1811 void CInterface3DFX::setName (const std::string &ht)
1813 _Name = ht;
1814 if (!_FX.empty())
1815 setStarted (true);
1818 // ----------------------------------------------------------------------------
1819 bool CInterface3DFX::getStarted() const
1821 return (!_FX.empty());
1824 // ----------------------------------------------------------------------------
1825 void CInterface3DFX::setStarted (bool b)
1827 if (b == true)
1829 CInterface3DScene *pI3DS = dynamic_cast<CInterface3DScene*>(_Parent);
1830 nlassert(pI3DS != NULL);
1831 if (!_FX.empty())
1832 pI3DS->getScene()->deleteInstance(_FX);
1833 _FX.cast (pI3DS->getScene()->createInstance(_Name));
1834 if (_FX.empty())
1835 return;
1836 _FX.setTransformMode(UTransformable::RotEuler);
1837 _FX.setPos (_Pos);
1838 _FX.setRotEuler (_Rot.x, _Rot.y, _Rot.z);
1840 else
1842 CInterface3DScene *pI3DS = dynamic_cast<CInterface3DScene*>(_Parent);
1843 nlassert(pI3DS != NULL);
1844 if (!_FX.empty())
1845 pI3DS->getScene()->deleteInstance(_FX);
1846 _FX = NULL;
1853 /* end of interface_3d_scene.cpp */