简体   繁体   English

Pyglet on_draw 不会绘制更新的 function

[英]Pyglet on_draw wont draw updated function

The crtaj() function (the main drawing function) takes two global matrices, "T" and "P", applies transformations on the global vertices "vrhovi" using the two matrices and stores the transformed vertices into "novivrhovi", and then draws the polygon using the transformed vertices "novivrhovi" and global "poligoni" (describes the connections of vertices). crtaj() function(主要绘图函数)采用两个全局矩阵“T”和“P”,使用这两个矩阵对全局顶点“vrhovi”进行变换,并将变换后的顶点存储到“novivrhovi”中,然后绘制使用变换顶点“novivrhovi”和全局“poligoni”的多边形(描述顶点的连接)。 When the key "up" is pressed, the matrices "T" and "P" are updated.当按下“向上”键时,矩阵“T”和“P”被更新。 What i want, is to draw on every one of these updates, but after the initial draw the screen goes blank after first "up" is pressed.我想要的是绘制这些更新中的每一个,但是在初始绘制之后,在按下第一个“向上”后屏幕变为空白。 I am 100% certain my transformations are okay, because pressing the key up once gives matrices for which i have tried to set to be initial, and it draws it correctly so no reason not to draw correctly when using default initial and pressing up, because the result is the same and the function even prints correct vertices every time I press "up" but just doesn't show anything.我 100% 确定我的转换是可以的,因为按下一次键会给出我试图设置为初始值的矩阵,并且它会正确绘制它,所以在使用默认初始值并按下时没有理由不正确绘制,因为结果是相同的,function 甚至在我每次按“向上”时都会打印正确的顶点,但没有显示任何内容。

@window.event
def on_draw():
    
    glScalef(150,150,150)
    glTranslatef(sum(xkord)/2,sum(ykord)/2,0)
    
    window.clear()
    crtaj()
    
    
@window.event
def on_key_press(key, modifiers):
    global vrhovi
    
    if (key == pyglet.window.key.UP):
        ociste[0][0]=ociste[0][0]+1
        transform()
        on_draw()
        
#main draw
def crtaj():
    
    
    glBegin(GL_LINE_LOOP)
    global T
    global P
    global vrhovi
    global poligoni
    
    #calculate new vertices of polygon-->novivrhovi
    
    novivrhovi = []

    for vrh in vrhovi:
        novivrh = vrh.copy()
        novivrh.append(1)
        novivrh = np.matrix(novivrh)
        novivrh = novivrh.dot(T)
        novivrh = novivrh.dot(P)
        novivrh = novivrh.tolist()
        novivrhovi.append(novivrh[0])
    print("N O V I V R H O V I")
    print(novivrhovi)

    #draw the poligon
    for poligon in poligoni:
        
        index0 = poligon[0]-1
        index1 = poligon[1]-1
        index2 = poligon[2]-1
        
        glVertex4f(novivrhovi[index0][0],novivrhovi[index0][1],novivrhovi[index0][2],novivrhovi[index0][3])
        glVertex4f(novivrhovi[index1][0],novivrhovi[index1][1],novivrhovi[index1][2],novivrhovi[index1][3])
        glVertex4f(novivrhovi[index2][0],novivrhovi[index2][1],novivrhovi[index2][2],novivrhovi[index2][3])
    glEnd()

pyglet.app.run()

but after the initial draw the screen goes blank after first "up" is pressed.但在初始绘制后,第一次按下“向上”后屏幕变为空白。

The Legacy OpenGL matrix operations like glScalef and glTranslatef do not just set a matrix, they define a new matrix and multiply the current matrix by the new matrix.传统的 OpenGL矩阵运算(如glScalefglTranslatef不仅仅设置矩阵,它们定义一个新矩阵并将当前矩阵乘以新矩阵。
OpenGL is a state engine, states are kept until they are changed again, even beyond frames. OpenGL 是一个 state 引擎,状态会一直保留,直到它们再次更改,甚至超出帧。 Hence in your application the current matrix is progressively scaled and translated.因此,在您的应用程序中,当前矩阵会逐渐缩放和平移。

Load the Identity matrix at the begin of on_draw :on_draw的开头加载身份矩阵

@window.event
def on_draw():

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glScalef(150,150,150)
    glTranslatef(sum(xkord)/2,sum(ykord)/2,0)

    window.clear()
    crtaj()

PyGlet sets by default an Orthographic projection projection matrix to window space. PyGlet 默认将正交投影投影矩阵设置为 window 空间。 See pyglet.window .请参阅pyglet.window If you want a different projection, it can be set by glOrtho :如果您想要不同的投影,可以通过glOrtho设置:

@window.event
def on_draw():

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0, window.width, window.height, 0, -1, 1)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glScalef(150,150,150)
    glTranslatef(sum(xkord)/2,sum(ykord)/2,0)

    window.clear()
    crtaj()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM