python - 错误绘制的八边形(随机)- 添加了新测试

标签 python opengl graphics polygon pyglet

我正在尝试使用 Python 3.4 和 Pyglet 1.2(使用 OpenGL)绘制一个八边形。我的代码似乎是正确的,但绘图有时在单帧的随机位置(大多数时候是 0, 0(左下)角)有额外的随机颜色三角形(大多数时候是白色或黑色)。以下是一些示例:

bug1 bug2 bug3

有些框架是完美的:

fine

这是我的简短方法:

from pyglet.gl import GL_TRIANGLES
from itertools import chain
C = 0.707 # const
def octagon (x, y, r, c, b, g):
    """ Returns a vertex list of regular octagon with center at (x, y) and corners
    distanced r away. Paints center and corners. Adds to batch b in group g. """
    i = list(chain.from_iterable( (0, x+1, x+2) for x in range(8) ) )
    i.extend( (0, 1, 8) )
    p = x, y
    p += x-r, y, x+int(-C*r), y+int(C*r), x, y+r, x+int(C*r), y+int(C*r)
    p += x+r, y, x+int(C*r), y+int(-C*r), x, y-r, x+int(-C*r), y+int(-C*r)
    return b.add_indexed(9, GL_TRIANGLES, g, i, ('v2i', p), ('c3B', c))

这是我的测试代码:

import pyglet
from circle import octagon

# Constants
WIN = 900, 900, 'TEST', False, 'tool' # x, y, caption, resizable, style
CENTER = WIN[0] // 2, WIN[1] // 2
RADIUS = 100
SPEED = 0.1 # duration of frame in seconds

# Color constants
WHITE = (255, 255, 255) # for center
RED = (255, 0, 0)       # for corners

# Variables
win = pyglet.window.Window(*WIN)
batch = pyglet.graphics.Batch() # recreate batch every time

def on_step(dt):
    global batch
    batch = pyglet.graphics.Batch()
    octagon(CENTER[0], CENTER[1], RADIUS, WHITE+RED*8, batch, None)

@win.event
def on_draw():
    win.clear()
    batch.draw()

pyglet.clock.schedule_interval(on_step, SPEED)
pyglet.app.run()

我尝试过的:

  • 重新启动计算机
  • 删除包含 *.pyc 文件的 py_cache 文件夹
  • 更改帧速率
  • 更改半径和位置
  • 重复打印点列表。它们都不会改变(尽管绘图发生变化)并且都在有效范围内。
  • 打印索引列表。它们都没有改变并且都是有效的。

但是,这些都不起作用。有什么问题?这个错误可以在您的机器上重现吗?该问题似乎是随机发生的。

编辑:我想这并不是那么随机。我尝试了一种不同的测试,它不会删除每个新帧的批处理,而是在现有的帧中添加另一个八边形。发生的情况如下:

connected

看来它们都是有联系的。特别是他们的中心。毕竟,我想这可能是代码中的错误。

这是我的第二个测试:

import pyglet
from circle import octagon
from random import randrange

WIN = 900, 900, 'TEST', 0, 'tool', 0, 1, 0 # vsync off to unlimit fps
RADIUS = 60
SPEED = 1.0
WHITE = (255, 255, 255) # for center
RED = (255, 0, 0)       # for points

win = pyglet.window.Window(*WIN)
batch = pyglet.graphics.Batch()

def on_step(dt):
    global counter
    x = randrange(RADIUS, WIN[0] - RADIUS)
    y = randrange(RADIUS, WIN[1] - RADIUS)
    octagon(x, y, RADIUS, WHITE+RED*8, batch, None)

@win.event
def on_draw():
    win.clear()
    batch.draw()

pyglet.clock.schedule_interval(on_step, SPEED)
pyglet.app.run()

最佳答案

您的索引列表不正确: i = list(chain.from_iterable( (0, x+1, x+2) for x in range(8) ) ) [0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 7, 0, 7, 8, 0, 8, 9、0、1、8]

应为范围(7)

关于python - 错误绘制的八边形(随机)- 添加了新测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30306527/

相关文章:

python - 如何在opengl和pygame中用键盘按键移动立方体?

c++ - 如何在应用 glRotate() 后获取矩形 block 的新顶点坐标

python - 如何将 3D 模型的 .obj 格式转换为 .json 格式?

python - 如何定位CSS伪元素?

python - 联合超过 2 个 Pandas 数据框

c++ - 旋转精度误差增长太快?

algorithm - 匹配颜色的最佳算法。

Java Doublebuffer - 为什么我们要清除暴露区域?

java - 如何使用图形类在 Java 中绘制复杂对象?

python - 使用 PIL 调整图像大小后如何恢复 EXIF 数据?