2014-03-28 278 views
3

有無論如何以vb形式繪製x(從x文本框),y(從y文本框)和z(從z文本框)? 這是Windows應用程序。我有三個文本框表示x,y,z座標。我想知道是否有任何工具或方法向用戶展示這一點。 enter image description here如何繪製(x,y,z)

+0

我想你想展示的不僅僅是一個點以上。您可能需要顯示具有標籤的座標軸,點的實心圓和點旁邊的標籤。另外,可能會顯示座標值。請具體說明輸出的內容。 「顯示點」的_how需要澄清。 – ja72

回答

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(); 
    } 

} 
+0

可能還想嘗試'GL.Hint(HintTarget.PointSmoothHint,HintMode.Nicest);'平滑點。 – ja72

+0

我的輸出將與您在上面發佈的圖片類似,但如您所說,我會擁有多個點。我已經有了一個表單,我想在表單的一側添加此功能。我應該如何將其添加到我的代碼?我安裝了opentk。我是否需要將此文件夾放在特定位置才能使用您的代碼? – user3396709

+0

您是否在項目中引用了'OpenTK'?您還需要添加對「OpenTK.GLControl」的引用,並將「GLControl」添加到您現有的表單中。使用opentk閱讀文檔並在[SO]中發佈任何問題。 – ja72

相關問題