简体   繁体   中英

Draw 2D OpenGL points with a delay in python

Suppose I want to draw 3 points in my window. One point at a time with 1 second delay each. So I want the window to open when I run the code, then wait 1 second and draw first point and then wait another second and write the second point in the same window.

But what is happening is that, when I run the code, it shows nothing and then shows all three points at once after 3 seconds have passed.

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


def draw_points(x0,y0):
    glPointSize(5) 
    glBegin(GL_POINTS)
    glVertex2f(x0,y0)
    glEnd()


def iterate():
    glViewport(0, 0, 1000, 1000)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0.0, 1000, 0.0, 1000, 0.0, 1.0)
    glMatrixMode (GL_MODELVIEW)
    glLoadIdentity()

def showScreen():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    iterate()
    glColor3f(255, 255, 255)
    draw_points(200,200)
    time.sleep(1)
    draw_points(300,300)
    time.sleep(1)
    draw_points(400,400)

    glutSwapBuffers()




glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(1000, 1000)
glutInitWindowPosition(0, 0)
wind = glutCreateWindow(b"") 
glutDisplayFunc(showScreen)

glutMainLoop()

Don't wait in the application loop. The application executed continuously and the scene is redrawn in every frame. Measure the time and draw the points depending on the time elapsed. You can get the elapsed time in milli seconds with glutGet(GLUT_ELAPSED_TIME) :

def showScreen():
    elapesed_ms = glutGet(GLUT_ELAPSED_TIME) 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    iterate()
    glColor3f(255, 255, 255)
    
    draw_points(200,200)
    if elapesed_ms > 1000:
        draw_points(300,300)
    if elapesed_ms > 2000:
        draw_points(400,400)

    glutSwapBuffers()

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