【问题标题】:opentk vb.net multiple rotations and translationopentk vb.net 多次旋转和平移
【发布时间】:2020-08-20 19:57:41
【问题描述】:

我正在用 vb.net 和 OpenTK 编写一个小 sfw,实际上我可以正确加载和渲染一些 stl 文件。

我的程序的目的是展示一台CNC机器并读取gcode文件以显示文件的模拟。

我的零件旋转有问题,某些零件必须旋转和平移几次,例如 Z 轴上 45° 和 X 轴上 50° 以及在 XYZ 上平移后

我试过这样:

For Each p As Pezzo In ListaPezzi   'Draw all peaces

    GL.PushMatrix()                                 'Fix actually situation
    GL.Translate(p.DeX, p.DeY, p.DeZ)               'Translation on peace reference
    GL.Rotate(p.AlfaX, 1, 0, 0)                     'Rotation X
    GL.Rotate(p.AlfaY, 0, 1, 0)                     'Rotation Y
    GL.Rotate(p.AlfaZ, 0, 0, 1)                     'Rotation Z
    GL.Translate(-p.DeX, -p.DeY, -p.DeZ)            'Restore peace position

    If p.Tipo = Pezzo.Type.STL Then DisegnaSTL(p)   'Draw peace

    GL.PopMatrix()                                  'return matrix to past state

Next

第一次旋转后,其他旋转被“投影”在第一次旋转上。

【问题讨论】:

  • 我认为您需要进一步解释这一点。究竟是什么问题?我假设你得到了一些意想不到的结果,它们是什么,它们应该是什么。什么是 GL&P 对象。在您的问题中抛出大量特定于任务的术语会使您的问题感到困惑。例如,gcode 文件是否相关,它的用途是什么。

标签: vb.net opentk


【解决方案1】:

这是我设置视角和绘图系统的代码:

Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Imports System.Drawing

Public Class Form1

Private Sub GlControl1_Paint(sender As Object, e As PaintEventArgs) Handles GlControl1.Paint

    GL.Clear(ClearBufferMask.ColorBufferBit)
    GL.Clear(ClearBufferMask.DepthBufferBit)


    Vista.Proporzioni = GlControl1.Width / GlControl1.Height

    Dim perspective As Matrix4
    perspective = Matrix4.CreateOrthographicOffCenter(Vista.Xmin, Vista.Xmin + Vista.Size, Vista.Ymin, Vista.Ymin + (Vista.Size / Vista.Proporzioni), Vista.ProfMin, Vista.ProfMax)


    'Perspective
    GL.MatrixMode(MatrixMode.Projection) 'load the camera
    GL.LoadIdentity()
    GL.LoadMatrix(perspective)


    GL.Viewport(0, 0, GlControl1.Width, GlControl1.Height)
    GL.Enable(EnableCap.DepthTest)
    GL.DepthFunc(DepthFunction.Less)


    GL.Rotate(Vista.AngoloX, New Vector3(1, 0, 0)) 'asse X
    GL.Rotate(0, New Vector3(0, 1, 0)) 'asse Y
    GL.Rotate(Vista.AngoloZ, New Vector3(0, 0, 1)) 'asse Z



    DisegnaOrg()
    DisegnaGriglia()
    DisegnaPezzi()


    'Finally..................
    GraphicsContext.CurrentContext.VSync = True ' to syncronize with the GPU ability
    GlControl1.SwapBuffers() 'takes from GL and put to control
    GlControl1.Invalidate()


End Sub

当我加载我的 stl 而不用代码进行转换时:

DisegnaSTL(p)   'Draw peace

DisegnaSTL 简单做:

    GL.Begin(BeginMode.Triangles)
    GL.Color3(Color.Silver)
    GL.Vertex3(_p1.X, _p1.Y, _p1.Z)
    GL.Vertex3(_p2.X, _p2.Y, _p2.Z)
    GL.Vertex3(_p3.X, _p3.Y, _p3.Z)
    GL.End()

对于stl的每个三角形

系统负载如下: loading

现在对我来说,必须先在 X 轴上旋转,然后在 Z 轴上旋转,然后在最终位置上移动和平,例如 X 轴 45°,Z 轴 90°,然后在 x=20 y=30 z=10 上移动

如果我像这样只应用 X 旋转:

GL.PushMatrix()                                 'Fix actually situation
GL.Rotate(-45, 1, 0, 0)                     'Rotation X
DisegnaSTL(p)   'Draw peace
GL.PopMatrix()                                  'return matrix to past state

结果正确: only x rotation

如果我像这样只应用 Z 旋转:

GL.PushMatrix()                                 'Fix actually situation
GL.Rotate(90, 0, 0, 1)                     'Rotation Z
DisegnaSTL(p)   'Draw peace
GL.PopMatrix()                                  'return matrix to past state

结果正确: only z rotation

如果我像这样应用 X 和 Z 旋转:

GL.PushMatrix()                                 'Fix actually situation
GL.Rotate(-45, 1, 0, 0)                     'Rotation X
GL.Rotate(90, 0, 0, 1)                     'Rotation Z
DisegnaSTL(p)   'Draw peace
GL.PopMatrix()                                  'return matrix to past state

结果不正确: xz rotation

然后,如果我也应用不正确的翻译,我尝试使用此代码在 Y 方向仅应用 10:

        GL.PushMatrix()                                 'Fix actually situation
        GL.Rotate(-45, 1, 0, 0)                     'Rotation X
        GL.Rotate(90, 0, 0, 1)                     'Rotation Z
        GL.Translate(-0, 10, -0)               'Translation 
        DisegnaSTL(p)   'Draw peace
        GL.PopMatrix()                                  'return matrix to past state

结果是: final

问题是,在 X 轴第一次旋转后,Z 轴也沿 X 方向旋转,我必须不是在新的正常 Z 上旋转,而是在标准 Z 上旋转,当我平移时,我必须在最终平面上非正常地在标准轴上移动

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    相关资源
    最近更新 更多