如何在opengl(python)中旋转三角形而不是三角形轴?

2024-09-27 21:22:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在旋转一个三角形,前提是它不接触渲染部分(我认为这个问题不能用相机解决) 我希望在上述状态下旋转的轴是中心(基于世界坐标(0,0))而不是局部空间。当三角形不在0,0时

这是我的密码

import glfw
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np

def render(T):
     glClear(GL_COLOR_BUFFER_BIT)
     glLoadIdentity()

 # draw cooridnate
     glBegin(GL_LINES)
     glColor3ub(255, 0, 0)
     glVertex2fv(np.array([0.,0.]))
     glVertex2fv(np.array([1.,0.]))
     glColor3ub(0, 255, 0)
     glVertex2fv(np.array([0.,0.]))
     glVertex2fv(np.array([0.,1.]))
     glEnd()

 # draw triangle
     glBegin(GL_TRIANGLES)
     glColor3ub(255, 255, 255)
     glVertex2fv( (T @ np.array([.0,.5,1.]))[:-1] )
     glVertex2fv( (T @ np.array([.0,.0,1.]))[:-1] )
     glVertex2fv( (T @ np.array([.5,.0,1.]))[:-1] )
     glEnd()
        
def main():
    if not glfw.init():
        return
    window = glfw.create_window(480,480,"1234", None,None)
    if not window:
        glfw.terminate()
        return
    glfw.make_context_current(window)
    glfw.swap_interval(1)


    
    while not glfw.window_should_close(window):
        glfw.poll_events()
        
        t = glfw.get_time()
        s= np.sin(t)
        
        q = np.array([[np.cos(t),-np.sin(t),.3],
                      [np.sin(t), np.cos(t),.3],
                      [0., 0., 0.]])
        #th = np.radians(60)
        #R = np.array([[np.cos(th), -np.sin(th),0.],
        #  [np.sin(th), np.cos(th),0.],
        #  [0., 0., 1.]])
        #T = np.array([[1.,0.,.4],
        #  [0.,1.,.1],
        #  [0.,0.,1.]])

        render(q)
        glfw.swap_buffers(window)
    glfw.terminate()

if __name__ == "__main__":
    main()

enter image description here

->;这是我的当前状态,但不是世界空间(0,0)


Tags: importifmainnpnotsincoswindow
1条回答
网友
1楼 · 发布于 2024-09-27 21:22:57

如果要围绕轴心点旋转和移动对象,请执行以下操作:

  1. 移动对象,使轴点位于(0,0)
  2. 旋转对象
  3. 将对象移动到其在世界中的位置
def main():
    # [...]

    while not glfw.window_should_close(window):
        glfw.poll_events()
        
        t = glfw.get_time()
        pivot = (0.15, 0.15)
        world_pos = (0.5, 0.5)

        trans_pivot = np.array([[1, 0, -pivot[0]], [0, 1, -pivot[1]], [0, 0, 1]])
        rotate = np.array([[np.cos(t),-np.sin(t), 0.0], 
                            [np.sin(t), np.cos(t), 0.0],
                            [0, 0, 1]])
        trans_world = np.array([[1, 0, world_pos[0]], [0, 1, world_pos[0]], [0, 0, 1]])

        q = trans_world @ rotate @ trans_pivot

        render(q)
        
        glfw.swap_buffers(window)

相关问题 更多 >

    热门问题