簡體   English   中英

如何在opengl(python)中旋轉三角形,而不是三角形軸?

[英]how to rotating triangle, not Triangular axis in opengl(python)?

我正在旋轉一個三角形,前提是它不接觸渲染部分(我認為這個問題不能使用相機)我希望在上述 state 中旋轉的軸為中心(基於世界坐標( 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()

在此處輸入圖像描述

-> 這是我當前的 state 旋轉,但不是世界空間 (0,0)

如果要圍繞 pivot 點旋轉 object:

  1. 移動 object,使 pivot 點位於 (0, 0)。
  2. 旋轉 object
  3. 將 object 移至其 position 在世界范圍內。
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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM