// Default Constructor public Camera() { position = new Vertex (0.0f, 0.0f, 0.0f); viewdir = new Vector (0.0f, 0.0f, -1.0f); right = new Vector (1.0f, 0.0f, 0.0f); up = new Vector (0.0f, 1.0f, 0.0f); }
public Camera(float x, float y, float z) { position = new Vertex (x, y, z); viewdir = new Vector (0.0f, 0.0f, -1.0f); right = new Vector (1.0f, 0.0f, 0.0f); up = new Vector (0.0f, 1.0f, 0.0f); }
// Positional Constructors public Camera(Vertex position) { this.position = position; viewdir = new Vector (0.0f, 0.0f, -1.0f); right = new Vector (1.0f, 0.0f, 0.0f); up = new Vector (0.0f, 1.0f, 0.0f); }
// Rotate camera left or right (aka Pan) public void Yaw(float angle) { // rotate around the up vector (normally the y-axis) viewdir = (viewdir * (float) Math.Cos (angle)) - (right * (float) Math.Sin (angle)); viewdir.Normalize (); right = viewdir.Cross (up); right.Normalize (); }
// Roll the camera left or right public void Roll(float angle) { // rotate around the view-direction vector (normally the z-axis) right = (right * (float) Math.Cos (angle)) + (up * (float) Math.Sin (angle)); right.Normalize (); up = viewdir.Cross (right) * -1.0f; up.Normalize (); }
// Rotate camera up or down (aka Tilt) public void Pitch(float angle) { // rotate around the right vector (normally the x-axis) viewdir = (viewdir * (float) Math.Cos (angle)) + (up * (float) Math.Sin (angle)); viewdir.Normalize (); up = viewdir.Cross (right); up.Normalize (); }
// Move the camera relative to its own axis/orientation public void MoveRelative(Vector amount) { Elevate (amount.y); Strafe (amount.x); Zoom (amount.z); }
// Move the camera relative to world axis public void Move(Vector amount) { position += amount; }
public VectorCubesScene(bool enableLighting) : base() { int i; lighting = enableLighting; if (formations == null) { //float[][,] floats = new float[7][,]; //floats[0] = flat_plane_floats; //floats[1] = snowflake_floats; //floats[2] = cross_plane_floats; //floats[3] = tunnel_floats; //floats[4] = hollow_cube_floats; //floats[5] = dice_floats; //floats[6] = nested_floats; // //formations = new Vector [7][]; // //for (i = 0; i < 7; i++) { // formations[i] = new Vector [144]; // for (int j = 0; i < 144; i++) { // float x = floats[i][j, 0]; // float y = floats[i][j, 1]; // float z = floats[i][j, 2]; // // formations[i][j] = new Vector (x, y, z); // } //} float x, y, z; formations = new Vector [7][]; for (i = 0; i < 7; i++) formations[i] = new Vector [144]; // flat plane formation for (i = 0; i < 144; i++) { x = flat_plane_floats[i, 0]; y = flat_plane_floats[i, 1]; z = flat_plane_floats[i, 2]; formations[0][i] = new Vector (x, y, z); } // snowflake formation for (i = 0; i < 144; i++) { x = snowflake_floats[i, 0]; y = snowflake_floats[i, 1]; z = snowflake_floats[i, 2]; formations[1][i] = new Vector (x, y, z); } // cross-plane formation for (i = 0; i < 144; i++) { x = cross_plane_floats[i, 0]; y = cross_plane_floats[i, 1]; z = cross_plane_floats[i, 2]; formations[2][i] = new Vector (x, y, z); } // tunnel formation for (i = 0; i < 144; i++) { x = tunnel_floats[i, 0]; y = tunnel_floats[i, 1]; z = tunnel_floats[i, 2]; formations[3][i] = new Vector (x, y, z); } // hollow cube formation for (i = 0; i < 144; i++) { x = hollow_cube_floats[i, 0]; y = hollow_cube_floats[i, 1]; z = hollow_cube_floats[i, 2]; formations[4][i] = new Vector (x, y, z); } // dice formation for (i = 0; i < 144; i++) { x = dice_floats[i, 0]; y = dice_floats[i, 1]; z = dice_floats[i, 2]; formations[5][i] = new Vector (x, y, z); } // nested cubes formation for (i = 0; i < 144; i++) { x = nested_floats[i, 0]; y = nested_floats[i, 1]; z = nested_floats[i, 2]; formations[6][i] = new Vector (x, y, z); } } cubes = new Vector [144]; for (i = 0; i < 144; i++) cubes[i] = new Vector (); }
// Dot product of 2 Vectors public float Dot(Vector vector) { return (x * vector.x) + (y * vector.y) + (z * vector.z); }
// Cross product of 2 Vectors public Vector Cross(Vector vector) { return new Vector ((y * vector.z) - (z * vector.y), (z * vector.x) - (x * vector.z), (x * vector.y) - (y * vector.x)); }
// Angle between this Vector and another Vector public float Angle(Vector vector) { float dot = this.Dot (vector); float n1 = vector.LengthSquared; float n0 = LengthSquared; return (float) Math.Acos (dot / Math.Sqrt ((double) (n0 * n1))); }
// Dot product of 2 Vectors public static float Dot(Vector v0, Vector v1) { return (v0.x * v1.x) + (v0.y * v1.y) + (v0.z * v1.z); }
// Cross product of 2 Vectors public static Vector Cross(Vector v0, Vector v1) { return new Vector ((v0.y * v1.z) - (v0.z * v1.y), (v0.z * v1.x) - (v0.x * v1.z), (v0.x * v1.y) - (v0.y * v1.x)); }
// Angle between 2 vectors public static float Angle(Vector v0, Vector v1) { float n0 = v0.LengthSquared; float n1 = v1.LengthSquared; float dot = v0.Dot (v1); return (float) Math.Acos (dot / Math.Sqrt ((double) (n0 * n1))); }
public void LookAt(Vertex point) { Vector delta = point - position; viewdir = delta.Normal; if (Math.Abs (delta.x) < 0.00001f && Math.Abs (delta.z) < 0.00001f) { delta.x = 0.0f; delta.Normalize (); right = new Vector (1.0f, 0.0f, 0.0f); up = delta.Cross (right); right = viewdir.Cross (up) * -1.0f; } else { delta.y = 0.0f; delta.Normalize (); up = new Vector (0.0f, 1.0f, 0.0f); right = delta.Cross (up) * -1.0f; up = viewdir.Cross (right); } right.Normalize (); up.Normalize (); }
// Direct the camera toward a specific point in space using the specified upward orientation public void LookAt(Vertex point, Vector upVector) { viewdir = point - position; viewdir.Normalize (); up = upVector.Normal; right = viewdir.Cross (up); right.Normalize (); }
public Camera(Vertex position, Vertex target, Vector upVector) { this.position = position; LookAt (target, upVector); }
static int MoveCube(ref Vector cube, Vector start, Vector finish) { Vector delta = finish - cube; if (delta <= 0.001f) { cube.x = finish.x; cube.y = finish.y; cube.z = finish.z; return 0; } delta = finish - start; delta *= 0.005f; cube += delta; return 1; }