簡體   English   中英

使用 PyImgui/PyOpenGL/GLFW 堆棧渲染三角形

[英]Rendering Triangle using PyImgui/PyOpenGL/GLFW stack

我是中間 GUI 框架世界和 OpenGL 的新手。我試圖讓這個例子工作:

# -*- coding: utf-8 -*-
import glfw
import OpenGL.GL as gl
import numpy as np

import imgui
from imgui.integrations.glfw import GlfwRenderer

def main():
    imgui.create_context()
    window = impl_glfw_init()
    impl = GlfwRenderer(window)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()

        imgui.new_frame()

        if imgui.begin_main_menu_bar():
            if imgui.begin_menu("File", True):

                clicked_quit, selected_quit = imgui.menu_item(
                    "Quit", 'Cmd+Q', False, True
                )

                if clicked_quit:
                    exit(1)

                imgui.end_menu()
            imgui.end_main_menu_bar()

        imgui.render()

        # show_test_window()
        # imgui.show_test_window()
        
        try:
            gl.glViewport(0, 0, int(impl.io.display_size.x), int(impl.io.display_size.y))
            gl.glClearColor(0., 0., 0., 1)
            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)

            gl.glBegin(gl.GL_TRIANGLES)
            gl.glVertex3f(-0.5, -0.5, 0.0)
            gl.glVertex3f(0.5, -0.5, 0.0)
            gl.glVertex3f(0.0, 0.5, 0.0)
            gl.glEnd()

        except gl.GLError as glex:
            print(f"{glex.err}: {glex.result}")

        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()


def impl_glfw_init():
    width, height = 1280, 720
    window_name = "minimal ImGui/GLFW3 example"

    if not glfw.init():
        print("Could not initialize OpenGL context")
        exit(1)

    # OS X supports only forward-compatible core profiles from 3.2
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

    glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)

    # Create a windowed mode window and its OpenGL context
    window = glfw.create_window(
        int(width), int(height), window_name, None, None
    )
    glfw.make_context_current(window)

    if not window:
        glfw.terminate()
        print("Could not initialize Window")
        exit(1)

    return window


if __name__ == "__main__":
    main()

雖然我繼續收到此錯誤:

錯誤

我確定我錯過了一些東西,盡管我似乎找不到給我提示的神奇文檔。 我可能會嘗試在我應該使用現代方法的地方使用舊版 OpenGL,或者在錯誤的位置添加我的渲染代碼。

任何提示將不勝感激。

我嘗試了一個使用緩沖區和着色器的現代 OpenGL 示例。 沒有成功。

您正在請求 OpenGL 3.3 核心配置文件:

    # OS X supports only forward-compatible core profiles from 3.2
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

    glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)

從這里復制的一些文檔: https://www.glfw.org/docs/3.3/window_guide.html#GLFW_OPENGL_FORWARD_COMPAT_hint

GLFW_OPENGL_FORWARD_COMPAT specifies whether the OpenGL context should be 
forward-compatible, i.e. one where all functionality deprecated in the 
requested version of OpenGL is removed. This must only be used if the 
requested OpenGL version is 3.0 or above. If OpenGL ES is requested, this 
hint is ignored.

老實說,我從未使用過 glfw,但我會看看注釋掉這些行是否可以解決問題? (這應該啟用所有舊版 OpenGL,例如 glBegin/glEnd)。

不過,理想情況下,您希望保留那些 window 提示,刪除 glBegin/glVertex/glEnd,並切換到使用 glGenBuffers / glBindBuffer / glBufferData 和 glDrawArrays。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM