Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nonlinear_transform.h
Go to the documentation of this file.
1 #ifndef NONLINEAR_TRANSFORM_H_
2 #define NONLINEAR_TRANSFORM_H_
3 #include "macosx_math.h"
4 #include <math.h>
5 #ifndef M_PI
6 #define M_PI (3.1415926536)
7 #endif
8 
13 {
14 public:
16  virtual QVector Transform( const QVector &v ) const
17  {
18  return v;
19  }
21  virtual QVector TransformNormal( const QVector &v, const QVector &n ) const
22  {
23  return n;
24  }
26  virtual QVector InvTransform( const QVector &v ) const
27  {
28  return v;
29  }
31  virtual CLIPSTATE BoxInFrustum( Vector &min, Vector &max, const Vector &campos ) const
32  {
33  return GFXBoxInFrustum( min, max );
34  }
35  float TransformS( float x, float scale ) const
36  {
37  return x*scale;
38  }
39  float TransformT( float y, float scale ) const
40  {
41  return y*scale;
42  }
43 };
44 
45 extern float SphereTransformRenderlevel;
47 {
48 protected:
49  float scalex, scalez, r;
50 public: SphericalTransform( float a, float b, float c ) : IdentityTransform()
51  {
52  SetXZ( a, c );
53  SetR( b );
54  }
55  void SetXZ( float x, float z )
56  {
57  this->scalex = 2*M_PI/x;
58  this->scalez = M_PI/z;
59  } //x ranges from 0 to 2PI x ranges from -PI/2 to PI/2
60  void SetR( float rr )
61  {
62  r = rr;
63  }
64  float GetR() const
65  {
66  return r;
67  }
68  float GetX() const
69  {
70  return 2*M_PI/scalex;
71  }
72  float GetZ() const
73  {
74  return M_PI/scalez;
75  }
76  QVector Transform( const QVector &v ) const
77  {
78  Vector T( v.i*scalex, r+v.j, v.k*scalez-.5*M_PI );
79  float cosphi = cos( T.k );
80  return QVector( T.j*cosphi*cos( T.i ), T.j*sin( T.k ), T.j*cosphi*sin( T.i ) );
81  }
82  QVector TransformNormal( const QVector &point, const QVector &n ) const
83  {
84  return SphericalTransform::Transform( n+point )-Transform( point );
85  }
86  QVector InvTransform( const QVector &v ) const
87  {
88  float rplusy = v.Magnitude();
89  //float lengthxypln = sqrtf (rplusy*rplusy-v.j*v.j);//pythagorus
90  return QVector( (atan2( -v.k, -v.i )+M_PI)/scalex, rplusy-r, (asin( v.j/rplusy )+M_PI*.5)/scalez );
91  }
92  CLIPSTATE BoxInFrustum( Vector &min, Vector &max, const Vector &campos ) const
93  {
94  const float rendermin = 3;
95  /*
96  * float tmpx = fabs(campos.i-min.i);float maxx = fabs(campos.i-max.i);
97  * if (tmpx>.35*GetX()&&tmpx<.65*GetX()&&maxx>.25*GetX()&&maxx<.75*GetX()) {return GFX_NOT_VISIBLE;}
98  * tmpx = fabs(campos.k-min.k); maxx = fabs(campos.k-max.k);
99  * if (tmpx>.25*GetZ()&&tmpx<.75*GetZ()&&maxx>.25*GetZ()&&maxx<.75*GetZ()) { return GFX_NOT_VISIBLE;//i/f it's on the other side of the hemisphere} */
100  if (SphereTransformRenderlevel < rendermin)
101  return GFX_PARTIALLY_VISIBLE;
104  tmax = .5*(tmax+tmin); //center
105  float rad = 1.8*(tmax-tmin).Magnitude();
106 
107  return GFXSpherePartiallyInFrustum( tmax, rad );
108  }
109 };
110 /*
111  * class PlanetaryTransform:public SphericalTransform {
112  * Vector Origin;
113  * public:
114  * PlanetaryTransform (Vector loc, float r, float scalex, float scaley): SphericalTransform (float r,float scalex, float scaley), origin(loc) {}
115  * void SetOrigin (const Vector &t) {Origin = t;}
116  * ~PlanetaryTransform () {while (1);}
117  * Vector Transform (const Vector & v) {return Origin+SphericalTransform::Transform(v);}
118  * Vector TransformNormal (const Vector &p, const vector & n){return SphericalTransform::TransformNormal (p,n);}
119  * Vector InvTransform (const Vector &v) {return SphericalTransform::InvTransform (v-Origin);}
120  * void GrabPerpendicularOrigin (const Vector &m, Matrix trans){
121  * Vector norm (m-Origin);
122  * Normalize(norm);
123  * Vector Intersection (norm*r);
124  *
125  *
126  *
127  * }
128  * CLIPSTATE BoxInFrustum (Vector &min, Vector &max, const Vector & campos) {
129  * min = min-Origin;
130  * max = max-Origin;
131  * return SphericalTransform::BoxInFrustum(min,max,campos-Origin);
132  * }
133  * };
134  */
135 #endif
136