llhudicon.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**
  2. * @file llhudicon.cpp
  3. * @brief LLHUDIcon class implementation
  4. *
  5. * $LicenseInfo:firstyear=2006&license=viewergpl$
  6. *
  7. * Copyright (c) 2006-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 "llhudicon.h"
  34. #include "llgl.h"
  35. #include "llrender.h"
  36. #include "lldrawable.h"
  37. #include "llviewercamera.h"
  38. #include "llviewerobject.h"
  39. #include "llviewertexture.h"
  40. #include "llviewerwindow.h"
  41. constexpr F32 ANIM_TIME = 0.4f;
  42. constexpr F32 DIST_START_FADE = 15.f;
  43. constexpr F32 DIST_END_FADE = 30.f;
  44. constexpr F32 FADE_OUT_TIME = 1.f;
  45. // Static members
  46. F32 LLHUDIcon::MAX_VISIBLE_TIME = 15.f;
  47. LLHUDIcon::icon_instance_t LLHUDIcon::sIconInstances;
  48. // helper function
  49. static F32 calc_bouncy_animation(F32 x)
  50. {
  51. return cosf(x * F_PI * 2.5f - F_PI_BY_TWO) * (0.1f * x - 0.4f) + x * 1.3f;
  52. }
  53. LLHUDIcon::LLHUDIcon(U8 type)
  54. : LLHUDObject(type),
  55. mImagep(NULL),
  56. mScale(0.1f),
  57. mHidden(false),
  58. mClickedCallback(NULL)
  59. {
  60. sIconInstances.emplace_back(this);
  61. }
  62. //virtual
  63. LLHUDIcon::~LLHUDIcon()
  64. {
  65. mImagep = NULL;
  66. }
  67. //virtual
  68. void LLHUDIcon::render()
  69. {
  70. LLGLSUIDefault texture_state;
  71. LLGLDepthTest gls_depth(GL_TRUE);
  72. if (mHidden)
  73. {
  74. return;
  75. }
  76. if (mSourceObject.isNull() || mImagep.isNull() ||
  77. mSourceObject->mDrawable.isNull())
  78. {
  79. markDead();
  80. return;
  81. }
  82. LLVector3 obj_position = mSourceObject->getRenderPosition();
  83. // Put the icon above and in front of the object.
  84. // RN: do not use drawable radius, it is fricking HUGE
  85. LLVector3 icon_relative_pos = gViewerCamera.getUpAxis() *
  86. ~mSourceObject->getRenderRotation();
  87. icon_relative_pos.abs();
  88. F32 distance_scale =
  89. llmin(mSourceObject->getScale().mV[VX] / icon_relative_pos.mV[VX],
  90. mSourceObject->getScale().mV[VY] / icon_relative_pos.mV[VY],
  91. mSourceObject->getScale().mV[VZ] / icon_relative_pos.mV[VZ]);
  92. F32 up_distance = 0.5f * distance_scale;
  93. LLVector3 icon_position =
  94. obj_position + 1.2f * up_distance * gViewerCamera.getUpAxis();
  95. LLVector3 icon_to_cam = gViewerCamera.getOrigin() - icon_position;
  96. icon_to_cam.normalize();
  97. icon_position +=
  98. icon_to_cam * mSourceObject->mDrawable->getRadius() * 1.1f;
  99. mDistance = dist_vec(icon_position, gViewerCamera.getOrigin());
  100. F32 alpha_factor = clamp_rescale(mDistance, DIST_START_FADE, DIST_END_FADE,
  101. 1.f, 0.f);
  102. LLVector3 x_pixel_vec;
  103. LLVector3 y_pixel_vec;
  104. gViewerCamera.getPixelVectors(icon_position, y_pixel_vec, x_pixel_vec);
  105. F32 scale_factor = 1.f;
  106. if (mAnimTimer.getElapsedTimeF32() < ANIM_TIME)
  107. {
  108. scale_factor =
  109. llmax(0.f,
  110. calc_bouncy_animation(mAnimTimer.getElapsedTimeF32() /
  111. ANIM_TIME));
  112. }
  113. F32 time_elapsed = mLifeTimer.getElapsedTimeF32();
  114. if (time_elapsed > MAX_VISIBLE_TIME)
  115. {
  116. markDead();
  117. return;
  118. }
  119. if (time_elapsed > MAX_VISIBLE_TIME - FADE_OUT_TIME)
  120. {
  121. alpha_factor *= clamp_rescale(time_elapsed,
  122. MAX_VISIBLE_TIME - FADE_OUT_TIME,
  123. MAX_VISIBLE_TIME, 1.f, 0.f);
  124. }
  125. F32 image_aspect =
  126. (F32)mImagep->getFullWidth() / (F32)mImagep->getFullHeight();
  127. LLVector3 x_scale = image_aspect * (F32)gViewerWindowp->getWindowHeight() *
  128. mScale * scale_factor * x_pixel_vec;
  129. LLVector3 y_scale = (F32)gViewerWindowp->getWindowHeight() * mScale *
  130. scale_factor * y_pixel_vec;
  131. LLVector3 lower_left = icon_position - x_scale * 0.5f;
  132. LLVector3 lower_right = icon_position + x_scale * 0.5f;
  133. LLVector3 upper_left = icon_position - x_scale * 0.5f + y_scale;
  134. LLVector3 upper_right = icon_position + x_scale * 0.5f + y_scale;
  135. LLColor4 icon_color = LLColor4::white;
  136. icon_color.mV[VALPHA] = alpha_factor;
  137. gGL.color4fv(icon_color.mV);
  138. gGL.getTexUnit(0)->bind(mImagep);
  139. gGL.begin(LLRender::TRIANGLES);
  140. {
  141. gGL.texCoord2f(0.f, 1.f);
  142. gGL.vertex3fv(upper_left.mV);
  143. gGL.texCoord2f(0.f, 0.f);
  144. gGL.vertex3fv(lower_left.mV);
  145. gGL.texCoord2f(1.f, 0.f);
  146. gGL.vertex3fv(lower_right.mV);
  147. gGL.texCoord2f(0.f, 1.f);
  148. gGL.vertex3fv(upper_left.mV);
  149. gGL.texCoord2f(1.f, 0.f);
  150. gGL.vertex3fv(lower_right.mV);
  151. gGL.texCoord2f(1.f, 1.f);
  152. gGL.vertex3fv(upper_right.mV);
  153. }
  154. gGL.end();
  155. }
  156. void LLHUDIcon::setImage(LLViewerTexture* imagep)
  157. {
  158. mImagep = imagep;
  159. mImagep->setAddressMode(LLTexUnit::TAM_CLAMP);
  160. }
  161. //virtual
  162. void LLHUDIcon::markDead()
  163. {
  164. if (mSourceObject)
  165. {
  166. mSourceObject->clearIcon();
  167. }
  168. LLHUDObject::markDead();
  169. }
  170. bool LLHUDIcon::lineSegmentIntersect(const LLVector4a& start,
  171. const LLVector4a& end,
  172. LLVector4a* intersection)
  173. {
  174. if (mHidden)
  175. {
  176. return false;
  177. }
  178. if (mSourceObject.isNull() || mImagep.isNull())
  179. {
  180. markDead();
  181. return false;
  182. }
  183. LLVector3 obj_position = mSourceObject->getRenderPosition();
  184. // put icon above object, and in front
  185. // RN: don't use drawable radius, it's fricking HUGE
  186. LLVector3 icon_relative_pos = (gViewerCamera.getUpAxis() *
  187. ~mSourceObject->getRenderRotation());
  188. icon_relative_pos.abs();
  189. F32 distance_scale =
  190. llmin(mSourceObject->getScale().mV[VX] / icon_relative_pos.mV[VX],
  191. mSourceObject->getScale().mV[VY] / icon_relative_pos.mV[VY],
  192. mSourceObject->getScale().mV[VZ] / icon_relative_pos.mV[VZ]);
  193. F32 up_distance = 0.5f * distance_scale;
  194. LLVector3 icon_position =
  195. obj_position + 1.2f * up_distance * gViewerCamera.getUpAxis();
  196. LLVector3 icon_to_cam = gViewerCamera.getOrigin() - icon_position;
  197. icon_to_cam.normalize();
  198. icon_position +=
  199. icon_to_cam * mSourceObject->mDrawable->getRadius() * 1.1f;
  200. mDistance = dist_vec(icon_position, gViewerCamera.getOrigin());
  201. LLVector3 x_pixel_vec;
  202. LLVector3 y_pixel_vec;
  203. gViewerCamera.getPixelVectors(icon_position, y_pixel_vec, x_pixel_vec);
  204. F32 scale_factor = 1.f;
  205. if (mAnimTimer.getElapsedTimeF32() < ANIM_TIME)
  206. {
  207. scale_factor =
  208. llmax(0.f,
  209. calc_bouncy_animation(mAnimTimer.getElapsedTimeF32() /
  210. ANIM_TIME));
  211. }
  212. F32 time_elapsed = mLifeTimer.getElapsedTimeF32();
  213. if (time_elapsed > MAX_VISIBLE_TIME)
  214. {
  215. markDead();
  216. return false;
  217. }
  218. F32 image_aspect =
  219. (F32)mImagep->getFullWidth() / (F32)mImagep->getFullHeight();
  220. LLVector3 x_scale = image_aspect * (F32)gViewerWindowp->getWindowHeight() *
  221. mScale * scale_factor * x_pixel_vec;
  222. LLVector3 y_scale = (F32)gViewerWindowp->getWindowHeight() * mScale *
  223. scale_factor * y_pixel_vec;
  224. LLVector4a x_scalea;
  225. LLVector4a icon_positiona;
  226. LLVector4a y_scalea;
  227. x_scalea.load3(x_scale.mV);
  228. x_scalea.mul(0.5f);
  229. y_scalea.load3(y_scale.mV);
  230. icon_positiona.load3(icon_position.mV);
  231. LLVector4a lower_left;
  232. lower_left.setSub(icon_positiona, x_scalea);
  233. LLVector4a lower_right;
  234. lower_right.setAdd(icon_positiona, x_scalea);
  235. LLVector4a upper_left;
  236. upper_left.setAdd(lower_left, y_scalea);
  237. LLVector4a upper_right;
  238. upper_right.setAdd(lower_right, y_scalea);
  239. LLVector4a dir;
  240. dir.setSub(end, start);
  241. F32 a,b,t;
  242. if (LLTriangleRayIntersect(upper_right, upper_left,
  243. lower_right, start, dir, a, b, t) ||
  244. LLTriangleRayIntersect(upper_left, lower_left,
  245. lower_right, start, dir, a, b, t))
  246. {
  247. if (intersection)
  248. {
  249. dir.mul(t);
  250. intersection->setAdd(start, dir);
  251. }
  252. return true;
  253. }
  254. return false;
  255. }
  256. void LLHUDIcon::fireClickedCallback(const LLUUID& id)
  257. {
  258. if (mClickedCallback)
  259. {
  260. mClickedCallback(id);
  261. }
  262. }
  263. //static
  264. LLHUDIcon* LLHUDIcon::lineSegmentIntersectAll(const LLVector4a& start,
  265. const LLVector4a& end,
  266. LLVector4a* intersection)
  267. {
  268. icon_instance_t::iterator icon_it;
  269. icon_instance_t::iterator icons_end = sIconInstances.end();
  270. LLVector4a local_end = end;
  271. LLVector4a position;
  272. LLHUDIcon* ret = NULL;
  273. for (icon_it = sIconInstances.begin(); icon_it != icons_end; ++icon_it)
  274. {
  275. LLHUDIcon* icon = *icon_it;
  276. if (icon->lineSegmentIntersect(start, local_end, &position))
  277. {
  278. ret = icon;
  279. if (intersection)
  280. {
  281. *intersection = position;
  282. }
  283. local_end = position;
  284. }
  285. }
  286. return ret;
  287. }
  288. //static
  289. void LLHUDIcon::cleanupDeadIcons()
  290. {
  291. icon_instance_t::iterator icon_it;
  292. icon_instance_t::iterator end = sIconInstances.end();
  293. icon_instance_t icons_to_erase;
  294. for (icon_it = sIconInstances.begin(); icon_it != end; ++icon_it)
  295. {
  296. if ((*icon_it)->mDead)
  297. {
  298. icons_to_erase.push_back(*icon_it);
  299. }
  300. }
  301. end = icons_to_erase.end();
  302. for (icon_it = icons_to_erase.begin(); icon_it != end; ++icon_it)
  303. {
  304. icon_instance_t::iterator found_it = std::find(sIconInstances.begin(),
  305. sIconInstances.end(),
  306. *icon_it);
  307. if (found_it != sIconInstances.end())
  308. {
  309. sIconInstances.erase(found_it);
  310. }
  311. }
  312. }