我正在扩展Pyglet / PyOpenGL教程,该教程称为“ DLC ENERGY的Pyglet OpenGL教程(https://youtu.be/Hqg4qePJV2U)。我几乎拥有所有必需的OpenGL代码,例如抗锯齿,雾化,灯光等。现在我想要可以使用PyCollada模块将3D模型加载到场景中。加载collada文件很容易,但是如何实际将其绘制到窗口中以便在应用程序中显示呢?
我是Python和PyOpenGL的初学者,所以请保持冷静。
我知道Assimp是将3D模型加载到Python和OpenGL的确定方式,但是PyCollada更加简单,我只需输入“ self.mesh = Collada('AcrylicSphere.dae ')”,我只是想真正看到游戏中的模型没有错误。
import sys
from collada import *
from pyglet.gl import *
from OpenGL.GLUT import *
from OpenGL.GL import *
from pyglet.window import key
import math
from Player import Player
from Model import Model
class ColladaImport():
def __init__(self):
self.mesh = Collada('AcrylicSphere.dae')
def draw(self):
self.mesh.draw()
class Window(pyglet.window.Window):
def push(self,pos,rot): glPushMatrix(); rot = self.player.rot; glRotatef(-rot[0],1,0,0) ;glRotatef(-rot[1],0,1,0); glTranslatef(-pos[0],-pos[1],-pos[2],)
def Projection(self): glMatrixMode(GL_PROJECTION); glLoadIdentity()
def Model(self): glMatrixMode(GL_MODELVIEW); glLoadIdentity()
def set2d(self): mself.Projection(); gluOrtho(0,self.width/self.height); self.Model()
def set3d(self): self.Projection(); gluPerspective(70,self.width/self.height,0.05,1000); self.Model()
def setLock(self,state): self.lock = state; self.set_exclusive_mouse(state)
lock = False; mouse_lock = property(lambda self:self.lock,setLock)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_minimum_size(300,200)
self.keys = key.KeyStateHandler()
self.push_handlers(self.keys)
pyglet.clock.schedule(self.update)
self.model = Model()
self.player = Player((0.5,1.5,1.5),(-30,0))
self.test = ColladaImport()
def on_mouse_motion(self,x,y,dx,dy):
if self.mouse_lock: self.player.mouse_motion(dx,dy)
def on_key_press(self,KEY,MOD):
if KEY == key.ESCAPE: self.close()
elif KEY == key.E: self.mouse_lock = not self.mouse_lock
def update(self,dt):
self.player.update(dt,self.keys)
def on_draw(self):
self.clear()
self.set3d()
self.push(self.player.pos,self.player.rot)
self.model.draw()
self.test.draw()
glPopMatrix()
if __name__ == '__main__':
window = Window(width=854,height=480,caption='Awesome Game',resizable=True)
glClearColor(0.5,0.7,1,1)
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
glShadeModel (GL_SMOOTH)
#lighting (light #1)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_NORMALIZE)
light_position = [1.0, 1.0, 5.0, 0]
glLight(GL_LIGHT0, GL_POSITION, light_position)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
glMaterialfv(GL_FRONT, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
glMaterialf(GL_FRONT, GL_SHININESS, 100.0)
#fog
glEnable(GL_FOG)
fogColor = [0.5, 0.5, 0.5, 1.0]
global fogMode
fogMode = GL_EXP
glFogi (GL_FOG_MODE, fogMode)
glFogfv (GL_FOG_COLOR, fogColor)
glFogf (GL_FOG_DENSITY, 0.35)
glHint (GL_FOG_HINT, GL_DONT_CARE)
glFogf (GL_FOG_START, 1.0)
glFogf (GL_FOG_END, 5.0)
#blending for billboard alpha textures
glEnable (GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
#antialiasing
glEnable (GL_LINE_SMOOTH)
glEnable (GL_POLYGON_SMOOTH)
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE)
glLineWidth(1.5)
#depth of field
glAccum (GL_ACCUM, 0.125)
#motion blur
glAccum (GL_MULT, 0.5)
glAccum (GL_RETURN, 1.0)
pyglet.app.run()
当我运行它时,出现此错误。
第17行,平局 self.mesh.draw() AttributeError:“ Collada”对象没有属性“ draw”。
最终,我希望在加载程序时在屏幕上显示名为“ AcrylicSphere.dae”的3D模型,就像从DLC ENERGY教程中制作的多维数据集一样。