如何绘制(x,y,z)

时间:2014-03-28 00:31:11

标签: vb.net winforms

无论如何以vb形式绘制x(来自x文本框),y(来自y文本框)和z(来自z文本框)? 这是Windows应用程序。我有三个文本框,表示x,y,z坐标。我想知道是否有任何工具或方法向用户显示这一点。enter image description here

1 个答案:

答案 0 :(得分:1)

这里有两条主要途径。 1)将(x,y,z)坐标转换为平面项目(x,y)并使用在屏幕上绘制,或2)使用直接绘制点到GLControl,但您必须先设置视口和投影。

它不是很漂亮,但这是VS2010使用OpenTK的概念验证。

Form

public partial class Form1 : Form
{
    bool loaded=false;

    public Form1()
    {
        InitializeComponent();
    }

    public Vector3 PointCoordinates
    {
        get
        {
            float x=0, y=0, z=0;
            float.TryParse(xTextBox.Text, out x);
            float.TryParse(yTextBox.Text, out y);
            float.TryParse(zTextBox.Text, out z);

            return new Vector3(x, y, z);
        }
        set
        {
            xTextBox.Text=value.X.ToString();
            yTextBox.Text=value.Y.ToString();
            zTextBox.Text=value.Z.ToString();
        }
    }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        PointCoordinates=new Vector3(0, 0, 0);
        loaded=true;
        SetupViewPort();
    }

    private void glControl1_Resize(object sender, EventArgs e)
    {
        if(!loaded) return;
        SetupViewPort();
    }

    private void glControl1_Paint(object sender, PaintEventArgs e)
    {
        glControl1.MakeCurrent();
        GL.ClearColor(glControl1.BackColor);
        GL.Clear(ClearBufferMask.ColorBufferBit|ClearBufferMask.DepthBufferBit);

        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadIdentity();

        SetupCamera();

        // Draw Coordinate System
        GL.LineWidth(1.5f);
        GL.Begin(PrimitiveType.Lines);
        GL.Color3(Color.Red);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(1, 0, 0);
        GL.Vertex3(0.85, 0.05, -0.05);
        GL.Vertex3(1, 0, 0);
        GL.Vertex3(0.85, -0.05, 0.05);
        GL.Vertex3(1, 0, 0);
        GL.Color3(Color.Green);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(0, 1, 0);
        GL.Vertex3(-0.05, 0.85, 0.05);
        GL.Vertex3(0, 1, 0);
        GL.Vertex3(0.05, 0.85, -0.05);
        GL.Vertex3(0, 1, 0);
        GL.Color3(Color.Blue);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(0, 0, 1);
        GL.Vertex3(-0.05, 0.05, 0.85);
        GL.Vertex3(0, 0, 1);
        GL.Vertex3(0.05, -0.05, 0.85);
        GL.Vertex3(0, 0, 1);
        GL.End();

        // Draw a single point
        var vector=PointCoordinates;
        GL.PointSize(5f);
        GL.Begin(PrimitiveType.Points);
        GL.Color3(Color.Black);
        GL.Vertex3(vector);
        GL.End();
        GL.PointSize(3f);
        GL.Begin(PrimitiveType.Points);
        GL.Color3(Color.Yellow);
        GL.Vertex3(vector);
        GL.End();

        glControl1.SwapBuffers();
    }

    void SetupViewPort()
    {
        float wt=Math.Max(1, glControl1.Width);
        float ht=Math.Max(1, glControl1.Height);
        float sz=(float)Math.Sqrt(ht*wt);
        GL.Viewport((int)(wt-sz)/2, (int)(ht-sz)/2, (int)sz, (int)sz);
        var ortho=Matrix4.CreateOrthographic(
            10f, 10f, 1f, 200f);
        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadMatrix(ref ortho);
    }

    void SetupCamera()
    {
        Matrix4 lookAt=Matrix4.LookAt(
                        10f, 5f, 15f,
                        0f, 0f, 0f,
                        0f, 1f, 0f);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadMatrix(ref lookAt);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        glControl1.Refresh();
    }

}