private void GetWindowCoordinates(MCommonPrimitive shape) { foreach (MPoint point in shape.GetVertices()) { TransformPointCoordsToDecart(point); } }
private void RotateShape(MCommonPrimitive shape, double angle, Vector3 axis) { double rads = angle * Math.PI / 180.0; Quaternion rotationQuaternion = GetRotationQuaternion(axis, rads); shape.RotationQuaternion *= rotationQuaternion; }
private void GetNormalizedCoordinates(MCommonPrimitive shape) { List <MPoint> vertices = shape.GetVertices(); for (int i = 0; i < vertices.Count; ++i) { float[,] newCoords = { { vertices[i].X / vertices[i].W }, { vertices[i].Y / vertices[i].W }, { vertices[i].Z / vertices[i].W }, { vertices[i].W } }; SetNewCoordinatesToPoint(vertices[i], newCoords); } }
private void MoveShapeToPreviousPosition(MCommonPrimitive shape, MPoint previousShapeCenter) { var vertices = shape.GetVertices(); for (int i = 0; i < vertices.Count; ++i) { vertices[i].X += previousShapeCenter.X; vertices[i].Y += previousShapeCenter.Y; vertices[i].Z += previousShapeCenter.Z; } }
//------------------SCALING------------------------------- public void Scale(MCommonPrimitive shape, float sx, float sy, float sz) { float[,] scaleMatrix = { { sx, 0, 0, 0 }, { 0, sy, 0, 0 }, { 0, 0, sz, 0 }, { 0, 0, 0, 1 } }; shape.ModelMatrix = MatrixMultiplier.MultiplyMatrix(shape.ModelMatrix, scaleMatrix); }
//------------------TRANSLATING------------------------------- public void Translate(MCommonPrimitive shape, float sx, float sy, float sz) { float[,] movementMatrix = { { 1, 0, 0, sx }, { 0, 1, 0, sy }, { 0, 0, 1, sz }, { 0, 0, 0, 1 } }; shape.ModelMatrix = MatrixMultiplier.MultiplyMatrix(movementMatrix, shape.ModelMatrix); }
private void GetClippedCoordinates(MCommonPrimitive shape, float[,] projectionMatrix) { List <MPoint> vertices = shape.GetVertices(); for (int i = 0; i < vertices.Count; ++i) { float[,] vertexCoords = { { vertices[i].X }, { vertices[i].Y }, { vertices[i].Z }, { 1 } }; float[,] newCoords = MatrixMultiplier.MultiplyMatrix(projectionMatrix, vertexCoords); SetNewCoordinatesToPoint(vertices[i], newCoords); } }
private void MoveShapeToOrigin(MCommonPrimitive shape, out MPoint shapeCenter) { var vertices = shape.GetVertices(); shapeCenter = shape.GetCenterPoint(); for (int i = 0; i < vertices.Count; ++i) { vertices[i].X -= shapeCenter.X; vertices[i].Y -= shapeCenter.Y; vertices[i].Z -= shapeCenter.Z; } }
public void RotateY(MCommonPrimitive shape, double angle) { double rads = angle * Math.PI / 180.0; float[,] rotateY = { { (float)Math.Cos(rads), 0, (float)Math.Sin(rads), 0 }, { 0, 1, 0, 0 }, { -(float)Math.Sin(rads), 0, (float)Math.Cos(rads), 0 }, { 0, 0, 0, 1 } }; shape.ModelMatrix = MatrixMultiplier.MultiplyMatrix(shape.ModelMatrix, rotateY); }
private void TransformShape(MCommonPrimitive shape) { List <MPoint> vertices = shape.GetVertices(); for (int i = 0; i < vertices.Count; ++i) { float[,] vertexCoords = { { vertices[i].SX }, { vertices[i].SY }, { vertices[i].SZ }, { 1 } }; float[,] newCoords = MatrixMultiplier.MultiplyMatrix(shape.ModelMatrix, vertexCoords); SetNewCoordinatesToPoint(vertices[i], newCoords); ////TEST //TransformPointCoordsToDecart(vertices[i]); } }
//public void Rotate(MCommonPrimitive shape, double sx, double sy, double sz) //{ // if (sx != 0) // RotateX(shape, sx); // if (sy != 0) // RotateY(shape, sy); // if (sz != 0) // RotateZ(shape, sz); //} //public void RotateX(MCommonPrimitive shape, double angle) //{ // var axis = Vector3.UnitX; // RotateShape(shape, angle, axis); //} //public void RotateY(MCommonPrimitive shape, double angle) //{ // var axis = Vector3.UnitY; // RotateShape(shape, angle, axis); //} //public void RotateZ(MCommonPrimitive shape, double angle) //{ // var axis = Vector3.UnitZ; // RotateShape(shape, angle, axis); //} //------------------ROTATING------------------------------- public void Rotate(MCommonPrimitive shape, double sx, double sy, double sz) { if (sx != 0) { RotateX(shape, sx); } if (sy != 0) { RotateY(shape, sy); } if (sz != 0) { RotateZ(shape, sz); } }
public void GetTransformedShape(MCommonPrimitive shape, Camera camera) { var vertices = shape.GetVertices(); for (int i = 0; i < vertices.Count; ++i) { float[,] vertexCoords = { { vertices[i].SX }, { vertices[i].SY }, { vertices[i].SZ }, { vertices[i].SW } }; var modelViewMatrix = MatrixMultiplier.MultiplyMatrix(camera.ViewMatrix, shape.ModelMatrix); var eyeCoordinates = MatrixMultiplier.MultiplyMatrix(modelViewMatrix, vertexCoords); var clipCoordinates = MatrixMultiplier.MultiplyMatrix(camera.ProjectionMatrix, eyeCoordinates); //if (clipCoordinates[0, 0] < -1 || clipCoordinates[0, 0] > 1 || // clipCoordinates[1, 0] < -1 || clipCoordinates[1, 0] > 1) //{ // vertices.RemoveAt(i); // continue; //} var ndc = new float[, ] { { clipCoordinates[0, 0] / clipCoordinates[3, 0] }, { clipCoordinates[1, 0] / clipCoordinates[3, 0] }, { clipCoordinates[2, 0] / clipCoordinates[3, 0] }, { clipCoordinates[3, 0] } }; var windowCoordinates = new float[, ] { { 640 / 2 * ndc[0, 0] + (640 / 2) }, { 360 / 2 * ndc[1, 0] + (360 / 2) }, { (50 - (-50)) / 2 * ndc[2, 0] + (50 + (-50)) / 2 }, { ndc[3, 0] } }; SetNewCoordinatesToPoint(vertices[i], windowCoordinates); } //float[,] // , // projectionModelViewMatrix = GetProjectionModelViewMatrix(camera.ProjectionMatrix, modelViewMatrix); //GetClippedCoordinates(shape, projectionModelViewMatrix); //GetNormalizedCoordinates(shape); //GetWindowCoordinates(shape); //float[,] modelView = MatrixMultiplier.MultiplyMatrix(camera.ViewMatrix, shape.ModelMatrix); //float[,] projModelView = MatrixMultiplier.MultiplyMatrix(camera.ProjectionMatrix, modelView); //for (int i = 0; i < projModelView.GetLength(0); ++i) //{ // for (int j = 0; j < projModelView.GetLength(1); ++j) // { // if (i != 3 && j != 3) // projModelView[i, j] /= projModelView[3, 3]; // } //} //List<MPoint> vertices = shape.GetVertices(); //for (int i = 0; i < vertices.Count; ++i) //{ // float[,] vertexCoords = { { vertices[i].SX }, { vertices[i].SY }, { vertices[i].SZ }, { vertices[i].SW } }; // float[,] newCoords = MatrixMultiplier.MultiplyMatrix(projModelView, vertexCoords); // SetNewCoordinatesToPoint(vertices[i], newCoords); // TransformPointCoordsToDecart(vertices[i]); //} }
public void AddShape(MCommonPrimitive shape) { Shapes.Add(shape); }