glDrawArrays only updates when i exit

♀尐吖头ヾ 提交于 2021-02-05 08:29:50

问题


I have this code in python3 which doesn't work on windows machines but it worked on a linux machine. I draw a green screen and a red triangle but the red triangle only appears when I exit.

import pygame
import numpy

import OpenGL.GL as gl
import OpenGL.GL.shaders as shaders
from pygame.rect import Rect

RED = (255, 0, 0)
WHITE = (255, 255, 255)
pygame.init()

screen = pygame.display.set_mode((800, 600), pygame.OPENGL)

vertes_shader = """
#version 330
in vec4 position;

void main()
{
    gl_Position = position;
}
"""

fragment_shader = """
#version 330
void main()
{
    gl_FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}
"""

shader = shaders.compileProgram(
    shaders.compileShader(vertes_shader, gl.GL_VERTEX_SHADER),
    shaders.compileShader(fragment_shader, gl.GL_FRAGMENT_SHADER)
)

vertes_data = numpy.array([
    0.0, 0.5, 0.0,
    0.5, -0.5, 0.0,
    -0.5, -0.5, 0.0
], dtype=numpy.float32)

# vrtex buffer object

vertex_buffer_object = gl.glGenBuffers(1)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vertex_buffer_object)
gl.glBufferData(gl.GL_ARRAY_BUFFER, 36, vertes_data, gl.GL_STATIC_DRAW)

# vertex array object

vertex_array_object = gl.glGenVertexArrays(1)
gl.glBindVertexArray(vertex_array_object)

# shaders

position = gl.glGetAttribLocation(shader, 'position')
gl.glEnableVertexAttribArray(position)
gl.glVertexAttribPointer(position, 3, gl.GL_FLOAT, False, 0, None)


done = False
gl.glClearColor(0.5, 1.0, 0.5, 1.0)


while not done:
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
            done = True

    gl.glClear(gl.GL_COLOR_BUFFER_BIT)

    gl.glUseProgram(shader)

    gl.glDrawArrays(gl.GL_TRIANGLES, 0, 3)

    pygame.display.flip()

Any ideas as to what could be happening?

Ive tried using display.update, disabling textures, drawing outside the loop, removing the for, using pygame.time.wait, drawing lines instead of triangle.


回答1:


I draw a green screen and a red triangle but the red triangle only appears when i exit.

If the display mode is pygame.OPENGL, pygame.display.flip() performs a GL buffer swap.

You need to enable double buffering by setting the pygame.DOUBLEBUF display mode flag (see pygame.display.set_mode()):

screen = pygame.display.set_mode((800, 600), pygame.OPENGL | pygame.DOUBLEBUF)

If you do not want to use double buffering, you must force the execution of GL commands manually with glFlush():

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # [...]

    gl.glFlush() # force the execution of GL commands 


来源:https://stackoverflow.com/questions/49957653/gldrawarrays-only-updates-when-i-exit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!