简体   繁体   English

作法:pyglet中图元的网格阵列和形状的动态填充

[英]How to: grid-array of primitives in pyglet and dynamic filling of shapes

I am trying to visualize the network load in a network simulation tool. 我正在尝试在网络仿真工具中可视化网络负载。 I want to display the network nodes as a grid of squares in a window (eg a 4x4 mesh network) where I can individually pick the fill-color for each square based on the traffic across the node (the traffic info has to be read from a dump file but thats for later). 我想在窗口(例如4x4网格网络)中将网络节点显示为正方形网格,在该窗口中,我可以根据跨节点的流量分别为每个正方形选择填充颜色(必须从中读取流量信息转储文件,但多数民众赞成在以后使用)。 I am trying to see if I can use pyglet for this. 我正在尝试查看是否可以使用pyglet。 So, for example, lets say we have a 2x2 network (4 squares). 例如,假设我们有一个2x2网络(4个正方形)。 I want a 2x2 matrix of squares where I can change the fill-color of each element individually. 我想要一个2x2的正方形矩阵,可以在其中单独更改每个元素的填充颜色。 I am beginner and so far I have learnt to draw a single filled square after looking up a lot of references. 我是初学者,到目前为止,我在查找了大量参考文献后学会了绘制一个填充的正方形。 See the code: 看代码:

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()
  1. I think in this way, the fill-color is static as it is initialized. 我认为通过这种方式,填充颜色在初始化时是静态的。 How can I pass the fill-color? 如何传递填充色?
  2. How to create a 2D array of FilledSquare (maybe something like square[i][j] which I can access in a loop. 如何创建FilledSquare的2D数组(也许像square [i] [j]之类的东西,我可以循环访问。

Let me know if it is possible at all. 让我知道是否有可能。

I got some suggestions from Adam in the pyglet-users google group. 我从pyglet-users谷歌小组的亚当那里得到了一些建议。 Here they are: 他们来了:

  1. As you're using immediate mode anyway you might as well not pass the vertex colour as part of your vertex list but use glColor* to set the colour before drawing eg 无论如何,当您使用即时模式时,您也可能不将顶点颜色作为顶点列表的一部分传递,而是在绘制之前使用glColor *设置颜色,例如

    glColor3f(0.0, 1.0, 0.0) glColor3f(0.0,1.0,0.0)

  2. You can put objects into a list so you could have 您可以将对象放入列表中,这样就可以

squares = [[FilledSquare(...), FilledSquare(...)], [FilledSquare(...), FilledSquare(...)]] 平方= [[FilledSquare(...),FilledSquare(...)],[FilledSquare(...),FilledSquare(...)]]

Thanks Adam (if you see this), it really helped. 谢谢亚当(如果您看到了这一点),它真的很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM