pyopengl中的转换顺序是如何工作的?

2024-05-14 06:13:24 发布

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

所以,我正在为一些机器人做模拟,但是旋转变换没有像我预期的那样变换

在这段代码中,每个部分都有其他部分的父级,例如第3部分是第2部分的父级,第2部分是第1部分的父级,第1部分的父级id是-1。这意味着如果第1部分做了任何转换,另一部分也会转换第2部分和第3部分,例如如果第2部分是转换,第1部分将不被转换,但第3部分将被转换-1是为人父母的结束,所以id为-1的部分没有任何父母(基)

为此,我首先转换i部分,然后如果i部分有父级(c.part[0][pe].parent=-1) ,其中pe是转换部件的人,i部件将继续由父部件的父部件转换,直到父部件=-1(递归)

def transformation_0(no_servo):
    glTranslate(c.part[0] [no_servo].coordinate_of_servo_axis.elemen[0][0]+tx/20, c.part[0][no_servo].coordinate_of_servo_axis.elemen[1][0]+ty/20, c.part[0][no_servo].coordinate_of_servo_axis.elemen[2][0]-zpos)
    if(no_servo==0 or no_servo==3 or no_servo==4 or no_servo==9 or no_servo==10 or no_servo==16 or no_servo==17 or no_servo==20 or no_servo==21 or no_servo==22 or no_servo==23):
        glRotatef(servo_rotation[no_servo],1,0,0)
    elif(no_servo==2 or no_servo==7 or no_servo==8 or no_servo==13 or no_servo==14 or no_servo==15):
        glRotatef(servo_rotation[no_servo],0,1,0)
    elif(no_servo==1 or no_servo==5 or no_servo==6 or no_servo==11 or no_servo==12 or no_servo==18 or no_servo==19 or no_servo==24 or no_servo==25 or no_servo==26):
        glRotatef(servo_rotation[no_servo],0,0,1)
    glTranslate(-c.part[0][no_servo].coordinate_of_servo_axis.elemen[0][0]-tx/20, -c.part[0][no_servo].coordinate_of_servo_axis.elemen[1][0]-ty/20, -c.part[0][no_servo].coordinate_of_servo_axis.elemen[2][0]+zpos)

def transformation_1(pe): #pe is the id that transform the 3d model
    glTranslate(c.part[0][pe].coordinate_of_servo_axis.elemen[0][0]+tx/20, c.part[0][pe].coordinate_of_servo_axis.elemen[1][0]+ty/20, c.part[0][pe].coordinate_of_servo_axis.elemen[2][0]-zpos)
    if(pe==0 or pe==3 or pe==4 or pe==9 or pe==10 or pe==16 or pe==17 or pe==20 or pe==21 or pe==22 or pe==23):
    glRotatef(servo_rotation[pe],1,0,0)
    elif(pe==2 or pe==7 or pe==8 or pe==13 or pe==14 or pe==15):
    glRotatef(servo_rotation[pe],0,1,0)
    elif(pe==1 or pe==5 or pe==6 or pe==11 or pe==12 or pe==18 or pe==19 or pe==24 or pe==25 or pe==26):
        glRotatef(servo_rotation[pe],0,0,1)
    glTranslate(-c.part[0][pe].coordinate_of_servo_axis.elemen[0][0]-tx/20, -c.part[0][pe].coordinate_of_servo_axis.elemen[1][0]-ty/20, -c.part[0][pe].coordinate_of_servo_axis.elemen[2][0]+zpos)
    if(c.part[0][pe].parent!=-1):
        transformation_1(c.part[0][pe].parent)

def transformation():
    for i in range(27):
        glLoadIdentity()
        transformation_0(i)
        if(c.part[0][i].parent!=-1):
            transformation_1(c.part[0][i].parent)
        glTranslate(tx/20., ty/20., - zpos)
        glCallList(part[i].gl_list)

我以为转变会变成这样 https://imgur.com/YpPAwYc

但是我得到了这个 https://imgur.com/V9mkvfP

所以,我想做的是 https://imgur.com/TeTuWtD

[编辑]哦,是的,我忘了提,上面的部分是底部的父部分

有人能帮我解释一下到底是怎么回事吗


Tags: orofnocoordinate部件parentpepart