Opengl中矩阵和perspective/ortho的相互转换

Opengl变换需要用四维矩阵。我们来定义这样的矩阵。

+BIT祝威+悄悄在此留下版了个权的信息说:

四维向量

首先,我们定义一个四维向量vec4。

  1     /// <summary>
  2     /// Represents a four dimensional vector.
  3     /// </summary>
  4     public struct vec4
  5     {
  6         public float x;
  7         public float y;
  8         public float z;
  9         public float w;
 10 
 11         public float this[int index]
 12         {
 13             get
 14             {
 15                 if (index == 0) return x;
 16                 else if (index == 1) return y;
 17                 else if (index == 2) return z;
 18                 else if (index == 3) return w;
 19                 else throw new Exception("Out of range.");
 20             }
 21             set
 22             {
 23                 if (index == 0) x = value;
 24                 else if (index == 1) y = value;
 25                 else if (index == 2) z = value;
 26                 else if (index == 3) w = value;
 27                 else throw new Exception("Out of range.");
 28             }
 29         }
 30 
 31         public vec4(float s)
 32         {
 33             x = y = z = w = s;
 34         }
 35 
 36         public vec4(float x, float y, float z, float w)
 37         {
 38             this.x = x;
 39             this.y = y;
 40             this.z = z;
 41             this.w = w;
 42         }
 43 
 44         public vec4(vec4 v)
 45         {
 46             this.x = v.x;
 47             this.y = v.y;
 48             this.z = v.z;
 49             this.w = v.w;
 50         }
 51 
 52         public vec4(vec3 xyz, float w)
 53         {
 54             this.x = xyz.x;
 55             this.y = xyz.y;
 56             this.z = xyz.z;
 57             this.w = w;
 58         }
 59 
 60         public static vec4 operator +(vec4 lhs, vec4 rhs)
 61         {
 62             return new vec4(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z, lhs.w + rhs.w);
 63         }
 64 
 65         public static vec4 operator +(vec4 lhs, float rhs)
 66         {
 67             return new vec4(lhs.x + rhs, lhs.y + rhs, lhs.z + rhs, lhs.w + rhs);
 68         }
 69 
 70         public static vec4 operator -(vec4 lhs, float rhs)
 71         {
 72             return new vec4(lhs.x - rhs, lhs.y - rhs, lhs.z - rhs, lhs.w - rhs);
 73         }
 74 
 75         public static vec4 operator -(vec4 lhs, vec4 rhs)
 76         {
 77             return new vec4(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z, lhs.w - rhs.w);
 78         }
 79 
 80         public static vec4 operator *(vec4 self, float s)
 81         {
 82             return new vec4(self.x * s, self.y * s, self.z * s, self.w * s);
 83         }
 84 
 85         public static vec4 operator *(float lhs, vec4 rhs)
 86         {
 87             return new vec4(rhs.x * lhs, rhs.y * lhs, rhs.z * lhs, rhs.w * lhs);
 88         }
 89 
 90         public static vec4 operator *(vec4 lhs, vec4 rhs)
 91         {
 92             return new vec4(rhs.x * lhs.x, rhs.y * lhs.y, rhs.z * lhs.z, rhs.w * lhs.w);
 93         }
 94 
 95         public static vec4 operator /(vec4 lhs, float rhs)
 96         {
 97             return new vec4(lhs.x / rhs, lhs.y / rhs, lhs.z / rhs, lhs.w / rhs);
 98         }
 99 
100         public float[] to_array()
101         {
102             return new[] { x, y, z, w };
103         }
104 
105         /// <summary>
106         /// 归一化向量
107         /// </summary>
108         /// <param name="vector"></param>
109         /// <returns></returns>
110         public void Normalize()
111         {
112             var frt = (float)Math.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
113 
114             this.x = x / frt;
115             this.y = y / frt;
116             this.z = z / frt;
117             this.w = w / frt;
118         }
119 
120         public override string ToString()
121         {
122             return string.Format("{0:0.00},{1:0.00},{2:0.00},{3:0.00}", x, y, z, w);
123         }
124     }
vec4

相关文章:

  • 2021-08-12
  • 2021-08-11
  • 2021-11-07
  • 2021-05-13
  • 2021-06-13
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案