/xbmc/visualizations/Vortex/VortexVis/Effects/Tunnel.cpp

http://github.com/xbmc/xbmc · C++ · 123 lines · 84 code · 16 blank · 23 comment · 2 complexity · bbc2fe62277c17d4251a6e0b4dff96e8 MD5 · raw file

  1. /*
  2. * Copyright Š 2010-2013 Team XBMC
  3. * http://xbmc.org
  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 as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include "Tunnel.h"
  20. #include "Renderer.h"
  21. #include "angelscript.h"
  22. struct TunnelParameters
  23. {
  24. float Time;
  25. float XRadius;
  26. float YRadius;
  27. float XCounter;
  28. float YCounter;
  29. int CircleSegments;
  30. int TunnelLength;
  31. float TunnelZ;
  32. float RotateX;
  33. float RotateY;
  34. float RotateZ;
  35. };
  36. float Min( float x, float y )
  37. {
  38. return x < y ? x : y;
  39. }
  40. float Max( float x, float y )
  41. {
  42. return x > y ? x : y;
  43. }
  44. float ApplySinX(int i, float time)
  45. {
  46. const float radian = (i/40.0f*(2.0f*3.14159f));
  47. float returnvalue = sinf(radian/2.0f+time)+6.0f*cosf(radian/4.0f+time);//+6*cosf((radian+time)/10.0f);
  48. return returnvalue*1.0f;
  49. }
  50. float ApplySinY(int i, float time)
  51. {
  52. const float radian = (i/40.0f*(2.0f*3.14159f));
  53. float returnvalue = 3.0f*sinf(radian/8.0f+time)+3.0f*cosf(radian/2.0f+time)*sinf(radian/4.0f+time);
  54. return returnvalue*0.2f;
  55. }
  56. void RenderTunnel( TunnelParameters& tp )
  57. {
  58. int TunnelLength = max( min( tp.TunnelLength, 100 ), 2 );
  59. int CircleSegments = max( min( tp.CircleSegments, 30 ), 3 );
  60. // Renderer::RotateAxis(atanf((ApplySinX(3, tp.XCounter)-ApplySinX(0,tp.XCounter))/3.0f)*(180.0f/3.14159f), 0.0f, 1.0f, 0.0f);
  61. // Renderer::RotateAxis(atanf((ApplySinY(0,tp.YCounter)-ApplySinY(7, tp.YCounter))/7.0f)*(180.0f/3.14159f), 1.0f, 0.0f, 0.0f);
  62. Renderer::RotateAxis( (tp.RotateZ) * 45, 0, 0, 1 );
  63. Renderer::Translate( -ApplySinX( 0, tp.XCounter ), -ApplySinY( 0, tp.YCounter ), 0.0f );
  64. for ( int z = 0; z < CircleSegments; z++ )
  65. {
  66. Renderer::Begin( D3DPT_TRIANGLESTRIP );
  67. for ( int i = 0; i < TunnelLength; i++ )
  68. {
  69. float col = 1-(((1.0f * i / TunnelLength))*1);
  70. Renderer::Colour( col, col, col, col );
  71. Renderer::TexCoord( 1-((float)z/CircleSegments*4.0f),
  72. 1 - ( (float)i / TunnelLength * 8 + tp.TunnelZ ) );
  73. Renderer::Vertex( tp.XRadius * sinf( (float)z / CircleSegments * ( 2.0f * 3.14159f ) ) + ApplySinX( i, tp.XCounter ),
  74. tp.YRadius * cosf( (float)z / CircleSegments * ( 2.0f * 3.14159f ) ) + ApplySinY( i, tp.YCounter ),
  75. (float)i );
  76. Renderer::TexCoord( 1 - ( ( (float)z + 1 ) / CircleSegments * 4.0f ),
  77. 1 - ( (float)i / TunnelLength * 8 + tp.TunnelZ ) );
  78. Renderer::Vertex( tp.XRadius * sinf( ( (float)z + 1 ) / CircleSegments * ( 2.0f * 3.14159f ) ) + ApplySinX( i, tp.XCounter ),
  79. tp.YRadius * cosf( ( (float)z + 1 ) / CircleSegments * ( 2.0f * 3.14159f ) ) + ApplySinY( i, tp.YCounter ),
  80. (float)i );
  81. }
  82. Renderer::End();
  83. }
  84. }
  85. #ifndef assert
  86. #define assert
  87. #endif
  88. void Tunnel::RegisterScriptInterface( asIScriptEngine* pScriptEngine )
  89. {
  90. int r;
  91. //----------------------------------------------------------------------------
  92. // Register Tunnel Parameter structure
  93. r = pScriptEngine->RegisterObjectType("TunnelParameters", sizeof(TunnelParameters), asOBJ_VALUE | asOBJ_POD); assert( r >= 0 );
  94. // Register the object properties
  95. r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float Time", offsetof(TunnelParameters, Time)); assert( r >= 0 );
  96. r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float XRadius", offsetof(TunnelParameters, XRadius)); assert( r >= 0 );
  97. r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float YRadius", offsetof(TunnelParameters, YRadius)); assert( r >= 0 );
  98. r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float XCounter", offsetof(TunnelParameters, XCounter)); assert( r >= 0 );
  99. r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float YCounter", offsetof(TunnelParameters, YCounter)); assert( r >= 0 );
  100. r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "int CircleSegments", offsetof(TunnelParameters, CircleSegments)); assert( r >= 0 );
  101. r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "int TunnelLength", offsetof(TunnelParameters, TunnelLength)); assert( r >= 0 );
  102. r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float TunnelZ", offsetof(TunnelParameters, TunnelZ)); assert( r >= 0 );
  103. r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float RotateX", offsetof(TunnelParameters, RotateX)); assert( r >= 0 );
  104. r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float RotateY", offsetof(TunnelParameters, RotateY)); assert( r >= 0 );
  105. r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float RotateZ", offsetof(TunnelParameters, RotateZ)); assert( r >= 0 );
  106. r = pScriptEngine->RegisterGlobalFunction("void RenderTunnel(TunnelParameters& in)", asFUNCTION(RenderTunnel), asCALL_CDECL); assert(r >= 0);
  107. }