大家好,当我尝试在 Visual Studio 终端中运行 Pygbag main.py 命令时,我目前遇到问题,问题是我编写的两个球体未在 Pygbag 中渲染,这是我的代码和我当前正在处理的屏幕截图,我正在调用 create objects 来初始化 main.py 第 32 行上的球体:
我已经尝试过诸如此类的 Stackoverflow 解决方案,但是,它们似乎不起作用:
主要.py:
from pygame import *
import pygame, asyncio
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import glfw
import math
pygame.init()
width, height = 1600, 800
display = (width, height)
window = glfw.create_window(width, height, "My OpenGL window", None, None)
scree = pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
glEnable(GL_DEPTH_TEST)
class createAllObjects:
def __init__(self, x1, x2, y1, y2, z1, z2, r1, g1, b1, r2, g2, b2):
self.x1 = x1
self.x2 = x2
self.y1 = y1
self.y2 = y2
self.z1 = z1
self.z2 = z2
self.r1 = r1
self.r2 = r2
self.g1 = g1
self.g2 = g2
self.b1 = b1
self.b2 = b2
self.mouse_x = 0
self.mouse_y = 0
#self.run_process()
data = self.read_data()
self.create_objects(x1=data[0], x2=data[6], y1=data[2], y2=data[4], z1=data[8], z2=data[10], r1=data[1], r2=data[3], g1=data[5], g2=data[7], b1=data[9], b2=data[11])
def Sphere(self, x, y, z, r, g, b):
sphere = gluNewQuadric()
glTranslatef(x, y, z)
glColor4f(r, g, b, 1)
gluSphere(sphere, 1.0, 32, 16)
async def create_objects(self, x1, x2, y1, y2, z1, z2, r1, g1, b1, r2, g2, b2):
self.object1 = self.Sphere(x1, y1, z1, r1, b1, g1)
self.object2 = self.Sphere(x2, y2, z2, r2, g2, b2)
async def get_mouse_press(self, x, y, button=None, modifiers=None):
self.a = (GLuint * 1)(0)
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_INT, self.a)
print(self.a[0])
async def read_data(self):
filename = 'test.txt'
with open(filename, 'r') as f:
data = f.read().replace('\n', '')
data = data.split(' ')
finalList = [ele for ele in data]
listLen = len(finalList)
new_dict = {}
for ele in range(listLen):
data = float(finalList[ele])
new_dict.update({int(ele):data})
f.close()
return new_dict
def run_process(self):
x_range = 0
y_range = 0
z_range = 0
x = 6
for i in range(x):
x_range += 1
y_range += 1
z_range += 1
print('please input a value for x, y, and z, thank you.')
output_pos = float(input())
print('please input the r, g, b color values, thank you.')
output_rgb = float(input())
filename = 'test.txt'
with open(filename, 'a+') as f:
f.write(str(output_pos)+' ')
f.write(str(output_rgb)+' '+'\n')
print(x_range, y_range, z_range)
if i == x:
print('Element found')
break
class runEngine():
def __init__(self):
pass
def convert_positions(self, mode):
if str(mode):
if mode == 'x'.lower():
x, y = self.mouse_pos
result = x
elif mode == 'y'.lower():
x, y = self.mouse_pos
result = y
return result
else:
raise Exception(mode+' is not a string, please enter a string.')
async def runAll(self):
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
gluLookAt(0, -8, 0, 0, 0, 0, 0, 0, 1)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glLoadIdentity()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN:
run = False
if event.type == pygame.MOUSEBUTTONUP:
self.mouse_pos = pygame.mouse.get_pos()
#print(self.mouse_pos)
x = self.convert_positions('x')
y = self.convert_positions('y')
colorPicker = self.mainEngine.get_mouse_press(x, y)
if self.mainEngine.a[0] != 0:
glMatrixMode(GL_MODELVIEW)
gluLookAt(0.1, 0.1, 0.1, 0, 0, 0, 0, 0, 1)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glLoadIdentity()
keypress = pygame.key.get_pressed()
mouse_clicked = pygame.event.get()
# init model view matrix
glLoadIdentity()
# init the view matrix
glPushMatrix()
glLoadIdentity()
# apply the movment
if keypress[pygame.K_w]:
glTranslatef(0,0,0.1)
if keypress[pygame.K_s]:
glTranslatef(0,0,-0.1)
if keypress[pygame.K_d]:
glTranslatef(-0.1,0,0)
if keypress[pygame.K_a]:
glTranslatef(0.1,0,0)
if keypress[pygame.K_UP]:
glRotatef(1, 0.1, 0, 0)
if keypress[pygame.K_DOWN]:
glRotatef(1, -0.1, 0, 0)
if keypress[pygame.K_LEFT]:
glRotatef(1, 0, 0.1, 0)
if keypress[pygame.K_RIGHT]:
glRotatef(1, 0, -0.1, 0)
if keypress[pygame.K_0]:
glMatrixMode(GL_MODELVIEW)
gluLookAt(0, -8, 0, 0, 0, 0, 0, 0, 1)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glLoadIdentity()
# multiply the current matrix by the get the new view matrix and store the final vie matrix
glMultMatrixf(viewMatrix)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
# apply view matrix
glPopMatrix()
glMultMatrixf(viewMatrix)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) #Clear the screen
glPushMatrix()
self.mainEngine = createAllObjects(-1.5, 6, 0, 0, 0, 0, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2)
asyncio.run(self.mainEngine.create_objects(self.mainEngine, createAllObjects.x1, createAllObjects.x2, createAllObjects.y1, createAllObjects.y2, createAllObjects.z1, createAllObjects.z2, createAllObjects.r1, createAllObjects.g1, createAllObjects.b1, createAllObjects.r2, createAllObjects.g2, createAllObjects.b2))
#Sphere(-1.5, 0, 0, 0.2, 0.5, 0.6)
#Sphere(6, 0, 0, 0.2, 0.5, 0.6)
glPopMatrix()
pygame.display.flip() #Update the screen
pygame.time.wait(10)
await asyncio.sleep(0)
pygame.quit()
class SimulationEngine:
def __init__(self):
init()
self.RES = self.WIDTH, self.HEIGHT = 1600, 900
self.H_WIDTH, self.H_HEIGHT = self.WIDTH // 2, self.HEIGHT // 2
self.FPS = 60
self.screen = display.set_mode(self.RES)
self.clock = time.Clock()
def draw(self):
self.screen.fill(Color('darkslategray'))
def run(self):
while True:
self.draw()
[exit() for i in event.get() if i.type == QUIT]
display.set_caption(str(self.clock.get_fps()))
display.flip()
self.clock.tick(self.FPS)
asyncio.run(runEngine().runAll())
app = SimulationEngine()
app.run()
app.run()
本期截图:
还有一个名为 test.txt 的文本文件,用于生成每个球体的位置和颜色,这样做是为了可以选取颜色并跟踪每个球体,该文本文件包含以下内容:-1.5, 0, 0, 6, 0 、0、0.2、0.2、0.2、0.2、0.2、0.2。选取颜色的函数名称是 getMousePress,位于第 41 行。
我该如何解决这个问题?我是否必须使用不同的显示类型,如果是的话,我必须使用什么显示类型?非常感谢您,任何帮助将不胜感激。
遗憾的是 pygame-ce 的 3D 部分尚未经过测试,并且可用的时间很少,因为它最初是一个 2D 库。
作为 pygbag 作者,我强烈建议您使用专门用于 3D 的软件包,例如 Panda3D 或 Harfang3D,它们都运行良好。