Hi Eelke, 

You only replied to my email last time, so I'm copying the conversation 
here so that others can see it in the future as well. 
I made some quick hacks to your code, to give you some additional 
examples.  Lots of OpenGL primitives should be drawn with batches and 
groups, but for your use case it's probably OK to just draw everything 
seperately like you are doing. This is not the "best" way, but for now it 
will work fine and won't use much CPU. 

For outlines, an easy way to do it is to make an additional vertex list 
with GL_LINES. You can also use `glLineWidth` to make this thicker if you 
want.
Each Sector would contain two vertex lists. 

In the example, it uses the key press/release Window events to set a draw 
list. This could be connected to the midi events instead. You will probably 
want to use a dictionary to map the midi events to the visual effect you 
want. 

from math import *

from pyglet.gl import *


class Segment:
    def __init__(self, vlist):
        self.vlist = vlist
    #     self.outline = outline_vlist
     
    # def draw(self):           # for example
    #     self.vlist.draw(GL_TRIANGLES)
    #     self.outline.draw(GL_LINES)


def create_segment(radius, inner_radius, angle_out, angle_in, points=360):
        vertex = []
        color = []
        indices = []
        for i in range(points):
            angle = angle_out * (i / (points - 1)) + angle_in
            x = cos(angle) * radius
            y = sin(angle) * radius
            z = 0
            vertex.extend([x, y, z])
            color.extend([255, 0, 0])
        for i in range(points):
            angle = angle_out - angle_out * (i / (points - 1)) + angle_in
            x = cos(angle) * inner_radius
            y = sin(angle) * inner_radius
            z = 0
            vertex.extend([x, y, z])
            color.extend([0, 255, 0])
        for i in range(points - 1):
            n = 2 * points - 1
            indices.extend([i, i + 1, n - i])
            indices.extend([n - i, n - 1 - i, i + 1])

        return Segment(vlist=pyglet.graphics.vertex_list_indexed(2 * points, 
indices,
                                                                 ('v3f', 
vertex),
                                                                 ('c3B', 
color)))


class MyWindow(pyglet.window.Window):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_minimum_size(300, 300)
        glClearColor(0.2, 0.2, 0.21, 1)

        self.sectors = [create_segment(0.5, 0.3, pi / 8, n * pi / 6) for n in 
range(12)]
        self.draw_list = []

    def on_draw(self):
        self.clear()
        for sector in self.draw_list:
            sector.vlist.draw(GL_TRIANGLES)

    def on_resize(self, width, height):
        glViewport(0, 0, width, height)

    def on_key_press(self, key, modifier):
        # replicate default behavior:
        if key == pyglet.window.key.ESCAPE:
            pyglet.app.exit()

        # Some examples of mapping key presses
        if key == pyglet.window.key._1:
            self.draw_list.append(self.sectors[0])
        elif key == pyglet.window.key._3:
            self.draw_list.extend(self.sectors[0:3])
        elif key == pyglet.window.key._4:
            self.draw_list.extend(self.sectors[0:4])
        elif key == pyglet.window.key.SPACE:
            self.draw_list.extend(self.sectors[0:12])

    def on_key_release(self, key, modifier):
        self.draw_list.clear()


if __name__ == "__main__":
    windows = MyWindow(800, 800, "midi_visualizer", resizable=True)

    pyglet.app.run()




On Mon, Feb 26, 2018 at 5:03 AM, Eelke Johnson wrote:

<https://lh3.googleusercontent.com/-nh1D0sN1xjE/WpMUxtMdULI/AAAAAAAAAEg/Or-NjvoGuAo287J3cfm1id45mgrwmKobACLcBGAs/s1600/Capture.png>

Hi,
I dealt with GL_TRIANGLE and now it work :D I implemented a function which 
draw some sectors.
Now I want three things:
1) How can I draw some dick borders lines? to my array of points?
2) I want to place 12 sectors in one ring. How do you suggest me to 
implement this? a new class named ring with an array who contain all my 
sectors?
3) Finally, I want to make a sector glow or light up when I press a 
specific Key. Which OpenGL function should I use?

I need to build my programm in a way that I can make live performance :p

Thank you for your support !

here is my code:
from pyglet.gl import *
from math import *

class secteur(object):
    def __init__(self, radius, inner_radius, angle, angle_in,points):
        self.radius = radius
        self.inner_radius = inner_radius
        self.angle = angle
        self.angle_in = angle_in
        self.points = points
        self.vertex = []
        self.color = []
        self.indices = []
        for i in range(points):
            angle=self.angle*(i/(points-1))+angle_in
            x=cos(angle)*radius
            y=sin(angle)*radius
            z=0
            self.vertex.extend([x,y,z])
            self.color.extend([255,0,0])
        for i in range(points):
            angle=self.angle-self.angle*(i/(points-1))+angle_in
            x=cos(angle)*inner_radius
            y=sin(angle)*inner_radius
            z=0
            self.vertex.extend([x,y,z])
            self.color.extend([0,255,0])
        for i in range(points-1):
            n = 2*points-1
            self.indices.extend([i,i+1,n-i])
            self.indices.extend([n-i,n-1-i,i+1])
        self.vertices = pyglet.graphics.vertex_list_indexed(2*points,self.
indices,('v3f', self.vertex),('c3B',self.color))

class myWindow(pyglet.window.Window):
    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.set_minimum_size(300,300)
        glClearColor(0.2,0.2,0.21,1)
        self.secteur = secteur(0.5,0.3,pi/12,2*pi/12,360)
        self.secteur2 = secteur(0.5,0.3,pi/12,4*pi/12,360)

    def  on_draw(self):
        self.clear()
        self.secteur.vertices.draw(GL_TRIANGLES)
        self.secteur2.vertices.draw(GL_TRIANGLES)


    def on_resize(self,width,height):
        glViewport(0,0,width,height)



if __name__ == "__main__":
    windows = myWindow(800,800,"midi_visualizer",resizable=True)

    pyglet.app.run()





On Monday, February 19, 2018 at 12:31:19 PM UTC+9, Benjamin Moran wrote:
>
> GL_POLYGONS are only for convex shapes, I believe. That might be why there 
> is the part at the bottom that's connecting together. 
> Since you're just starting out, I would strongly suggest that you use 
> GL_TRIANGLES.
>
> On Monday, February 19, 2018 at 4:21:49 AM UTC+9, Eelke Johnson wrote:
>>
>>
>> <https://lh3.googleusercontent.com/-vjANSBsggYo/WonRZLeZuaI/AAAAAAAAADY/SBo95v_t_YQN6tilNDfrX8ODWuMBy1YjQCLcBGAs/s1600/capture.png>
>> Hi,
>> I began to use openGL and pyglet. Now I'm trying to get a function who 
>> return a sector. I want to use GL_POLYGON draw method but I've got weird 
>> result... Can you help me? I use cos(pi/2) for my x coordinate and 
>> sin(pi/2) for my y coordinate. I used red for the outter ring and green for 
>> the inner ring. I only want to fill my array of points^^ I dont know much 
>> about openGL but it's a start! I already prepared the midi interface. Im 
>> only stuck with the graphic parts...
>>
>> here is my code
>>
>>
>> from pyglet.gl import *
>>> from math import *
>>>
>>> class secteur(object):
>>>     def __init__(self, radius, inner_radius, angle, points):
>>>         self.radius = radius
>>>         self.inner_radius = inner_radius
>>>         self.angle = angle
>>>         self.points = points
>>>         self.vertex = []
>>>         self.color = []
>>>         for i in range(points):
>>>             angle=self.angle/points*i
>>>             x=cos(angle)*radius
>>>             y=sin(angle)*radius
>>>             z=0
>>>             self.vertex.extend([x,y,z])
>>>             self.color.extend([255,0,0])
>>>         for i in range(points):
>>>             angle=self.angle-self.angle/points*i
>>>             x=cos(angle)*inner_radius
>>>             y=sin(angle)*inner_radius
>>>             z=0
>>>             self.vertex.extend([x,y,z])
>>>             self.color.extend([0,255,0])
>>>         self.vertices = pyglet.graphics.vertex_list(2*points,('v3f', 
>>> self.vertex),('c3B',self.color))
>>>
>>> class myWindow(pyglet.window.Window):
>>>     def __init__(self,*args,**kwargs):
>>>         super().__init__(*args,**kwargs)
>>>         self.set_minimum_size(300,300)
>>>         glClearColor(0.2,0.2,0.21,1)
>>>         self.secteur = secteur(0.5,0.3,pi/2,11)
>>>
>>>
>>>     def  on_draw(self):
>>>         self.clear()
>>>         self.secteur.vertices.draw(GL_POLYGON)
>>>
>>>
>>>     def on_resize(self,width,height):
>>>         glViewport(0,0,width,height)
>>>
>>>
>>>
>>> if __name__ == "__main__":
>>>     windows = myWindow(800,800,"midi_visualizer",resizable=True)
>>>
>>>     pyglet.app.run()
>>>
>>>
>>>
>>>
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pyglet-users+unsubscr...@googlegroups.com.
To post to this group, send email to pyglet-users@googlegroups.com.
Visit this group at https://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to