Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using OpenTK;
- using OpenTK.Graphics;
- using OpenTK.Graphics.OpenGL;
- using OpenTK.Input;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Windows.Forms;
- using Filler;
- using System.Linq;
- namespace vertexcloud
- {
- public class vertexcloud : GameWindow
- {
- private Matrix4 CameraMatrix = Matrix4.Identity;
- private Matrix4 ViewMatrix = Matrix4.Identity;
- private Vector3 Position = new Vector3(0, 0, 0);
- private float Pitch = -0.8f;
- private List<PointCloud> pointclouds = new List<PointCloud>();
- private const float CameraSpeed = 615f;
- private const float MoveSpeed = 550f*4;
- private const float HalfPI = 1.570796f;
- private Vector2 MouseSpeed;
- private Vector2 MouseDelta;
- private Vector2 MousePosition;
- private static Vector2 CursorPosition;
- private float Yaw;
- private Point WindowCenter;
- private bool RenderCrossHair = false;
- public vertexcloud() : base(1024, 768, GraphicsMode.Default, "Camera")
- {
- GL.Enable(EnableCap.DepthTest);
- }
- public Vector3 convertScreenToWorldCoords(int x, int y)
- {
- int[] viewport = new int[4];
- Matrix4 modelViewMatrix, projectionMatrix;
- GL.GetFloat(GetPName.ModelviewMatrix, out modelViewMatrix);
- GL.GetFloat(GetPName.ProjectionMatrix, out projectionMatrix);
- GL.GetInteger(GetPName.Viewport, viewport);
- Vector2 mouse;
- mouse.X = x;
- mouse.Y = y + (viewport[2] - viewport[3]);
- Vector4 vector = UnProject(ref projectionMatrix, CameraMatrix, new Size(viewport[2], viewport[3]), mouse);
- return vector.Xyz;
- }
- public static Vector4 UnProject(ref Matrix4 projection, Matrix4 view, Size viewport, Vector2 mouse)
- {
- Vector4 vec;
- vec.X = 2.0f * mouse.X / (float)viewport.Width - 1;
- vec.Y = -(2.0f * mouse.Y / (float)viewport.Height - 1);
- vec.Z = 0;
- vec.W = 1.0f;
- Matrix4 viewInv = Matrix4.Invert(view);
- Matrix4 projInv = Matrix4.Invert(projection);
- Vector4.Transform(ref vec, ref projInv, out vec);
- Vector4.Transform(ref vec, ref viewInv, out vec);
- if (vec.W > float.Epsilon || vec.W < float.Epsilon)
- {
- vec.X /= vec.W;
- vec.Y /= vec.W;
- vec.Z /= vec.W;
- }
- return vec;
- }
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- GL.ClearColor(0.1f, 0.1f, 0.1f, 1f);
- this.WindowCenter = new Point(this.Bounds.Left + this.Bounds.Width / 2, this.Bounds.Top + this.Bounds.Height / 2);
- Cursor.Position = this.WindowCenter;
- Cursor.Hide();
- string[] files = Directory.GetFiles(@"C:\WindowsLP\SAMPLE_PROJECT\brisport2", "*.las");
- int num = 0;
- foreach (string file in files)
- {
- PointCloud pointCloud = new PointCloud(LasImporter.ReadDataFromFile(file));
- pointCloud.ApplyColorMap(LasReader.Colors);
- this.pointclouds.Add(pointCloud);
- ++num;
- if (num >= 1)
- break;
- }
- float[] pts = new float[180 * 180*3];
- float m = 200f;
- float n = 200f;
- for (int i = 0; i < 200; i++)
- {
- for (int j = 0; j < 200; j++)
- {
- // xyz (sin(Pi * m/M) cos(2Pi * n/N), sin(Pi * m/M) sin(2Pi * n/N), cos(Pi * m/M))
- // YZX
- pts[(j * 100 + i) * 3] = (float)( Math.Sin(MathHelper.Pi * m/i) * Math.Sin(2 * Math.PI * n/j))*200f;
- pts[(j * 100 + i) * 3 + 1] = (float)Math.Cos(Math.PI * m / i) * 200f;
- pts[(j * 100 + i) * 3 + 2] = (float)(Math.Sin(Math.PI * m / i) * Math.Cos(2 * Math.PI * n / j)) * 200f;
- }
- }
- pointclouds.Add(new PointCloud(pts) { HasColor = false });
- this.Mouse.Move += new EventHandler<MouseMoveEventArgs>(this.OnMouseMove);
- this.Position = new Vector3(LasReader.cameraX, LasReader.cameraY, LasReader.cameraZ);
- }
- private void OnMouseMove(object sender, MouseMoveEventArgs e)
- {
- if (this.Mouse[MouseButton.Right])
- this.MousePosition = new Vector2((float)e.X, (float)e.Y);
- CursorPosition = new Vector2((float)e.X, (float)e.Y);
- }
- public static Vector4 UnProject(Matrix4 projection, Matrix4 view, Size viewport, Vector2 mouse)
- {
- Vector4 vec;
- vec.X = 2.0f * mouse.X / (float)viewport.Width - 1;
- vec.Y = -(2.0f * mouse.Y / (float)viewport.Height - 1);
- vec.Z = 0;
- vec.W = 1.0f;
- Matrix4 viewInv = Matrix4.Invert(view);
- Matrix4 projInv = Matrix4.Invert(projection);
- Vector4.Transform(ref vec, ref projInv, out vec);
- Vector4.Transform(ref vec, ref viewInv, out vec);
- if (vec.W > float.Epsilon || vec.W < float.Epsilon)
- {
- vec.X /= vec.W;
- vec.Y /= vec.W;
- vec.Z /= vec.W;
- }
- return vec;
- }
- protected override void OnResize(EventArgs e)
- {
- base.OnResize(e);
- this.WindowCenter = new Point(this.Bounds.Left + this.Bounds.Width / 2, this.Bounds.Top + this.Bounds.Height / 2);
- GL.Viewport(this.ClientRectangle.X, this.ClientRectangle.Y, this.ClientRectangle.Width, this.ClientRectangle.Height);
- ViewMatrix = Matrix4.CreatePerspectiveFieldOfView(0.7853982f, (float)this.Width / (float)this.Height, 1f, 100000f);
- GL.MatrixMode(MatrixMode.Projection);
- GL.LoadMatrix(ref ViewMatrix);
- }
- private List<Vector3> points = new List<Vector3>();
- protected override void OnUpdateFrame(FrameEventArgs e)
- {
- base.OnUpdateFrame(e);
- if (this.Keyboard[Key.Up])
- {
- this.Position.X += (float)(Math.Cos((double)this.Yaw) * MoveSpeed) * (float)e.Time;
- this.Position.Y += (float)(Math.Tan((double)this.Pitch) * MoveSpeed) * (float)e.Time;
- this.Position.Z += (float)(Math.Sin((double)this.Yaw) * MoveSpeed) * (float)e.Time;
- }
- if (this.Keyboard[Key.Down])
- {
- this.Position.X -= (float)(Math.Cos((double)this.Yaw) * MoveSpeed) * (float)e.Time;
- this.Position.Y -= (float)(Math.Tan((double)this.Pitch) * MoveSpeed) * (float)e.Time;
- this.Position.Z -= (float)(Math.Sin((double)this.Yaw) * MoveSpeed) * (float)e.Time;
- }
- if (this.Keyboard[Key.Left])
- {
- this.Position.X -= (float)(Math.Cos((double)this.Yaw + Math.PI / 2.0) * MoveSpeed) * (float)e.Time;
- this.Position.Z -= (float)(Math.Sin((double)this.Yaw + Math.PI / 2.0) * MoveSpeed) * (float)e.Time;
- }
- if (this.Keyboard[Key.Right])
- {
- this.Position.X += (float)(Math.Cos((double)this.Yaw + Math.PI / 2.0) * MoveSpeed) * (float)e.Time;
- this.Position.Z += (float)(Math.Sin((double)this.Yaw + Math.PI / 2.0) * MoveSpeed) * (float)e.Time;
- }
- if (this.Keyboard[Key.P])
- {
- pointclouds.ForEach(i => i.Visible = false);
- }
- else
- {
- pointclouds.ForEach(i => i.Visible = true);
- }
- if (this.Keyboard[Key.D])
- {
- pointclouds.ForEach(i => i.Delete());
- pointclouds.Clear();
- }
- if (Mouse[MouseButton.Left])
- {
- points.Clear();
- //var x = UnProject(ViewMatrix, CameraMatrix, this.Size, new Vector2(Mouse.X, Mouse.Y));
- var x = convertScreenToWorldCoords(Mouse.X, Mouse.Y);
- points.Add(new Vector3(x));
- Title = x.ToString();
- }
- if (this.Mouse[MouseButton.Right])
- {
- RenderCrossHair = false;
- this.MouseDelta = new Vector2(this.MousePosition.X - (float)this.PointToClient(this.WindowCenter).X, this.MousePosition.Y - (float)this.PointToClient(this.WindowCenter).Y);
- Cursor.Position = this.WindowCenter;
- this.MouseSpeed.X *= 0.7f;
- this.MouseSpeed.Y *= 0.7f;
- this.MouseSpeed.X += this.MouseDelta.X / 40f * (float)e.Time;
- this.MouseSpeed.Y += this.MouseDelta.Y / 40f * (float)e.Time;
- this.Yaw += this.MouseSpeed.X;
- this.Pitch -= this.MouseSpeed.Y;
- if ((double)this.Pitch <= -1.56079638004303)
- this.Pitch = -1.560796f;
- if ((double)this.Pitch >= 1.56079638004303)
- this.Pitch = 1.560796f;
- this.CameraMatrix = Matrix4.LookAt(this.Position, this.Position + new Vector3((float)Math.Cos((double)this.Yaw), (float)Math.Tan((double)this.Pitch), (float)Math.Sin((double)this.Yaw)), Vector3.UnitY);
- }
- else
- {
- RenderCrossHair = true;
- }
- if (this.Keyboard[Key.Escape])
- this.Exit();
- }
- protected override void OnRenderFrame(FrameEventArgs e)
- {
- base.OnRenderFrame(e);
- GL.MatrixMode(MatrixMode.Modelview);
- GL.Clear(ClearBufferMask.DepthBufferBit | ClearBufferMask.ColorBufferBit);
- GL.LoadMatrix(ref this.CameraMatrix);
- GL.PushMatrix();
- this.pointclouds.ForEach((Action<PointCloud>)(i =>
- {
- //blend
- GL.Enable(EnableCap.Blend);
- if (i.Visible)
- {
- GL.PushMatrix();
- GL.PointSize(2.0f);
- if (!i.HasColor)
- {
- GL.Color4(Color.White);
- }
- i.Render(e);
- GL.PopMatrix();
- }
- }));
- GL.PopMatrix();
- //line
- //GL.PushMatrix();
- //GL.LineWidth(100.0f);
- //GL.Color4(Color.White);
- //GL.Begin(BeginMode.Lines);
- //GL.Vertex3(0, 0, 0);
- //GL.Vertex3(5000, 5000, 5000);
- //GL.End();
- //GL.PopMatrix();
- GL.PushMatrix();
- GL.Enable(EnableCap.Blend);
- GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.SrcAlpha);
- GL.MatrixMode(MatrixMode.Projection);
- GL.PushMatrix();
- GL.LoadIdentity();
- GL.Ortho(0, Width, Height, 0, 0.0f, 100.0f);
- if (RenderCrossHair)
- RenderCursor();
- GL.PopMatrix();
- this.SwapBuffers();
- }
- public static void RenderCursor()
- {
- GL.PointSize(3.0f);
- GL.Color4(Color.White);
- GL.Begin(BeginMode.Points);
- GL.Vertex2(CursorPosition.X, CursorPosition.Y);
- GL.Vertex2(CursorPosition.X + 10, CursorPosition.Y);
- GL.Vertex2(CursorPosition.X + 10, CursorPosition.Y + 10);
- GL.Vertex2(CursorPosition.X, CursorPosition.Y + 10);
- GL.End();
- }
- public static void Main()
- {
- using (vertexcloud vertexcloud = new vertexcloud())
- vertexcloud.Run(60.0, 0.0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement