/xbmc/screensavers/rsxs-0.9/src/euphoria/wisp.cc

http://github.com/xbmc/xbmc · C++ · 277 lines · 229 code · 17 blank · 31 comment · 29 complexity · 4a7f81f8564894817ed5ef58739dacf5 MD5 · raw file

  1. /*
  2. * Really Slick XScreenSavers
  3. * Copyright (C) 2002-2006 Michael Chapman
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. *****************************************************************************
  19. *
  20. * This is a Linux port of the Really Slick Screensavers,
  21. * Copyright (C) 2002 Terence M. Welsh, available from www.reallyslick.com
  22. */
  23. #include <common.hh>
  24. #include <euphoria.hh>
  25. #include <wisp.hh>
  26. Wisp::Wisp() {
  27. float recHalfDens = 1.0f / (float(Hack::density) * 0.5f);
  28. _vertex.resize( Hack::density + 1, Hack::density + 1);
  29. _intensity.resize(Hack::density + 1, Hack::density + 1);
  30. _gridPos.resize( Hack::density + 1, Hack::density + 1);
  31. for (unsigned int i = 0; i <= Hack::density; ++i)
  32. for (unsigned int j = 0; j <= Hack::density; ++j) {
  33. Vector v(
  34. float(i) * recHalfDens - 1.0f,
  35. float(j) * recHalfDens - 1.0f,
  36. 0.0f
  37. );
  38. v.z() = v.lengthSquared();
  39. _gridPos(i, j) = v;
  40. }
  41. // initialize constants
  42. for (unsigned int i = 0; i < NUMCONSTS; ++i) {
  43. _c[i] = Common::randomFloat(2.0f) - 1.0f;
  44. _cr[i] = Common::randomFloat(M_PI * 2.0f);
  45. _cv[i] = Common::randomFloat(Hack::speed * 0.03f) +
  46. (Hack::speed * 0.001f);
  47. }
  48. // pick color
  49. _HSL.set(
  50. Common::randomFloat(1.0f),
  51. 0.1f + Common::randomFloat(0.9f),
  52. 1.0f
  53. );
  54. _hueSpeed = Common::randomFloat(0.1f) - 0.05f;
  55. _saturationSpeed = Common::randomFloat(0.04f) + 0.001f;
  56. }
  57. void Wisp::update() {
  58. // visibility constants
  59. static float viscon1 = Hack::visibility * 0.01f;
  60. static float viscon2 = 1.0f / viscon1;
  61. // update constants
  62. for (unsigned int i = 0; i < NUMCONSTS; ++i) {
  63. _cr[i] += _cv[i] * Common::elapsedSecs;
  64. if (_cr[i] > M_PI * 2.0f)
  65. _cr[i] -= M_PI * 2.0f;
  66. _c[i] = std::cos(_cr[i]);
  67. }
  68. // update vertex positions
  69. for (unsigned int i = 0; i <= Hack::density; ++i)
  70. for (unsigned int j = 0; j <= Hack::density; ++j)
  71. _vertex(i, j).set(
  72. _gridPos(i, j).x() * _gridPos(i, j).x() *
  73. _gridPos(i, j).y() * _c[0] +
  74. _gridPos(i, j).z() * _c[1] + 0.5f * _c[2],
  75. _gridPos(i, j).y() * _gridPos(i, j).y() *
  76. _gridPos(i, j).z() * _c[3] +
  77. _gridPos(i, j).x() * _c[4] + 0.5f * _c[5],
  78. _gridPos(i, j).z() * _gridPos(i, j).z() *
  79. _gridPos(i, j).x() * _c[6] +
  80. _gridPos(i, j).y() * _c[7] + _c[8]
  81. );
  82. // update vertex normals for most of mesh
  83. for (unsigned int i = 1; i < Hack::density; ++i)
  84. for (unsigned int j = 1; j < Hack::density; ++j) {
  85. Vector up(_vertex(i, j + 1) - _vertex(i, j - 1));
  86. Vector right(_vertex(i + 1, j) - _vertex(i - 1, j));
  87. up.normalize();
  88. right.normalize();
  89. Vector crossVec(Vector::cross(right, up));
  90. // Use depth component of normal to compute intensity
  91. // This way only edges of wisp are bright
  92. _intensity(i, j) = Common::clamp(
  93. viscon2 * (viscon1 - std::abs(crossVec.z())),
  94. 0.0f, 1.0f
  95. );
  96. }
  97. // update color
  98. float h = _HSL.h() + _hueSpeed * Common::elapsedSecs;
  99. if (h < 0.0f) h += 1.0f;
  100. if (h > 1.0f) h -= 1.0f;
  101. float s = _HSL.s() + _saturationSpeed * Common::elapsedSecs;
  102. if (s <= 0.1f) {
  103. s = 0.1f;
  104. _saturationSpeed = -_saturationSpeed;
  105. }
  106. if (s >= 1.0f) {
  107. s = 1.0f;
  108. _saturationSpeed = -_saturationSpeed;
  109. }
  110. _HSL.h() = h;
  111. _HSL.s() = s;
  112. _RGB = _HSL;
  113. }
  114. void Wisp::draw() const {
  115. glPushMatrix();
  116. if (Hack::wireframe) {
  117. for (unsigned int i = 1; i < Hack::density; ++i) {
  118. glBegin(GL_LINE_STRIP);
  119. for (unsigned int j = 0; j <= Hack::density; ++j) {
  120. glColor3f(
  121. _RGB.r() + _intensity(i, j) - 1.0f,
  122. _RGB.g() + _intensity(i, j) - 1.0f,
  123. _RGB.b() + _intensity(i, j) - 1.0f
  124. );
  125. glTexCoord2d(
  126. _gridPos(i, j).x() - _vertex(i, j).x(),
  127. _gridPos(i, j).y() - _vertex(i, j).y()
  128. );
  129. glVertex3fv(_vertex(i, j).get());
  130. }
  131. glEnd();
  132. }
  133. for (unsigned int j = 1; j < Hack::density; ++j) {
  134. glBegin(GL_LINE_STRIP);
  135. for (unsigned int i = 0; i <= Hack::density; ++i) {
  136. glColor3f(
  137. _RGB.r() + _intensity(i, j) - 1.0f,
  138. _RGB.g() + _intensity(i, j) - 1.0f,
  139. _RGB.b() + _intensity(i, j) - 1.0f
  140. );
  141. glTexCoord2d(
  142. _gridPos(i, j).x() - _vertex(i, j).x(),
  143. _gridPos(i, j).y() - _vertex(i, j).y()
  144. );
  145. glVertex3fv(_vertex(i, j).get());
  146. }
  147. glEnd();
  148. }
  149. } else {
  150. for (unsigned int i = 0; i < Hack::density; ++i) {
  151. glBegin(GL_TRIANGLE_STRIP);
  152. for (unsigned int j = 0; j <= Hack::density; ++j) {
  153. glColor3f(
  154. _RGB.r() + _intensity(i + 1, j) - 1.0f,
  155. _RGB.g() + _intensity(i + 1, j) - 1.0f,
  156. _RGB.b() + _intensity(i + 1, j) - 1.0f
  157. );
  158. glTexCoord2d(
  159. _gridPos(i + 1, j).x() - _vertex(i + 1, j).x(),
  160. _gridPos(i + 1, j).y() - _vertex(i + 1, j).y()
  161. );
  162. glVertex3fv(_vertex(i + 1, j).get());
  163. glColor3f(
  164. _RGB.r() + _intensity(i, j) - 1.0f,
  165. _RGB.g() + _intensity(i, j) - 1.0f,
  166. _RGB.b() + _intensity(i, j) - 1.0f
  167. );
  168. glTexCoord2d(
  169. _gridPos(i, j).x() - _vertex(i, j).x(),
  170. _gridPos(i, j).y() - _vertex(i, j).y()
  171. );
  172. glVertex3fv(_vertex(i, j).get());
  173. }
  174. glEnd();
  175. }
  176. }
  177. glPopMatrix();
  178. }
  179. void Wisp::drawAsBackground() const {
  180. glPushMatrix();
  181. glTranslatef(_c[0] * 0.2f, _c[1] * 0.2f, 1.6f);
  182. if (Hack::wireframe) {
  183. for (unsigned int i = 1; i < Hack::density; ++i) {
  184. glBegin(GL_LINE_STRIP);
  185. for (unsigned int j = 0; j <= Hack::density; ++j) {
  186. glColor3f(
  187. _RGB.r() + _intensity(i, j) - 1.0f,
  188. _RGB.g() + _intensity(i, j) - 1.0f,
  189. _RGB.b() + _intensity(i, j) - 1.0f
  190. );
  191. glTexCoord2d(
  192. _gridPos(i, j).x() - _vertex(i, j).x(),
  193. _gridPos(i, j).y() - _vertex(i, j).y()
  194. );
  195. glVertex3f(
  196. _gridPos(i, j).x(),
  197. _gridPos(i, j).y(),
  198. _intensity(i, j)
  199. );
  200. }
  201. glEnd();
  202. }
  203. for (unsigned int j = 1; j < Hack::density; ++j) {
  204. glBegin(GL_LINE_STRIP);
  205. for (unsigned int i = 0; i <= Hack::density; ++i) {
  206. glColor3f(
  207. _RGB.r() + _intensity(i, j) - 1.0f,
  208. _RGB.g() + _intensity(i, j) - 1.0f,
  209. _RGB.b() + _intensity(i, j) - 1.0f
  210. );
  211. glTexCoord2d(
  212. _gridPos(i, j).x() - _vertex(i, j).x(),
  213. _gridPos(i, j).y() - _vertex(i, j).y()
  214. );
  215. glVertex3f(
  216. _gridPos(i, j).x(),
  217. _gridPos(i, j).y(),
  218. _intensity(i, j)
  219. );
  220. }
  221. glEnd();
  222. }
  223. } else {
  224. for (unsigned int i = 0; i < Hack::density; ++i) {
  225. glBegin(GL_TRIANGLE_STRIP);
  226. for (unsigned int j = 0; j <= Hack::density; ++j) {
  227. glColor3f(
  228. _RGB.r() + _intensity(i + 1, j) - 1.0f,
  229. _RGB.g() + _intensity(i + 1, j) - 1.0f,
  230. _RGB.b() + _intensity(i + 1, j) - 1.0f
  231. );
  232. glTexCoord2d(
  233. _gridPos(i + 1, j).x() - _vertex(i + 1, j).x(),
  234. _gridPos(i + 1, j).y() - _vertex(i + 1, j).y()
  235. );
  236. glVertex3f(
  237. _gridPos(i + 1, j).x(),
  238. _gridPos(i + 1, j).y(),
  239. _intensity(i + 1, j)
  240. );
  241. glColor3f(
  242. _RGB.r() + _intensity(i, j) - 1.0f,
  243. _RGB.g() + _intensity(i, j) - 1.0f,
  244. _RGB.b() + _intensity(i, j) - 1.0f
  245. );
  246. glTexCoord2d(
  247. _gridPos(i, j).x() - _vertex(i, j).x(),
  248. _gridPos(i, j).y() - _vertex(i, j).y()
  249. );
  250. glVertex3f(
  251. _gridPos(i, j).x(),
  252. _gridPos(i, j).y(),
  253. _intensity(i, j)
  254. );
  255. }
  256. glEnd();
  257. }
  258. }
  259. glPopMatrix();
  260. }