简体   繁体   English

在 python 中延迟绘制 2D OpenGL 点

[英]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.假设我想在我的 window 中绘制 3 个点。一次一个点,每个点延迟 1 秒。 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.所以我希望 window 在我运行代码时打开,然后等待 1 秒绘制第一个点,然后再等一秒并在同一个 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.但是发生的事情是,当我运行代码时,它什么也没显示,然后在 3 秒后一次显示所有三个点。

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) :您可以使用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()

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

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