Python:pyglet学习(2)图形的旋转&batch

这次讲讲图形的旋转和批量渲染(rotate、batch)

1:图形旋转

先看看上次的代码中的一段:

glRotatef(rot_y, 0, 1, 0)
glRotatef(rot_z,0,0,1)
glRotatef(rot_x,1,0,0)

 

这就是旋转的代码,函数是glRotatef,第一个参数是旋转的角度,后三个中那个是1就转哪个(从左往右依次是x,y,z)

简化成一个函数:

def rotate(xdeg,ydeg,zdeg):
    glRotatef(ydeg,0,1,0)
    glRotatef(zdeg,0,0,1)
    glRotatef(xdeg,1,0,0)

 

2:batch

还是看看官网:

Batched rendering
For optimal OpenGL performance, you should render as many vertex lists as possible in a single draw call. Internally, pyglet uses VertexDomain and IndexedVertexDomain to keep vertex lists that share the same attribute formats in adjacent areas of memory. The entire domain of vertex lists can then be drawn at once, without calling draw() on each individual list.

It is quite difficult and tedious to write an application that manages vertex domains itself, though. In addition to maintaining a vertex domain for each set of attribute formats, domains must also be separated by primitive mode and required OpenGL state.

The Batch class implements this functionality, grouping related vertex lists together and sorting by OpenGL state automatically. A batch is created with no arguments:

batch = pyglet.graphics.Batch()
Vertex lists can now be created with the add() and add_indexed() methods instead of pyglet.graphics.vertex_list() and pyglet.graphics.vertex_list_indexed() functions. Unlike the module functions, these methods accept a mode parameter (the primitive mode) and a group parameter (described below).

The two coloured points from previous pages can be added to a batch as a single vertex list with:

vertex_list = batch.add(2, pyglet.gl.GL_POINTS, None,
    ('v2i', (10, 15, 30, 35)),
    ('c3B', (0, 0, 255, 0, 255, 0))
)
The resulting vertex_list can be modified as described in the previous section. However, instead of calling VertexList.draw to draw it, call Batch.draw() to draw all vertex lists contained in the batch at once:

batch.draw()
For batches containing many vertex lists this gives a significant performance improvement over drawing individual vertex lists.

To remove a vertex list from a batch, call VertexList.delete(). If you don’t need to modify or delete vertex lists after adding them to the batch, you can simply ignore the return value of the add() and add_indexed() methods.

  

之前绘制正方形的代码可以简化成(吗)

b=Batch()
b.add(1,GL_POLYGON,None,('v3i',(-5,-5,0,5,-5,0,5,5,0,-5,5,0)))

b.draw()

  

 然后。。。

Traceback (most recent call last):
  File "D:\桌面\python\python程序\制作中\3d-2.py", line 14, in <module>
b.add(1,GL_POLYGON,None,('v3i',(-5,-5,0,5,-5,0,5,5,0,-5,5,0)))
File "C:\Users\steven\AppData\Local\Programs\Python\Python310-32\lib\site-packages\pyglet\graphics\__init__.py", line 371, in add vlist._set_attribute_data(i, array) 
File "C:\Users\steven\AppData\Local\Programs\Python\Python310-32\lib\site-packages\pyglet\graphics\vertexdomain.py", line 447, in _set_attribute_data region.array[:] = data
ValueError: Can only assign sequence of same size

 

再看看官网:

Alternatively, the NV_primitive_restart extension can be used if it is present. This also permits use of GL_POLYGON, GL_LINE_LOOP and GL_TRIANGLE_FAN. Unfortunately the extension is not provided by older video drivers, and requires indexed vertex lists.

  

不能在Batch中使用GL_POLYGON, GL_LINE_LOOP,GL_TRIANGLE_FAN!

所以改为

b=pyglet.graphics.Batch()
b.add(8,GL_LINES,None,("v3i",dotlst[0][0]+dotlst[0][1]+dotlst[0][2]+dotlst[0][3]+dotlst[0][1]+dotlst[0][2]+dotlst[0][3]+dotlst[0][0]))

b.draw()

  

 

 

但不能涂色了。这里的话,GL_LINES的参数是2n个坐标,表示A0->A1,A2->A3......

 

如果要涂色的话用三角形覆盖

 

下一讲

posted @ 2022-02-27 21:15  steven-eleven  阅读(843)  评论(0编辑  收藏  举报