public Form1() { InitializeComponent(); this.Width = width; this.Height = height; this.DoubleBuffered = true; cubePosition = POSITION.LEFT; movingVector = new Vector3D(cube.size * 2, 0, 0); timer1.Start(); }
public List<Vector3D> ViewProjectionTransform(Matrix m, List<Vector3D> vb) { List<Vector3D> result = new List<Vector3D>(); Matrix Tview = Matrix.ViewMatrix(r, theta, phi); Vector3D vp = new Vector3D(); foreach (Vector3D v in vb) { vp = m * v; vp = Tview * vp; vp = Matrix.ProjectionMatrix(d, vp.z) * vp; result.Add(vp); } return result; }
public static Vector3D[,] Viewporttransformation(int width, int height, Vector3D[,] vb, int vbSize) { Vector3D[,] result = new Vector3D[vbSize, vbSize]; float cx = (float)(width * 0.75) + (_3DWorld.worldSize); float cy = (float)(height * 0.75); for (int i = 0; i < vbSize; i++) { for (int j = 0; j < vbSize; j++) { var v = vb[i, j]; result[i, j] = new Vector3D(v.x + cx, v.y + cy, 0); } } return result; }
public _3DWorld(int width, int height) { this.width = width; this.height = height; this.lineLength = (width - 40) / worldSize; if (this.lineLength > 1) this.lineLength = 1; this.values = new Vector3D[worldSize, worldSize]; for (int i = 0; i < worldSize; i++) for (int j = 0; j < worldSize; j++) values[i, j] = new Vector3D(i, 0, j); //Set the middle height to 1; var midPoint = worldSize / 2; values[midPoint, midPoint] = new Vector3D(midPoint, 1, midPoint); CalculateHeightValues(); }
public void CalculateHeightValues(Vector3D topLeft, Vector3D bottomRight, int levelOfDetail, float yDelta) { if (levelOfDetail > levelOfDetails) return; //Diamond step //Get the square midpoint int midpointX = (int)(topLeft.x + bottomRight.x) / 2; int midpointZ = (int)(topLeft.z + bottomRight.z) / 2; float midpointAvgY = (topLeft.y + bottomRight.y + GetTopRight(topLeft, bottomRight).y + GetBottomLeft(topLeft, bottomRight).y) / 4; midpointAvgY += (float)rand.NextDouble() * yDelta; Vector3D midpoint = new Vector3D(midpointX, midpointAvgY, midpointZ); values[(int)midpointX, (int)midpointZ] = midpoint; //Update some values before proceeding to the next level of detail levelOfDetail++; yDelta = yDelta / 2; //Continue recursively //x|0 //0|0 CalculateHeightValues(topLeft, midpoint, levelOfDetail, yDelta); //0|0 //0|x CalculateHeightValues(midpoint, bottomRight, levelOfDetail, yDelta); //0|x //0|0 var middleTop = values[(int)midpoint.x, (int)topLeft.z]; var middleRight = values[(int)bottomRight.x, (int)midpoint.z]; CalculateHeightValues(middleTop, middleRight, levelOfDetail, yDelta); //0|0 //x|0 var middleLeft = values[(int)topLeft.x, (int)midpoint.z]; var middleBottom = values[(int)midpoint.x, (int)bottomRight.z]; CalculateHeightValues(middleLeft, middleBottom, levelOfDetail, yDelta); }
private Vector3D GetTopRight(Vector3D topLeft, Vector3D bottomRight) { return values[(int)bottomRight.x, (int)topLeft.z]; }
public void Draw(Graphics g, Vector3D[,] transValues) { for (int i = 0; i < worldSize; i++) { for (int j = 0; j < worldSize; j++) { //Current point var currentPoint = transValues[i, j]; if ((i + 1) < worldSize && (j + 1) < worldSize) { Point[] points = new Point[4]; points[0] = new Point((int)currentPoint.x, (int)currentPoint.y); points[1] = new Point((int)(transValues[i + 1, j].x * lineLength), (int)(transValues[i + 1, j].y * lineLength)); points[2] = new Point((int)(transValues[i + 1, j + 1].x * lineLength), (int)(transValues[i + 1, j + 1].y * lineLength)); points[3] = new Point((int)(transValues[i, j + 1].x * lineLength), (int)(transValues[i, j + 1].y * lineLength)); g.FillPolygon(Brushes.White, points); g.DrawPolygon(pen, points); } } } }
public static Matrix Translate(Vector3D t) { return new Matrix(1, 0, 0, t.x, 0, 1, 0, -t.y, 0, 0, 1, t.z, 0, 0, 0, 1); }
public static Matrix InvertTranslate(Vector3D t) { return Translate(t * -1f); }
public Vector3D[,] ViewProjectionTransform(Matrix m, Vector3D[,] vb, int vbSize) { Vector3D[,] result = new Vector3D[vbSize, vbSize]; Matrix Tview = Matrix.ViewMatrix(r, theta, phi); Vector3D vp = new Vector3D(); for (int i = 0; i < vbSize; i++) { for (int j = 0; j < vbSize; j++) { Vector3D v = vb[i, j]; vp = m * v; vp = Tview * vp; vp = Matrix.ProjectionMatrix(d, vp.z) * vp; result[i, j] = vp; } } return result; }