[英]How to: grid-array of primitives in pyglet and dynamic filling of shapes
我正在嘗試在網絡仿真工具中可視化網絡負載。 我想在窗口(例如4x4網格網絡)中將網絡節點顯示為正方形網格,在該窗口中,我可以根據跨節點的流量分別為每個正方形選擇填充顏色(必須從中讀取流量信息轉儲文件,但多數民眾贊成在以后使用)。 我正在嘗試查看是否可以使用pyglet。 例如,假設我們有一個2x2網絡(4個正方形)。 我想要一個2x2的正方形矩陣,可以在其中單獨更改每個元素的填充顏色。 我是初學者,到目前為止,我在查找了大量參考文獻后學會了繪制一個填充的正方形。 看代碼:
import sys, time, math, os, random
from pyglet.gl import *
window = pyglet.window.Window()
label = pyglet.text.Label('Simulation',
font_name='Times New Roman',
font_size=16,
color=(204,204,0,255), #red font (255,0,0) opacity=255
x=window.width, y=window.height,
anchor_x='right', anchor_y='top')
class FilledSquare:
def __init__(self, width, height, xpos, ypos):
self.xpos = xpos
self.ypos = ypos
self.angle = 0
self.size = 1
x = width/2.0
y = height/2.0
self.vlist = pyglet.graphics.vertex_list(4, ('v2f', [-x,-y, x,-y, -x,y, x,y]), ('t2f', [0,0, 1,0, 0,1, 1,1]), ('c3B',(0,255,0,0,255,0,0,255,0,0,255,0)))
def draw(self,w,h,x,y):
self.width=w
self.height=h
self.xpos=x
self.ypos=y
glPushMatrix()
glTranslatef(self.xpos, self.ypos, 0)
self.vlist.draw(GL_TRIANGLE_STRIP)
glPopMatrix()
@window.event
def on_draw():
window.clear()
glClearColor(0, 0.3, 0.5, 0)
glClear(GL_COLOR_BUFFER_BIT)
label.draw()
square1.draw(60,60,460,240)
square1 = FilledSquare(30, 30, 100, 200)
pyglet.app.run()
讓我知道是否有可能。
我從pyglet-users谷歌小組的亞當那里得到了一些建議。 他們來了:
無論如何,當您使用即時模式時,您也可能不將頂點顏色作為頂點列表的一部分傳遞,而是在繪制之前使用glColor *設置顏色,例如
glColor3f(0.0,1.0,0.0)
您可以將對象放入列表中,這樣就可以
平方= [[FilledSquare(...),FilledSquare(...)],[FilledSquare(...),FilledSquare(...)]]
謝謝亞當(如果您看到了這一點),它真的很有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.