llhudobject.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * @file llhudobject.cpp
  3. * @brief LLHUDObject class implementation
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llhudobject.h"
  34. #include "llfasttimer.h"
  35. #include "llvertexbuffer.h"
  36. #include "llagent.h"
  37. #include "llhudeffectlookat.h"
  38. #include "llhudeffectspiral.h"
  39. #include "llhudicon.h"
  40. #include "llhudtext.h"
  41. #include "llviewerobject.h"
  42. #include "llviewershadermgr.h"
  43. #include "llvoicevisualizer.h"
  44. // statics
  45. std::list<LLPointer<LLHUDObject> > LLHUDObject::sHUDObjects;
  46. struct hud_object_further_away
  47. {
  48. bool operator()(const LLPointer<LLHUDObject>& lhs,
  49. const LLPointer<LLHUDObject>& rhs) const;
  50. };
  51. LL_INLINE bool hud_object_further_away::operator()(const LLPointer<LLHUDObject>& lhs,
  52. const LLPointer<LLHUDObject>& rhs) const
  53. {
  54. return lhs->getDistance() > rhs->getDistance();
  55. }
  56. LLHUDObject::LLHUDObject(U8 type)
  57. : mPositionGlobal(),
  58. mSourceObject(NULL),
  59. mTargetObject(NULL),
  60. mVisible(true),
  61. mType(type),
  62. mDead(false),
  63. mOnHUDAttachment(false)
  64. {
  65. }
  66. //virtual
  67. LLHUDObject::~LLHUDObject()
  68. {
  69. // NOTE: it is important that this empty destructor is declared here
  70. // instead of = default in the header file; because of circular
  71. // dependencies in includes between llviewerobject.h and llhud*.h, the
  72. // compiler would fail when attempting to complete this class too early. HB
  73. // *TODO: solve the circular dependency issue.
  74. }
  75. void LLHUDObject::markDead()
  76. {
  77. mVisible = false;
  78. mDead = true;
  79. mSourceObject = NULL;
  80. mTargetObject = NULL;
  81. }
  82. void LLHUDObject::setSourceObject(LLViewerObject* objectp)
  83. {
  84. if (objectp != mSourceObject)
  85. {
  86. mSourceObject = objectp;
  87. }
  88. }
  89. void LLHUDObject::setTargetObject(LLViewerObject* objectp)
  90. {
  91. if (objectp != mTargetObject)
  92. {
  93. mTargetObject = objectp;
  94. }
  95. }
  96. void LLHUDObject::setPositionGlobal(const LLVector3d& position_global)
  97. {
  98. mPositionGlobal = position_global;
  99. }
  100. void LLHUDObject::setPositionAgent(const LLVector3& position_agent)
  101. {
  102. mPositionGlobal = gAgent.getPosGlobalFromAgent(position_agent);
  103. }
  104. //static
  105. void LLHUDObject::cleanupHUDObjects()
  106. {
  107. LLHUDIcon::cleanupDeadIcons();
  108. hud_object_list_t::iterator object_it;
  109. for (hud_object_list_t::iterator object_it = sHUDObjects.begin(),
  110. end = sHUDObjects.end();
  111. object_it != end; ++object_it)
  112. {
  113. LLHUDObject* hud_objp = *object_it;
  114. S32 numrefs = hud_objp->getNumRefs();
  115. S32 type = (S32)hud_objp->getType();
  116. // *HACK: voice visualizers may have 2 references on logout,
  117. // depending whether the corresponding hud effect gets destroyed before
  118. // or after the hud objects are cleaned up... which in turns depends
  119. // on the compiler being used. *TODO: explicitely destroy the HUD
  120. // effects before calling LLHUDObject::cleanupHUDObjects().
  121. if (numrefs > (type != LL_HUD_EFFECT_VOICE_VISUALIZER ? 1 : 2))
  122. {
  123. llinfos << "HUD Object " << std::hex << hud_objp << std::dec
  124. << " type " << type << " still had " << numrefs
  125. << " active references" << llendl;
  126. }
  127. hud_objp->markDead();
  128. }
  129. sHUDObjects.clear();
  130. }
  131. //static
  132. LLHUDObject* LLHUDObject::addHUDObject(U8 type)
  133. {
  134. LLHUDObject* hud_objectp = NULL;
  135. switch (type)
  136. {
  137. case LL_HUD_TEXT:
  138. hud_objectp = new LLHUDText(type);
  139. break;
  140. case LL_HUD_ICON:
  141. hud_objectp = new LLHUDIcon(type);
  142. break;
  143. default:
  144. llwarns << "Unknown type of hud object:" << (U32)type << llendl;
  145. }
  146. if (hud_objectp)
  147. {
  148. sHUDObjects.push_back(hud_objectp);
  149. }
  150. return hud_objectp;
  151. }
  152. LLHUDEffect* LLHUDObject::addHUDEffect(U8 type)
  153. {
  154. LLHUDEffect* hud_objectp = NULL;
  155. switch (type)
  156. {
  157. case LL_HUD_EFFECT_BEAM:
  158. {
  159. LLHUDEffectSpiral* spiralp = new LLHUDEffectSpiral(type);
  160. hud_objectp = spiralp;
  161. spiralp->setDuration(0.7f);
  162. spiralp->setVMag(0.f);
  163. spiralp->setVOffset(0.f);
  164. spiralp->setInitialRadius(0.1f);
  165. spiralp->setFinalRadius(0.2f);
  166. spiralp->setSpinRate(10.f);
  167. spiralp->setFlickerRate(0.f);
  168. spiralp->setScaleBase(0.05f);
  169. spiralp->setScaleVar(0.02f);
  170. spiralp->setColor(LLColor4U(255, 255, 255, 255));
  171. break;
  172. }
  173. case LL_HUD_EFFECT_POINT:
  174. {
  175. LLHUDEffectSpiral* spiralp = new LLHUDEffectSpiral(type);
  176. hud_objectp = spiralp;
  177. spiralp->setDuration(0.5f);
  178. spiralp->setVMag(1.f);
  179. spiralp->setVOffset(0.f);
  180. spiralp->setInitialRadius(0.5f);
  181. spiralp->setFinalRadius(1.f);
  182. spiralp->setSpinRate(10.f);
  183. spiralp->setFlickerRate(0.f);
  184. spiralp->setScaleBase(0.1f);
  185. spiralp->setScaleVar(0.1f);
  186. spiralp->setColor(LLColor4U(255, 255, 255, 255));
  187. break;
  188. }
  189. case LL_HUD_EFFECT_SPHERE:
  190. {
  191. LLHUDEffectSpiral* spiralp = new LLHUDEffectSpiral(type);
  192. hud_objectp = spiralp;
  193. spiralp->setDuration(0.5f);
  194. spiralp->setVMag(1.f);
  195. spiralp->setVOffset(0.f);
  196. spiralp->setInitialRadius(0.5f);
  197. spiralp->setFinalRadius(0.5f);
  198. spiralp->setSpinRate(20.f);
  199. spiralp->setFlickerRate(0.f);
  200. spiralp->setScaleBase(0.1f);
  201. spiralp->setScaleVar(0.1f);
  202. spiralp->setColor(LLColor4U(255, 255, 255, 255));
  203. break;
  204. }
  205. case LL_HUD_EFFECT_SPIRAL:
  206. {
  207. LLHUDEffectSpiral* spiralp = new LLHUDEffectSpiral(type);
  208. hud_objectp = spiralp;
  209. spiralp->setDuration(2.f);
  210. spiralp->setVMag(-2.f);
  211. spiralp->setVOffset(0.5f);
  212. spiralp->setInitialRadius(1.f);
  213. spiralp->setFinalRadius(0.5f);
  214. spiralp->setSpinRate(10.f);
  215. spiralp->setFlickerRate(20.f);
  216. spiralp->setScaleBase(0.02f);
  217. spiralp->setScaleVar(0.02f);
  218. spiralp->setColor(LLColor4U(255, 255, 255, 255));
  219. break;
  220. }
  221. case LL_HUD_EFFECT_EDIT:
  222. {
  223. LLHUDEffectSpiral* spiralp = new LLHUDEffectSpiral(type);
  224. hud_objectp = spiralp;
  225. spiralp->setDuration(2.f);
  226. spiralp->setVMag(2.f);
  227. spiralp->setVOffset(-1.f);
  228. spiralp->setInitialRadius(1.5f);
  229. spiralp->setFinalRadius(1.f);
  230. spiralp->setSpinRate(4.f);
  231. spiralp->setFlickerRate(200.f);
  232. spiralp->setScaleBase(0.1f);
  233. spiralp->setScaleVar(0.1f);
  234. spiralp->setColor(LLColor4U(255, 255, 255, 255));
  235. break;
  236. }
  237. case LL_HUD_EFFECT_LOOKAT:
  238. hud_objectp = new LLHUDEffectLookAt(type);
  239. break;
  240. case LL_HUD_EFFECT_VOICE_VISUALIZER:
  241. hud_objectp = new LLVoiceVisualizer(type);
  242. break;
  243. case LL_HUD_EFFECT_POINTAT:
  244. hud_objectp = new LLHUDEffectPointAt(type);
  245. break;
  246. default:
  247. llwarns << "Unknown type of hud effect:" << (U32)type << llendl;
  248. }
  249. if (hud_objectp)
  250. {
  251. sHUDObjects.push_back(hud_objectp);
  252. }
  253. return hud_objectp;
  254. }
  255. //static
  256. void LLHUDObject::updateAll()
  257. {
  258. LL_FAST_TIMER(FTM_HUD_OBJECTS);
  259. LLHUDText::updateAll();
  260. LLHUDIcon::updateAll();
  261. sortObjects();
  262. }
  263. //static
  264. void LLHUDObject::renderObjects()
  265. {
  266. for (hud_object_list_t::iterator object_it = sHUDObjects.begin(),
  267. end = sHUDObjects.end();
  268. object_it != end; )
  269. {
  270. hud_object_list_t::iterator cur_it = object_it++;
  271. LLHUDObject* hud_objp = *cur_it;
  272. if (hud_objp->getNumRefs() == 1)
  273. {
  274. sHUDObjects.erase(cur_it);
  275. }
  276. else if (hud_objp->isVisible())
  277. {
  278. hud_objp->render();
  279. }
  280. }
  281. }
  282. //static
  283. void LLHUDObject::renderAll()
  284. {
  285. if (gUsePBRShaders)
  286. {
  287. LLGLSUIDefault gls_ui;
  288. gUIProgram.bind();
  289. gGL.color4f(1.f, 1.f, 1.f, 1.f);
  290. LLGLDepthTest depth(GL_FALSE, GL_FALSE);
  291. renderObjects();
  292. LLVertexBuffer::unbind();
  293. gUIProgram.unbind();
  294. }
  295. else
  296. {
  297. renderObjects();
  298. LLVertexBuffer::unbind();
  299. }
  300. }
  301. //static
  302. void LLHUDObject::removeExpired()
  303. {
  304. for (hud_object_list_t::iterator object_it = sHUDObjects.begin(),
  305. end = sHUDObjects.end();
  306. object_it != end; )
  307. {
  308. hud_object_list_t::iterator cur_it = object_it++;
  309. LLHUDObject* hud_objp = *cur_it;
  310. if (hud_objp->getNumRefs() == 1)
  311. {
  312. sHUDObjects.erase(cur_it);
  313. }
  314. }
  315. }
  316. //static
  317. void LLHUDObject::sortObjects()
  318. {
  319. sHUDObjects.sort(hud_object_further_away());
  320. }