Browse Source

cleanup: filenames and animations

micha 2 years ago
parent
commit
fd50e583cf
5 changed files with 505 additions and 73 deletions
  1. 0 0
      3d/demo_animation.py
  2. 150 0
      3d/demo_pil.py
  3. 341 0
      3d/demo_pyglet.py
  4. 0 68
      3d/img2.py
  5. 14 5
      3d/stage_3d.py

+ 0 - 0
3d/animation.py → 3d/demo_animation.py


+ 150 - 0
3d/demo_pil.py

@@ -0,0 +1,150 @@
+import PIL
+import PIL.Image
+import PIL.ImageFilter
+
+import os
+import time
+
+
+import pygame
+pg = pygame
+
+pygame.init()
+
+w = 1600 
+w = 600 
+h = int(600/16*9) # 16:9
+main_size=(w,h)
+window = pygame.display.set_mode(main_size,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
+#window = pygame.display.set_mode(main_size,pygame.FULLSCREEN) #x left->right ,y top-> bottom
+pg.display.set_caption('LibreLight PIL')
+#pg.transform.smoothscale(window,(100,100))
+
+
+class IMG():
+    def __init__(self,w=300,h=200,color=[255,0,0]):
+        self.w = w
+        self.h = h
+        self.color = color
+        self.img = PIL.Image.new("RGBA", (w, h))
+        self.pixels = [(color[0],color[1],color[2],100)]*(w*h)
+        self.img.putdata(self.pixels)
+        self._blur_dir = 1
+        self._blur = 5
+    def reset(self):
+        self.pixels = [(self.color[0],self.color[1],self.color[2],100)]*(self.w*self.h)
+    def draw(self,x=10,y=10,b=10,h=10,color=(255,255,255,255)):
+            
+        _len = len(self.pixels)
+
+        for i in range(b):
+            _x = (i+x)* (self.w )
+            if _x < _len:
+                pass#print(self.pixels[_x])
+            for j in range(h):
+                _y = j +y
+                idx = _x+_y
+                if idx < _len:
+                    self.pixels[idx] = color #(255,255,255)
+
+    
+    def get(self):
+        self.img.putdata(self.pixels)
+        #self.img = self.img.resize((300, 200), PIL.Image.ANTIALIAS)
+        #self.img = self.img.filter(PIL.ImageFilter.BLUR)
+        if self._blur_dir:
+            self._blur += .1
+        else:
+            self._blur -= .1
+        if self._blur > 6:
+            self._blur_dir = 0
+        elif self._blur < 0:
+            self._blur_dir = 1
+
+        #self.img = self.img.filter(PIL.ImageFilter.GaussianBlur(self._blur))
+        self.img = self.img.filter(PIL.ImageFilter.GaussianBlur(4))
+        #print( dir(self.img)) #.getpixel((1,1)) )
+        t = self.img.tobytes() 
+        #print( t[:20] ) 
+        tt = bytearray(t)
+        for i in range(100):
+             tt[i+600] = 255 #).to_bytes(1,byteorder='big')
+        t = bytes(tt)
+        s = self.img.size
+        m = self.img.mode
+
+        img = PIL.Image.frombytes(m ,s ,t) #t,s,m) #s,tb) 
+        #img = PIL.Image.frombytes(m ,s ,t) #t,s,m) #s,tb) 
+        #img = img.resize((main_size[0], main_size[1]))
+
+        #print( self.img.getpixel((1,1)) )
+        t = img.tobytes()
+        s = img.size
+        m = img.mode
+
+        out = pygame.image.fromstring( t,s,m).convert()
+        return out
+
+
+
+run = True
+x = 10
+x_dir = 1
+
+#img = IMG(w=300,h=168)
+img = IMG(w=600,h=337)
+#img = IMG(w=500,h=281)
+#img = IMG(w=1900,h=800)
+
+start_fps = time.time()
+fps_c = 0
+fps = 0
+while run:
+    #pg.clock.tick(60)
+    for event in pygame.event.get():
+        if event.type == pygame.QUIT:
+            run = False
+
+    pg.transform.smoothscale(window,(600,600))
+    window.fill(0)
+
+    
+    #img = IMG(w=400,h=300)
+    #img = IMG(w=1900,h=800)
+    img.reset()
+    img.draw()
+    img.draw(x=30,y=30, color=(0,0,0))
+    img.draw(x=x,y=x)#50)
+    pygameSurface = img.get()
+    pygameSurface = pygame.transform.scale(pygameSurface,[main_size[0]-10,main_size[1]-10])
+
+    #player_rect = img.get_rect(center=(200, 200))
+    #window.blit(pygameSurface, pygameSurface.get_rect(center = (150, 150)))
+    window.blit(pygameSurface, pygameSurface.get_rect(topleft= (5, 5)))
+    pygame.display.flip()
+    #pg.transform.smoothscale(window,(100,200))
+    pg.time.wait(10)
+    
+
+    print(fps,x_dir,x)
+    if x_dir:
+        x+=1
+        if x > 200:
+            x_dir = 0
+    else:
+        x-=1
+        if x <= 0:
+            x_dir = 1
+
+    if start_fps+1 < time.time():
+        start_fps = time.time()
+        fps = fps_c
+        fps_c = 0
+    fps_c += 1
+
+
+
+
+
+exit()
+

+ 341 - 0
3d/demo_pyglet.py

@@ -0,0 +1,341 @@
+import pyglet
+import math
+from pyglet import shapes
+import random
+import time
+
+
+
+
+
+
+class _Particale():
+    def __init__(self,x,y,xvel,yvel,radius,color):
+        self.x = x
+        self.y = y
+        self.xvel = xvel
+        self.yvel = yvel
+        self.radius = radius
+        self.color = color
+        self.time = time.time()
+        self.start = time.time()
+        self.start2 = random.randint(1,20)/10.
+        if self.start2 > 1.8:
+            self.start2 += random.randint(1,20)/10.
+        self.colors = [(255,255,0),(255,210,0),(255,90,0)]
+        self.color = random.choice(self.colors)
+    def draw(self,win):
+        if time.time() > self.time+0.05:
+            self.x += self.xvel
+            self.y += self.yvel
+            self.time = time.time()
+        if self.start+self.start2 < time.time():
+            self.radius -= 0.1
+        #if time.time() > self.time+0.2:
+        #pygame.draw.circle(win, color, (int(self.x),int(self.y)),self.radius)
+        color = self.color
+        x= round(self.x)
+        y= round(self.y)
+        r = round(self.radius)
+        if len(color) == 3:
+            color = list(color)
+            color.append(0)
+        #pygame.gfxdraw.filled_circle(win, x,y ,r,color )#[0,0,255])
+        #pygame.gfxdraw.aacircle(win, x,y ,r,color )#[0,0,255])
+        r = round(r)
+
+        #img3 = img2.copy()
+        #img3 = colorize(img2, color ) #(0, 0, 255,15) )
+        #img3 = colorize(img2,(255, 120, 255,15) )
+        #img3 = colorize(img2,color )
+        #img3 = pygame.transform.scale(img3, (r, r))
+        #player_rect3 = img3.get_rect(center=(x,y))
+        #window.blit(img3, player_rect3)
+        if r > 0:
+            batch1 = pyglet.graphics.Batch()
+            circle = shapes.Circle(x,y, r+5, color=(50, 225, 30), batch=batch1)
+
+            batch1.draw()
+        #print("ok")    
+        return [x,0,y,0,color]
+
+class Particales():
+    def __init__(self):
+        self.data = []
+    def add(self,x,y):
+        for z in range(random.randint(1,1)):
+            s = 10
+            xvel = random.randint(0,s) -(s/2)
+            yvel = random.randint(0,s) -(s/2) 
+            r = random.randint(1,2)
+            p = _Particale(x ,y ,xvel ,yvel,r,(255,255,255))
+            self.data.append(p)
+
+    def draw(self,win=None):
+        rem = []
+        for p in self.data:
+            p.draw(win)
+            if p.radius <= 0:
+                rem.append(p)
+
+        for p in rem:
+            self.data.remove(p)
+
+particales = Particales()
+
+class Planet():
+    def __init__(self,x,y,ang=0):
+        self._pos_center = (x,y)
+        self._quadrant = 0
+            
+        self._ang = ang 
+        self._ang_dir = 1 
+        self._r  = 2 # 
+        self._orbit = 60 # orbit,umlaufbahn 
+        self._color_org = [255,255,0]
+        self._color = [0,255,0]
+        self._x=0
+        self._y=0
+        self._ix = 0
+        self._iy = 0 
+
+    def rotate(self):
+        q = 0
+
+        if self._ang_dir: 
+            self._ang += 2 # degree
+        else:
+            self._ang -= 1 # degree
+
+        if self._ang >= 360:
+            self._ang = 0 #self._ang -360
+        elif self._ang < 0:
+            self._ang = 360
+
+        ang = self._ang
+        self._quadrant = ang//90
+        ang -= self._quadrant * 90
+        
+        
+        self._ix = math.sin(math.radians(ang))*self._orbit
+        self._iy = math.sqrt(self._orbit**2 - self._ix**2) 
+    
+        y = self._iy 
+        x = self._ix 
+        if   self._quadrant == 1:
+            self._iy = -x
+            self._ix = y
+        elif self._quadrant == 2:
+            self._iy = -y
+            self._ix = -x
+        elif self._quadrant == 3:
+            self._iy = x
+            self._ix = -y
+
+
+    def draw(self,x,y):
+        self._pos_center = (x,y)
+        self.rotate()
+        self._x = int(self._pos_center[0] + self._ix)
+        self._y = int(self._pos_center[1] + self._iy)
+        if self._ang > 300:
+             f = (self._ang -300) / 60
+             f = 1-f
+             rgb = self._color_org # = [255,255,0]
+             self._color = [ int(rgb[0]*f) , int(rgb[1]*f) ,int(rgb[2]*f) ]
+        elif self._ang < 60:
+             f = self._ang / 60
+             rgb = self._color_org # = [255,255,0]
+             self._color = [ int(rgb[0]*f) , int(rgb[1]*f) ,int(rgb[2]*f) ]
+        #print("ang {} {} {:3} {:3} {}".format( self._ang,self._quadrant,self._x,self._y,self._color))
+        #print(self,"Q:",int(self._quadrant),self._ang)
+        return (self._x,self._y,self._color)
+
+
+class Animation():
+    def __init__(self,x=20,y=20,speed=1,_dir=1):
+        self.pos_x=x
+        self.pos_x_dir = 1 
+        self.pos_y=y
+        self.pos_y_dir = 1 
+        self.r = 7
+        self.r_dir = 1
+        self.speed = speed
+        self.ang = 0
+        self.ix=0
+        self.iy=0
+        self.planetes = []
+        a = 360
+        d = 3
+        for i in range(d+1):
+            i=i+1
+            p = Flow(self.pos_x,self.pos_y,ang=a/d*i) 
+            p._ang_dir = _dir 
+            self.planetes.append(p)
+
+    def rotate(self):
+        self.ix = math.sin(math.radians(0))*self.r
+        self.iy = math.sqrt(self.r**2 - self.ix**2) 
+        self.ang+=1
+        if self.ang >= 360:
+            self.ang = 0
+        
+    def draw(self,color=[255,255,255,255]):
+        self.rotate()
+        #pixel_array = pygame.PixelArray(window)
+        pixel_array = {}
+        self.color = [255,255,255,255] #pygame.Color(color[0],color[1],color[2],color[3])
+        
+        x=self.pos_x
+        y=self.pos_y
+        for i,planet in enumerate(self.planetes):
+            px,py,pcolor = planet.draw(x,y)
+            k = "{}.{}:{},{}:{}".format(i,px,px+10,py,py+10)
+            pixel_array[k] = (px,px,py,py , pcolor )
+
+
+        if self.pos_x > 300:
+            self.pos_x_dir = 0
+        if self.pos_x <= self.speed:
+            self.pos_x_dir = 1
+
+        if self.pos_x_dir:
+            self.pos_x += self.speed
+        else:
+            self.pos_x -= self.speed
+
+        if self.r > 20:
+            self.r_dir = 0
+        if self.r <=7:
+            self.r_dir = 1
+
+        if self.r_dir:
+            self.r+=1
+        else:
+            self.r-=1
+        return pixel_array
+
+class Gobo1():
+    def __init__(self,x=20,y=20,speed=1,_dir=1):
+        self.pos_x=x
+        self.pos_x_dir = 1 
+        self.pos_y=y
+        self.pos_y_dir = 1 
+        self.r = 17
+        self.r_dir = 1
+        self.speed = speed
+        self.ang = 0
+        self.ix=0
+        self.iy=0
+        self.planetes = []
+        a = 360
+        d = 3
+        for i in range(d+1):
+            i=i+1
+            p = Planet(self.pos_x,self.pos_y,ang=a/d*i) 
+            p._ang_dir = _dir 
+            self.planetes.append(p)
+
+    def rotate(self):
+        self.ix = math.sin(math.radians(0))*self.r
+        self.iy = math.sqrt(self.r**2 - self.ix**2) 
+        self.ang+=1
+        if self.ang >= 360:
+            self.ang = 0
+        
+    def draw(self,color=[255,255,255]):
+        self.rotate()
+        #pixel_array = pygame.PixelArray(window)
+        pixel_array = {}
+        self.color = [255,255,255,255] #pygame.Color(color[0],color[1],color[2])
+        
+        x=self.pos_x
+        y=self.pos_y
+        for i,planet in enumerate(self.planetes):
+            px,py,pcolor = planet.draw(x,y)
+            k = "{}.{}:{},{}:{}".format(i,px,px+10,py,py+10)
+            pixel_array[k] = (px,px,py,py , pcolor )
+
+
+        if self.pos_x > 1600:
+            self.pos_x_dir = 0
+        if self.pos_x <= self.speed:
+            self.pos_x_dir = 1
+
+        if self.pos_x_dir:
+            self.pos_x += self.speed
+        else:
+            self.pos_x -= self.speed
+
+        if self.r > 20:
+            self.r_dir = 0
+        if self.r <=7:
+            self.r_dir = 1
+
+        if self.r_dir:
+            self.r+=1
+        else:
+            self.r-=1
+        return pixel_array
+
+
+
+
+
+def display_get_width():
+    display = pyglet.canvas.get_display()
+    screen = display.get_default_screen()
+    return screen.width
+
+def display_get_height():
+    display = pyglet.canvas.get_display()
+    screen = display.get_default_screen()
+    return screen.height
+
+
+
+sw = display_get_width()
+sh = display_get_height()
+print(sw, sh)
+
+window = pyglet.window.Window(sw//2,sh//2)
+gobo1 = Gobo1()
+
+
+
+def loop(event=None):
+    #print(event)
+    batch = pyglet.graphics.Batch()
+    x = 100
+    y = 100
+    rectangle = shapes.BorderedRectangle(x, y, y+100, y+100, border=1, color=(255, 255, 255), border_color=(100, 100, 100), batch=batch)
+    rectangle2 = shapes.BorderedRectangle(x, y , x+100,y+100, border=1, color=(255, 255, 255), border_color=(100, 100, 100), batch=batch)
+    circle = shapes.Circle(x,y, 100, color=(50, 225, 30), batch=batch)
+
+
+    window.clear()
+    batch.draw()
+
+    batch1 = pyglet.graphics.Batch()
+    d1 = gobo1.draw()
+    for k in d1:
+        i = d1[k]
+        #print("i",i)
+        x=i[0] +200
+        y=i[2] +200
+        circle = shapes.Circle(x,y, 10, color=(50, 225, 30), batch=batch1)
+           
+        particales.add(x,y)
+    batch1.draw()
+    particales.draw()
+    #print("ok")    
+
+
+@window.event
+def on_draw():
+    loop()
+
+pyglet.clock.schedule_interval(loop, 0.001)
+
+
+pyglet.app.run()

+ 0 - 68
3d/img2.py

@@ -1,68 +0,0 @@
-import PIL
-import PIL.Image
-
-import os
-import time
-
-
-import pygame
-pg = pygame
-
-
-
-class IMG():
-    def __init__(self,w=300,h=200,color=[255,0,0]):
-        self.w = w
-        self.h = h
-        img = PIL.Image.new("RGB", (w, h))
-        pixels = (color[0],color[1],color[2])*(w*h)
-        img.putdata(pixels)
-
-    def draw(self,x=10,y=10,b=10,w=10):
-        img = PIL.Image.new("RGB", (200, 200))
-    
-        #img.show() # see a black image
-        pixels = [(255,0,0)]*(200*200)
-
-        for i in range(10):
-            x = (i+20)* (200 )
-            print(pixels[x])
-            for j in range(10):
-                y = j +10
-                pixels[x+y] = (255,255,255)
-
-        img.putdata(pixels)
-        self.img = img
-    
-    def get(self):
-        t = self.img.tobytes()
-        s = self.img.size
-        m = self.img.mode
-        out = pygame.image.fromstring( t,s,m).convert()
-        return out
-
-
-img = IMG()
-pygame.init()
-
-main_size=(600,300)
-window = pygame.display.set_mode(main_size,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
-#window = pygame.display.set_mode(main_size,pygame.FULLSCREEN) #x left->right ,y top-> bottom
-pg.display.set_caption('LibreLight PIL')
-
-img.draw()
-pygameSurface = img.get()
-
-run = True
-while run:
-    #pg.clock.tick(60)
-    for event in pygame.event.get():
-        if event.type == pygame.QUIT:
-            run = False
-
-    window.fill(0)
-    window.blit(pygameSurface, pygameSurface.get_rect(center = (150, 150)))
-    pygame.display.flip()
-
-exit()
-

+ 14 - 5
3d/stage_3d.py

@@ -1,6 +1,6 @@
 #! /usr/bin/python3
 # -*- coding: utf-8 -*-
-
+import time
 """
 This file is part of LibreLight.
 
@@ -200,7 +200,7 @@ def event_read():
 #pg.mixer.init() #pulsaudio assert error after some time
 pg.display.init()
 pg.key.set_repeat(1,100)
-pg.display.set_caption('LibreLight 3D Stage')
+pg.display.set_caption('LibreLight 3D Stage (Demo!)')
 display= (400,400)
 display= (800,600)
 pg.display.set_mode(display,pg.DOUBLEBUF|pg.OPENGL)
@@ -223,11 +223,16 @@ for z in [1,3,5]:
     s = Spot(v=(4,z,0))
     spots.append(s)
 frame = 0
-
+frame_time = time.time()
+fps = 0
 while True:
     try:
-        pg.display.set_caption('LibreLight 3D Stage {: 10} frame'.format(frame))
-        frame += 1
+        pg.display.set_caption('LibreLight 3D Stage {: 10} frame (DEMO!)'.format(fps))
+        if frame_time+1 < time.time():
+            frame_time = time.time()
+            fps = frame
+            frame = 0
+        
         event_read()
         
         #gl.glRotatef(1,1,1,1)
@@ -244,7 +249,11 @@ while True:
             s.draw()
         pg.display.flip()
         pg.time.wait(10)
+        pg.time.wait(10)
+
+        frame += 1
     except Exception as e:
         print("Exception ",e)
+        time.sleep(1)
 #finally:
 #    print("end -- frame:",frame)