【问题标题】:I can't make working keyCallback function in OpenGL and glfw我无法在 OpenGL 和 glfw 中使用 keyCallback 函数
【发布时间】:2020-07-20 10:31:45
【问题描述】:
import glfw
from OpenGL.GL import *
import numpy as np

currentMode = GL_LINE_LOOP

def render(currentMode):


def key_callback(window, key, scancode, action, mods):
    if key==glfw.KEY_1:
        if action==glfw.PRESS:
            currentMode = GL_POINTS
    elif key==glfw.KEY_2:
        if action==glfw.PRESS:
            currentMode = GL_LINES
    elif key==glfw.KEY_3:
        if action==glfw.PRESS:
            currentMode = GL_LINE_STRIP

while not glfw.window_should_close(window):
    glfw.poll_events()
    render(currentMode)
    glfw.swap_buffers(window)
    print(currentMode)
glfw.terminate()

我尝试更改原始类型以使用渲染函数的参数。 但是,它不起作用。 我该怎么办?

【问题讨论】:

  • 我减少了一小部分代码。但核心就是这样。

标签: opengl glfw pyopengl


【解决方案1】:

你错过了key_callback中的global statement

def key_callback(window, key, scancode, action, mods):
    global currentMode # <--- THIS IS MISSING
   
    if key==glfw.KEY_1:
        if action==glfw.PRESS:
            currentMode = GL_POINTS
    # [...]

变量currentMode 存在两次。它是全局命名空间中的变量,也是函数key_callback 范围内的局部变量。使用global 语句将currentMode 解释为全局变量,并将key_callback 写入全局变量currentMode

当然,main 函数也是如此:

def main():
    global currentMode
    # [...]

此外,您必须清除 render 中的帧缓冲区:

def render(currentMode):
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

看例子:

import glfw
from OpenGL.GL import *
import numpy as np

def render(currentMode):
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    
    print(currentMode)
    glBegin(currentMode)
    #Draw Something
    glVertex2f(-0.5, -0.5)
    glVertex2f(0.5, 0.5)
    glVertex2f(-0.5, 0.5)
    glEnd()

def key_callback(window, key, scancode, action, mods):
    global currentMode
    if key==glfw.KEY_1:
        if action==glfw.PRESS:
            currentMode = GL_POINTS
    elif key==glfw.KEY_2:
        if action==glfw.PRESS:
            currentMode = GL_LINES
    elif key==glfw.KEY_3:
        if action==glfw.PRESS:
            currentMode = GL_LINE_STRIP
    
def main():
    global currentMode

    if not glfw.init():
        return
    window = glfw.create_window(480, 480, "Test", None, None)
    
    if not window:
        glfw.terminate()
        return
    glfw.make_context_current(window)
    glfw.set_key_callback(window, key_callback)
    glfw.swap_interval(1)    

    while not glfw.window_should_close(window):
        glfw.poll_events()
        render(currentMode)
        glfw.swap_buffers(window)
    glfw.terminate()
        
if __name__ == "__main__":
    currentMode = GL_LINE_LOOP
    main()

【讨论】:

  • 谢谢,但重新渲染没有改变。我在循环中制作 print(currentMode) 并检查了这个参数的变化......但是在屏幕上渲染并没有改变。我不知道为什么。我在 main() 之前设置了 currentMode = GL_LINE_LOOP 的初始状态,我应该更改代码“currentMode = GL_LINE_LOOP”的位置
  • def render(currentMode): #glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glBegin(currentMode) #Draw 12 vertice glEnd()
  • community.khronos.org/t/rendering-doesnt-work-properly/105577完整代码,stackoverflow没有办法上传完整代码
  • @MintStout 我已经扩展了答案。
  • 非常感谢您的帮助。它确实有效!如果你不介意,你能告诉我为什么它不起作用吗? (解释更多关于“全球空间”)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
相关资源
最近更新 更多