void IMouseHandler.canvas_MouseMove(object sender, GLMouseEventArgs e) { if (this.mouseDownFlag && ((e.Button & this.lastBindingMouseButtons) != GLMouseButtons.None) && (e.X != lastPosition.x || e.Y != lastPosition.y)) { IViewCamera camera = this.camera; if (camera == null) { return; } vec3 back = this.back; vec3 right = this.right; vec3 up = this.up; GUISize bound = this.bound; ivec2 downPosition = this.lastPosition; { float deltaX = -this.HorizontalRotationFactor * (e.X - downPosition.x) / (float)bound.Width; float cos = (float)Math.Cos(deltaX); float sin = (float)Math.Sin(deltaX); vec3 newBack = new vec3( back.x * cos + right.x * sin, back.y * cos + right.y * sin, back.z * cos + right.z * sin); back = newBack; right = up.cross(back); back = back.normalize(); right = right.normalize(); } { float deltaY = this.VerticalRotationFactor * (e.Y - downPosition.y) / (float)bound.Height; float cos = (float)Math.Cos(deltaY); float sin = (float)Math.Sin(deltaY); vec3 newBack = new vec3( back.x * cos - up.x * sin, back.y * cos - up.y * sin, back.z * cos - up.z * sin); back = newBack; up = back.cross(right); back = back.normalize(); up = up.normalize(); } //camera.Position = camera.Target + // back * (float)((camera.Position - camera.Target).length()); rootNode.RotationAxis = up; camera.UpVector = up; this.back = back; this.right = right; this.up = up; this.lastPosition = e.Location; IGLCanvas canvas = this.canvas; if (canvas != null && canvas.RenderTrigger == RenderTrigger.Manual) { canvas.Repaint(); } } }
/// <summary> /// camera, canvas, nodes. /// rendering, picking. /// </summary> /// <param name="camera"></param> /// <param name="canvas"></param> public Scene(ICamera camera, IGLCanvas canvas) { this.Camera = camera; this.Canvas = canvas; this.Lights = new List <LightBase>(); }
void IMouseHandler.canvas_MouseDown(object sender, GLMouseEventArgs e) { this.lastBindingMouseButtons = this.BindingMouseButtons; if ((e.Button & this.lastBindingMouseButtons) != GLMouseButtons.None) { IGLCanvas canvas = this.canvas; this.SetBounds(this.canvas.Width, this.canvas.Height); if (!cameraState.IsSameState(this.camera)) { SetCamera(this.camera.Position, this.camera.Target, this.camera.UpVector); } this._startPosition = GetArcBallPosition(e.X, e.Y); mouseDownFlag = true; { var mouseDown = this.MouseDown; if (mouseDown != null) { mouseDown(this, e); } } } }
public DesignModeAssist(IGLCanvas canvas) { var camera = new Camera(new vec3(0, 0, 4), new vec3(0, 0, 0), new vec3(0, 1, 0), CameraType.Perspecitive, canvas.Width, canvas.Height); GroupNode group; { var propeller = new PropellerRenderer() { WorldPosition = new vec3(0, -1.5f, 0) }; var clock = new ClockNode(); group = new GroupNode(propeller, clock); } var scene = new Scene(camera, canvas) { ClearColor = Color.Black.ToVec4(), RootElement = group, }; this.scene = scene; var list = new ActionList(); var transformAction = new TransformAction(scene); list.Add(transformAction); var renderAction = new RenderAction(scene); list.Add(renderAction); this.actionList = list; this.fullname = canvas.GetType().FullName; }
private void glCanvas1_MouseDown(object sender, MouseEventArgs e) { this.lastMousePosition = e.Location; if (e.Button == System.Windows.Forms.MouseButtons.Right) { //// operate camera //rotator.SetBounds(this.glCanvas1.Width, this.glCanvas1.Height); //rotator.MouseDown(e.X, e.Y); } else if (e.Button == System.Windows.Forms.MouseButtons.Left) { // move vertex if (pickedGeometry != null) { IGLCanvas canvas = this.winGLCanvas1; var viewport = new vec4(0, 0, canvas.Width, canvas.Height); var lastWindowSpacePos = new vec3(e.X, this.winGLCanvas1.Height - e.Y - 1, pickedGeometry.PickedPosition.z); mat4 projectionMatrix = this.scene.Camera.GetProjectionMatrix(); mat4 viewMatrix = this.scene.Camera.GetViewMatrix(); mat4 modelMatrix = (pickedGeometry.FromRenderer as PickableNode).GetModelMatrix(); var lastModelSpacePos = glm.unProject(lastWindowSpacePos, viewMatrix * modelMatrix, projectionMatrix, viewport); var dragParam = new DragParam( lastModelSpacePos, this.scene.Camera.GetProjectionMatrix(), this.scene.Camera.GetViewMatrix(), viewport, new ivec2(e.X, this.winGLCanvas1.Height - e.Y - 1)); dragParam.pickedVertexIds.AddRange(pickedGeometry.VertexIds); this.dragParam = dragParam; } } }
void IKeyboardHandler.canvas_KeyPress(object sender, GLKeyPressEventArgs e) { bool updated = false; if (e.KeyChar == frontKey || e.KeyChar == upcaseFrontKey) { vec3 right = this.camera.GetRight(); vec3 standardFront = this.camera.UpVector.cross(right).normalize(); this.camera.Position += standardFront * this.StepLength; this.camera.Target += standardFront * this.StepLength; updated = true; } else if (e.KeyChar == backKey || e.KeyChar == upcaseBackKey) { vec3 right = this.camera.GetRight(); vec3 standardBack = right.cross(this.camera.UpVector).normalize(); this.camera.Position += standardBack * this.StepLength; this.camera.Target += standardBack * this.StepLength; updated = true; } else if (e.KeyChar == leftKey || e.KeyChar == upcaseLeftKey) { vec3 right = this.camera.GetRight(); vec3 left = (-right).normalize(); this.camera.Position += left * this.StepLength; this.camera.Target += left * this.StepLength; updated = true; } else if (e.KeyChar == rightKey || e.KeyChar == upcaseRightKey) { vec3 right = this.camera.GetRight().normalize(); this.camera.Position += right * this.StepLength; this.camera.Target += right * this.StepLength; updated = true; } else if (e.KeyChar == upKey || e.KeyChar == upcaseUpKey) { vec3 up = this.camera.UpVector.normalize(); this.camera.Position += up * this.StepLength; this.camera.Target += up * this.StepLength; updated = true; } else if (e.KeyChar == downKey || e.KeyChar == upcaseDownKey) { vec3 down = -this.camera.UpVector.normalize(); this.camera.Position += down * this.StepLength; this.camera.Target += down * this.StepLength; updated = true; } if (updated) { IGLCanvas canvas = this.canvas; if (canvas.RenderTrigger == RenderTrigger.Manual) { canvas.Repaint(); } } }
public void Design00_HowToUseCSharpGL() { IGLCanvas canvas = GetCanvas(); GLRenderContext renderContext = canvas.RenderContext; renderContext.MakeCurrent(); RenderScene(); }
void IMouseHandler.canvas_MouseWheel(object sender, GLMouseEventArgs e) { this.camera.MouseWheel(e.Delta); IGLCanvas canvas = this.canvas; if (canvas != null && canvas.RenderTrigger == RenderTrigger.Manual) { canvas.Repaint(); } }
/// <summary> /// /// </summary> public override void Unbind() { if (this.canvas != null && (!this.canvas.IsDisposed)) { this.canvas.MouseDown -= this.mouseDownEvent; this.canvas.MouseMove -= this.mouseMoveEvent; this.canvas.MouseUp -= this.mouseUpEvent; this.canvas.MouseWheel -= this.mouseWheelEvent; this.canvas = null; this.camera = null; } }
public FormArcball(ICamera camera, ArcBallManipulater manipulater, IGLCanvas canvas) { InitializeComponent(); this.modelCamera = camera; this.modelManipulater = manipulater; this.modelCanvas = canvas; this.Load += FormMain_Load; this.winGLCanvas1.OpenGLDraw += winGLCanvas1_OpenGLDraw; this.winGLCanvas1.Resize += winGLCanvas1_Resize; this.winGLCanvas1.MouseWheel += winGLCanvas1_MouseWheel; }
void IMouseHandler.canvas_MouseMove(object sender, GLMouseEventArgs e) { if (mouseDownFlag && ((e.Button & this.lastBindingMouseButtons) != GLMouseButtons.None)) { if (!cameraState.IsSameState(this.camera)) { SetCamera(this.camera.Position, this.camera.Target, this.camera.UpVector); } this._endPosition = GetArcBallPosition(e.X, e.Y); var cosRadian = _startPosition.dot(_endPosition) / (_startPosition.length() * _endPosition.length()); if (cosRadian > 1.0f) { cosRadian = 1.0f; } else if (cosRadian < -1) { cosRadian = -1.0f; } float angle = MouseSensitivity * (float)(Math.Acos(cosRadian) / Math.PI * 180); _normalVector = _startPosition.cross(_endPosition).normalize(); if (! ((_normalVector.x == 0 && _normalVector.y == 0 && _normalVector.z == 0) || float.IsNaN(_normalVector.x) || float.IsNaN(_normalVector.y) || float.IsNaN(_normalVector.z))) { _startPosition = _endPosition; mat4 newRotation = glm.rotate(angle, _normalVector); this.totalRotation = newRotation * this.totalRotation; { var rotated = this.Rotated; if (rotated != null) { Quaternion quaternion = this.totalRotation.ToQuaternion(); float angleInDegree; vec3 axis; quaternion.Parse(out angleInDegree, out axis); rotated(this, new Rotation(axis, angleInDegree, e.Button, e.Clicks, e.X, e.Y, e.Delta)); } } } IGLCanvas canvas = this.canvas; if (canvas != null && canvas.RenderTrigger == RenderTrigger.Manual) { canvas.Repaint(); } } }
public void Render(bool drawText, int height, double fps, IGLCanvas canvas) { this.actionList.Act(new ActionParams(Viewport.GetCurrent())); FontBitmaps.DrawText(10, 10, Color.White, "Courier New",// "Courier New", 25.0f, this.fullname); if (drawText) { FontBitmaps.DrawText(10, height - 20 - 1, Color.Red, "Courier New",// "Courier New", 20.0f, string.Format("FPS: {0}", fps.ToShortString())); } }
/// <summary> /// /// </summary> public override void Bind(ICamera camera, IGLCanvas canvas) { if (camera == null || canvas == null) { throw new ArgumentNullException(); } this.camera = camera; this.canvas = canvas; canvas.MouseDown += this.mouseDownEvent; canvas.MouseMove += this.mouseMoveEvent; canvas.MouseUp += this.mouseUpEvent; canvas.MouseWheel += this.mouseWheelEvent; }
/// <summary> /// /// </summary> /// <param name="canvas"></param> public override void Bind(IGLCanvas canvas) { var winCanvas = canvas as WinGLCanvas; if (winCanvas == null) { throw new ArgumentException(); } winCanvas.MouseMove += mouseMove; winCanvas.MouseDown += mouseDown; winCanvas.MouseUp += mouseUp; winCanvas.KeyDown += keyDown; winCanvas.KeyUp += keyUp; winCanvas.Resize += resize; this.BindingCanvas = winCanvas; }
public DesignModeAssist(IGLCanvas canvas) { var camera = new Camera(new vec3(0, 0, 4), new vec3(0, 0, 0), new vec3(0, 1, 0), CameraType.Perspecitive, canvas.Width, canvas.Height); GroupRenderer group; { var propeller = new PropellerRenderer() { WorldPosition = new vec3(0, -1.5f, 0) }; var clock = new ClockRenderer(); group = new GroupRenderer(propeller, clock); } var scene = new Scene(camera, canvas) { ClearColor = Color.Black.ToVec4(), RootElement = group, }; this.scene = scene; this.fullname = canvas.GetType().FullName; }
void IMouseHandler.canvas_MouseMove(object sender, GLMouseEventArgs e) { if (this.mouseDownFlag && ((e.Button & this.lastBindingMouseButtons) != GLMouseButtons.None) && (e.X != this.lastPosition.x || e.Y != this.lastPosition.y)) { mat4 rotationMatrix = glm.rotate(this.HorizontalRotationSpeed * (e.X - this.lastPosition.x), -this.camera.UpVector); var front = new vec4(this.camera.GetFront(), 1.0f); vec4 front1 = rotationMatrix * front; rotationMatrix = glm.rotate(this.VerticalRotationSpeed * (this.lastPosition.y - e.Y), this.camera.GetRight()); vec4 front2 = rotationMatrix * front1; //front2 = front2.normalize(); this.camera.Target = this.camera.Position + new vec3(front2); this.lastPosition = e.Location; IGLCanvas canvas = this.canvas; if (canvas != null && canvas.RenderTrigger == RenderTrigger.Manual) { canvas.Repaint(); } } }
void IMouseHandler.canvas_MouseMove(object sender, GLMouseEventArgs e) { if (mouseDownFlag && ((e.Button & this.lastBindingMouseButtons) != GLMouseButtons.None)) { if (!cameraState.IsSameState(this.camera)) { SetCamera(this.camera.Position, this.camera.Target, this.camera.UpVector); } Point location = new Point(e.X, this.canvas.Height - e.Y - 1); Point differenceOnScreen = new Point(location.X - this._lastPosition.X, location.Y - this._lastPosition.Y); mat4 model = this.renderer.GetModelMatrix(); mat4 view = this.camera.GetViewMatrix(); mat4 projection = this.camera.GetProjectionMatrix(); vec4 viewport; { var result = new int[4]; GL.Instance.GetIntegerv((uint)GetTarget.Viewport, result); viewport = new vec4(result[0], result[1], result[2], result[3]); } var position = new vec3(0.0f);// imangine we have a point at (0, 0, 0). vec3 windowPos = glm.project(position, view * model, projection, viewport); var newWindowPos = new vec3(windowPos.x + differenceOnScreen.X, windowPos.y + differenceOnScreen.Y, windowPos.z); vec3 newPosition = glm.unProject(newWindowPos, view * model, projection, viewport); var worldPosition = new vec3(model * new vec4(position, 1.0f)); var newWorldPosition = new vec3(model * new vec4(newPosition, 1.0f)); this.renderer.WorldPosition += newWorldPosition - worldPosition; this._lastPosition = location; IGLCanvas canvas = this.canvas; if (canvas != null && canvas.RenderTrigger == RenderTrigger.Manual) { canvas.Repaint(); } } }
/// <summary> /// Bind this manipulater to specified <paramref name="camera"/> and <paramref name="canvas"/>. /// </summary> /// <param name="camera"></param> /// <param name="canvas"></param> public override void Bind(ICamera camera, IGLCanvas canvas) { if (camera == null || canvas == null) { throw new ArgumentNullException(); } if (this.isBinded) { return; } this.camera = camera; this.canvas = canvas; canvas.MouseDown += this.mouseDownEvent; canvas.MouseMove += this.mouseMoveEvent; canvas.MouseUp += this.mouseUpEvent; canvas.MouseWheel += this.mouseWheelEvent; SetCamera(camera.Position, camera.Target, camera.UpVector); this.isBinded = true; }
/// <summary> /// start to manipulate specified <paramref name="camera"/> or model. /// </summary> /// <param name="camera"></param> /// <param name="canvas"></param> public abstract void Bind(ICamera camera, IGLCanvas canvas);
///// <summary> ///// Ambient light color. ///// </summary> //public vec3 AmbientLight { get; set; } /// <summary> /// camera, canvas, nodes. /// rendering, picking. /// </summary> /// <param name="camera"></param> /// <param name="canvas"></param> public Scene(ICamera camera, IGLCanvas canvas) { this.Camera = camera; this.Canvas = canvas; }
/// <summary> /// /// </summary> /// <param name="canvas"></param> public abstract void Bind(IGLCanvas canvas);