PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/llsprite.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 308 lines | 200 code | 59 blank | 49 comment | 9 complexity | 824de7350336dfe4db8e28b403015d16 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llsprite.cpp
  3. * @brief LLSprite class implementation
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. /* -*- c++ -*-
  27. * Notes:
  28. * PR - Should add a creator that can take a pointer rather than handle for streaming
  29. * object textures.
  30. * PR - Need to add support for lit/non-lit conditions, set normals?
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include <llglheaders.h>
  34. #include "llsprite.h"
  35. #include "math.h"
  36. #include "lldrawable.h"
  37. #include "llface.h"
  38. #include "llviewercamera.h"
  39. #include "llviewertexturelist.h"
  40. LLVector3 LLSprite::sCameraUp(0.0f,0.0f,1.0f);
  41. LLVector3 LLSprite::sCameraRight(1.0f,0.0f,0.0f);
  42. LLVector3 LLSprite::sCameraPosition(0.f, 0.f, 0.f);
  43. LLVector3 LLSprite::sNormal(0.0f,0.0f,0.0f);
  44. //////////////////////////////////////////////////////////////////////
  45. // Construction/Destruction
  46. //////////////////////////////////////////////////////////////////////
  47. // A simple initialization
  48. LLSprite::LLSprite(const LLUUID &image_uuid) :
  49. mImageID(image_uuid),
  50. mImagep(NULL),
  51. mPitch(0.f),
  52. mYaw(0.f),
  53. mPosition(0.0f, 0.0f, 0.0f),
  54. mFollow(TRUE),
  55. mUseCameraUp(TRUE),
  56. mColor(0.5f, 0.5f, 0.5f, 1.0f),
  57. mTexMode(GL_REPLACE)
  58. {
  59. setSize(1.0f, 1.0f);
  60. }
  61. //////////////////////////////////////////////////////////////////////
  62. LLSprite::~LLSprite()
  63. {
  64. }
  65. void LLSprite::updateFace(LLFace &face)
  66. {
  67. LLViewerCamera &camera = *LLViewerCamera::getInstance();
  68. // First, figure out how many vertices/indices we need.
  69. U32 num_vertices, num_indices;
  70. U32 vertex_count = 0;
  71. // Get the total number of vertices and indices
  72. if (mFollow)
  73. {
  74. num_vertices = 4;
  75. num_indices = 6;
  76. }
  77. else
  78. {
  79. num_vertices = 4;
  80. num_indices = 12;
  81. }
  82. face.setSize(num_vertices, num_indices);
  83. if (mFollow)
  84. {
  85. sCameraUp = camera.getUpAxis();
  86. sCameraRight = -camera.getLeftAxis();
  87. sCameraPosition = camera.getOrigin();
  88. sNormal = -camera.getAtAxis();
  89. if (mUseCameraUp)
  90. {
  91. // these need to live here because the height/width may change between render calls
  92. mScaledUp = sCameraUp;
  93. mScaledRight = sCameraRight;
  94. mScaledUp *= mHeightDiv2;
  95. mScaledRight *= mWidthDiv2;
  96. mA = mPosition + mScaledRight + mScaledUp;
  97. mB = mPosition - mScaledRight + mScaledUp;
  98. mC = mPosition - mScaledRight - mScaledUp;
  99. mD = mPosition + mScaledRight - mScaledUp;
  100. }
  101. else
  102. {
  103. // The up vector is perpendicular to the camera vector...
  104. LLVector3 camera_vec = mPosition - sCameraPosition;
  105. mScaledRight = camera_vec % LLVector3(0.f, 0.f, 1.f);
  106. mScaledUp = -(camera_vec % mScaledRight);
  107. mScaledUp.normalize();
  108. mScaledRight.normalize();
  109. mScaledUp *= mHeightDiv2;
  110. mScaledRight *= mWidthDiv2;
  111. mA = mPosition + mScaledRight + mScaledUp;
  112. mB = mPosition - mScaledRight + mScaledUp;
  113. mC = mPosition - mScaledRight - mScaledUp;
  114. mD = mPosition + mScaledRight - mScaledUp;
  115. }
  116. }
  117. else
  118. {
  119. // this is equivalent to how it was done before. . .
  120. // we need to establish a way to
  121. // identify the orientation of a particular sprite rather than
  122. // just banging it in on the x,z plane if it's not following the camera.
  123. LLVector3 x_axis;
  124. LLVector3 y_axis;
  125. F32 dot = sNormal * LLVector3(0.f, 1.f, 0.f);
  126. if (dot == 1.f || dot == -1.f)
  127. {
  128. x_axis.setVec(1.f, 0.f, 0.f);
  129. y_axis.setVec(0.f, 1.f, 0.f);
  130. }
  131. else
  132. {
  133. x_axis = sNormal % LLVector3(0.f, -1.f, 0.f);
  134. x_axis.normalize();
  135. y_axis = sNormal % x_axis;
  136. }
  137. LLQuaternion yaw_rot(mYaw, sNormal);
  138. // rotate axes by specified yaw
  139. x_axis = x_axis * yaw_rot;
  140. y_axis = y_axis * yaw_rot;
  141. // rescale axes by width and height of sprite
  142. x_axis = x_axis * mWidthDiv2;
  143. y_axis = y_axis * mHeightDiv2;
  144. mA = -x_axis + y_axis;
  145. mB = x_axis + y_axis;
  146. mC = x_axis - y_axis;
  147. mD = -x_axis - y_axis;
  148. mA += mPosition;
  149. mB += mPosition;
  150. mC += mPosition;
  151. mD += mPosition;
  152. }
  153. face.setFaceColor(mColor);
  154. LLStrider<LLVector3> verticesp;
  155. LLStrider<LLVector3> normalsp;
  156. LLStrider<LLVector2> tex_coordsp;
  157. LLStrider<U16> indicesp;
  158. U16 index_offset;
  159. // Setup face
  160. if (!face.getVertexBuffer())
  161. {
  162. LLVertexBuffer* buff = new LLVertexBuffer(LLVertexBuffer::MAP_VERTEX |
  163. LLVertexBuffer::MAP_TEXCOORD0,
  164. GL_STREAM_DRAW_ARB);
  165. buff->allocateBuffer(4, 12, TRUE);
  166. face.setGeomIndex(0);
  167. face.setIndicesIndex(0);
  168. face.setVertexBuffer(buff);
  169. }
  170. index_offset = face.getGeometry(verticesp,normalsp,tex_coordsp, indicesp);
  171. *tex_coordsp = LLVector2(0.f, 0.f);
  172. *verticesp = mC;
  173. tex_coordsp++;
  174. verticesp++;
  175. vertex_count++;
  176. *tex_coordsp = LLVector2(0.f, 1.f);
  177. *verticesp = mB;
  178. tex_coordsp++;
  179. verticesp++;
  180. vertex_count++;
  181. *tex_coordsp = LLVector2(1.f, 1.f);
  182. *verticesp = mA;
  183. tex_coordsp++;
  184. verticesp++;
  185. vertex_count++;
  186. *tex_coordsp = LLVector2(1.f, 0.0f);
  187. *verticesp = mD;
  188. tex_coordsp++;
  189. verticesp++;
  190. vertex_count++;
  191. // Generate indices, since they're easy.
  192. // Just a series of quads.
  193. *indicesp++ = index_offset;
  194. *indicesp++ = 2 + index_offset;
  195. *indicesp++ = 1 + index_offset;
  196. *indicesp++ = index_offset;
  197. *indicesp++ = 3 + index_offset;
  198. *indicesp++ = 2 + index_offset;
  199. if (!mFollow)
  200. {
  201. *indicesp++ = 0 + index_offset;
  202. *indicesp++ = 1 + index_offset;
  203. *indicesp++ = 2 + index_offset;
  204. *indicesp++ = 0 + index_offset;
  205. *indicesp++ = 2 + index_offset;
  206. *indicesp++ = 3 + index_offset;
  207. }
  208. face.getVertexBuffer()->flush();
  209. face.mCenterAgent = mPosition;
  210. }
  211. void LLSprite::setPosition(const LLVector3 &position)
  212. {
  213. mPosition = position;
  214. }
  215. void LLSprite::setPitch(const F32 pitch)
  216. {
  217. mPitch = pitch;
  218. }
  219. void LLSprite::setSize(const F32 width, const F32 height)
  220. {
  221. mWidth = width;
  222. mHeight = height;
  223. mWidthDiv2 = width/2.0f;
  224. mHeightDiv2 = height/2.0f;
  225. }
  226. void LLSprite::setYaw(F32 yaw)
  227. {
  228. mYaw = yaw;
  229. }
  230. void LLSprite::setFollow(const BOOL follow)
  231. {
  232. mFollow = follow;
  233. }
  234. void LLSprite::setUseCameraUp(const BOOL use_up)
  235. {
  236. mUseCameraUp = use_up;
  237. }
  238. void LLSprite::setTexMode(const LLGLenum mode)
  239. {
  240. mTexMode = mode;
  241. }
  242. void LLSprite::setColor(const LLColor4 &color)
  243. {
  244. mColor = color;
  245. }
  246. void LLSprite::setColor(const F32 r, const F32 g, const F32 b, const F32 a)
  247. {
  248. mColor.setVec(r, g, b, a);
  249. }