Skip to content

Commit

Permalink
new shaders and Jukebox demo
Browse files Browse the repository at this point in the history
  • Loading branch information
paddywwoof committed Sep 11, 2013
1 parent d6ef370 commit 4af979e
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 166 deletions.
135 changes: 135 additions & 0 deletions Jukebox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/usr/bin/python
from __future__ import absolute_import, division, print_function, unicode_literals

""" Music and animation with changing images. Needs some mp3 files in the
music subdirectory and mpg321 installed
"""
import math, random, time, glob, threading
from subprocess import Popen, PIPE, STDOUT

import demo
import pi3d

def _tex_load(tex_list, slot, fName):
tex_list[slot] = pi3d.Texture(fName)

# Setup display and initialise pi3d
DISPLAY = pi3d.Display.create(x=50, y=50, frames_per_second=40)
DISPLAY.set_background(0.4, 0.6, 0.8, 1.0) # r,g,b,alpha

#persp_cam = pi3d.Camera.instance() # default instance camera perspecive view

#setup textures, light position and initial model position
pi3d.Light((0, 5, 0))
#create shaders
shader = pi3d.Shader("star")
flatsh = pi3d.Shader("uv_flat")
post = pi3d.PostProcess("shaders/filter_outline")

#Create textures
tFiles = glob.glob("textures/*.*")
nTex = len(tFiles)
slot = 0
tex_list = [pi3d.Texture(tFiles[slot]), None] #ensure first texture there
slot = 1
#next texture load in background
t = threading.Thread(target=_tex_load, args=(tex_list, slot % 2, tFiles[slot % nTex]))
t.daemon = True
t.start()

#Create shape
myshape = pi3d.MergeShape()
asphere = pi3d.Sphere(sides=32, slices=32)
myshape.radialCopy(asphere, step=72)
myshape.position(0.0, 0.0, 5.0)
myshape.set_draw_details(shader, [tex_list[0]], 8.0, 0.1)

mysprite = pi3d.Sprite(w=15.0, h=15.0)
mysprite.position(0.0, 0.0, 15.0)
mysprite.set_draw_details(flatsh, [tex_list[0]])

# Fetch key presses.
mykeys = pi3d.Keyboard()
pic_next = 5.0
pic_dt = 5.0
tm = 0.0
dt = 0.02
sc = 0.0
ds = 0.001
x = 0.0
dx = 0.001

mFiles = glob.glob("music/*.mp3")
random.shuffle(mFiles)
nMusic = len(mFiles)
iMusic = 0

p = Popen(['mpg321', '-R', '-F', 'testPlayer'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
p.stdin.write(b'LOAD ' + mFiles[iMusic] + b'\n')
rgb = {48:0.0, 49:0.0, 50:0.0}
mx, my = 1.0, 1.0
dx, dy = 1.0, 1.0
# Display scene and rotate shape
while DISPLAY.loop_running():
tm = tm + dt
sc = (sc + ds) % 10.0
myshape.set_custom_data(48, [tm, sc, -0.5 * sc])
post.start_capture()
# 1. drawing objects now renders to an offscreen texture ####################

mysprite.draw()
myshape.draw()

post.end_capture()
# 2. drawing now back to screen. The texture can now be used by post.draw()

# 3. redraw these two objects applying a shader effect ###############
#read several lines so the video frame rate doesn't restrict the music
#TODO ought to be a better way of doing this.
for i in range(11):
l = p.stdout.readline()
if b'@P' in l:
iMusic = (iMusic + 1) % nMusic
p.stdin.write(b'LOAD ' + mFiles[iMusic] + b'\n')
if b'FFT' in l: #frequency analysis
val_str = l.split()
rgb[48] = float(val_str[2]) / 115.0
rgb[49] = float(val_str[6]) / 115.0
rgb[50] = float(val_str[10]) / 115.0
post.draw(rgb)

if mx > 3.0:
dx = -1.0
elif mx < 0.0:
dx = 1.0
if my > 5.0:
dy = -1.0
elif my < 0.0:
dy = 1.0
mx += dx * rgb[49] / 100.0
my += dy * rgb[50] / 50.0
myshape.scale(mx, my, mx)
myshape.rotateIncY(0.6471 + rgb[48])
myshape.rotateIncX(0.513 - rgb[50])
mysprite.rotateIncZ(0.5)

if tm > pic_next:
"""change the pictures and start a thread to load into tex_list"""
pic_next += pic_dt
myshape.set_draw_details(shader, [tex_list[slot % 2]])
mysprite.set_draw_details(flatsh, [tex_list[slot % 2]])
slot += 1
t = threading.Thread(target=_tex_load, args=(tex_list, slot % 2, tFiles[slot % nTex]))
t.daemon = True
t.start()


k = mykeys.read()
if k==112:
pi3d.screenshot("post.jpg")
elif k==27:
mykeys.close()
DISPLAY.destroy()
break

p.stdin.write(b'QUIT\n')
30 changes: 0 additions & 30 deletions Pi3d2.py

This file was deleted.

97 changes: 0 additions & 97 deletions Post2.py

This file was deleted.

Binary file added music/60miles.mp3
Binary file not shown.
39 changes: 0 additions & 39 deletions post_base1.fs

This file was deleted.

23 changes: 23 additions & 0 deletions shaders/filter_blurradial.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/////RADIAL BLUR FILTER/////
//www.cloneproduction.net

precision mediump float;
varying vec2 uv;
uniform sampler2D tex0;
uniform sampler2D tex1;

vec2 BlurXY = vec2 (-0.4, -0.0); //blur center position
float Amount = 0.0; //blur radial amount
float BlurR = 0.3; //blur rotation amount

void main(void){
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
vec2 piv = vec2(BlurXY.x+0.5, BlurXY.y-0.5);
float wd = texture2D(tex1,uv).x;
vec2 d = (uv - piv); // from centre to this pixel
// use radial vec = -dy, dx
d = d * Amount / 5.0 + vec2(-d.y, d.x) * BlurR / 5.0;
for (float i=0.0; i<1.0; i+=0.125){
gl_FragColor += texture2D(tex0, uv + d * pow(2.0, i)) * 0.125;
}
}
17 changes: 17 additions & 0 deletions shaders/filter_blurradial.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
precision highp float;

attribute vec3 vertex;
attribute vec2 texcoord;

uniform mat4 modelviewmatrix[2]; // 0 model movement in real coords, 1 in camera coords
uniform vec3 unib[4];
//uniform vec2 umult, vmult => unib[2]
//uniform vec2 u_off, v_off => unib[3]

varying vec2 uv;

void main(void) {
uv = texcoord * unib[2].xy + unib[3].xy;
uv.y = -uv.y;
gl_Position = modelviewmatrix[1] * vec4(vertex,1.0);
}
17 changes: 17 additions & 0 deletions shaders/filter_charcoal.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/////CHARCOAL FILTER/////
//www.cloneproduction.net

precision mediump float;
varying vec2 uv;
varying vec2 d;
uniform sampler2D tex0;

void main(void){
vec4 c1 = texture2D(tex0, uv);
vec4 c2 = texture2D(tex0, uv + vec2(d.x, 0));
vec4 c3 = texture2D(tex0, uv + vec2(0, d.y));
float f = distance(c1.rgb, c2.rgb) + distance(c1.rgb, c3.rgb) - 0.2;
f = clamp(f, 0.0, 1.0);
c1.rgb = vec3(1.0, 1.0, 1.0) * (1.0 - f);
gl_FragColor = c1;
}
24 changes: 24 additions & 0 deletions shaders/filter_charcoal.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
precision highp float;

attribute vec3 vertex;
attribute vec2 texcoord;

uniform mat4 modelviewmatrix[2]; // 0 model movement in real coords, 1 in camera coords
uniform vec3 unib[4];
//uniform vec2 umult, vmult => unib[2]
//uniform vec2 u_off, v_off => unib[3]
uniform vec3 unif[20];
//w => unif[15][0] set in PostProcess to display size
//h => unif[15][1]

float width = 4.0; //charcoal line width

varying vec2 uv;
varying vec2 d;

void main(void) {
uv = texcoord * unib[2].xy + unib[3].xy;
uv.y = -uv.y; // flip vertically
d = vec2(width / unif[15][0], width / unif[15][1]);
gl_Position = modelviewmatrix[1] * vec4(vertex,1.0);
}
Loading

0 comments on commit 4af979e

Please sign in to comment.