-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.py
executable file
·125 lines (96 loc) · 3.99 KB
/
graphics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'''Displays a rotating torus using the pyglet.graphics API.
This example is very similar to examples/opengl.py, but uses the
pyglet.graphics API to construct the indexed vertex arrays instead of
using OpenGL calls explicitly. This has the advantage that VBOs will
be used on supporting hardware automatically.
The vertex list is added to a batch, allowing it to be easily rendered
alongside other vertex lists with minimal overhead.
'''
from pyglet.gl import *
from node import *
from torus import Torus
try:
# Try and create a window with multisampling (antialiasing)
config = Config(sample_buffers=1, samples=4,
depth_size=16, double_buffer=True,)
window = pyglet.window.Window(resizable=True, config=config)
except pyglet.window.NoSuchConfigException:
# Fall back to no multisampling for old hardware
window = pyglet.window.Window(resizable=True)
@window.event
def on_resize(width, height):
viewport0[0], viewport0[1], viewport0[2], viewport0[3] = 0, 0, width, height
viewport1[0], viewport1[1], viewport1[2], viewport1[3] = 0, 0, width // 2, height // 2
viewport2[0], viewport2[1], viewport2[2], viewport2[3] = 0, height // 2, width // 2, height // 2
viewport3[0], viewport3[1], viewport3[2], viewport3[3] = width // 2, 0, width // 2, height // 2
viewport4[0], viewport4[1], viewport4[2], viewport4[3] = width // 2, height // 2, width // 2, height // 2
glViewport(0, 0, width, height)
return pyglet.event.EVENT_HANDLED
def update(dt):
global rx, ry, rz
rx += dt * 1
ry += dt * 80
rz += dt * 30
rx %= 360
ry %= 360
rz %= 360
pyglet.clock.schedule(update)
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(0, 0, -4)
glRotatef(rz, 0, 0, 1)
glRotatef(ry, 0, 1, 0)
glRotatef(rx, 1, 0, 0)
batch.draw()
# label.draw()
def setup():
# One-time GL setup
glClearColor(1, 1, 1, 1)
glColor3f(1, 0, 0)
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
# Uncomment this line for a wireframe view
#glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
# Simple light setup. On Windows GL_LIGHT0 is enabled by default,
# but this is not the case on Linux or Mac, so remember to always
# include it.
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_LIGHT1)
# Define a simple function to create ctypes arrays of floats:
def vec(*args):
return (GLfloat * len(args))(*args)
glLightfv(GL_LIGHT0, GL_POSITION, vec(.5, .5, 1, 0))
glLightfv(GL_LIGHT0, GL_SPECULAR, vec(.5, .5, 1, 1))
glLightfv(GL_LIGHT0, GL_DIFFUSE, vec(1, 1, 1, 1))
glLightfv(GL_LIGHT1, GL_POSITION, vec(1, 0, .5, 0))
glLightfv(GL_LIGHT1, GL_DIFFUSE, vec(.5, .5, .5, 1))
glLightfv(GL_LIGHT1, GL_SPECULAR, vec(1, 1, 1, 1))
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.5, 0, 0.3, 1))
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vec(1, 1, 1, 1))
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50)
setup()
batch = pyglet.graphics.Batch()
torus = Torus(1, 0.3, 50, 30)
viewport0 = [0, 0, 640, 480]
viewport1 = [0, 0, 320, 240]
viewport2 = [0 , 240, 320, 240]
viewport3 = [320, 0, 320, 240]
viewport4 = [320, 240, 320, 240]
perspectiveGroup = PerspectiveGroup(None)
torus.add_to_batch(batch=batch, group=ViewportGroup(viewport1, perspectiveGroup))
torus.add_to_batch(batch=batch, group=ViewportGroup(viewport2, perspectiveGroup))
torus.add_to_batch(batch=batch, group=ViewportGroup(viewport3, perspectiveGroup))
torus.add_to_batch(batch=batch, group=ViewportGroup(viewport4, perspectiveGroup))
rx = ry = rz = 0
guiGroup = GUIProjectionGroup()
label = pyglet.text.Label('Hello, world',
font_name='Times New Roman',
font_size=36,
x=window.width//2, y=window.height//2,
anchor_x='center', anchor_y='center',
group=guiGroup,
batch=batch)
pyglet.app.run()