/// <summary> /// Sample the Perlin Noise Map at a coordinate (x,y,z). /// </summary> /// <param name="x">Coordinate from which to sample the Perlin Noise Map.</param> /// <param name="y">Coordinate from which to sample the Perlin Noise Map.</param> /// <param name="z">Coordinate from which to sample the Perlin Noise Map.</param> /// <returns>The Perlin Noise Map value at (x,y,z).</returns> public static double GetNoise(double x, double y, double z) { int xindex = (int)Math.Floor(x); int yindex = (int)Math.Floor(y); int zindex = (int)Math.Floor(z); // Logival bitwise AND caps index values at 255. xindex = xindex & 255; yindex = yindex & 255; zindex = zindex & 255; double dx = x - (int)x; double dy = y - (int)y; double dz = z - (int)z; double u = PerlinFade(dx); double v = PerlinFade(dy); double w = PerlinFade(dz); int m000 = p[p[p[xindex] + yindex] + zindex]; int m010 = p[p[p[xindex] + yindex + 1] + zindex]; int m100 = p[p[p[xindex + 1] + yindex] + zindex]; int m110 = p[p[p[xindex + 1] + yindex + 1] + zindex]; int m001 = p[p[p[xindex] + yindex] + zindex + 1]; int m011 = p[p[p[xindex] + yindex + 1] + zindex + 1]; int m101 = p[p[p[xindex + 1] + yindex] + zindex + 1]; int m111 = p[p[p[xindex + 1] + yindex + 1] + zindex + 1]; double x1 = JMathUtils.Lerp(Grad(m000, dx, dy, dz), Grad(m100, dx - 1, dy, dz), u ); double x2 = JMathUtils.Lerp(Grad(m010, dx, dy - 1, dz), Grad(m110, dx - 1, dy - 1, dz), u ); double y1 = JMathUtils.Lerp(x1, x2, v ); x1 = JMathUtils.Lerp(Grad(m001, dx, dy, dz - 1), Grad(m101, dx - 1, dy, dz - 1), u ); x2 = JMathUtils.Lerp(Grad(m011, dx, dy - 1, dz - 1), Grad(m111, dx - 1, dy - 1, dz - 1), u ); double y2 = JMathUtils.Lerp(x1, x2, v ); return((JMathUtils.Lerp(y1, y2, w) + 1) / 2); }
/// <summary> /// Create a JMousePicker instance. JMousePicker will project a 2D screen coordinate into a 3D unit vector within the world. /// It can determine intersection with terrain as well as check for intersection with a JBoundingSphere. /// </summary> /// <param name="camera">The JCamera entity.</param> /// <param name="projectionMatrix">The JMasterRenderers Projection Matrix based off of the JCameras properties.</param> /// <param name="terrain">The JTerrain the Mouse Ray will intersect with.</param> /// <param name="gameWindow">The active JGameWindow. We need this to get it's height and width.</param> public JMousePicker(JCamera camera, Matrix4 projectionMatrix, JPerlinTerrain terrain, JGameWindow gameWindow) { Camera = camera; ProjectionMatrix = projectionMatrix; ViewMatrix = JMathUtils.createViewMatrix(camera); Terrain = terrain; GameWindow = gameWindow; }
/// <summary> /// Update CurrentRay each frame from the current 2D mouse coordinate. Also updates CurrentTerrainPoint reflecting CurrentRays intersection with /// Terrain. /// </summary> public void Update() { ViewMatrix = JMathUtils.createViewMatrix(Camera); CurrentRay = CalculateMouseRay(); if (IntersectionInRange(0, JConfig.MOUSE_PICKER_RAY_RANGE, CurrentRay)) { CurrentTerrainPoint = BinarySearch(0, 0, JConfig.MOUSE_PICKER_RAY_RANGE, CurrentRay); } else { CurrentTerrainPoint = new Vector3(0, 0, 0); } }