/// <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(); }
protected override void RenderModel(SimpleUIRectArgs args, OpenGL gl, SceneGraph.Core.RenderMode renderMode) { // Draw rectangle to show UI's scope. base.RenderModel(args, gl, renderMode); this.colorBarTransform.ScaleX = (float)args.UIWidth / (float)ColorIndicatorBar.barWidth; this.colorBarTransform.ScaleY = (float)args.UIHeight / (float)ColorIndicatorBar.barHeight; //this.colorBarTransform.ScaleZ = 1;// This is not needed. this.colorBarTransform.TranslateZ = -base.zNear;// make sure UI shows in front of enything else. this.colorNumber.CurrentArgs = args; }
protected void CalculateViewport(OpenGL gl, SimpleUIRectArgs args) { IRenderContextProvider rcp = gl.RenderContextProvider; Debug.Assert(rcp != null, "The gl.RenderContextProvider is null!"); if (rcp != null) { args.viewWidth = rcp.Width; args.viewHeight = rcp.Height; } else { int[] viewport = new int[4]; gl.GetInteger(OpenGL.GL_VIEWPORT, viewport); args.viewWidth = viewport[2]; args.viewHeight = viewport[3]; } }
protected override void RenderModel(SimpleUIRectArgs args, OpenGL gl, SceneGraph.Core.RenderMode renderMode) { // Draw rectangle to show UI's scope. base.RenderModel(args, gl, renderMode); // ** / 2: half of width/height, // ** / 3: CylinderAxis' length is 3. this.axisTransform.ScaleX = args.UIWidth / 2 / 3; this.axisTransform.ScaleY = args.UIHeight / 2 / 3; int max = Math.Max(args.UIWidth, args.UIHeight); this.axisTransform.ScaleZ = (this.CoordSystem == CoordinateSystem.RightHand ? 1 : -1) * max / 2 / 3; //this.axisTransform.TranslateZ = base.zFar;// make sure UI shows in front of enything else. //var target2Position = base.Camera.Position - base.Camera.Target; //target2Position.Normalize(); //target2Position *= 100; //this.axisTransform.TranslateX = base.Camera.Position.X + target2Position.X; //this.axisTransform.TranslateY = base.Camera.Position.Y + target2Position.Y; //this.axisTransform.TranslateZ = base.Camera.Position.Z + target2Position.Z; }
/// <summary> /// render UI model at axis's center(0, 0, 0) in <paramref name="UIWidth"/> and <paramref name="UIHeight"/>. /// <para>The <see cref="SimpleUIRect.RenderMode()"/> only draws a rectangle to show the UI's scope.</para> /// </summary> /// <param name="UIWidth"></param> /// <param name="UIHeight"></param> /// <param name="gl"></param> /// <param name="renderMode"></param> protected virtual void RenderModel(SimpleUIRectArgs args, OpenGL gl, RenderMode renderMode) { if (this.RenderBound) { gl.Begin(Enumerations.BeginMode.LineLoop); gl.Color(RectColor); switch (this.RectDirection) { case ERectDirection.XZ: gl.Vertex(-args.UIWidth / 2, 0, -args.UIHeight / 2); gl.Vertex(args.UIWidth / 2, 0, -args.UIHeight / 2); gl.Vertex(args.UIWidth / 2, 0, args.UIHeight / 2); gl.Vertex(-args.UIWidth / 2, 0, args.UIHeight / 2); break; case ERectDirection.XY: gl.Vertex(-args.UIWidth / 2, -args.UIHeight / 2, 0); gl.Vertex(args.UIWidth / 2, -args.UIHeight / 2, 0); gl.Vertex(args.UIWidth / 2, args.UIHeight / 2, 0); gl.Vertex(-args.UIWidth / 2, args.UIHeight / 2, 0); break; case ERectDirection.YZ: gl.Vertex(0, -args.UIWidth / 2, -args.UIHeight / 2); gl.Vertex(0, args.UIWidth / 2, -args.UIHeight / 2); gl.Vertex(0, args.UIWidth / 2, args.UIHeight / 2); gl.Vertex(0, -args.UIWidth / 2, args.UIHeight / 2); break; default: throw new NotImplementedException(); } gl.End(); } }
public void Render(OpenGL gl, RenderMode renderMode) { SimpleUIRectArgs lastArgs = this.CurrentArgs; if (lastArgs == null) { return; } ColorIndicatorData data = this.Data; if (data == null) { return; } int blockCount = data.GetBlockCount(); if (blockCount <= 0) { return; } String formatStr = MinorFormatString(data.Step); GLColor[] colors = data.ColorPalette.Colors; int blockWidth = 0; if (data.MaxValue - data.MinValue == 0) { blockWidth = lastArgs.UIWidth; } else { blockWidth = (int)(lastArgs.UIWidth * (data.Step / (data.MaxValue - data.MinValue))); } //draw numbers for (int i = 0; i <= blockCount; i++) { string value = null; if (i == blockCount) { if (!data.UseLogarithmic) { value = data.MaxValue.ToString(formatStr, CultureInfo.InvariantCulture); } else { value = Math.Pow(data.LogBase, data.MaxValue).ToString(formatStr, CultureInfo.InvariantCulture); } } else { float tickValue = data.MinValue + data.Step * i; if (!data.UseLogarithmic) { value = tickValue.ToString(formatStr, CultureInfo.InvariantCulture); } else { value = Math.Pow(data.LogBase, tickValue).ToString(formatStr, CultureInfo.InvariantCulture); } } double valueLength = 100.0 * value.Length / fontSize; double x = 0; if (i == blockCount) { x = -(double)lastArgs.UIWidth / 2 - lastArgs.left + lastArgs.UIWidth - valueLength / 2; } else { x = -(double)lastArgs.UIWidth / 2 - lastArgs.left + i * blockWidth - valueLength / 2; } double y = -(double)lastArgs.UIHeight / 2 - lastArgs.bottom - 14; gl.DrawText((int)x, (int)y, 1, 1, 1, "Courier New", fontSize, value); } }
protected void CalculateCoords(int viewWidth, int viewHeight, SimpleUIRectArgs args) { if ((Anchor & leftRightAnchor) == leftRightAnchor) { args.UIWidth = viewWidth - Margin.Left - Margin.Right; if (args.UIWidth < 0) { args.UIWidth = 0; } } else { args.UIWidth = this.Size.Width; } if ((Anchor & topBottomAnchor) == topBottomAnchor) { args.UIHeight = viewHeight - Margin.Top - Margin.Bottom; if (args.UIHeight < 0) { args.UIHeight = 0; } } else { args.UIHeight = this.Size.Height; } if ((Anchor & leftRightAnchor) == AnchorStyles.None) { args.left = -(args.UIWidth / 2 + (viewWidth - args.UIWidth) * ((double)Margin.Left / (double)(Margin.Left + Margin.Right))); } else if ((Anchor & leftRightAnchor) == AnchorStyles.Left) { args.left = -(args.UIWidth / 2 + Margin.Left); } else if ((Anchor & leftRightAnchor) == AnchorStyles.Right) { args.left = -(viewWidth - args.UIWidth / 2 - Margin.Right); } else // if ((Anchor & leftRightAnchor) == leftRightAnchor) { args.left = -(args.UIWidth / 2 + Margin.Left); } if ((Anchor & topBottomAnchor) == AnchorStyles.None) { args.bottom = -viewHeight / 2; args.bottom = -(args.UIHeight / 2 + (viewHeight - args.UIHeight) * ((double)Margin.Bottom / (double)(Margin.Bottom + Margin.Top))); } else if ((Anchor & topBottomAnchor) == AnchorStyles.Bottom) { args.bottom = -(args.UIHeight / 2 + Margin.Bottom); } else if ((Anchor & topBottomAnchor) == AnchorStyles.Top) { args.bottom = -(viewHeight - args.UIHeight / 2 - Margin.Top); } else // if ((Anchor & topBottomAnchor) == topBottomAnchor) { args.bottom = -(args.UIHeight / 2 + Margin.Bottom); } }