public void PrepareGrass(MTerrainTile tile) { MTerrainTile Tile = tile; Matrix4d TreeRotation = Matrix4d.CreateFromQuaternion(Quaterniond.FromEulerAngles(tile.transform.Up())); float numxinterps = LOD0Size / Tile.x_res; float numzinterps = LOD0Size / Tile.z_res; for (int x = 0; x < LOD0Size; x++) { for (int z = 0; z < LOD0Size; z++) { Vector3d PlantingPos = new Vector3d((x / numxinterps), 0, (z / numzinterps)); //PlantingPos = Tile.GetInterpolatedPointOnSurfaceFromGrid2(PlantingPos); //double y = tile.ElevationAtPoint(x / numxinterps, z / numzinterps); //PlantingPos = new Vector3d(x, y, z); PlantingPos = Tile.GetInterpolatedPointOnSurfaceFromGrid2(PlantingPos); Matrix4d TreeScale = Matrix4d.Scale(10, 10, 10); Matrix4d TreePosition = Matrix4d.CreateTranslation(PlantingPos); Matrix4 final = MTransform.GetFloatMatrix(TreeScale * TreeRotation * TreePosition); Grassmats[x, z] = final; } } IsComplete = true; Console.WriteLine("Grass Planted:" + Tile.TileX + "," + Tile.TileY); }
public static Vector3d RotateBy(this Vector3d direction, Quaterniond rotation) { var rot = Matrix4d.CreateFromQuaternion(rotation); Vector3d newDirection; Vector3d.Transform(ref direction, ref rot, out newDirection); return(newDirection); }
public void PlantPatch(Barycentric bary, int BladeCount = 4096, double Area = 1.1, double MaxHeight = 1) { double sq2 = Math.Sqrt(BladeCount); Matrix4d TreeRotation = Matrix4d.CreateFromQuaternion(Globals.LocalUpRotation()); Matrix4d TreePosition; Matrix4 final; Matrix4d TreeScale; Vector3d PlantingPos = new Vector3d(); int bposx = (int)(Tile.x_res * (1 - bary.u)); int bposz = (int)(Tile.z_res * (bary.v)); // Tile.Biome.SetPixel(bposx, bposz, new float[] { 0, 1, 0, 1 }); for (int x = (int)-sq2; x < sq2; x++) { for (int z = (int)-sq2; z < sq2; z++) { bposx = (int)(Tile.x_res * (1 - bary.u)) - x; bposz = (int)(Tile.z_res * (bary.v)) - z; int t = Tile._biome.GetBiomeAt(bposx, bposz); if ((t != MBiome.GRASS) && (t != MBiome.TREES) && (t != MBiome.TREES2) && (t != MBiome.WATER)) { continue; } PlantingPos.X = (int)(Tile.x_res * (1 - bary.u)) - x * 0.05 * Area; PlantingPos.Y = 0; PlantingPos.Z = (int)(Tile.z_res * (bary.v)) - z * 0.05 * Area; double yv = (MPerlin.Noise(PlantingPos.X, PlantingPos.Z) * 0.4) + (MPerlin.Noise(PlantingPos.X * 1.1, PlantingPos.Z * 1.1) * 0.4) ; //if (yv < 0.01) continue; TreeScale = Matrix4d.Scale(0.05 + 0.06 * yv, 0.01 + Math.Abs(yv) * 0.15 * MaxHeight, 0.05 + 0.06 * yv); PlantingPos.X += MPerlin.Noise(PlantingPos.X * 4.0, 0, PlantingPos.Z * 2.0) * 1.1; PlantingPos.Z += MPerlin.Noise(PlantingPos.X * 4.0, 0, PlantingPos.Z * 2.0) * 1.1; PlantingPos = Tile.GetInterpolatedPointOnSurfaceFromGrid2(PlantingPos); TreePosition = Matrix4d.CreateTranslation(PlantingPos); final = MTransform.GetFloatMatrix(TreeScale * TreeRotation * TreePosition); if (TotalInstances < Settings.MaxGrassPerTerrain) { mats[TotalInstances] = final; TotalInstances++; } } } }
protected override void OnRenderFrame(FrameEventArgs e) { GL.Viewport(0, 0, Width, Height); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); Integrator.Draw((Matrix4d.CreateFromQuaternion(CamRot) * Matrix4d.CreateTranslation(CamPos)).Inverted() * Matrix4d.CreatePerspectiveFieldOfView(1, (double)Width / (double)Height, NearZ, FarZ), Frustum); SwapBuffers(); }
public Matrix4d GetMatrix() { Matrix4d model = Matrix4d.Identity; model = Matrix4d.Scale(Scale) * Matrix4d.CreateFromQuaternion(Rotation * RotationOffset) * Matrix4d.CreateTranslation(Position); return(model); }
Vector3d GetQuantizedPoint(Vector3d p, Vector3d origin, double xrot, double yrot) { Vector3d delta = p - origin; Matrix4d rotx = Matrix4d.CreateFromQuaternion( Quaterniond.FromEulerAngles(xrot * Math.PI / 180.0, yrot * Math.PI / 180.0, 0) ); Matrix4d pos = Matrix4d.CreateTranslation(delta); Matrix4d result = pos * rotx; Vector3d vresult = result.ExtractTranslation() + origin; return(vresult); }
/// <summary> /// Render the entity as seen normally, in 3D. /// </summary> /// <param name="context">The render context.</param> public override void RenderStandard(RenderContext context) { if (DiffuseTexture != null) { GL.ActiveTexture(TextureUnit.Texture0); DiffuseTexture.Bind(); } context.Engine.Rendering.SetColor(Color4F.White, context.Engine.MainView); Matrix4d mat = Matrix4d.Scale(Scale.ToOpenTK3D()) * Matrix4d.CreateFromQuaternion(RenderOrientation.ToOpenTKDoubles()) * Matrix4d.CreateTranslation(RenderAt.ToOpenTK3D()); context.Engine.MainView.SetMatrix(ShaderLocations.Common.WORLD, mat); Rend.Render(context, true); }
public void PlaceInstances(Vector3d Anchorpoint) { //DistanceThreshold = tile.DistanceThreshold; this.transform.Position = Anchorpoint; Random ran = new Random(1234); Matrix4d TreeRotation = Matrix4d.CreateFromQuaternion(Globals.LocalUpRotation()); int i = 0; for (int z = 0; z < 30; z++) { for (int x = 0; x < 30; x++) { if (i >= MaxInstances) { break; } //Vector3d pos = new Vector3d(1000 * ran.NextDouble(), 200 * ran.NextDouble(), 1000 * ran.NextDouble()); Vector3d pos = MassiveTools.RandomPointInSphere(Vector3d.Zero, 1000, ran); //Vector3d PlantingPos = planet.GetNearestPointOnSphere(SeedPos + Treepos, 0) - SeedPos; double scale = MinSize + ran.NextDouble() * (MaxSize - MinSize); Matrix4d TreeScale = Matrix4d.Scale(scale, scale, scale); Matrix4d TreePosition = Matrix4d.CreateTranslation(pos); TreeRotation = Matrix4d.CreateFromQuaternion( Quaterniond.FromEulerAngles( ran.NextDouble(), ran.NextDouble(), ran.NextDouble())); //find point at y with raycast Matrix4 final = MTransform.GetFloatMatrix(TreeScale * TreeRotation * TreePosition); mats[i] = final; i++; } } TotalInstances = i; //null out the remainder for (int j = TotalInstances; j < MaxInstances; j++) { Matrix4 final = Matrix4.Identity; mats[j] = final; } Planted = false; }
public override void Update() { if (Widget.Enabled) { //ScreenPos = new Vector3d(Settings.Tweak1 * 0.01, Settings.Tweak2*0.01, Settings.Tweak3*0.01); Widget.transform.Position = ScreenPos; Vector3d AP = Globals.Avatar.GetPosition(); if (Target == AP) { Target = AP + new Vector3d(0, 0, 1); } Matrix4d Rot = Matrix4d.CreateFromQuaternion(Globals.Avatar.GetRotation()) * Matrix4d.LookAt(Target, AP - Globals.Avatar.Up() * 2, Globals.Avatar.Up()); Widget.transform.Rotation = Rot.Inverted().ExtractRotation(); } }
/// <summary> /// Render the entity as seen normally, in 3D. /// </summary> /// <param name="context">The render context.</param> public override void RenderStandard(RenderContext context) { foreach (EnableCap ec in DisabledCaps) { GL.Disable(ec); } if (DiffuseTexture != null) { GL.ActiveTexture(TextureUnit.Texture0); DiffuseTexture.Bind(); } context.Engine.Rendering.SetColor(Color, context.Engine.MainView); Matrix4d mat = Matrix4d.Scale(Scale.ToOpenTK3D()) * Matrix4d.CreateFromQuaternion(RenderOrientation.ToOpenTKDoubles()) * Matrix4d.CreateTranslation(RenderAt.ToOpenTK3D()); context.Engine.MainView.SetMatrix(ShaderLocations.Common.WORLD, mat); EntityModel.Draw(); foreach (EnableCap ec in DisabledCaps) { GL.Enable(ec); } }
public static Matrix4d MatrixFromYawPitchRoll(double yaw, double pitch, double roll) { return(Matrix4d.CreateFromQuaternion(QuaternionFromYawPitchRoll(yaw, pitch, roll))); }
public Matrix4d GetOrientationMatrix() { return(Matrix4d.CreateFromQuaternion(new Quaterniond(Angles.X, Angles.Y, Angles.Z, Angles.W))); }
public void PlantTrees(MAstroBody planet, MTerrainTile tile) { //DistanceThreshold = tile.DistanceThreshold; Tile = tile; if (tile.material == null) { return; } MTexture tex = Tile.Biome; if (tex == null) { return; } this.transform.Position = tile.transform.Position; Random ran = new Random(1234); Matrix4d TreeRotation = Matrix4d.CreateFromQuaternion(Globals.LocalUpRotation()); int i = 0; for (int z = 0; z < tile.z_res - 1; z++) { for (int x = 0; x < tile.x_res - 1; x++) { float[] c = tex.GetPixel(x, z); float r = c[0]; float g = c[1]; float b = c[2]; float a = c[3]; float t = 0; //if ((b > r) && (b > g)) t = g; if ((r < 0.05) && (g > 0.1) && (b < 0.05)) { t = g; } else { continue; } if (ran.NextDouble() > Settings.TreeDensity) { continue; } //if (g < 0.7) continue; //Console.WriteLine(c[0] + " " + c[1] + " " + c[2] + " " + c[3]); if (i >= Settings.MaxTreesPerTerrain) { break; } Vector3d Treepos = new Vector3d(x, 0, z); //Vector3d PlantingPos = planet.GetNearestPointOnSphere(Treepos, 0); Matrix4d TreeScale = Matrix4d.Scale(1 + ran.NextDouble(), 1 + ran.NextDouble() * 2, 1 + ran.NextDouble()); Vector3d PlantingPos = Tile.GetPointOnSurfaceFromGrid(Treepos); //; + new Vector3d(r.NextDouble()*5, r.NextDouble() * 5, r.NextDouble()*5); Matrix4d TreePosition = Matrix4d.CreateTranslation(PlantingPos); //find point at y with raycast Matrix4 final = MTransform.GetFloatMatrix(TreeScale * TreeRotation * TreePosition); mats[i] = final; i++; } } TotalInstances = i; for (int j = TotalInstances; j < Settings.MaxTreesPerTerrain; j++) { Matrix4 final = Matrix4.CreateTranslation(j, 0, 0); mats[j] = final; } //Setup(); //UploadBuffer(); Planted = false; }
protected override void OnUpdateFrame(FrameEventArgs e) { double displacement = 500; base.OnUpdateFrame(e); Planet.Update(CamPos); if (Focused) { var Keyboard = OpenTK.Input.Keyboard.GetState(); Matrix4d rot = Matrix4d.CreateFromQuaternion(CamRot); Vector3d left = rot.Row0.Xyz; Vector3d up = rot.Row1.Xyz; Vector3d front = rot.Row2.Xyz;; if (Keyboard[OpenTK.Input.Key.ShiftLeft]) { displacement *= 100; } if (Keyboard[OpenTK.Input.Key.W]) { CamPos -= front * displacement; } if (Keyboard[OpenTK.Input.Key.S]) { CamPos += front * displacement; } if (Keyboard[OpenTK.Input.Key.D]) { CamPos += left * displacement; } if (Keyboard[OpenTK.Input.Key.A]) { CamPos -= left * displacement; } if (Keyboard[OpenTK.Input.Key.Q]) { CamPos -= up * displacement; } if (Keyboard[OpenTK.Input.Key.E]) { CamPos += up * displacement; } Vector3d NewCamRot = new Vector3d(); if (Keyboard[OpenTK.Input.Key.I]) { NewCamRot.X -= 0.1; } if (Keyboard[OpenTK.Input.Key.K]) { NewCamRot.X += 0.1; } if (Keyboard[OpenTK.Input.Key.J]) { NewCamRot.Y -= 0.1; } if (Keyboard[OpenTK.Input.Key.L]) { NewCamRot.Y += 0.1; } if (Keyboard[OpenTK.Input.Key.U]) { NewCamRot.Z -= 0.1; } if (Keyboard[OpenTK.Input.Key.O]) { NewCamRot.Z += 0.1; } if (Keyboard[OpenTK.Input.Key.Space]) { CamPos = DefaultPos; CamRot = Quaterniond.Identity; } if (Keyboard[OpenTK.Input.Key.R]) { Shader.Recompile(); } if (Keyboard[OpenTK.Input.Key.X]) { if (IsLine) { GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); IsLine = false; } else { GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); IsLine = true; } } if (Keyboard[OpenTK.Input.Key.Z]) { Integrator.Reset(); Planet.Layers[0].RegeneratePlanet(); } if (Keyboard[OpenTK.Input.Key.Escape]) { Exit(); } Frustum.SetCamDef(CamPos, CamPos + front, up); CamRot *= Quaterniond.FromMatrix(Matrix3d.CreateRotationX(NewCamRot.X) * Matrix3d.CreateRotationY(NewCamRot.Y) * Matrix3d.CreateRotationZ(NewCamRot.Z)); } }
public void PlantGrass(MAstroBody planet, MTerrainTile tile) { // if (Planted == true) return; //DistanceThreshold = tile.DistanceThreshold; Tile = tile; if (tile.material == null) { return; } MTexture tex = Tile.Biome; if (tex == null) { return; } this.transform.Position = tile.transform.Position; //Random ran = new Random(1234); Matrix4d TreeRotation = Matrix4d.CreateFromQuaternion(Globals.LocalUpRotation()); //int i = 0; TotalInstances = 0; Vector3d AP = Globals.Avatar.GetPosition(); // Console.WriteLine(AP); Barycentric bary = new Barycentric(Tile.Boundary.TL, Tile.Boundary.BR, Tile.Boundary.TR, AP); PlantPatch(bary, 64 * 64, 2, 0.8); //PlantPatch(bary, 32 * 32, 8, 0.3); // Console.WriteLine(bary.ToString()); /* * double sq = 64 * 64; * double sq2 = Math.Sqrt(sq); * * Matrix4d TreePosition; * Matrix4 final; * Matrix4d TreeScale; * Vector3d PlantingPos = new Vector3d(); * for (int x = (int)-sq2; x < sq2; x++) * for (int z = (int)-sq2; z < sq2; z++) * { * PlantingPos.X = (int)(Tile.x_res * (1 - bary.u)) - x * 0.05; * PlantingPos.Y = 0; * PlantingPos.Z = (int)(Tile.z_res * (bary.v)) - z * 0.05; * * double yv = * (MPerlin.Noise(PlantingPos.X, PlantingPos.Z) * 0.4) + (MPerlin.Noise(PlantingPos.X * 1.1, PlantingPos.Z * 1.1) * 0.4) + ; + if (yv < 0.1) continue; + + TreeScale = Matrix4d.Scale(0.03+0.04 * yv, 0.01 + yv * 0.14 , 0.03+0.04* yv); + + PlantingPos.X += MPerlin.Noise(PlantingPos.X * 4.0, PlantingPos.Z * 4.1) * 1.1; + PlantingPos.Z += MPerlin.Noise(PlantingPos.X * 3.8, PlantingPos.Z * 4.2) * 1.1; + PlantingPos = Tile.GetInterpolatedPointOnSurfaceFromGrid2(PlantingPos); + + TreePosition = Matrix4d.CreateTranslation(PlantingPos); + final = MTransform.GetFloatMatrix(TreeScale * TreeRotation * TreePosition); + if (i < Settings.MaxGrassPerTerrain) + { + mats[i] = final; + i++; + } + } + + TotalInstances = i; */ for (int j = TotalInstances; j < Settings.MaxGrassPerTerrain; j++) { Matrix4 final = Matrix4.CreateTranslation(9999999999, 0, 0); mats[j] = final; } //Setup(); //UploadBuffer(); Planted = true; }
/// <summary> /// Called to allow position changes to sync /// </summary> public void OnUpdate() { Transform = Matrix4d.CreateFromQuaternion(Parent.Transform.Rotation) * Matrix4d.CreateTranslation(Parent.Transform.Position); }