简体   繁体   English

如何在 PyGlet 中绘制 3D model

[英]How to draw 3D model in PyGlet

I was able to get a 3D model in.obj format, but I don't know how to draw it on the screen.我能够得到一个 3D model in.obj 格式,但我不知道如何在屏幕上绘制它。 Here's my code:这是我的代码:

import pyglet

model = pyglet.resource.model('untitled.obj')
window = pyglet.window.Window()

@window.event
def on_draw():
    window.clear()
    model.draw()

pyglet.app.run()

But it outputs this error:但它输出此错误:

pyglet.gl.lib.GLException: b'\xed\xe5\xe4\xee\xef\xf3\xf1\xf2\xe8\xec\xee\xe5 \xe7\xed\xe0\xf7\xe5\xed\xe8\xe5' 

If you do the following如果您执行以下操作

model = pyglet.resource.model('untitled.obj')
print(model)

Output: Output:

<pyglet.model.Model object at 0x02E356E8>

There is a simple example in the pyglet documentation on GitHub just like this: GitHub 的 pyglet 文档中有一个简单的示例,如下所示:

import pyglet
from pyglet.gl import *

window = pyglet.window.Window(width=720, height=480)
window.projection = pyglet.window.Projection3D()
batch = pyglet.graphics.Batch()


@window.event
def on_draw():
    window.clear()
    batch.draw()


@window.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
    glRotatef(1, dx, dy, 0)


def rotate(dt):
    glRotatef(0.5, dt, dt, dt)


if __name__ == "__main__":
    glEnable(GL_MULTISAMPLE_ARB)
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)

    model = pyglet.model.load("logo3d.obj", batch=batch)
    glTranslatef(0, 0, -3)
    pyglet.clock.schedule_interval(rotate, 1/60)
    pyglet.app.run()

I'm pretty sure pyglet is just made for 2d stuff.我很确定 pyglet 只是为 2d 东西制作的。 The .draw() function rasterisers 2d images pixel by pixel, but more complicated rendering methods are needed for 3d models, even a simple cube. .draw() function 光栅化 2d 图像逐像素,但 3d 模型需要更复杂的渲染方法,即使是简单的立方体。 You may need to write a lot more code for this project.您可能需要为此项目编写更多代码。 A simple way may be to raycast, as in the original doom game, which gives very simple 3d graphics which can have textures.一种简单的方法可能是光线投射,就像在原始的厄运游戏中一样,它给出了非常简单的 3d 图形,可以有纹理。 Unfortunately, this method makes complicated shapes impossible and you have to code all the models in your project, not using external sources such as blender.不幸的是,这种方法使复杂的形状变得不可能,您必须对项目中的所有模型进行编码,而不是使用诸如搅拌机之类的外部资源。 There are numerous methods of compressing a 3d model into a 2d image, but a .draw() function just isn't enough.有许多方法可以将 3d model 压缩成二维图像,但是.draw() function 还不够。 depending on what kind of game/program you're making, you may want to se raycasting or other methods, But I'd recommend using some other game engine t create 3d games.根据您制作的游戏/程序类型,您可能想要使用光线投射或其他方法,但我建议使用其他游戏引擎来创建 3d 游戏。 Unity is a good example, unreal engine too. Unity 就是一个很好的例子,虚幻引擎也是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM