public override void Initialize() { GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.Lighting); GL.Enable(EnableCap.Light0); cameraPlane = new Plane(); Resize(MainGameWindow.Window.Width, MainGameWindow.Window.Height); grid = new Grid(true); model = new OBJLoader("Assets/test_object.obj"); GL.Light(LightName.Light0, LightParameter.Position, new float[] { 0.0f, 0.5f, 0.5f, 0.0f }); GL.Light(LightName.Light0, LightParameter.Ambient, new float[] { 0f, 1f, 0f, 1f }); GL.Light(LightName.Light0, LightParameter.Diffuse, new float[] { 0f, 1f, 0f, 1f }); GL.Light(LightName.Light0, LightParameter.Specular, new float[] { 1f, 1f, 1f, 1f }); for (int i = 0; i < 6; i++) { frustum[i] = new Plane(); } MouseState mouse = OpenTK.Input.Mouse.GetState(); LastMousePosition = new Vector2(mouse.X, mouse.Y); viewMatrix = Move3DCamera(0f); }
public static Matrix4 operator /(Matrix4 matrixA, Matrix4 matrixB) { Matrix4 result = new Matrix4(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { result[i, j] = matrixA[i, j] / matrixB[i, j]; } } return result; }
public static Matrix4 operator *(Matrix4 matrixA, float scale) { Matrix4 result = new Matrix4(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { result[i, j] = matrixA[i, j] * scale; } } return result; }
public void Push() { //new matrix Matrix4 top = new Matrix4(); //copy values of top matrix for (int i = 0; i < stack[stack.Count - 1].Matrix.Length; i++) { top.Matrix[i] = stack[stack.Count-1].Matrix[i]; } //add new copy to top stack.Add(top); }
// Has a default implementation, making the need to override in child // classes purley optional public virtual void Resize(int width, int height) { GL.Viewport(0, 0, width, height); GL.MatrixMode(MatrixMode.Projection); float aspect = (float)width / (float)height; Math_Implementation.Matrix4 perspective = Math_Implementation.Matrix4.Perspective(60, aspect, 0.01f, 1000.0f); GL.LoadMatrix(Math_Implementation.Matrix4.Transpose(perspective).Matrix); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); }
public virtual void Render() { // Again, Vector3 and Matrix4 must be fully scoped, because // the OpenTK namespace contains both classes already Math_Implementation.Vector3 eyePos = new Math_Implementation.Vector3(); eyePos.X = _baseCameraAngle.Z * -(float)System.Math.Sin(_baseCameraAngle.X * _baseRads * (float)System.Math.Cos(_baseCameraAngle.Y * _baseRads)); eyePos.Y = _baseCameraAngle.Z * -(float)System.Math.Sin(_baseCameraAngle.Y * _baseRads); eyePos.Z = -_baseCameraAngle.Z * (float)System.Math.Cos(_baseCameraAngle.X * _baseRads * (float)System.Math.Cos(_baseCameraAngle.Y * _baseRads)); Math_Implementation.Matrix4 lookAt = Math_Implementation.Matrix4.LookAt(eyePos, new Math_Implementation.Vector3(0.0f, 0.0f, 0.0f), new Math_Implementation.Vector3(0.0f, 1.0f, 0.0f)); GL.LoadMatrix(Math_Implementation.Matrix4.Transpose(lookAt).Matrix); }
public static Matrix4 LookAt(Vector3 position, Vector3 target, Vector3 worldUp) { Vector3 cameraForward = Vector3.Normalize(target - position); Vector3 cameraRight = Vector3.Normalize(Vector3.Cross(cameraForward, worldUp)); Vector3 cameraUp = Vector3.Cross(cameraRight, cameraForward); Matrix4 rot = new Matrix4(cameraRight.X, cameraUp.X, -cameraForward.X, 0.0f, cameraRight.Y, cameraUp.Y, -cameraForward.Y, 0.0f, cameraRight.Z, cameraUp.Z, -cameraForward.Z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); Matrix4 trans = Translate(position * -1.0f); return Transpose(rot) * trans; }
public static Matrix4 ToMatrix(Quaternion q) { Matrix4 result = new Matrix4(); float xSq = q.X * q.X; float ySq = q.Y * q.Y; float zSq = q.Z * q.Z; float wSq = q.W * q.W; float twoX = 2.0f * q.X; float twoY = 2.0f * q.Y; float twoW = 2.0f * q.W; float xy = twoX * q.Y; float xz = twoX * q.Z; float yz = twoY * q.Z; float wx = twoW * q.X; float wy = twoW * q.Y; float wz = twoW * q.Z; result[0, 0] = wSq + xSq - ySq - zSq; result[0, 1] = xy - wz; result[0, 2] = xz + wy; result[0, 3] = 0.0f; result[1, 0] = xy + wz; result[1, 1] = wSq - xSq + ySq - zSq; result[1, 2] = yz - wx; result[1, 3] = 0.0f; result[2, 0] = xz - wy; result[2, 1] = yz + wx; result[2, 2] = wSq - xSq - ySq + zSq; result[2, 3] = 0.0f; result[3, 0] = 0.0f; result[3, 1] = 0.0f; result[3, 2] = 0.0f; result[3, 3] = 1.0f; return result; }
public static Matrix4 Ortho(float left, float right, float bottom, float top, float near, float far) { Matrix4 result = new Matrix4(); result[0, 0] = 2.0f / (right - left); result[0, 3] = -((right + left) / (right - left)); result[1, 1] = 2.0f / (top - bottom); result[1, 3] = -((top + bottom) / (top - bottom)); result[2, 2] = -2.0f / (far - near); result[2, 3] = -((far + near) / (far - near)); result[3, 3] = 1.0f; return result; }
public static Matrix4 Frustum(float left, float right, float bottom, float top, float near, float far) { Matrix4 result = new Matrix4(); result[0, 0] = (2.0f * near) / (right - left); result[0, 2] = (right + left) / (right - left); result[1, 1] = (2.0f * near) / (top - bottom); result[1, 2] = (top + bottom) / (top - bottom); result[2, 2] = -((far + near) / (far - near)); result[2, 3] = -((2.0f * far * near) / (far - near)); result[3, 2] = -1.0f; result[3, 3] = 0.0f; return result; }
public static Matrix4 Scale(Vector3 vectorA) { Matrix4 result = new Matrix4(); result[0, 0] = vectorA[0]; result[1, 1] = vectorA[1]; result[2, 2] = vectorA[2]; result[3, 3] = 1.0f; return result; }
public static Vector3 MultiplyPoint(Matrix4 matrixA, Vector3 vectorA) { Vector3 result = new Vector3(); result[0] = (matrixA[0, 0] * vectorA[0]) + (matrixA[0, 1] * vectorA[1]) + (matrixA[0, 2] * vectorA[2]) + (matrixA[0, 3] * 1.0f); result[1] = (matrixA[1, 0] * vectorA[0]) + (matrixA[1, 1] * vectorA[1]) + (matrixA[1, 2] * vectorA[2]) + (matrixA[1, 3] * 1.0f); result[2] = (matrixA[2, 0] * vectorA[0]) + (matrixA[2, 1] * vectorA[1]) + (matrixA[2, 2] * vectorA[2]) + (matrixA[2, 3] * 1.0f); return result; }
//matrix matrix multiplication public static Matrix4 operator *(Matrix4 matrixA, Matrix4 matrixB) { Matrix4 result = new Matrix4(); result[0, 0] = matrixA[0, 0] * matrixB[0, 0] + matrixA[0, 1] * matrixB[1, 0] + matrixA[0, 2] * matrixB[2, 0] + matrixA[0, 3] * matrixB[3, 0]; result[0, 1] = matrixA[0, 0] * matrixB[0, 1] + matrixA[0, 1] * matrixB[1, 1] + matrixA[0, 2] * matrixB[2, 1] + matrixA[0, 3] * matrixB[3, 1]; result[0, 2] = matrixA[0, 0] * matrixB[0, 2] + matrixA[0, 1] * matrixB[1, 2] + matrixA[0, 2] * matrixB[2, 2] + matrixA[0, 3] * matrixB[3, 2]; result[0, 3] = matrixA[0, 0] * matrixB[0, 3] + matrixA[0, 1] * matrixB[1, 3] + matrixA[0, 2] * matrixB[2, 3] + matrixA[0, 3] * matrixB[3, 3]; // result[1, 0] = matrixA[1, 0] * matrixB[0, 0] + matrixA[1, 1] * matrixB[1, 0] + matrixA[1, 2] * matrixB[2, 0] + matrixA[1, 3] * matrixB[3, 0]; result[1, 1] = matrixA[1, 0] * matrixB[0, 1] + matrixA[1, 1] * matrixB[1, 1] + matrixA[1, 2] * matrixB[2, 1] + matrixA[1, 3] * matrixB[3, 1]; result[1, 2] = matrixA[1, 0] * matrixB[0, 2] + matrixA[1, 1] * matrixB[1, 2] + matrixA[1, 2] * matrixB[2, 2] + matrixA[1, 3] * matrixB[3, 2]; result[1, 3] = matrixA[1, 0] * matrixB[0, 3] + matrixA[1, 1] * matrixB[1, 3] + matrixA[1, 2] * matrixB[2, 3] + matrixA[1, 3] * matrixB[3, 3]; // result[2, 0] = matrixA[2, 0] * matrixB[0, 0] + matrixA[2, 1] * matrixB[1, 0] + matrixA[2, 2] * matrixB[2, 0] + matrixA[2, 3] * matrixB[3, 0]; result[2, 1] = matrixA[2, 0] * matrixB[0, 1] + matrixA[2, 1] * matrixB[1, 1] + matrixA[2, 2] * matrixB[2, 1] + matrixA[2, 3] * matrixB[3, 1]; result[2, 2] = matrixA[2, 0] * matrixB[0, 2] + matrixA[2, 1] * matrixB[1, 2] + matrixA[2, 2] * matrixB[2, 2] + matrixA[2, 3] * matrixB[3, 2]; result[2, 3] = matrixA[2, 0] * matrixB[0, 3] + matrixA[2, 1] * matrixB[1, 3] + matrixA[2, 2] * matrixB[2, 3] + matrixA[2, 3] * matrixB[3, 3]; // result[3, 0] = matrixA[3, 0] * matrixB[0, 0] + matrixA[3, 1] * matrixB[1, 0] + matrixA[3, 2] * matrixB[2, 0] + matrixA[3, 3] * matrixB[3, 0]; result[3, 1] = matrixA[3, 0] * matrixB[0, 1] + matrixA[3, 1] * matrixB[1, 1] + matrixA[3, 2] * matrixB[2, 1] + matrixA[3, 3] * matrixB[3, 1]; result[3, 2] = matrixA[3, 0] * matrixB[0, 2] + matrixA[3, 1] * matrixB[1, 2] + matrixA[3, 2] * matrixB[2, 2] + matrixA[3, 3] * matrixB[3, 2]; result[3, 3] = matrixA[3, 0] * matrixB[0, 3] + matrixA[3, 1] * matrixB[1, 3] + matrixA[3, 2] * matrixB[2, 3] + matrixA[3, 3] * matrixB[3, 3]; return result; }
public static Matrix4 CoFactor(Matrix4 matrixA) { Matrix4 result = Minor(matrixA); result[0, 0] = result[0, 0] * 1; result[0, 1] = result[0, 1] * -1; result[0, 2] = result[0, 2] * 1; result[0, 3] = result[0, 3] * -1; result[1, 0] = result[1, 0] * -1; result[1, 1] = result[1, 1] * 1; result[1, 2] = result[1, 2] * -1; result[1, 3] = result[1, 3] * 1; result[2, 0] = result[2, 0] * 1; result[2, 1] = result[2, 1] * -1; result[2, 2] = result[2, 2] * 1; result[2, 3] = result[2, 3] * -1; result[3, 0] = result[3, 0] * -1; result[3, 1] = result[3, 1] * 1; result[3, 2] = result[3, 2] * -1; result[3, 3] = result[3, 3] * 1; return result; }
public void Load(Matrix4 matrix) { for (int i = 0; i <matrix.Matrix.Length; i++) { stack[stack.Count-1].Matrix[i] = matrix.Matrix[i]; } }
//transpose public static Matrix4 Transpose(Matrix4 matrixA) { Matrix4 result = new Matrix4(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { result[i, j] = matrixA[j, i]; } } return result; }
public static Matrix4 Inverse(Matrix4 matrixA) { Matrix4 result = Adjugate(matrixA); float determinant = Determinant(matrixA); result *= 1.0f / determinant; return result; }
public static float Determinant(Matrix4 matrix) { Matrix4 cofactor = CoFactor(matrix); return (matrix[0, 0] * cofactor[0, 0]) + (matrix[0, 1] * cofactor[0, 1]) + (matrix[0, 2] * cofactor[0, 2]) + (matrix[0, 3] * cofactor[0, 3]); }
public static Matrix4 Adjugate(Matrix4 matrixA) { return Transpose(CoFactor(matrixA)); }
public Matrix4 ToMatrix() { Matrix4 result = new Matrix4(); float xSq = X * X; float ySq = Y * Y; float zSq = Z * Z; float wSq = W * W; float twoX = 2.0f *X; float twoY = 2.0f *Y; float twoW = 2.0f *W; float xy = twoX * Y; float xz = twoX * Z; float yz = twoY * Z; float wx = twoW * X; float wy = twoW * Y; float wz = twoW * Z; result[0, 0] = wSq + xSq - ySq - zSq; result[0, 1] = xy - wz; result[0, 2] = xz + wy; result[0, 3] = 0.0f; result[1, 0] = xy + wz; result[1, 1] = wSq - xSq + ySq - zSq; result[1, 2] = yz - wx; result[1, 3] = 0.0f; result[2, 0] = xz - wy; result[2, 1] = yz + wx; result[2, 2] = wSq - xSq - ySq + zSq; result[2, 3] = 0.0f; result[3, 0] = 0.0f; result[3, 1] = 0.0f; result[3, 2] = 0.0f; result[3, 3] = 1.0f; return result; }
public static Matrix4 Translate(Vector3 vectorA) { Matrix4 result = new Matrix4(); result[0, 3] = vectorA[0]; result[1, 3] = vectorA[1]; result[2, 3] = vectorA[2]; result[3, 3] = 1.0f; return result; }
public static Matrix4 XRotation(float theta) { theta = theta * (float)(Math.PI / 180); //convert to rads Matrix4 result = new Matrix4(1, 0, 0, 0, 0, (float)Math.Cos(theta), -(float)Math.Sin(theta), 0, 0, (float)Math.Sin(theta), (float)Math.Cos(theta), 0, 0,0,0,1); return result; }
public static Matrix4 Minor(Matrix4 matrixA) { Matrix4 result = new Matrix4(); result[0, 0] = Matrix3.Determinant(new Matrix3(matrixA[1, 1], matrixA[1, 2], matrixA[1, 3], matrixA[2, 1], matrixA[2, 2], matrixA[2, 3], matrixA[3, 1], matrixA[3, 2], matrixA[3, 3])); result[0, 1] = Matrix3.Determinant(new Matrix3(matrixA[1, 0], matrixA[1, 2], matrixA[1, 3], matrixA[2, 0], matrixA[2, 2], matrixA[2, 3], matrixA[3, 0], matrixA[3, 2], matrixA[3, 3])); result[0, 2] = Matrix3.Determinant(new Matrix3(matrixA[1, 0], matrixA[1, 1], matrixA[1, 3], matrixA[2, 0], matrixA[2, 1], matrixA[2, 3], matrixA[3, 0], matrixA[3, 1], matrixA[3, 3])); result[0, 3] = Matrix3.Determinant(new Matrix3(matrixA[1, 0], matrixA[1, 1], matrixA[1, 2], matrixA[2, 0], matrixA[2, 1], matrixA[2, 2], matrixA[3, 0], matrixA[3, 1], matrixA[3, 2])); result[1, 0] = Matrix3.Determinant(new Matrix3(matrixA[0, 1], matrixA[0, 2], matrixA[0, 3], matrixA[2, 1], matrixA[2, 2], matrixA[2, 3], matrixA[3, 1], matrixA[3, 2], matrixA[3, 3])); result[1, 1] = Matrix3.Determinant(new Matrix3(matrixA[0, 0], matrixA[0, 2], matrixA[0, 3], matrixA[2, 0], matrixA[2, 2], matrixA[2, 3], matrixA[3, 0], matrixA[3, 2], matrixA[3, 3])); result[1, 2] = Matrix3.Determinant(new Matrix3(matrixA[0, 0], matrixA[0, 1], matrixA[0, 3], matrixA[2, 0], matrixA[2, 1], matrixA[2, 3], matrixA[3, 0], matrixA[3, 1], matrixA[3, 3])); result[1, 3] = Matrix3.Determinant(new Matrix3(matrixA[0, 0], matrixA[0, 1], matrixA[0, 2], matrixA[2, 0], matrixA[2, 1], matrixA[2, 2], matrixA[3, 0], matrixA[3, 1], matrixA[3, 2])); result[2, 0] = Matrix3.Determinant(new Matrix3(matrixA[0, 1], matrixA[0, 2], matrixA[0, 3], matrixA[1, 1], matrixA[1, 2], matrixA[1, 3], matrixA[3, 1], matrixA[3, 2], matrixA[3, 3])); result[2, 1] = Matrix3.Determinant(new Matrix3(matrixA[0, 0], matrixA[0, 2], matrixA[0, 3], matrixA[1, 0], matrixA[1, 2], matrixA[1, 3], matrixA[3, 0], matrixA[3, 2], matrixA[3, 3])); result[2, 2] = Matrix3.Determinant(new Matrix3(matrixA[0, 0], matrixA[0, 1], matrixA[0, 3], matrixA[1, 0], matrixA[1, 1], matrixA[1, 3], matrixA[3, 0], matrixA[3, 1], matrixA[3, 3])); result[2, 3] = Matrix3.Determinant(new Matrix3(matrixA[0, 0], matrixA[0, 1], matrixA[0, 2], matrixA[1, 0], matrixA[1, 1], matrixA[1, 2], matrixA[3, 0], matrixA[3, 1], matrixA[3, 2])); result[3, 0] = Matrix3.Determinant(new Matrix3(matrixA[0, 1], matrixA[0, 2], matrixA[0, 3], matrixA[1, 1], matrixA[1, 2], matrixA[1, 3], matrixA[2, 1], matrixA[2, 2], matrixA[2, 3])); result[3, 1] = Matrix3.Determinant(new Matrix3(matrixA[0, 0], matrixA[0, 2], matrixA[0, 3], matrixA[1, 0], matrixA[1, 2], matrixA[1, 3], matrixA[2, 0], matrixA[2, 2], matrixA[2, 3])); result[3, 2] = Matrix3.Determinant(new Matrix3(matrixA[0, 0], matrixA[0, 1], matrixA[0, 3], matrixA[1, 0], matrixA[1, 1], matrixA[1, 3], matrixA[2, 0], matrixA[2, 1], matrixA[2, 3])); result[3, 3] = Matrix3.Determinant(new Matrix3(matrixA[0, 0], matrixA[0, 1], matrixA[0, 2], matrixA[1, 0], matrixA[1, 1], matrixA[1, 2], matrixA[2, 0], matrixA[2, 1], matrixA[2, 2])); return result; }
public void Mul(Matrix4 matrix) { stack[stack.Count - 1] = stack[stack.Count - 1] * matrix; }
public static Matrix4 ZRotation(float theta) { theta = theta * (float)(Math.PI / 180); Matrix4 result = new Matrix4((float)Math.Cos(theta), -(float)Math.Sin(theta), 0, 0, (float)Math.Sin(theta), (float)Math.Cos(theta), 0, 0, 0, 0, 1, 0, 0,0,0,1); return result; }
public override void Update(float dTime) { viewMatrix = Move3DCamera(dTime); }
public static Matrix4 AngleAxis(float angle, float u, float v, float w) { Matrix4 result = new Matrix4(); float L = (u * u + v * v + w * w); float l = (float)Math.Sqrt(L); angle = angle * (float)(Math.PI / 180.0); //converting to radian value float u2 = u * u; float v2 = v * v; float w2 = w * w; result[0, 0] = (u2 + (v2 + w2) * (float)Math.Cos(angle)) / L; result[0, 1] = (u * v * (1 - (float)Math.Cos(angle)) - w * l * (float)Math.Sin(angle)) / L; result[0, 2] = (u * w * (1 - (float)Math.Cos(angle)) + v * l * (float)Math.Sin(angle)) / L; result[0, 3] = 0.0f; result[1, 0] = (u * v * (1 - (float)Math.Cos(angle)) + w * l * (float)Math.Sin(angle)) / L; result[1, 1] = (v2 + (u2 + w2) * (float)Math.Cos(angle)) / L; result[1, 2] = (v * w * (1 - (float)Math.Cos(angle)) - u * l * (float)Math.Sin(angle)) / L; result[1, 3] = 0.0f; result[2, 0] = (u * w * (1 - (float)Math.Cos(angle)) - v * l * (float)Math.Sin(angle)) / L; result[2, 1] = (v * w * (1 - (float)Math.Cos(angle)) + u * l * (float)Math.Sin(angle)) / L; result[2, 2] = (w2 + (u2 + v2) * (float)Math.Cos(angle)) / L; result[2, 3] = 0.0f; result[3, 0] = 0.0f; result[3, 1] = 0.0f; result[3, 2] = 0.0f; result[3, 3] = 1.0f; return result; }