Question

I'm new to openGL.I wonder why in function Mutate GetPixelDifference() is always called before glutPostRedisplay().And how can I fix it.

import sys
import time
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *


def GetPixelDifference():
    print("calculate")


def Draw():
    print("draw")
    glClear(GL_COLOR_BUFFER_BIT)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glEnable(GL_BLEND)
    glColor4f(1.0, 1.0, 1.0, 0.4)
    glPolygonMode(GL_FRONT, GL_FILL)
    glBegin(GL_POLYGON)
    glVertex2f(1,1)
    glVertex2f(-1,1)
    glVertex2f(0,0)
    glEnd()
    glFlush()
    GetPixelDifference()


def Mutate():
    print("mutate")
    glutPostRedisplay()  
    #time.sleep(5)
    print("=======")
    print("=wrong=")
    print("=======")
    GetPixelDifference() 


glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(300, 300)
glutCreateWindow(b"My clock")
glutDisplayFunc(Draw)
print("main")
glutIdleFunc(Mutate)
glutMainLoop()

'''   
And this is what i get

main
draw
calculate
mutate
=======
=wrong=
=======
calculate
draw
calculate
draw
'''

calculate is called before draw.But I want call calculate when drawing is finished,cause this calculate is for all the pixels.

Was it helpful?

Solution

glutPostRedisplay merely sets a flag, that in the next iteration of the event loop as the final operation the display callback shall be called. I.e. it doesn't take immediate action.

This is the documented behavior, see: https://www.opengl.org/documentation/specs/glut/spec3/node20.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top