Python:pyglet學習(1):想弄點3D,還發現了pyglet


某一天,我突然喜歡上了3D,在一些scratch教程中見過一些3D引擎,找了一個簡單的,結果z軸太大了,於是網上一搜,就發現了pyglet

還是先講如何啟動一個窗口

先看看官網:

Creating a window
If the Window constructor is called with no arguments, defaults will be assumed for all parameters:

window = pyglet.window.Window()
The default parameters used are:

The window will have a size of 640x480, and not be resizable.
A default context will be created using template config described in OpenGL configuration options.
The window caption will be the name of the executing Python script (i.e., sys.argv[0]).
Windows are visible as soon as they are created, unless you give the visible=False argument to the constructor. The following example shows how to create and display a window in two steps:

window = pyglet.window.Window(visible=False)
# ... perform some additional initialisation
window.set_visible()
Context configuration
The context of a window cannot be changed once created. There are several ways to control the context that is created:

Supply an already-created Context using the context argument:

context = config.create_context(share)
window = pyglet.window.Window(context=context)
Supply a complete Config obtained from a Screen using the config argument. The context will be created from this config and will share object space with the most recently created existing context:

config = screen.get_best_config(template)
window = pyglet.window.Window(config=config)
Supply a template Config using the config argument. The context will use the best config obtained from the default screen of the default display:

config = gl.Config(double_buffer=True)
window = pyglet.window.Window(config=config)
Specify a Screen using the screen argument. The context will use a config created from default template configuration and this screen:

screen = display.get_screens()[screen_number]
window = pyglet.window.Window(screen=screen)
Specify a Display using the display argument. The default screen on this display will be used to obtain a context using the default template configuration:

display = platform.get_display(display_name)
window = pyglet.window.Window(display=display)
If a template Config is given, a Screen or Display may also be specified; however any other combination of parameters overconstrains the configuration and some parameters will be ignored.

總結了就是

import pyglet as p
p.window.Window(600,600)
p.app.run()

 

效果:

 

 

 

 

 然后畫個正方形

import pyglet as p
from pyglet.gl import *
win=p.window.Window(600,600)
@win.event
def on_draw():
    #(
    win.clear()
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(90, 1, 0.1, 100)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    #)
    pos=[0,0,-20]
    glTranslatef(*pos)
    glBegin(GL_POLYGON)
    glVertex3f(-5,-5,0)
    glVertex3f(5,-5,0)
    glVertex3f(5,5,0)
    glVertex3f(-5,5,0)
    glEnd()

    glFlush()
p.app.run()

 

#(  和  #)中間的是固定的,不能改

glTranslatef(*pos):攝像頭位置
glBegin(GL_POLYGON):開始繪制多邊形

    glVertex3f(-5,-5,0)
    glVertex3f(5,-5,0)
    glVertex3f(5,5,0)
    glVertex3f(-5,5,0):給出所有坐標點,繪制圖形
glEnd():結束繪制當前圖形
glFlush():刷新界面

都包含在pyglet.gl里,可使用
from pyglet.gl import *

 效果:

 

 

 

 

這就是本篇文章的內容

最后留個彩蛋

import pyglet
from pyglet.gl import *

pos = [0, 0, -20]
rot_y = 0
rot_x=0
rot_z=0
config = Config(sample_buffers=1, samples=8)
tela = pyglet.window.Window(height=500, width=500, #config=config)
                            )
mode=True
dotlst=[[[-5,-5,0],[5,-5,0],[5,5,0],[-5,5,0]]]
input_=""
@tela.event
def on_draw():

    global pos_z, rot_y,rot_z,rot_X

    tela.clear()

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(90, 1, 0.1, 100)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glTranslatef(*pos)
    glRotatef(rot_y, 0, 1, 0)
    glRotatef(rot_z,0,0,1)
    glRotatef(rot_x,1,0,0)
    glBegin(GL_POLYGON)
    glVertex3f(-5,-5,0)
    glVertex3f(5,-5,0)
    glVertex3f(5,5,0)
    glVertex3f(-5,5,0)
    glEnd()

    glFlush()

@tela.event
def on_key_press(s,m):

    global pos_z, rot_y,rot_z,rot_x
    if s == pyglet.window.key.W:
        pos[2] -= 1
    if s == pyglet.window.key.S:
        pos[2] += 1
    if s == pyglet.window.key.A:
        pos[0]-=1
    if s == pyglet.window.key.D:
        pos[0] += 1
    if s == pyglet.window.key.R:
        pos[1]-=1
    if s == pyglet.window.key.F:
        pos[1] += 1
    if s == pyglet.window.key.I:
        rot_z -= 5
    if s == pyglet.window.key.K:
        rot_z += 5
    if s == pyglet.window.key.J:
        rot_x-=5
    if s == pyglet.window.key.L:
        rot_x+= 5
    if s == pyglet.window.key.O:
        rot_y-=5
    if s == pyglet.window.key.P:
        rot_y += 5
        

pyglet.app.run()

  

WSADRFJKLIOP操作

原理下次講

 

后面的出了會寫在最下面的。

下一講


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM