[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / visualizations / Vortex / VortexVis / Effects / Tunnel.cpp
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
20 #include "Tunnel.h"
21 #include "Renderer.h"
22 #include "angelscript.h"
23
24 struct TunnelParameters
25 {
26         float   Time;
27         float   XRadius;
28         float   YRadius;
29         float   XCounter;
30         float   YCounter;
31         int             CircleSegments;
32         int             TunnelLength;
33         float   TunnelZ;
34         float   RotateX;
35         float   RotateY;
36         float   RotateZ;
37 };
38
39 float Min( float x, float y )
40 {
41         return x < y ? x : y;
42 }
43
44 float Max( float x, float y )
45 {
46         return x > y ? x : y;
47 }
48
49 float ApplySinX(int i, float time)
50 {
51         const float radian = (i/40.0f*(2.0f*3.14159f));
52         float returnvalue = sinf(radian/2.0f+time)+6.0f*cosf(radian/4.0f+time);//+6*cosf((radian+time)/10.0f);
53         return returnvalue*1.0f;
54 }
55
56 float ApplySinY(int i, float time)
57 {
58         const float radian = (i/40.0f*(2.0f*3.14159f));
59         float returnvalue = 3.0f*sinf(radian/8.0f+time)+3.0f*cosf(radian/2.0f+time)*sinf(radian/4.0f+time);
60         return returnvalue*0.2f;
61 }
62
63 void RenderTunnel( TunnelParameters& tp )
64 {
65         int TunnelLength = max( min( tp.TunnelLength, 100 ), 2 );
66         int CircleSegments = max( min( tp.CircleSegments, 30 ), 3 );
67
68 //      Renderer::RotateAxis(atanf((ApplySinX(3, tp.XCounter)-ApplySinX(0,tp.XCounter))/3.0f)*(180.0f/3.14159f), 0.0f, 1.0f, 0.0f);
69 //      Renderer::RotateAxis(atanf((ApplySinY(0,tp.YCounter)-ApplySinY(7, tp.YCounter))/7.0f)*(180.0f/3.14159f), 1.0f, 0.0f, 0.0f);
70
71         Renderer::RotateAxis( (tp.RotateZ) * 45, 0, 0, 1 );
72
73         Renderer::Translate( -ApplySinX( 0, tp.XCounter ), -ApplySinY( 0, tp.YCounter ), 0.0f );
74
75         for ( int z = 0; z < CircleSegments; z++ ) 
76         {
77                 Renderer::Begin( D3DPT_TRIANGLESTRIP );
78                 for ( int i = 0; i < TunnelLength; i++ )
79                 {
80                         float col = 1-(((1.0f * i / TunnelLength))*1);
81                         Renderer::Colour( col, col, col, col );
82                         Renderer::TexCoord( 1-((float)z/CircleSegments*4.0f),
83                                                                 1 - ( (float)i / TunnelLength * 8 + tp.TunnelZ ) );
84                         Renderer::Vertex( tp.XRadius * sinf( (float)z / CircleSegments * ( 2.0f * 3.14159f ) ) + ApplySinX( i, tp.XCounter ),
85                                                           tp.YRadius * cosf( (float)z / CircleSegments * ( 2.0f * 3.14159f ) ) + ApplySinY( i, tp.YCounter ),
86                                                           (float)i );
87                         Renderer::TexCoord( 1 - ( ( (float)z + 1 ) / CircleSegments * 4.0f ),
88                                                                 1 - ( (float)i / TunnelLength * 8 + tp.TunnelZ ) );
89                         Renderer::Vertex( tp.XRadius * sinf( ( (float)z + 1 ) / CircleSegments * ( 2.0f * 3.14159f ) ) + ApplySinX( i, tp.XCounter ),
90                                                           tp.YRadius * cosf( ( (float)z + 1 ) / CircleSegments * ( 2.0f * 3.14159f ) ) + ApplySinY( i, tp.YCounter ),
91                                                           (float)i );
92                 }
93                 Renderer::End();
94         }
95 }
96
97 #ifndef assert
98 #define assert
99 #endif
100
101 void Tunnel::RegisterScriptInterface( asIScriptEngine* pScriptEngine )
102 {
103         int r;
104
105         //----------------------------------------------------------------------------
106         // Register Tunnel Parameter structure
107         r = pScriptEngine->RegisterObjectType("TunnelParameters", sizeof(TunnelParameters), asOBJ_VALUE | asOBJ_POD); assert( r >= 0 );
108
109         // Register the object properties
110         r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float Time", offsetof(TunnelParameters, Time)); assert( r >= 0 );
111         r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float XRadius", offsetof(TunnelParameters, XRadius)); assert( r >= 0 );
112         r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float YRadius", offsetof(TunnelParameters, YRadius)); assert( r >= 0 );
113         r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float XCounter", offsetof(TunnelParameters, XCounter)); assert( r >= 0 );
114         r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float YCounter", offsetof(TunnelParameters, YCounter)); assert( r >= 0 );
115         r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "int CircleSegments", offsetof(TunnelParameters, CircleSegments)); assert( r >= 0 );
116         r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "int TunnelLength", offsetof(TunnelParameters, TunnelLength)); assert( r >= 0 );
117         r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float TunnelZ", offsetof(TunnelParameters, TunnelZ)); assert( r >= 0 );
118         r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float RotateX", offsetof(TunnelParameters, RotateX)); assert( r >= 0 );
119         r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float RotateY", offsetof(TunnelParameters, RotateY)); assert( r >= 0 );
120         r = pScriptEngine->RegisterObjectProperty("TunnelParameters", "float RotateZ", offsetof(TunnelParameters, RotateZ)); assert( r >= 0 );
121
122         r = pScriptEngine->RegisterGlobalFunction("void RenderTunnel(TunnelParameters& in)", asFUNCTION(RenderTunnel), asCALL_CDECL); assert(r >= 0);
123 }