简体   繁体   中英

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). When the key "up" is pressed, the matrices "T" and "P" are updated. 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.

@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 is a state engine, states are kept until they are changed again, even beyond frames. Hence in your application the current matrix is progressively scaled and translated.

Load the Identity matrix at the begin of 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. See pyglet.window . If you want a different projection, it can be set by 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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