double alpha, beta; //角度 //コンストラクタ public Camera() { Upward = new Vector3D(); Setposition = new Vector3D(); LookatPoint = new Vector3D(); CameraX = new Vector3D(); CameraY = new Vector3D(); CameraZ = new Vector3D(); Light = new Vector3D(); rotatematrix = new Matrix(3); projectionmatrix = new Matrix(3); Upward.X = 0.0f; Upward.Y = 0.0f; Upward.Z = 1.0f; Setposition.X = 100.0f; Setposition.Y = 300.0f; Setposition.Z = 100.0f; LookatPoint.X = 0.0f; LookatPoint.Y = 0.0f; LookatPoint.Z = 0.0f; temp2 = new Vector3D(-Setposition.X, -Setposition.Y, LookatPoint.Z); r = Setposition.Getnorm(); alpha = Math.Atan(Setposition.Y / Setposition.X); beta = Math.Asin((double)Setposition.Z / r); Light.X = 50.0f; Light.Y = 60.0f; Light.Z = 100.0f; angle = Math.PI / 3.0; aspect = 1.0f; nearZ = 10.0f; farZ = 20.0f; }
//ベクトル*行列 public static Vector3D operator *(Vector3D A, Matrix B) { Vector3D temp = new Vector3D(); temp.X = A.X * B.matrix[0, 0] + A.Y * B.matrix[1, 0] + A.Z * B.matrix[2, 0]; temp.Y = A.X * B.matrix[0, 1] + A.Y * B.matrix[1, 1] + A.Z * B.matrix[2, 1]; temp.Z = A.X * B.matrix[0, 2] + A.Y * B.matrix[1, 2] + A.Z * B.matrix[2, 2]; return temp; }
//軸回転行列の生成 public void Set_Rotate_ArbAx(Vector3D n, double angle) { this.matrix[0, 0] = (float)(n.X * n.X + (1 - n.X * n.X) * Math.Cos(angle)); this.matrix[1, 0] = (float)(n.X * n.Y * (1 - Math.Cos(angle)) - n.Z * Math.Sin(angle)); this.matrix[2, 0] = (float)(n.Z * n.X * (1 - Math.Cos(angle)) + n.Y * Math.Sin(angle)); this.matrix[0, 1] = (float)(n.X * n.Y * (1 - Math.Cos(angle)) + n.Z * Math.Sin(angle)); this.matrix[1, 1] = (float)(n.Y * n.Y + (1 - n.Y * n.Y) * Math.Cos(angle)); this.matrix[2, 1] = (float)(n.Y * n.Z * (1 - Math.Cos(angle)) - n.X * Math.Sin(angle)); this.matrix[0, 2] = (float)(n.Z * n.X * (1 - Math.Cos(angle)) - n.Y * Math.Sin(angle)); this.matrix[1, 2] = (float)(n.Y * n.Z * (1 - Math.Cos(angle)) + n.X * Math.Sin(angle)); this.matrix[2, 2] = (float)(n.Z * n.Z + (1 - n.Z * n.Z) * Math.Cos(angle)); }
//誤差の範囲内かを判定 public static bool NearlyEcoal(Vector3D A, Vector3D B, float errorrange) { if (Math.Abs(A.X - B.X) < errorrange && Math.Abs(A.Y - B.Y) < errorrange && Math.Abs(A.Z - B.Z) < errorrange) return true; else return false; }
//等しいか判定 public static bool Ecoal(Vector3D A, Vector3D B) { if (A.X == B.X && A.Y == B.Y && A.Z == B.Z) return true; else return false; }
//外積 public static Vector3D Outer(Vector3D A, Vector3D B) { Vector3D P = new Vector3D(); P.X = A.Y * B.Z - A.Z * B.Y; P.Y = A.Z * B.X - A.X * B.Z; P.Z = A.X * B.Y - A.Y * B.X; return P; }
//2つのベクトルの距離を取得 public float Getdistance(Vector3D A) { return (A - this).Getnorm(); }
//一括座標変換(関数オーバロード) public Vector2D Translate(Vector3D A) { Vector3D P = new Vector3D(); Vector2D R = new Vector2D(); P = ViewTranslate(A); P = ProjectionTranslate(P); R = ViewPortTranslate(P); return R; }
//スクリーン座標変換 public Vector3D ViewTranslate(Vector3D A) { Vector3D P = new Vector3D(); P.X = Vector3D.Inner(A, CameraX) - Vector3D.Inner(Setposition, CameraX); P.Y = Vector3D.Inner(A, CameraY) - Vector3D.Inner(Setposition, CameraY); P.Z = Vector3D.Inner(A, CameraZ) - Vector3D.Inner(Setposition, CameraZ); return P; }
//ビューポート変換 public Vector2D ViewPortTranslate(Vector3D A) { Vector2D P = new Vector2D(); P.X = (float)(A.X * (w / 2) + (w / 2)); P.Y = (float)(A.Y * -(h / 2) + (h / 2)); return P; }
//射影変換 public Vector3D ProjectionTranslate(Vector3D A) { Vector3D P = new Vector3D(); SetProjectionMatrix(); P = A * projectionmatrix; P.Z += (farZ * nearZ) / (farZ - nearZ); P.X = P.X / A.Z; P.Y = P.Y / A.Z; P.Z = P.Z / A.Z; return P; }
//カメラ座標の設定 public void SetCaemraAxes() { CameraZ = LookatPoint - Setposition; CameraZ.Normalization(); CameraX = Vector3D.Outer(Upward, CameraZ); CameraX.Normalization(); CameraY = Vector3D.Outer(CameraZ, CameraX); }
//座標軸より上か判定 public bool CheckIsUp() { Vector3D A = new Vector3D(0, 0, -1); return (Vector3D.Inner(A, -1 * Setposition) < 0); }
//カメラの回転 public void RotateCamera(double x1, double y1, double x2, double y2) { Vector2D A = new Vector2D(); Vector3D N1 = new Vector3D(); Vector3D N2 = new Vector3D(); Vector3D temp = new Vector3D(); double alpha, beta; float x, y, z; N1.X = Setposition.Y; N1.Y = -Setposition.X; N1.Z = LookatPoint.Z; N1.Normalization(); N2.X = LookatPoint.X; N2.Y = LookatPoint.Y; N2.Z = 1; N2.Normalization(); if (Upward.Z > 0) { A.X = (float)((x2 - x1) / w); A.Y = (float)((y2 - y1) / h); } if (Upward.Z < 0) { A.X = (float)((x1 - x2) / w); A.Y = (float)((y1 - y2) / h); } alpha = 2 * Math.PI * A.Y; beta = 2 * Math.PI * A.X; x = Setposition.X; y = Setposition.Y; z = Setposition.Z; rotatematrix.Set_Rotate_ArbAx(N1, alpha); temp = Setposition * rotatematrix; rotatematrix.Set_Rotate_ArbAx(N2, beta); temp = temp * rotatematrix; SetPosition(temp.X, temp.Y, temp.Z); if (Vector3D.Inner(temp2, Setposition - LookatPoint) > 0 && Upward.Z > 0) Upward.Z = -Upward.Z; if (Vector3D.Inner(temp2, Setposition - LookatPoint) < 0 && Upward.Z < 0) Upward.Z = -Upward.Z; if (Upward.Z > 0) temp2.Set(-Setposition.X, -Setposition.Y, LookatPoint.Z); if (Upward.Z < 0) temp2.Set(Setposition.X, Setposition.Y, LookatPoint.Z); }
//任意の点の回転 public void RotatePoint(Vector3D n, ref Vector3D v, float a) { rotatematrix.Set_Rotate_ArbAx(n, a); v = v * rotatematrix; }
//座標を設定 public Vector3D(Vector3D vtr3D) { this.Set(vtr3D.X, vtr3D.Y, vtr3D.Z); }
public static float Inner(Vector3D A, Vector3D B) { return A.X * B.X + A.Y * B.Y + A.Z * B.Z; }
//一括座標変換 public Vector2D[] Translate(Vector3D[] A) { int len = A.Length; Vector3D[] P = new Vector3D[len]; Vector2D[] R = new Vector2D[len]; for (int i = 0; i < len; i++) { P[i] = new Vector3D(); R[i] = new Vector2D(); } for (int i = 0; i < len; i++) { P[i] = ViewTranslate(A[i]); P[i] = ProjectionTranslate(P[i]); R[i] = ViewPortTranslate(P[i]); } return R; }