private void DrawStereoImages() { Vector3d vectorLeftEye = this.eye - this.distEye * (double)this.StereoDistance * this.esq; GL.DrawBuffer(DrawBufferMode.BackRight); GL.ClearColor(this.ClearColor[0], this.ClearColor[1], this.ClearColor[2], 0.0f); GL.Clear(ClearBufferMask.DepthBufferBit | ClearBufferMask.ColorBufferBit); GL.LoadIdentity(); Matrix4d perspectiveFieldOfView1 = Matrix4d.CreatePerspectiveFieldOfView(Math.PI / 4.0, (double)this.glControl1.Width / (double)this.glControl1.Height, (double)this.zFar * 0.001, (double)this.zFar + 1000.0); GL.LoadMatrix(ref perspectiveFieldOfView1); Matrix4d mat1 = Matrix4d.LookAt(vectorLeftEye.X, vectorLeftEye.Y, vectorLeftEye.Z, this.center.X, this.center.Y, this.center.Z, this.up.X, this.up.Y, this.up.Z); GL.MultMatrix(ref mat1); this.DrawAll3DObjects(); Vector3d vectorRightEye = this.eye + this.distEye * (double)this.StereoDistance * this.esq; GL.DrawBuffer(DrawBufferMode.BackLeft); GL.ClearColor(this.ClearColor[0], this.ClearColor[1], this.ClearColor[2], 0.0f); GL.Clear(ClearBufferMask.DepthBufferBit | ClearBufferMask.ColorBufferBit); GL.LoadIdentity(); Matrix4d perspectiveFieldOfView2 = Matrix4d.CreatePerspectiveFieldOfView(Math.PI / 4.0, (double)this.glControl1.Width / (double)this.glControl1.Height, (double)this.zFar * 0.001, (double)this.zFar + 1000.0); GL.LoadMatrix(ref perspectiveFieldOfView2); Matrix4d mat2 = Matrix4d.LookAt(vectorRightEye.X, vectorRightEye.Y, vectorRightEye.Z, this.center.X, this.center.Y, this.center.Z, this.up.X, this.up.Y, this.up.Z); GL.MultMatrix(ref mat2); this.DrawAll3DObjects(); this.glControl1.SwapBuffers(); }
public bool UpdateMatrices() { if (hasChanged == false) { return(false); } hasChanged = false; ViewMatrix = Matrix4d.LookAt(Position, LookAt, Vector3d.UnitY); switch (ProjectionType) { case ProjectionType.Perspective: ProjectionMatrix = Matrix4d.CreatePerspectiveFieldOfView(FovRads, Width / (float)Height, NearZ, FarZ); break; case ProjectionType.Ortographic: var halfWidth = Width / 2f; var halfHeight = Height / 2f; ProjectionMatrix = Matrix4d.CreateOrthographicOffCenter(-halfWidth, halfWidth, -halfHeight, halfHeight, NearZ, FarZ); break; default: throw new ArgumentOutOfRangeException(); } return(true); }
public static Matrix4d GetProjectionMatrix(VideoCamera videoCamera, UIImage frame) { // Aspect is inverted because we rotate the image return(Matrix4d.CreatePerspectiveFieldOfView( fovy: videoCamera.FieldOfView *Math.PI / 180, aspect: frame.Size.Height / frame.Size.Width, zNear: 0.01, zFar: 4700)); }
protected override void OnRenderFrame(FrameEventArgs e) { double fpsstability = 10.0; this._AverageFPS = (this.RenderFrequency + this._AverageFPS * fpsstability) / (fpsstability + 1); this.Title = DefaultTitle + " - Press H for help!"; // fps messurement is off GL.ClearColor(0.3f, 0.5f, 1.0f, 1.0f); GL.Clear(ClearBufferMask.DepthBufferBit | ClearBufferMask.ColorBufferBit); GL.MatrixMode(MatrixMode.Projection); Matrix4d proj = Matrix4d.CreatePerspectiveFieldOfView(0.9, (double)this.Width / (double)this.Height, 0.2, 600.0); GL.LoadMatrix(ref proj); Matrix4d view = Matrix4d.LookAt( Vector.FromDoubleVector(this._CamPos), Vector.FromDoubleVector(Vector.Add(this._CamPos, this.CamDirection)), new Vector3d(0.0, 0.0, 1.0)); GL.MultMatrix(ref view); // Test picking int mx = this.Mouse.X; int my = this.Mouse.Y; int sx = this.ClientSize.Width; int sy = this.ClientSize.Height; Vector3d p = Unproject(new Vector3d(-1.0 + 2.0 * ((double)mx / (double)sx), 1.0 - 2.0 * ((double)my / (double)sy), 0.995), view, proj); Vector <double> tracestart = this._CamPos; Vector <double> traceend = new Vector <double>(p.X, p.Y, p.Z); Vector <double> dif = Vector.Normalize(Vector.Subtract(traceend, tracestart)); traceend = Vector.Add(Vector.Multiply(dif, 400), tracestart); foreach (TraceHit <MinecraftRenderer.HitBorder> hb in Trace.TraceRay(this._Renderer.GetHitSurface(this.Slice), tracestart, traceend, MinecraftRenderer.HitBorder.Default)) { if ((hb.Border.Value.Polarity == Polarity.Positive && tracestart[hb.Border.Direction] > hb.Border.Position[hb.Border.Direction]) || (hb.Border.Value.Polarity == Polarity.Negative && tracestart[hb.Border.Direction] < hb.Border.Position[hb.Border.Direction])) { this._LastHit = hb.Border; break; } } Point <int>?highlight = this._LastHit.HasValue ? this._LastHit.Value.Value.Chunk : null; if (this._SliceAxis.HasValue) { if (this._SliceAxis.Value == Axis.Z) { this._SliceLevel = this._SliceLevel < 1 ? 1 : this._SliceLevel; this._SliceLevel = this._SliceLevel > MinecraftRenderer.ChunkSize - 1 ? MinecraftRenderer.ChunkSize - 1 : this._SliceLevel; } } this._Renderer.Render(this._CamPos, this.Slice, highlight); this._RendererTrans.Render(this._CamPos, this.Slice, highlight); this.SwapBuffers(); }
// VideoCamera videoCamera (videoCamera.FieldOfView), UIImage frame.Size.Height, frame.Size.Height public static Matrix4d GetProjectionMatrix(double aspect, float zNear, float zFar) { var fov = 60; // Aspect is height/widht (in portrait mode only?) return(Matrix4d.CreatePerspectiveFieldOfView( fovy: fov *Math.PI / 180, aspect: aspect, zNear: zNear, zFar: zFar)); }
/// <summary> /// Convert a world space coordinate into a screen space coordinate. /// </summary> /// <param name="world">The world coordinate</param> /// <returns>The screen coordinate</returns> public Coordinate WorldToScreen(Coordinate world) { var viewport = new[] { 0, 0, Width, Height }; var pm = Matrix4d.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(Camera.FOV), Width / (float)Height, 0.1f, 50000); var vm = Matrix4d.LookAt( new Vector3d(Camera.Location.X, Camera.Location.Y, Camera.Location.Z), new Vector3d(Camera.LookAt.X, Camera.LookAt.Y, Camera.LookAt.Z), Vector3d.UnitZ); return(MathFunctions.Project(world, viewport, pm, vm)); }
// update viewport internal static void UpdateViewport() { GL.Viewport(0, 0, Renderer.ScreenWidth, Renderer.ScreenHeight); World.AspectRatio = (double)Renderer.ScreenWidth / (double)Renderer.ScreenHeight; World.HorizontalViewingAngle = 2.0 * Math.Atan(Math.Tan(0.5 * World.VerticalViewingAngle) * World.AspectRatio); GL.MatrixMode(MatrixMode.Projection); Matrix4d perspective = Matrix4d.CreatePerspectiveFieldOfView(World.VerticalViewingAngle, World.AspectRatio, 0.2, 1000.0); GL.LoadMatrix(ref perspective); GL.Scale(-1, 1, 1); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); }
// update viewport internal static void UpdateViewport() { GL.Viewport(0, 0, Screen.Width, Screen.Height); Screen.AspectRatio = (double)Screen.Width / (double)Screen.Height; Camera.HorizontalViewingAngle = 2.0 * Math.Atan(Math.Tan(0.5 * Camera.VerticalViewingAngle) * Screen.AspectRatio); GL.MatrixMode(MatrixMode.Projection); Matrix4d perspective = Matrix4d.CreatePerspectiveFieldOfView(Camera.VerticalViewingAngle, Screen.AspectRatio, 0.2, 1000.0); GL.LoadMatrix(ref perspective); GL.Scale(-1, 1, 1); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); }
public override void CalcProjectionMatrix() { double aspect = mViewWidth / mViewHeight; mProjectionMatrix = Matrix4d.CreatePerspectiveFieldOfView( mFovY, aspect, mProjectionNear, mProjectionFar ); mProjectionMatrixInv = mProjectionMatrix.Invert(); }
// This is the main function for determining what will be rendered private void glControl1_Paint(object sender, PaintEventArgs e) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); //Basic Setup for viewing //Setup Perspective (lookat) //the first argument (Set of 3) is the location of your camera //the second argument (Set of 3) is the point the camera centers on //the third argument (Set of 3) is to indicate what direction should be considered "up" lookat = Matrix4.LookAt(0, 0, -1500 + zoomFactor, 0, 0, 0, 0, 1, 0); perspective = Matrix4d.CreatePerspectiveFieldOfView(1.04f, 16 / 9f, 1f, 100000f); //Setup camera GL.MatrixMode(MatrixMode.Projection); //Load Perspective GL.LoadIdentity(); GL.LoadMatrix(ref perspective); //Load POV/Camera GL.MatrixMode(MatrixMode.Modelview); GL.LoadMatrix(ref lookat); //Apply transformations and rotations based on UI controls (Mouse) GL.Rotate(angleY, 1.0f, 0, 0); GL.Rotate(angleX, 0, 1.0f, 0); //GL.Translate(-panX1, 0, 0); //GL.Translate(0, panY1, 0); GL.Viewport((int)panX1, (int)panY1, glControl1.Width, glControl1.Height); // Use all of the glControl painting area //Size of window GL.Enable(EnableCap.DepthTest); //Enable correct Z Drawings GL.DepthFunc(DepthFunction.Less); GL.PushMatrix(); // Objects to be displayed go here. // Y is up and down, Z is towards you, X is left and right GL.Begin(PrimitiveType.Points); paintVertices(); GL.End(); GL.Begin(PrimitiveType.Lines); paintAxis(); GL.End(); GL.PopMatrix(); glControl1.SwapBuffers(); }
/// <summary> /// Project the 2D coordinates from the screen coordinates outwards /// from the camera along the lookat vector, taking the frustrum /// into account. The resulting line will be run from the camera /// position along the view axis and end at the back clipping pane. /// </summary> /// <param name="x">The X coordinate on screen</param> /// <param name="y">The Y coordinate on screen</param> /// <returns>A line beginning at the camera location and tracing /// along the 3D projection for at least 1,000,000 units.</returns> public Line CastRayFromScreen(int x, int y) { var near = new Coordinate(x, Height - y, 0); var far = new Coordinate(x, Height - y, 1); var pm = Matrix4d.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(Camera.FOV), Width / (float)Height, 0.1f, 50000); var vm = Matrix4d.LookAt( new Vector3d(Camera.Location.X, Camera.Location.Y, Camera.Location.Z), new Vector3d(Camera.LookAt.X, Camera.LookAt.Y, Camera.LookAt.Z), Vector3d.UnitZ); var viewport = new[] { 0, 0, Width, Height }; var un = MathFunctions.Unproject(near, viewport, pm, vm); var uf = MathFunctions.Unproject(far, viewport, pm, vm); return((un == null || uf == null) ? null : new Line(un, uf)); }
/// <summary> /// Initialize a viewport and set up a perspective scene /// </summary> private void setupViewport() { GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); Matrix4d cache = Matrix4d.CreatePerspectiveFieldOfView( Math.PI / 4, GLControl.Width / (double)(GLControl.Height == 0 ? 1 : GLControl.Height), 1.0, 100.0); GL.LoadMatrix(ref cache); GL.Viewport(new Rectangle(0, 0, GLControl.Width, GLControl.Height)); GL.MatrixMode(MatrixMode.Modelview); Matrix4d cameraCache = camera.cameraMatrix; GL.LoadMatrix(ref cameraCache); }
/// <summary> /// Generates a 4x4 double Matrix representing the camera's View (with Perspective added) /// </summary> /// <param name="Pos">The camera's Position</param> /// <param name="Up">The camera's Up vector</param> /// <param name="Target">The camera's Target vector</param> /// <param name="cameraType">The camera's Type</param> /// <param name="zFar">The camera's ZFar</param> /// <param name="zNear">The camera's zNear</param> /// <param name="cameraWidth">The camera's width</param> /// <param name="cameraHeight">The camera's height</param> /// <param name="cameraAspect">The camera's aspect ratio</param> /// <param name="cameraFOV">The camera's field of view</param> /// <returns>a 4x4 double Matrix</returns> private Matrix4d generateMatrix(Vector3d Pos, Vector3d Up, Vector3d Target, ComponentConstants.CAM_TYPES cameraType, double zNear, double zFar, double cameraWidth = 0, double cameraHeight = 0, double cameraAspect = 0, double cameraFOV = 0) { Matrix4d ProjectionMatrix; if (cameraType == ComponentConstants.CAM_TYPES.ORTHOGRAPHIC) { ProjectionMatrix = Matrix4d.CreateOrthographic(cameraWidth, cameraHeight, zNear, zFar); } else { ProjectionMatrix = Matrix4d.CreatePerspectiveFieldOfView(cameraFOV, cameraAspect, zNear, zFar); } Matrix4d ViewMatrix = Matrix4d.LookAt(Pos, Target, Up); Matrix4d ResultMatrix = ProjectionMatrix * ViewMatrix; return(ResultMatrix); }
private void Draw3D() { GL.DrawBuffer(DrawBufferMode.Back); GL.ClearColor(this.ClearColor[0], this.ClearColor[1], this.ClearColor[2], 0.0f); GL.Clear(ClearBufferMask.DepthBufferBit | ClearBufferMask.ColorBufferBit); GL.LoadIdentity(); Matrix4d perspectiveFieldOfView = Matrix4d.CreatePerspectiveFieldOfView(Math.PI / 4.0, (double)this.glControl1.Width / (double)this.glControl1.Height, (double)this.zFar * 0.001, (double)this.zFar + 1000.0); GL.LoadMatrix(ref perspectiveFieldOfView); Matrix4d mat = Matrix4d.LookAt(this.eye.X, this.eye.Y, this.eye.Z, this.center.X, this.center.Y, this.center.Z, this.up.X, this.up.Y, this.up.Z); GL.MultMatrix(ref mat); this.DrawAll3DObjects(); GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Shininess, new float[4] { 0.9f, 0.9f, 0.9f, 1f }); this.glControl1.SwapBuffers(); }
private void Render(object sender, PaintEventArgs e) { if (!loaded) { return; } MakeCurrent(); SetupViewport(); //double size = Scene.Size(); double size = Scene.Max.Y - Scene.Min.Y; double SceneYOffset = -size / 2f; double defaultzoom = (double)Math.Sqrt(Math.Pow(TableRadius, 2) + Math.Pow(TableHeight, 2)); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.LoadIdentity(); var aspect_ratio = Width / (double)Height; var projection = Matrix4d.CreatePerspectiveFieldOfView( MathHelper.PiOver4, double.Parse(aspect_ratio.ToString()), 1f, 1024); // var modelview = Matrix4d.LookAt(0, size / 2 , - DragZoom * defaultzoom * 2f, // 0,TableHeight/2, 0, 0, size, 0); var viewHeight = (TableHeight / 2.50) + SceneYOffset; var modelview = Matrix4d.LookAt(0, viewHeight, Math.Max(0.00000001f, -(-Drag.Zoom * defaultzoom * 1.5f)), 0, viewHeight, 0, 0, size, 0); GL.MatrixMode(MatrixMode.Projection); if (Projection) { GL.LoadMatrix(ref projection); } else { GL.LoadIdentity(); double x = Math.Max(TableRadius * 2, TableHeight) * Drag.Zoom / 2.0; double y = x * ((double)Height / (double)Width); GL.Ortho(-x, x, -y, y, 0.0, 10000.0); } GL.MatrixMode(MatrixMode.Modelview); GL.LoadMatrix(ref modelview); #region LIGHT //if (Lightning) { float[] lightPos = new float[] { (float)(-TableRadius * 2f), (float)TableRadius * 2f, (float)TableHeight * 2f, 1.0f }; GL.PointSize(5); GL.Disable(EnableCap.Lighting); GL.Begin(PrimitiveType.Points); GL.Color3(Color.Yellow); GL.Vertex3(lightPos); GL.End(); GL.PointSize(1); float[] light_ambient = { 0.2f, 0.2f, 0.2f, 1.0f }; float[] light_diffuse = { 1.0f, 1.0f, 1.0f, 1.0f }; float[] light_specular = { 1.0f, 1.0f, 1.0f, 1.0f }; float[] spotdirection = { 0.5f, -1.0f, -1.0f }; GL.Light(LightName.Light0, LightParameter.Ambient, light_ambient); GL.Light(LightName.Light0, LightParameter.Diffuse, light_diffuse); GL.Light(LightName.Light0, LightParameter.Specular, light_specular); GL.Enable(EnableCap.Lighting); GL.Light(LightName.Light0, LightParameter.Position, lightPos); GL.Enable(EnableCap.Light0); //GL.Light(LightName.Light0, LightParameter.SpotDirection, spotdirection); //GL.Light(LightName.Light0, LightParameter.SpotCutoff, 20); } #endregion GL.Translate(Drag.Pane.X, -Drag.Pane.Y, 0); Matrix4d m = MatrixFromYawPitchRoll(-Math.PI * Drag.Angle.X / 360.0f, -Math.PI * Drag.Angle.Y / 360.0f, 0); GL.MultMatrix(ref m); // Model space #region Box/Axis GL.Disable(EnableCap.Lighting); Vector3d scannerCenter = new Vector3d(0, -size / 2f, 0); Axis(new Vector3d(scannerCenter.X - TableRadius, scannerCenter.Y, scannerCenter.Z + TableRadius), 10); #endregion ; GL.Translate(0, SceneYOffset, 0); if (BoundingBox) { DrawBoundingBox(Color.Red, Scene.Min, Scene.Max); } if (Lightning) { GL.Enable(EnableCap.Lighting); GL.ColorMaterial(MaterialFace.FrontAndBack, ColorMaterialParameter.AmbientAndDiffuse); GL.Enable(EnableCap.ColorMaterial); } GL.PointSize(2); GL.LineWidth(2); RenderingContext context = RenderingContext.From(ViewerConfig); context.ApplyFaceDefault(); GL.ShadeModel(Smooth ? ShadingModel.Smooth : ShadingModel.Flat); GL.Enable(EnableCap.CullFace); GL.CullFace(CullFaceMode.Back); Scene.Render(ref context); GL.Translate(0, -SceneYOffset, 0); DrawScannerTable(scannerCenter); SwapBuffers(); using (Graphics g = Graphics.FromHwnd(this.Handle)) { using (SolidBrush bg = new SolidBrush(this.BackColor)) { using (SolidBrush bf = new SolidBrush(this.ForeColor)) { string text = String.Format("{0} points", Scene.GetNumVertices().ToString("N0")); SizeF s = g.MeasureString(text, this.Font); RectangleF rect = new RectangleF(0, 0, s.Width, s.Height); rect.Y = this.Height - s.Height; g.FillRectangle(bg, rect); g.DrawString(text, this.Font, bf, rect.Location); } } } // this.Label.Text = String.Format("{0} points", Scene.GetNumVertices()); }
/// <summary> /// Render the robot model and paint the GLControl /// </summary> /// <param name="sender">Object sending the event</param> /// <param name="e">Paint event arguments</param> private void GLControl1_Paint(object sender, PaintEventArgs e) { GL.Enable(EnableCap.Lighting); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); if (!IsLoaded || !ModelLoaded) { glControl1.SwapBuffers(); return; } UpdateCameraMode(); baseNode.Compute(true); // Project GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); double aspect = (double)Width / (double)Height; Matrix4d perspective = Matrix4d.CreatePerspectiveFieldOfView(Math.PI / 2, aspect, 0.1, 1000); GL.MultMatrix(ref perspective); GL.Viewport(0, 0, Width, Height); // Transform GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); cam.translate(); #region LIGHTS GL.LightModel(LightModelParameter.LightModelAmbient, ambient); GL.Light(LightName.Light0, LightParameter.Position, l0_position); GL.Light(LightName.Light0, LightParameter.Diffuse, l_diffuse); GL.Light(LightName.Light0, LightParameter.Specular, l_specular); GL.Light(LightName.Light1, LightParameter.Position, l1_position); GL.Light(LightName.Light1, LightParameter.Diffuse, l_diffuse); GL.Light(LightName.Light1, LightParameter.Specular, l_specular); #endregion DoSelect(); RenderInternal(); // Overlay: foreach (RigidNode_Base node in nodes) { if ((((OGL_RigidNode)node).highlight & OGL_RigidNode.HighlightState.ACTIVE) == OGL_RigidNode.HighlightState.ACTIVE) { ((OGL_RigidNode)node).RenderDebug(settings.modelDrawAxes); } } #region OVERLAY GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(0, Width, Height, 0, 0, 10); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); GL.UseProgram(0); GL.Disable(EnableCap.Lighting); RenderOverlay(); #endregion glControl1.SwapBuffers(); }
/// <summary> /// Creates the projection matrix based on the control setup. /// </summary> /// <returns>The projection matrix</returns> protected Matrix4d CreateProjectionMatrix() { float aspect_ratio = this.ClientSize.Width / (float)this.ClientSize.Height; return(Matrix4d.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(Camera.FieldOfView), aspect_ratio, (float)Camera.NearClip, (float)Camera.FarClip)); }
public override Matrix4d ComputeProjectionMatrix(ICamera camera) { var aspectRatio = camera.Width / camera.Height; return(Matrix4d.CreatePerspectiveFieldOfView(camera.HorizontalFieldOfView / aspectRatio, aspectRatio, NearPlane, FarPlane)); }
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(); }
// This is the main function for determining what will be rendered private void glControl1_Paint(object sender, PaintEventArgs e) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); //Basic Setup for viewing Matrix4d perspective = Matrix4d.CreatePerspectiveFieldOfView(1.04f, 3 / 4f, 1f, 100000f); //Setup Perspective //the first argument (Set of 3) is the location of your camera, so m_eye seems correct //the second argument (Set of 3) is the point the camera centers on, so you'll want a //location relative to your eye location or you'll always be focused on a single point in space //the third argument (Set of 3) is to indicate what direction should be considered "up" Matrix4d lookat = Matrix4d.LookAt(SphereCoordinates.getX(), SphereCoordinates.getY(), SphereCoordinates.getZ(), viewingAngle[3], viewingAngle[4], viewingAngle[5], 0, -1, 0); //Set labels cameraPositionLabel.Text = "<" + Math.Round(SphereCoordinates.getX()) + ", " + Math.Round(SphereCoordinates.getY()) + ", " + Math.Round(SphereCoordinates.getZ()) + "> with Radius: " + SphereCoordinates.getRadius(); lookingAtLabel.Text = "<" + Math.Round(viewingAngle[3]) + ", " + Math.Round(viewingAngle[4]) + ", " + Math.Round(viewingAngle[5]) + ">"; //Setup camera GL.MatrixMode(MatrixMode.Projection); //Load Perspective GL.LoadIdentity(); GL.LoadMatrix(ref perspective); GL.MatrixMode(MatrixMode.Modelview); //Load Camera GL.LoadIdentity(); GL.LoadMatrix(ref lookat); GL.Viewport(0, 0, glControl1.Width, glControl1.Height); //Size of window GL.Enable(EnableCap.DepthTest); //Enable correct Z Drawings GL.DepthFunc(DepthFunction.Less); // GL.Rotate(1, 0, 1, 0); GL.PushMatrix(); //Rotating //GL.Rotate(1, 0, 0, 1); //GL.Rotate(1, 0, 1, 0); // Translation of object across axii //GL.Translate(new Vector3((float)this.viewingAngle[3], (float)this.viewingAngle[4], (float)this.viewingAngle[5])); // Objects to be displayed go here. //Draw pyramid, Y is up and down, Z is towards you, X is left and right //Vertex goes (X,Y,Z) GL.Begin(PrimitiveType.Points); paintVertices(); GL.End(); GL.Begin(PrimitiveType.Lines); //Line X GL.Color3(Color.Red); GL.Vertex3(0, 0, 0); GL.Vertex3(100, 0, 0); //Line Y GL.Color3(Color.Blue); GL.Vertex3(0, 0, 0); GL.Vertex3(0, 100, 0); //Line Z GL.Color3(Color.Green); GL.Vertex3(0, 0, 0); GL.Vertex3(0, 0, 100); GL.End(); GL.PopMatrix(); //Rotating //GL.Rotate(1, 0, 0, 1); //GL.Rotate(1, 0, 1, 0); glControl1.SwapBuffers(); }
public void Execute(CameraMan man) { if (man.Dirty || _dirty) { LookAt = man.LookAt; // // Distance from // var distanceToLookAt = (man.Pos3d - man.LookAt3d).Length; var near = distanceToLookAt - LandThickness / 2; if (near < Nearest) { near = Nearest; } var far = near + LandThickness; // // Regenerate the matrices // _projectionMatrix = Matrix4d.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45), _aspect, near, far); _projectionMatrixF = ToMatrix4(_projectionMatrix); var pos = man.Pos3d; var lookAt = man.LookAt3d; var _up = pos; _up.Normalize(); _viewMatrix = Matrix4d.LookAt(pos, lookAt, _up); _viewMatrixF = ToMatrix4(_viewMatrix); // // Calculate some spots on the screen so we know what's visible // ScreenLocations[0] = GetLocation(new PointF(Width, 0)); ScreenLocations[1] = GetLocation(new PointF(Width, Height / 8)); ScreenLocations[2] = GetLocation(new PointF(Width, Height / 3)); ScreenLocations[3] = GetLocation(new PointF(Width, Height / 2)); ScreenLocations[4] = GetLocation(new PointF(Width, Height * 7 / 8)); ScreenLocations[5] = GetLocation(new PointF(Width, Height * 2 / 3)); ScreenLocations[6] = GetLocation(new PointF(Width, Height)); ScreenLocations[7] = GetLocation(new PointF(0, 0)); ScreenLocations[8] = GetLocation(new PointF(0, Height / 8)); ScreenLocations[9] = GetLocation(new PointF(0, Height / 3)); ScreenLocations[10] = GetLocation(new PointF(0, Height / 2)); ScreenLocations[11] = GetLocation(new PointF(0, Height * 7 / 8)); ScreenLocations[12] = GetLocation(new PointF(0, Height * 2 / 3)); ScreenLocations[13] = GetLocation(new PointF(0, Height)); ScreenLocations[14] = GetLocation(new PointF(Width / 2, 0)); ScreenLocations[15] = GetLocation(new PointF(Width / 2, Height / 8)); ScreenLocations[16] = GetLocation(new PointF(Width / 2, Height / 3)); ScreenLocations[17] = GetLocation(new PointF(Width / 2, Height / 2)); ScreenLocations[18] = GetLocation(new PointF(Width / 2, Height * 7 / 8)); ScreenLocations[19] = GetLocation(new PointF(Width / 2, Height * 2 / 3)); ScreenLocations[20] = GetLocation(new PointF(Width / 2, Height)); // // Calculate the rads per pixel X // var dp = 100; var left = new PointF(Width / 2 - dp / 2, 15 * Height / 24); var right = new PointF(Width / 2 + dp / 2, 15 * Height / 24); var leftLoc = GetLocation(left); var rightLoc = GetLocation(right); var leftPos = GetPosition(left); var rightPos = GetPosition(right); if (leftLoc != null && rightLoc != null) { var ddegs = rightLoc.VectorTo(leftLoc).Length; RadiansPerPixel = ddegs / dp * Math.PI / 180; var dkm = (rightPos.Value - leftPos.Value).Length; var kmPerPixel = dkm / dp; UnitsPerPixel = (float)(kmPerPixel); MetersPerPixel = kmPerPixel * 1000; // // Calculate the zoom level // var IdealZoom = Math.Log(2 * Math.PI / 256 / RadiansPerPixel) / Math.Log(2); Zoom = (int)(IdealZoom + 0.5); // Rounded to nearest if (Zoom > 18) { Zoom = 18; } else if (Zoom < 4) { Zoom = 4; } //Console.WriteLine ("rpp = {0}; mpp = {1}; zoom = {2}", RadiansPerPixel, MetersPerPixel, IdealZoom); } _dirty = false; man.Dirty = false; } GL.MatrixMode(All.Projection); GL.LoadMatrix(ref _projectionMatrixF.Row0.X); GL.MatrixMode(All.Modelview); GL.LoadMatrix(ref _viewMatrixF.Row0.X); }
public void UseDefaultProjection() { UseDefaultProjectionMatrix = true; ProjectionMatrix = Matrix4d.CreatePerspectiveFieldOfView((Math.PI / 180) * FieldOfView, ((double)Viewport.Width) / ((double)Viewport.Height), NearPlane, FarPlane); }
/// <summary>Calculate the projection matrix. Needs to be done on initial startup and anytime game width, height or FOV changes.</summary> internal void CalculateProjectionMatrix() { Matrix4d.CreatePerspectiveFieldOfView(Settings.FieldOfView, Width / (float)Height, 0.01f, Settings.ZFar, out Projection); }