void element_BeforeRendering(object sender, Objects.RenderEventArgs e) { GL.MatrixMode(GL.GL_PROJECTION); GL.PushMatrix(); GL.LoadIdentity(); GL.gluPerspective(60, (double)glCanvas1.Width / (double)glCanvas1.Height, 0.001, 1000); IViewCamera camera = this.camera; if (camera == null) { GL.gluLookAt(0, 0, 1, 0, 0, 0, 0, 1, 0); //throw new Exception("Camera not set!"); } else { vec3 position = (camera.Position - camera.Target).normalize(); GL.gluLookAt(position.x, position.y, position.z, 0, 0, 0, camera.UpVector.x, camera.UpVector.y, camera.UpVector.z); } GL.MatrixMode(GL.GL_MODELVIEW); GL.PushMatrix(); GL.LoadIdentity(); GL.Scale(0.1, 0.1, 0.1); }
/// <summary> /// Get right vector in world space. /// </summary> /// <returns></returns> public static vec3 GetRight(this IViewCamera camera) { vec3 back = camera.Position - camera.Target; vec3 result = camera.UpVector.cross(back); return(result); }
/// <summary> /// Prepare projection matrix. /// </summary> /// <param name="gl"></param> public virtual void PushObjectSpace(OpenGL gl) { this.args = new SimpleUIRectArgs(); //int viewWidth; //int viewHeight; CalculateViewport(gl, args); //int UIWidth, UIHeight, left, bottom; CalculateCoords(args.viewWidth, args.viewHeight, args); gl.MatrixMode(SharpGL.Enumerations.MatrixMode.Projection); gl.PushMatrix(); gl.LoadIdentity(); gl.Ortho(args.left, args.right, args.bottom, args.top, zNear, zFar); IViewCamera camera = this.Camera; if (camera == null) { gl.LookAt(0, 0, 1, 0, 0, 0, 0, 1, 0); //throw new Exception("Camera not set!"); } else { Vertex position = camera.Position - camera.Target; position.Normalize(); gl.LookAt(position.X, position.Y, position.Z, 0, 0, 0, camera.UpVector.X, camera.UpVector.Y, camera.UpVector.Z); } gl.MatrixMode(SharpGL.Enumerations.MatrixMode.Modelview); gl.PushMatrix(); }
/// <summary> /// Get left vector in world space. /// </summary> /// <returns></returns> public static vec3 GetLeft(this IViewCamera camera) { vec3 back = camera.Position - camera.Target; vec3 result = back.cross(camera.UpVector); return(result); }
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(); } } }
void legacyUIRect_BeforeRendering(object sender, Objects.RenderEventArgs e) { LegacySimpleUIRect element = sender as LegacySimpleUIRect; IUILayoutArgs args = element.GetArgs(); GL.MatrixMode(GL.GL_PROJECTION); GL.PushMatrix(); GL.LoadIdentity(); GL.Ortho((float)args.left, (float)args.right, (float)args.bottom, (float)args.top, element.Param.zNear, element.Param.zFar); //GL.Ortho(args.left / 2, args.right / 2, args.bottom / 2, args.top / 2, element.Param.zNear, element.Param.zFar); IViewCamera camera = this.camera; if (camera == null) { GL.gluLookAt(0, 0, 1, 0, 0, 0, 0, 1, 0); //throw new Exception("Camera not set!"); } else { vec3 position = (camera.Position - camera.Target).normalize(); GL.gluLookAt(position.x, position.y, position.z, 0, 0, 0, camera.UpVector.x, camera.UpVector.y, camera.UpVector.z); } GL.MatrixMode(GL.GL_MODELVIEW); GL.PushMatrix(); GL.Scale(args.UIWidth / 2, args.UIHeight / 2, args.UIWidth); }
/// <summary> /// Gets standard down. /// </summary> /// <param name="camera"></param> /// <returns></returns> public static vec3 GetDown(this IViewCamera camera) { vec3 back = camera.Position - camera.Target; vec3 right = camera.UpVector.cross(back); vec3 down = right.cross(back); return(down); }
/// <summary> /// Gets standard up. /// </summary> /// <param name="camera"></param> /// <returns></returns> public static vec3 GetUp(this IViewCamera camera) { vec3 back = camera.Position - camera.Target; vec3 right = camera.UpVector.cross(back); vec3 up = back.cross(right); return(up); }
/// <summary> /// Extension method for <see cref="IViewCamera"/> to get view matrix. /// </summary> /// <param name="camera"></param> /// <returns></returns> public static GlmNet.mat4 GetViewMat4(this IViewCamera camera) { GlmNet.vec3 position = ToVec3(camera.Position); GlmNet.vec3 target = ToVec3(camera.Target); GlmNet.vec3 up = ToVec3(camera.UpVector); GlmNet.mat4 lookAt = GlmNet.glm.lookAt(position, target, up); return(lookAt); }
void IMouseHandler.canvas_MouseMove(object sender, MouseEventArgs e) { if (this.mouseDownFlag && ((e.Button & this.lastBindingMouseButtons) != MouseButtons.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; Size bound = this.bound; Point downPosition = this.lastPosition; { float deltaX = -this.HorizontalRotationFactor * (e.X - downPosition.X) / 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) / 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()); camera.UpVector = up; this.back = back; this.right = right; this.up = up; this.lastPosition = e.Location; if (this.canvas.RenderTrigger == RenderTrigger.Manual) { this.canvas.Invalidate(); } } }
public override void PushObjectSpace(OpenGL gl) { //base.PushObjectSpace(gl); this.args = new SimpleUIRectArgs(); //int viewWidth; //int viewHeight; CalculateViewport(gl, args); //int UIWidth, UIHeight, left, bottom; CalculateCoords(args.viewWidth, args.viewHeight, args); gl.MatrixMode(SharpGL.Enumerations.MatrixMode.Projection); gl.PushMatrix(); gl.LoadIdentity(); gl.Ortho(args.left, args.right, args.bottom, args.top, zNear, zFar); IViewCamera camera = this.Camera; if (camera == null) { gl.LookAt(0, 0, 1, 0, 0, 0, 0, 1, 0); //throw new Exception("Camera not set!"); } else { Vertex position = camera.Position; Vertex target = camera.Target; //position.Normalize(); gl.LookAt(position.X, position.Y, position.Z, target.X, target.Y, target.Z, camera.UpVector.X, camera.UpVector.Y, camera.UpVector.Z); } gl.MatrixMode(SharpGL.Enumerations.MatrixMode.Modelview); gl.PushMatrix(); if (mouseTransform.Camera != null) { //mouseTransform.TransformMatrix(gl); } var target2Position = base.Camera.Position - base.Camera.Target; target2Position.Normalize(); target2Position *= 100; var x = base.Camera.Position.X + target2Position.X; var y = base.Camera.Position.Y + target2Position.Y; var z = base.Camera.Position.Z + target2Position.Z; gl.Translate(x, y, z); }
public void MouseMove(int x, int y) { if (this.mouseDownFlag) { IViewCamera camera = this.Camera; if (camera == null) { return; } Vertex back = this.back; Vertex right = this.right; Vertex up = this.up; Size bound = this.bound; Point downPosition = this.downPosition; { float deltaX = -horizontalRotationFactor * (x - downPosition.X) / bound.Width; float cos = (float)Math.Cos(deltaX); float sin = (float)Math.Sin(deltaX); Vertex newBack = new Vertex( back.X * cos + right.X * sin, back.Y * cos + right.Y * sin, back.Z * cos + right.Z * sin); back = newBack; right = up.VectorProduct(back); back.Normalize(); right.Normalize(); } { float deltaY = verticalRotationFactor * (y - downPosition.Y) / bound.Height; float cos = (float)Math.Cos(deltaY); float sin = (float)Math.Sin(deltaY); Vertex newBack = new Vertex( back.X * cos + up.X * sin, back.Y * cos + up.Y * sin, back.Z * cos + up.Z * sin); back = newBack; up = back.VectorProduct(right); back.Normalize(); up.Normalize(); } camera.Position = camera.Target + back * (float)((camera.Position - camera.Target).Magnitude()); camera.UpVector = up; this.back = back; this.right = right; this.up = up; this.downPosition.X = x; this.downPosition.Y = y; } }
/// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void MouseMove(int x, int y) { if (this.MouseDownFlag) { IViewCamera camera = this.Camera; if (camera == null) { return; } vec3 back = this.back; vec3 right = this.right; vec3 up = this.up; Size bound = this.bound; Point downPosition = this.downPosition; { float deltaX = -horizontalRotationFactor * (x - downPosition.X) / 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.Normalize(); right.Normalize(); } { float deltaY = verticalRotationFactor * (y - downPosition.Y) / 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.Normalize(); up.Normalize(); } camera.Position = camera.Target + back * (float)((camera.Position - camera.Target).Magnitude()); camera.UpVector = up; this.back = back; this.right = right; this.up = up; this.downPosition.X = x; this.downPosition.Y = y; } }
private void UpdateCameraAxis() { IViewCamera camera = this._camera; if (camera == null) { return; } _back = camera.Position - camera.Target; _back.Normalize(); _right = camera.UpVector.VectorProduct(_back); _right.Normalize(); _up = _back.VectorProduct(_right); _up.Normalize(); }
/// <summary> /// /// </summary> public void Reset() { IViewCamera camera = this.Camera; if (camera == null) { return; } IViewCamera originalCamera = this.originalCamera; if (originalCamera == null) { return; } camera.Position = originalCamera.Position; camera.UpVector = originalCamera.UpVector; }
void IMouseHandler.canvas_MouseMove(object sender, MouseEventArgs e) { if (this.mouseDownFlag && ((e.Button & this.lastBindingMouseButtons) != MouseButtons.None)) { IViewCamera camera = this.camera; if (camera == null) { return; } Point newPosition = e.Location; int diffX = newPosition.X - this.lastPosition.X; int diffY = newPosition.Y - this.lastPosition.Y; mat4 viewMatrix = this.camera.GetViewMatrix(); mat4 projectionMatrix = this.camera.GetProjectionMatrix(); vec3 cameraPosition = this.camera.Position; vec4 viewport; { int[] tmp = OpenGL.GetViewport(); viewport = new vec4(tmp[0], tmp[1], tmp[2], tmp[3]); } vec3 windowPos = glm.project(cameraPosition, viewMatrix, projectionMatrix, viewport); var newWindowPos = new vec3(windowPos.x - diffX, windowPos.y + diffY, windowPos.z); vec3 newCameraPosition = glm.unProject(newWindowPos, viewMatrix, projectionMatrix, viewport); vec3 cameraDiff = newCameraPosition - cameraPosition; this.camera.Position = newCameraPosition; this.camera.Target += cameraDiff; this.lastPosition = newPosition; if (this.canvas.RenderTrigger == RenderTrigger.Manual) { this.canvas.Repaint(); } } }
/// <summary> /// 获取此UI元素的投影矩阵、视图矩阵和模型矩阵 /// </summary> /// <param name="uiElement"></param> /// <param name="projectionMatrix"></param> /// <param name="viewMatrix"></param> /// <param name="modelMatrix"></param> /// <param name="camera">如果为null,会以glm.lookAt(new vec3(0, 0, 1), new vec3(0, 0, 0), new vec3(0, 1, 0))计算默认值。</param> /// <param name="maxDepth">UI元素的外接球半径的倍数。</param> public static void GetMatrix(this IUILayout uiElement, out mat4 projectionMatrix, out mat4 viewMatrix, out mat4 modelMatrix, IViewCamera camera = null, float maxDepth = 2.0f) { IUILayoutArgs args = uiElement.GetArgs(); float max = (float)Math.Max(args.UIWidth, args.UIHeight); { //projectionMatrix = glm.ortho((float)args.left, (float)args.right, (float)args.bottom, (float)args.top, // TODO: / 2后与legacy opengl的UI元素显示就完全一致了。为什么??? projectionMatrix = glm.ortho((float)args.left / 2, (float)args.right / 2, (float)args.bottom / 2, (float)args.top / 2, uiElement.Param.zNear, uiElement.Param.zFar); // 下面注释掉的代码是用来测试legacy OpenGL的matrix与GLM库计算的matrix是否相同用的。已经证明了两者完全相同,此处仅作留念+以防万一。 //{ // float[] matrix = new float[16]; // GL.MatrixMode(GL.GL_PROJECTION); // GL.PushMatrix(); // GL.GetFloat(GetTarget.ProjectionMatrix, matrix); // GL.LoadIdentity(); // GL.GetFloat(GetTarget.ProjectionMatrix, matrix); // GL.Ortho(args.left / 2, args.right / 2, args.bottom / 2, args.top / 2, uiElement.Param.zNear, uiElement.Param.zFar); // GL.GetFloat(GetTarget.ProjectionMatrix, matrix);// this equals projectionMatrix // GL.PopMatrix(); //} // 把UI元素移到ortho长方体的最靠近camera的地方,这样就可以把UI元素放到OpenGL最前方。 projectionMatrix = glm.translate(projectionMatrix, new vec3(0, 0, uiElement.Param.zFar - max / 2 * maxDepth)); } { // UI元素不在三维场景中,所以其Camera可以是null。 if (camera == null) { //viewMatrix = glm.lookAt(new vec3(0, 0, 1), new vec3(0, 0, 0), new vec3(0, 1, 0)); viewMatrix = glm.lookAt( Camera.defaultPosition, Camera.defaultTarget, Camera.defaultUpVector); } else { vec3 position = camera.Position - camera.Target; position.Normalize(); viewMatrix = glm.lookAt(position, new vec3(0, 0, 0), camera.UpVector); } // 下面注释掉的代码是用来测试legacy OpenGL的matrix与GLM库计算的matrix是否相同用的。已经证明了两者完全相同,此处仅作留念+以防万一。 //{ // float[] matrix = new float[16]; // GL.MatrixMode(GL.GL_MODELVIEW); // GL.PushMatrix(); // GL.GetFloat(GetTarget.ModelviewMatix, matrix); // GL.LoadIdentity(); // GL.GetFloat(GetTarget.ModelviewMatix, matrix); // if(camera==null) // { // GL.gluLookAt(0, 0, 1, 0, 0, 0, 0, 1, 0); // } // else // { // vec3 position = camera.Position - camera.Target; // position.Normalize(); // GL.gluLookAt(position.x, position.y, position.z, 0, 0, 0, camera.UpVector.x, camera.UpVector.y, camera.UpVector.z); // } // GL.GetFloat(GetTarget.ModelviewMatix, matrix);// this equals viewMatrix // GL.PopMatrix(); //} } { modelMatrix = glm.scale(mat4.identity(), new vec3(args.UIWidth / 2, args.UIHeight / 2, max / 2)); // 下面注释掉的代码是用来测试legacy OpenGL的matrix与GLM库计算的matrix是否相同用的。已经证明了两者完全相同,此处仅作留念+以防万一。 //{ // float[] matrix = new float[16]; // GL.MatrixMode(GL.GL_MODELVIEW); // GL.PushMatrix(); // GL.GetFloat(GetTarget.ModelviewMatix, matrix); // GL.LoadIdentity(); // GL.GetFloat(GetTarget.ModelviewMatix, matrix); // GL.Scale(args.UIWidth / 2, args.UIHeight / 2, max / 2); // GL.GetFloat(GetTarget.ModelviewMatix, matrix);// this equals modelMatrix // GL.PopMatrix(); //} } }
/// <summary> /// Get front vector in world space. /// </summary> /// <returns></returns> public static vec3 GetFront(this IViewCamera camera) { vec3 result = camera.Target - camera.Position; return(result); }
/// <summary> /// Get back vector in world space. /// </summary> /// <returns></returns> public static vec3 GetBack(this IViewCamera camera) { vec3 result = camera.Position - camera.Target; return(result); }
/// <summary> /// Extension method for <see cref="IViewCamera"/> to get view matrix. /// </summary> /// <param name="camera"></param> /// <returns></returns> public static mat4 GetViewMat4(this IViewCamera camera) { mat4 lookAt = glm.lookAt(camera.Position, camera.Target, camera.UpVector); return(lookAt); }