| 1 |
import pyglet |
| 2 |
|
| 3 |
from pyglet.gl import glEnable, GL_DEPTH_TEST, GL_CULL_FACE |
| 4 |
from pyglet.math import Mat4, Vec3 |
| 5 |
|
| 6 |
|
| 7 |
window = pyglet.window.Window(resizable=True) |
| 8 |
batch = pyglet.graphics.Batch() |
| 9 |
time = 0 |
| 10 |
|
| 11 |
|
| 12 |
@window.event |
| 13 |
def on_resize(width, height): |
| 14 |
window.viewport = (0, 0, width, height) |
| 15 |
|
| 16 |
window.projection = Mat4.perspective_projection(window.aspect_ratio, z_near=0.1, z_far=255) |
| 17 |
return pyglet.event.EVENT_HANDLED |
| 18 |
|
| 19 |
|
| 20 |
@window.event |
| 21 |
def on_draw(): |
| 22 |
window.clear() |
| 23 |
batch.draw() |
| 24 |
|
| 25 |
|
| 26 |
def animate(dt): |
| 27 |
global time |
| 28 |
time += dt |
| 29 |
|
| 30 |
rot_x = Mat4.from_rotation(time, Vec3(1, 0, 0)) |
| 31 |
rot_y = Mat4.from_rotation(time/2, Vec3(0, 1, 0)) |
| 32 |
rot_z = Mat4.from_rotation(time/3, Vec3(0, 0, 1)) |
| 33 |
trans = Mat4.from_translation(Vec3(1.25, 0, 2)) |
| 34 |
model_logo.matrix = trans @ rot_x @ rot_y @ rot_z |
| 35 |
|
| 36 |
rot_x = Mat4.from_rotation(time, Vec3(1, 0, 0)) |
| 37 |
rot_y = Mat4.from_rotation(time/3, Vec3(0, 1, 0)) |
| 38 |
rot_z = Mat4.from_rotation(time/2, Vec3(0, 0, 1)) |
| 39 |
trans = Mat4.from_translation(Vec3(-1.75, 0, 0)) |
| 40 |
model_box.matrix = trans @ rot_x @ rot_y @ rot_z |
| 41 |
|
| 42 |
|
| 43 |
if __name__ == "__main__": |
| 44 |
glEnable(GL_DEPTH_TEST) |
| 45 |
glEnable(GL_CULL_FACE) |
| 46 |
|
| 47 |
logo_scene = pyglet.resource.scene('logo3d.obj') |
| 48 |
box_scene = pyglet.resource.scene('box.obj') |
| 49 |
model_logo = logo_scene.create_models(batch=batch)[0] |
| 50 |
model_box = box_scene.create_models(batch=batch)[0] |
| 51 |
|
| 52 |
|
| 53 |
window.view = Mat4.look_at(position=Vec3(0, 0, 5), target=Vec3(0, 0, 0), up=Vec3(0, 1, 0)) |
| 54 |
|
| 55 |
pyglet.clock.schedule_interval(animate, 1 / 60) |
| 56 |
pyglet.app.run() |