Python Pyglet 3D(2)
大家好,今天继续来给大家分享关于python pyglet模块的知识吧。
一.Pyglet函数、类介绍
首先今天的学习需要你把创建窗口的代码调成这样:
window = pyglet.window.Window()
context = window.context
context.set_current()
glEnable(GL_DEPTH_TEST)
1.Batch类
Batch类用于创建正方体。
Batch类格式:
batch = pyglet.graphics.Batch()
2.TextureGroup类
这个类用于给3D立体图形添加贴图。
TextureGroup类格式:
group = TextureGroup(texture)
3.image.load函数
image.load函数用于加载图片,可以在函数后面加一个.texture用于将这张图片转换成贴图图片。
image.load函数格式1:
image = pyglet.image.load(path)
image.load函数格式2:
image = pyglet.image.load(path).texture
4.resource.load函数
resource.load函数用于加载与程序、应用捆绑在一起的图片。
resource.load函数格式:
image = resource.load(image_name)
5.blit函数
blit函数用于将图像绘制在窗口上。
blit函数格式:
image.blit(x,y)
6.on_key_press函数
这个函数我们在上节课已经用过了,这节课我们再详细讲一讲。首先,这个函数有symbol、 modifiers两个参数,symbol表示按下了哪个键(这里的值是pyglet.window.key模块里的常量),modifiers这个参数不常用。注意函数的前面必须有@window.event,而且函数名必须是on_key_press。
on_key_press函数格式:
def on_key_press(symbol,modifiers):
pass
7.on_mouse_press函数
这个函数有x、y、button、modifiers这几个参数。
x:按下鼠标坐标的x坐标
y:按下鼠标坐标的y坐标
button:按下鼠标的左键、中键还是右键
modifiers参数不常用
注意函数的前面必须有@window.event,而且函数名必须是on_mouse_press。
on_mouse_press函数格式:
@window.event
def on_mouse_press(x,y,button,modifier):
pass
8.on_mouse_drag函数
这个函数的参数有:x、y、dx、dy、btn,用于在鼠标拖动时执行程序。
x:鼠标原来的x坐标
y:鼠标原来的y坐标
dx:鼠标x轴的变化
dy:鼠标y轴的变化
btn:这个参数不常用
注意函数的前面必须有@window.event,而且函数名必须是on_mouse_drag。
on_mouse_drag函数格式:
@window.event
def on_mouse_draw(x,y,dx,dy,btn):
pass
二.Pyglet.key模块常量
1.ASCII控制按键
2.光标控制按键
3.更多控制按键
4.数字按键
5.F1-F12按键
6.其他控制按键
7.字符按键
三.用Pyglet创建多个随机3D爱心
代码:
import pyglet
from pyglet.gl import *
from pyglet.gl.glu import *
from pyglet.window import key
from pyglet import clock
import math
from random import random, randint
WIDTH=400
HEIGHT=400
angle_xy = math.pi / 2
angle_z = 0
distance = 20
centerx,centery,centerz = 38,9, -11
data = [(3.2045, 50.7902), (1.5227, 49.5507), (0.2268, 47.9115), (-0.6292, 46.0584), (-0.9964, 44.1779), (-0.8378, 42.4295), (-0.5548, 41.6411), (-0.1348, 40.9221),
(0.4219, 40.2782), (1.9425, 39.2276), (2.9045, 38.8233), (4.0, 38.5), (2.9045, 38.1766), (1.9424, 37.7722), (0.4218, 36.7216), (-0.1349, 36.0778), (-0.5548, 35.3589), (-0.8378, 34.5706), (-0.9964, 32.8226),
(-0.6291, 30.9425), (0.227, 29.09), (1.5229, 27.4513), (3.2048, 26.2122), (5.2203, 25.5298), (7.5231, 25.5104), (10.0749, 26.2121), (12.8454, 27.6578), (15.812, 29.848),
(18.9593, 32.7712), (22.2779, 36.4131), (24.0, 38.5), (22.2778, 40.5874), (18.9591, 44.2302), (15.8118, 47.1541), (12.8451, 49.3448), (10.0746, 50.7907),
(7.5228, 51.4924), (5.22, 51.4729)]
z1=8
z2=10
conf = pyglet.gl.Config(sample_buffers=1, samples=4, depth_size=1)
window = pyglet.window.Window(height=HEIGHT, width=WIDTH,config=conf)
def convert_pos():
if math.pi/2 < angle_z < math.pi * 1.5:
flag = -1
else:
flag = 1
cam_x=math.cos(angle_xy)*distance *math.cos(angle_z) + centerx
cam_y=math.sin(angle_xy)*distance *math.cos(angle_z) + centery
cam_z=math.sin(angle_z)*distance + centerz
return cam_x,cam_y,cam_z,flag
def draw_heart(dx,dy,dz):
glBegin(GL_POLYGON)
glColor3f(random()*0.5+0.5, 0, random()*0.5+0.5)
for x, y in data:
glVertex3f(y+dx, z1+dy, -x+dz)
glEnd()
glBegin(GL_POLYGON)
for x, y in data:
glVertex3f(y+dx, z2+dy, -x+dz)
glEnd()
glColor4f(0.5, 0, 0.5, 0.5)
for i in range(len(data)):
if i + 1 == len(data):
next_point = data[0]
else:
next_point = data[i + 1]
point = data[i]
glBegin(GL_POLYGON)
glVertex3f(point[1]+dx, z1+dy, -point[0]+dz)
glVertex3f(next_point[1]+dx, z1+dy, -next_point[0]+dz)
glVertex3f(next_point[1]+dx, z2+dy, -next_point[0]+dz)
glVertex3f(point[1]+dx, z2+dy, -point[0]+dz)
glEnd()
@window.event
def on_draw():
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glFrustum(-5, 5, -5, 5, 2, 1000)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glViewport(0, 0, WIDTH, HEIGHT)
window.clear()
glClear(GL_DEPTH_BUFFER_BIT)
cam_x,cam_y,cam_z,flag = convert_pos()
gluLookAt(cam_x,cam_y,cam_z,centerx,centery,centerz,0,0,flag)
for dx,dy,dz in lst_pos:
draw_heart(dx,dy,dz)
glFlush()
def animate(event):
global centerx
centerx+=1
xpos=lst_pos[0][0]
lst_pos[0]=(xpos+1,0,0)
on_draw()
clock.schedule_interval(animate, 0.02)
@window.event
def on_key_press(k):
global angle_xy,angle_z,distance
if k==key.DOWN:
angle_z -= math.pi * 1/18
elif k==key.UP:
angle_z += math.pi * 1/18
elif k==key.LEFT:
angle_xy -= math.pi * 1/18
elif k==key.RIGHT:
angle_xy += math.pi * 1/18
angle_z %= math.pi*2
on_draw()
@window.event
def on_mouse_drag(x,y,dx,dy,btn,_):
global angle_xy, angle_z
angle_xy -= dx / 100
angle_z -= dy / 100
angle_z %= math.pi * 2
on_draw()
@window.event
def on_mouse_scroll(x,y, _, d):
global distance
distance /= 1.1**d
on_draw()
lst_pos = [(0,0,0)]
for i in range(20):
lst_pos.append((randint(-200,200),randint(-200,200),randint(-200,200)))
glClearColor(0.8, 1, 1, 1)
glEnable(GL_DEPTH_TEST)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
pyglet.app.run()
效果:
作者:墨水云烟