/xbmc/screensavers/rsxs-0.9/src/hyperspace/tunnel.cc

http://github.com/xbmc/xbmc · C++ · 174 lines · 123 code · 25 blank · 26 comment · 20 complexity · 9ed7e7f9a4ba4dcb3e55f2e550ff58ba 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) 2005 Terence M. Welsh, available from www.reallyslick.com
  22. */
  23. #include <common.hh>
  24. #include <caustic.hh>
  25. #include <color.hh>
  26. #include <spline.hh>
  27. #include <tunnel.hh>
  28. #include <vector.hh>
  29. #define TUNNEL_RESOLUTION 20
  30. namespace Tunnel {
  31. unsigned int _numSections;
  32. unsigned int _section;
  33. float _radius;
  34. float _widthOffset;
  35. float _texSpin;
  36. stdx::dim3<Vector, TUNNEL_RESOLUTION + 1, TUNNEL_RESOLUTION + 1> _v;
  37. stdx::dim3<Vector, TUNNEL_RESOLUTION + 1, TUNNEL_RESOLUTION + 1> _t;
  38. stdx::dim3<RGBColor, TUNNEL_RESOLUTION + 1, TUNNEL_RESOLUTION + 1> _c;
  39. float _loH, _loS, _loL;
  40. float _hiH, _hiS, _hiL;
  41. void make();
  42. };
  43. void Tunnel::init() {
  44. CausticTextures::init();
  45. _radius = 0.1f;
  46. _widthOffset = 0.0f;
  47. _texSpin = 0.0f;
  48. _numSections = Spline::points - 5;
  49. _section = 0;
  50. _v.resize(_numSections);
  51. _t.resize(_numSections);
  52. _c.resize(_numSections);
  53. _loH = _loS = _hiH = _hiS = 0.0f;
  54. _loL = _hiL = M_PI;
  55. }
  56. void Tunnel::make() {
  57. _widthOffset += Common::elapsedTime * 1.5f;
  58. while (_widthOffset >= M_PI * 2.0f)
  59. _widthOffset -= M_PI * 2.0f;
  60. _texSpin += Common::elapsedTime * 0.1f;
  61. while (_texSpin >= M_PI * 2.0f)
  62. _texSpin -= M_PI * 2.0f;
  63. _loH += Common::elapsedTime * 0.04f;
  64. _hiH += Common::elapsedTime * 0.15f;
  65. _loS += Common::elapsedTime * 0.04f;
  66. _hiS += Common::elapsedTime;
  67. _loL += Common::elapsedTime * 0.04f;
  68. _hiL += Common::elapsedTime * 0.5f;
  69. while (_loH > M_PI * 2.0f) _loH -= M_PI * 2.0f;
  70. while (_hiH > M_PI * 2.0f) _hiH -= M_PI * 2.0f;
  71. while (_loS > M_PI * 2.0f) _loS -= M_PI * 2.0f;
  72. while (_hiS > M_PI * 2.0f) _hiS -= M_PI * 2.0f;
  73. while (_loL > M_PI * 2.0f) _loL -= M_PI * 2.0f;
  74. while (_hiL > M_PI * 2.0f) _hiL -= M_PI * 2.0f;
  75. unsigned int n = _numSections;
  76. for (unsigned int k = 0; k < n; ++k) {
  77. // create new vertex data for this section
  78. for (unsigned int i = 0; i <= TUNNEL_RESOLUTION; ++i) {
  79. float where = float(i) / float(TUNNEL_RESOLUTION);
  80. Vector pos(Spline::at(k + 2, where));
  81. Vector dir(Spline::direction(k + 2, where));
  82. RotationMatrix rot(RotationMatrix::lookAt(Vector(), dir, Vector(0.0f, 1.0f, 0.0f)));
  83. for (unsigned int j = 0; j <= TUNNEL_RESOLUTION; ++j) {
  84. float angle = float(j) * M_PI * 2.0f / float(TUNNEL_RESOLUTION);
  85. Vector vertex(rot.transform(Vector(
  86. (_radius + _radius * 0.5f * std::cos(2.0f * pos.x() + _widthOffset)) * std::cos(angle),
  87. (_radius + _radius * 0.5f * std::cos(pos.z() + _widthOffset)) * std::sin(angle),
  88. 0.0f
  89. )));
  90. // set vertex coordinates
  91. _v(k, i, j) = pos + vertex;
  92. // set texture coordinates
  93. _t(k, i, j).x() = 4.0f * float(i) / float(TUNNEL_RESOLUTION);
  94. _t(k, i, j).y() = float(j) / float(TUNNEL_RESOLUTION) + std::cos(_texSpin);
  95. // set colors
  96. HSLColor HSL(
  97. 2.0f * std::cos(0.1f * _v(k, i, j).x() + _loH) - 1.0f,
  98. 0.25f * (std::cos(0.013f * _v(k, i, j).y() - _loS)
  99. + std::cos(_v(k, i, j).z() + _hiS) + 2.0f),
  100. 2.0f * std::cos(0.01f * _v(k, i, j).z() + _loL)
  101. + std::cos(0.4f * _v(k, i, j).x() - _hiL)
  102. + 0.3f * std::cos(4.0f * (_v(k, i, j).x() + _v(k, i, j).y() + _v(k, i, j).z()))
  103. );
  104. HSL.clamp();
  105. if (HSL.s() > 0.7f) HSL.s() = 0.7f;
  106. _c(k, i, j) = RGBColor(HSL);
  107. }
  108. }
  109. }
  110. }
  111. void Tunnel::draw() {
  112. Tunnel::make();
  113. glPushAttrib(GL_ENABLE_BIT);
  114. glEnable(GL_TEXTURE_2D);
  115. CausticTextures::use();
  116. unsigned int n = _numSections;
  117. if (Hack::shaders) {
  118. for (unsigned int k = 0; k < n; ++k) {
  119. for (unsigned int i = 0; i < TUNNEL_RESOLUTION; ++i) {
  120. glBegin(GL_TRIANGLE_STRIP);
  121. for (unsigned int j = 0; j <= TUNNEL_RESOLUTION; ++j) {
  122. glColor4f(_c(k, i + 1, j).r(), _c(k, i + 1, j).g(), _c(k, i + 1, j).b(), Hack::lerp);
  123. glTexCoord2fv(_t(k, i + 1, j).get());
  124. glVertex3fv(_v(k, i + 1, j).get());
  125. glColor4f(_c(k, i, j).r(), _c(k, i, j).g(), _c(k, i, j).b(), Hack::lerp);
  126. glTexCoord2fv(_t(k, i, j).get());
  127. glVertex3fv(_v(k, i, j).get());
  128. }
  129. glEnd();
  130. }
  131. }
  132. } else {
  133. for (unsigned int k = 0; k < n; ++k) {
  134. for (unsigned int i = 0; i < TUNNEL_RESOLUTION; ++i) {
  135. glBegin(GL_TRIANGLE_STRIP);
  136. for (unsigned int j = 0; j <= TUNNEL_RESOLUTION; ++j) {
  137. glColor3fv(_c(k, i + 1, j).get());
  138. glTexCoord2fv(_t(k, i + 1, j).get());
  139. glVertex3fv(_v(k, i + 1, j).get());
  140. glColor3fv(_c(k, i, j).get());
  141. glTexCoord2fv(_t(k, i, j).get());
  142. glVertex3fv(_v(k, i, j).get());
  143. }
  144. glEnd();
  145. }
  146. }
  147. }
  148. glPopAttrib();
  149. }