PyOpenGL 上的奇怪 "X" + PyGame
Strange "X" on PyOpenGL + PyGame
我正在尝试创建一个 PyOpenGL“Hello World”项目。我编写了一些代码,为我生成一个立方体并将其显示在屏幕上,问题是它不显示立方体,它显示一个奇怪的平面二维“X”。我几乎可以肯定问题出在我的 PyGame 设置上,因为我试图显示一条简单的线,但实际上什么也没有显示。
我不知道这是否有用,但我的操作系统是 Linux - Pop!_os.
完整代码: https://github.com/caastilho/PyOpenGL-Problem
LAUNCHER.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
所有绘图函数都存储在这里
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
永远不会设置透视投影和模型视图矩阵,因为您在 self.__setupSurface()
.
之前调用函数 setup
请注意,对于任何 OpenGL 指令,一个有效且当前的 OpenGL Context is requried, else the instruction has no effect. The OpenGL Context is created when the pygame display surface is generated (display.set_mode()
)。因此 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()
我正在尝试创建一个 PyOpenGL“Hello World”项目。我编写了一些代码,为我生成一个立方体并将其显示在屏幕上,问题是它不显示立方体,它显示一个奇怪的平面二维“X”。我几乎可以肯定问题出在我的 PyGame 设置上,因为我试图显示一条简单的线,但实际上什么也没有显示。
我不知道这是否有用,但我的操作系统是 Linux - Pop!_os.
完整代码: https://github.com/caastilho/PyOpenGL-Problem
LAUNCHER.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
所有绘图函数都存储在这里
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
永远不会设置透视投影和模型视图矩阵,因为您在 self.__setupSurface()
.
之前调用函数 setup
请注意,对于任何 OpenGL 指令,一个有效且当前的 OpenGL Context is requried, else the instruction has no effect. The OpenGL Context is created when the pygame display surface is generated (display.set_mode()
)。因此 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()