| 1 |
|
| 2 |
"""Displays a rotating torus using the pyglet.graphics API. |
| 3 |
|
| 4 |
This example uses the pyglet.graphics API to render an indexed vertex |
| 5 |
list. The vertex list is added to a batch, allowing easy rendered |
| 6 |
alongside other vertex lists with minimal overhead. |
| 7 |
|
| 8 |
This example demonstrates: |
| 9 |
|
| 10 |
* Setting a 3D projection on a window by overriding the default |
| 11 |
on_resize handler |
| 12 |
* Enabling multisampling if available |
| 13 |
* Drawing simple 3D primitives using the pyglet.graphics API |
| 14 |
* Fixed-pipeline lighting |
| 15 |
""" |
| 16 |
from math import pi, sin, cos |
| 17 |
|
| 18 |
import pyglet |
| 19 |
from pyglet.gl import ( |
| 20 |
glEnable, |
| 21 |
glClearColor, |
| 22 |
Config, |
| 23 |
GL_DEPTH_TEST, |
| 24 |
GL_CULL_FACE, |
| 25 |
GL_TRIANGLES, |
| 26 |
) |
| 27 |
from pyglet.math import Mat4, Vec3 |
| 28 |
|
| 29 |
try: |
| 30 |
|
| 31 |
config = Config(sample_buffers=1, samples=4, depth_size=16, double_buffer=True) |
| 32 |
window = pyglet.window.Window(width=960, height=540, resizable=True, config=config) |
| 33 |
except pyglet.window.NoSuchConfigException: |
| 34 |
|
| 35 |
window = pyglet.window.Window(width=960, height=540, resizable=True) |
| 36 |
|
| 37 |
|
| 38 |
@window.event |
| 39 |
def on_draw(): |
| 40 |
window.clear() |
| 41 |
batch.draw() |
| 42 |
|
| 43 |
|
| 44 |
@window.event |
| 45 |
def on_resize(width, height): |
| 46 |
window.viewport = (0, 0, width, height) |
| 47 |
window.projection = Mat4.perspective_projection(window.aspect_ratio, z_near=0.1, z_far=255, fov=60) |
| 48 |
return pyglet.event.EVENT_HANDLED |
| 49 |
|
| 50 |
|
| 51 |
def update(dt): |
| 52 |
global time |
| 53 |
time += dt |
| 54 |
rot_x = Mat4.from_rotation(time, Vec3(1, 0, 0)) |
| 55 |
rot_y = Mat4.from_rotation(time / 2, Vec3(0, 1, 0)) |
| 56 |
rot_z = Mat4.from_rotation(time / 4, Vec3(0, 0, 1)) |
| 57 |
trans = Mat4.from_translation(Vec3(0, 0, -3.0)) |
| 58 |
torus_model.matrix = trans @ rot_x @ rot_y @ rot_z |
| 59 |
|
| 60 |
|
| 61 |
def setup(): |
| 62 |
|
| 63 |
glClearColor(1, 1, 1, 1) |
| 64 |
glEnable(GL_DEPTH_TEST) |
| 65 |
glEnable(GL_CULL_FACE) |
| 66 |
on_resize(*window.size) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
def create_torus(radius, inner_radius, slices, inner_slices, shader, batch): |
| 73 |
|
| 74 |
vertices = [] |
| 75 |
normals = [] |
| 76 |
|
| 77 |
u_step = 2 * pi / (slices - 1) |
| 78 |
v_step = 2 * pi / (inner_slices - 1) |
| 79 |
u = 0. |
| 80 |
for i in range(slices): |
| 81 |
cos_u = cos(u) |
| 82 |
sin_u = sin(u) |
| 83 |
v = 0. |
| 84 |
for j in range(inner_slices): |
| 85 |
cos_v = cos(v) |
| 86 |
sin_v = sin(v) |
| 87 |
|
| 88 |
d = (radius + inner_radius * cos_v) |
| 89 |
x = d * cos_u |
| 90 |
y = d * sin_u |
| 91 |
z = inner_radius * sin_v |
| 92 |
|
| 93 |
nx = cos_u * cos_v |
| 94 |
ny = sin_u * cos_v |
| 95 |
nz = sin_v |
| 96 |
|
| 97 |
vertices.extend([x, y, z]) |
| 98 |
normals.extend([nx, ny, nz]) |
| 99 |
v += v_step |
| 100 |
u += u_step |
| 101 |
|
| 102 |
|
| 103 |
indices = [] |
| 104 |
for i in range(slices - 1): |
| 105 |
for j in range(inner_slices - 1): |
| 106 |
p = i * inner_slices + j |
| 107 |
indices.extend([p, p + inner_slices, p + inner_slices + 1]) |
| 108 |
indices.extend([p, p + inner_slices + 1, p + 1]) |
| 109 |
|
| 110 |
|
| 111 |
diffuse = [0.5, 0.0, 0.3, 1.0] |
| 112 |
ambient = [0.5, 0.0, 0.3, 1.0] |
| 113 |
specular = [1.0, 1.0, 1.0, 1.0] |
| 114 |
emission = [0.0, 0.0, 0.0, 1.0] |
| 115 |
shininess = 50 |
| 116 |
|
| 117 |
material = pyglet.model.SimpleMaterial("custom", diffuse, ambient, specular, emission, shininess) |
| 118 |
group = pyglet.model.MaterialGroup(material=material, program=shader) |
| 119 |
|
| 120 |
vertex_list = shader.vertex_list_indexed(len(vertices) // 3, GL_TRIANGLES, indices, batch, group, |
| 121 |
POSITION=('f', vertices), |
| 122 |
NORMAL=('f', normals), |
| 123 |
COLOR_0=('f', material.diffuse * (len(vertices) // 3))) |
| 124 |
|
| 125 |
return pyglet.model.Model([vertex_list], [group], batch) |
| 126 |
|
| 127 |
|
| 128 |
setup() |
| 129 |
time = 0.0 |
| 130 |
batch = pyglet.graphics.Batch() |
| 131 |
shader = pyglet.model.get_default_shader() |
| 132 |
torus_model = create_torus(1.0, 0.3, 50, 30, shader, batch) |
| 133 |
|
| 134 |
pyglet.clock.schedule(update) |
| 135 |
pyglet.app.run() |