public static Matrix4x4 LookAtLeftHanded(Vector3 camera, Vector3 target, Vector3 up) { var zAxis = target - camera; zAxis = zAxis.Normalize(); var xAxis = up.CrossProduct(zAxis); xAxis = xAxis.Normalize(); var yAxis = zAxis.CrossProduct(xAxis); var result = MatrixPrefabs.Identity(); result[0, 0] = xAxis.X; result[1, 0] = xAxis.Y; result[2, 0] = xAxis.Z; result[0, 1] = yAxis.X; result[1, 1] = yAxis.Y; result[2, 1] = yAxis.Z; result[0, 2] = zAxis.X; result[1, 2] = zAxis.Y; result[2, 2] = zAxis.Z; result[3, 0] = xAxis.DotProduct(camera); result[3, 1] = yAxis.DotProduct(camera); result[3, 2] = zAxis.DotProduct(camera); result[3, 0] = -result[3, 0]; result[3, 1] = -result[3, 1]; result[3, 2] = -result[3, 2]; return(result); }
private static Matrix4x4 TranslationMatrix(float x, float y, float z) { // 1 0 0 0 // 0 1 0 0 // 0 0 1 0 // x y z 1 var result = MatrixPrefabs.Identity(); result[3, 0] = x; result[3, 1] = y; result[3, 2] = z; return(result); }
public void Render(Camera camera, Bitmap bmp, IEnumerable <LightSource> lightSources, params Mesh[] meshes) { workingBitmap = bmp; zBuffer = new ZBuffer(workingBitmap.Size.Width, workingBitmap.Size.Height); pointDrawingAlgorithm = new PointDrawingAlgorithm(workingBitmap, zBuffer); var viewMatrix = MatrixPrefabs.LookAtLeftHanded(camera.Position, camera.Target, Vector3Prefabs.UnitY); var projectionMatrix = MatrixPrefabs.PerspectiveFieldOfViewRightHanded( 0.78f, (float)workingBitmap.Size.Width / workingBitmap.Size.Height, 0.01f, 1.0f); foreach (Mesh mesh in meshes) { var worldMatrix = MatrixPrefabs.RotationMatrixYawPitchRoll( mesh.Rotation.Y, mesh.Rotation.X, mesh.Rotation.Z) * MatrixPrefabs.TranslationMatrix(mesh.Position); var transformMatrix = worldMatrix * viewMatrix * projectionMatrix; foreach (var face in mesh.Faces) { var vertexA = mesh.Vertices[face.A]; var vertexB = mesh.Vertices[face.B]; var vertexC = mesh.Vertices[face.C]; var vertexATransformed = Project(vertexA, transformMatrix, worldMatrix); var vertexBTransformed = Project(vertexB, transformMatrix, worldMatrix); var vertexCTransformed = Project(vertexC, transformMatrix, worldMatrix); //DrawLine(pixelA, pixelB, vertexA.Color, vertexB.Color); //DrawLine(pixelB, pixelC, vertexB.Color, vertexC.Color); //DrawLine(pixelC, pixelA, vertexC.Color, vertexA.Color); //DrawLineBresenham(pixelA, pixelB, vertexA.Color, vertexB.Color); //DrawLineBresenham(pixelB, pixelC, vertexB.Color, vertexC.Color); //DrawLineBresenham(pixelC, pixelA, vertexC.Color, vertexA.Color); DrawTriangle(vertexATransformed, vertexBTransformed, vertexCTransformed, lightSources); } } }
public static Matrix4x4 RotationMatrixYawPitchRoll(float yaw, float pitch, float roll) { // TODO learn quaternions float halfRoll = roll * 0.5f; float halfPitch = pitch * 0.5f; float halfYaw = yaw * 0.5f; float sinRoll = (float)Math.Sin(halfRoll); float cosRoll = (float)Math.Cos(halfRoll); float sinPitch = (float)Math.Sin(halfPitch); float cosPitch = (float)Math.Cos(halfPitch); float sinYaw = (float)Math.Sin(halfYaw); float cosYaw = (float)Math.Cos(halfYaw); var vec4 = new Vector4(); vec4.X = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll); vec4.Y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll); vec4.Z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll); vec4.W = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll); float xx = vec4.X * vec4.X; float yy = vec4.Y * vec4.Y; float zz = vec4.Z * vec4.Z; float xy = vec4.X * vec4.Y; float zw = vec4.Z * vec4.W; float zx = vec4.Z * vec4.X; float yw = vec4.Y * vec4.W; float yz = vec4.Y * vec4.Z; float xw = vec4.X * vec4.W; var result = MatrixPrefabs.Identity(); result[0, 0] = 1.0f - (2.0f * (yy + zz)); result[0, 2] = 2.0f * (xy + zw); result[0, 2] = 2.0f * (zx - yw); result[1, 0] = 2.0f * (xy - zw); result[1, 1] = 1.0f - (2.0f * (zz + xx)); result[1, 2] = 2.0f * (yz + xw); result[2, 0] = 2.0f * (zx + yw); result[2, 1] = 2.0f * (yz - xw); result[2, 2] = 1.0f - (2.0f * (yy + xx)); return(result); }
public static Matrix4x4 TranslationMatrix(Vector3 toPosition) { return(MatrixPrefabs.TranslationMatrix(toPosition.X, toPosition.Y, toPosition.Z)); }