gpt4 book ai didi

python - PyOpenGL + PyGame 上的奇怪 "X"

转载 作者:行者123 更新时间:2023-12-03 09:50:58 26 4
gpt4 key购买 nike

我正在尝试创建一个 PyOpenGL “ Hello World ”项目。我编写了一些代码,为我生成一个立方体并将其显示在屏幕上,问题是它没有显示立方体,而是显示了一个奇怪的平面 2D“X”。我几乎可以肯定问题出在我的 PyGame 设置上,因为我试图显示一条简单的行,但实际上,什么也没显示。
我不知道这是不是有用的信息,但我的操作系统是 Linux - 流行!_os .
完整代码: https://github.com/caastilho/PyOpenGL-Problem

启动器.PY
所有 PyGame 显示设置都存储在这里

import pygame
from pygame import event as events
from pygame import display

import sys, os
from screeninfo import get_monitors
from canvas import setup, draw


# Root class: "Launcher"
class Launcher:

def __init__(self):
pygame.init()

# Create default settings
self.title = "PyGame Screen"
self.size = (500, 500)
self.flags = 0

setup(self)
self.__setupSurface()

# Run screen
self.__runSurface()


# Setup environment
def __setupSurface(self):
"""Setup environment."""

# Get the main monitor dimensions
for monitor in get_monitors():
if monitor.x == monitor.y == 0:
middle_x = (monitor.width - self.size[0]) // 2
middle_y = (monitor.height - self.size[1]) // 2

offset = f"{middle_x},{middle_y}"

# Setup window
os.environ["SDL_VIDEO_WINDOW_POS"] = offset
display.set_caption(self.title)
self.surface = display.set_mode(self.size, self.flags)


# Run environment
def __runSurface(self):
"""Run environment."""

while True:
draw(self)
pygame.time.wait(10)

# Close condition
for event in events.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()


# Run launcher
if __name__ == "__main__":
Launcher()

Canvas .PY
所有的draw函数都存储在这里
from pygame import display
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

from Shapes.shape import OpenGL_Cube # Imports the cube class


cube = None
clear_flags = None

# Setup canvas
def setup(app):
global cube, clear_flags

app.size = (1280, 720) # Setup window size
app.title = "PyOpenGl" # Setup window title
app.flags = DOUBLEBUF|OPENGL # Setup PyGame flags

# Setup OpenGL environment
clear_flags = GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT
gluPerspective(45, (app.size[0]/app.size[1]), 0.1, 50)
glTranslatef(0, 0, -5)
glRotatef(0, 0, 0, 0)

# Setup objects
cube = OpenGL_Cube()


# Run canvas
def draw(app):
cube.draw()

display.flip()
glClear(clear_flags)

输出


编辑
只是为了澄清,我用来显示立方体边缘的代码行是这样的:
glBegin(GL_LINES)
for node in nodes: # Where node = (x, y, z)
glVertex3fv(node)
glEnd

最佳答案

透视投影和模型 View 矩阵从未设置,因为您调用了函数 setup之前 self.__setupSurface() .
请注意,对于任何 OpenGL 指令,一个有效且当前的 OpenGL Context是必需的,否则指令无效。生成 pygame 显示表面 ( display.set_mode() ) 时会创建 OpenGL 上下文。因此 setup必须在 self.__setupSurface() 之后调用:

class Launcher:
# [...]

def __init__(self):
pygame.init()

# [...]

self.size = (1280, 720)
self.title = "Marching Cubes"
self.flags = DOUBLEBUF|OPENGL

# create OpenGL window (and make OpenGL context current)
self.__setupSurface()

# setup projection and view
setup(self)
# Setup canvas
def setup(app):
global cube, clear_flags

# Setup OpenGL environment
clear_flags = GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT
gluPerspective(45, (app.size[0]/app.size[1]), 0.1, 50)
glTranslatef(0, 10, -5)
glRotatef(0, 0, 0, 0)

# Setup objects
cube = OpenGL_Cube()

关于python - PyOpenGL + PyGame 上的奇怪 "X",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63024240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com