Skip to content

Commit d2f9823

Browse files
committed
first proper commit
1 parent cc2dd9c commit d2f9823

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+3645
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
*~
12
*.gem
23
*.rbc
34
/.config

data_path/Rakefile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# encoding: utf-8
2+
# frozen_string_literal: true
3+
# Simple demo Rakefile to autorun samples in current directory
4+
# NB: If using atom install build and build-rake packages to run from atom
5+
6+
SAMPLES_DIR = './'
7+
8+
desc 'run demo'
9+
task default: [:demo]
10+
11+
desc 'demo'
12+
task :demo do
13+
samples_list.shuffle.each { |sample| run_sample sample }
14+
end
15+
16+
def samples_list
17+
files = []
18+
Dir.chdir(SAMPLES_DIR)
19+
Dir.glob('*.rb').each do |file|
20+
files << File.join(SAMPLES_DIR, file)
21+
end
22+
return files
23+
end
24+
25+
def run_sample(sample_name)
26+
puts "Running #{sample_name}...quit to run next sample"
27+
open("|jruby #{sample_name}", 'r') do |io|
28+
while l = io.gets
29+
puts(l.chop)
30+
end
31+
end
32+
end

data_path/bw_shader.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env jruby -v -W2
2+
require 'propane'
3+
4+
class BwShader < Propane::App
5+
# Texture from Jason Liebig's FLICKR collection of vintage labels and wrappers:
6+
# http://www.flickr.com/photos/jasonliebigstuff/3739263136/in/photostream/
7+
8+
attr_reader :label, :can, :angle, :bw_shader
9+
10+
def setup
11+
size(640, 360, P3D)
12+
@label = load_image(data_path('lachoy.jpg'))
13+
@can = create_can(100, 200, 32, label)
14+
@bw_shader = load_shader(data_path('bwfrag.glsl'))
15+
@angle = 0
16+
end
17+
18+
def draw
19+
background(0)
20+
shader(bw_shader)
21+
translate(width/2, height/2)
22+
rotate_y(angle)
23+
shape(can)
24+
@angle += 0.01
25+
end
26+
27+
def create_can(r, h, detail, tex)
28+
texture_mode(NORMAL)
29+
sh = create_shape
30+
sh.begin_shape(QUAD_STRIP)
31+
sh.no_stroke
32+
sh.texture(tex)
33+
(0..detail).each do |i|
34+
angle = TAU / detail
35+
x = sin(i * angle)
36+
z = cos(i * angle)
37+
u = i.to_f / detail
38+
sh.normal(x, 0, z)
39+
sh.vertex(x * r, -h/2, z * r, u, 0)
40+
sh.vertex(x * r, +h/2, z * r, u, 1)
41+
end
42+
sh.end_shape
43+
sh
44+
end
45+
end
46+
47+
BwShader.new title: 'Black & White Shader'

data_path/data/Texture01.jpg

1.35 MB
Loading

data_path/data/Texture02.jpg

1.25 MB
Loading

data_path/data/Univers45.vlw

93 KB
Binary file not shown.

data_path/data/bwfrag.glsl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
#define PROCESSING_TEXTURE_SHADER
7+
8+
uniform sampler2D texture;
9+
10+
varying vec4 vertColor;
11+
varying vec4 vertTexCoord;
12+
13+
const vec4 lumcoeff = vec4(0.299, 0.587, 0.114, 0);
14+
15+
void main() {
16+
vec4 col = texture2D(texture, vertTexCoord.st);
17+
float lum = dot(col, lumcoeff);
18+
if (0.5 < lum) {
19+
gl_FragColor = vertColor;
20+
} else {
21+
gl_FragColor = vec4(0, 0, 0, 1);
22+
}
23+
}

data_path/data/displaceFrag.glsl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
uniform sampler2D colorMap;
3+
4+
varying vec4 vertTexCoord;
5+
6+
void main() {
7+
gl_FragColor = texture2D(colorMap, vertTexCoord.st);
8+
}

data_path/data/displaceVert.glsl

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
2+
#define PROCESSING_TEXLIGHT_SHADER
3+
4+
uniform mat4 modelview;
5+
uniform mat4 transform;
6+
uniform mat3 normalMatrix;
7+
uniform mat4 texMatrix;
8+
9+
uniform int lightCount;
10+
uniform vec4 lightPosition[8];
11+
uniform vec3 lightNormal[8];
12+
uniform vec3 lightAmbient[8];
13+
uniform vec3 lightDiffuse[8];
14+
uniform vec3 lightSpecular[8];
15+
uniform vec3 lightFalloff[8];
16+
uniform vec2 lightSpot[8];
17+
18+
attribute vec4 vertex;
19+
attribute vec4 color;
20+
attribute vec3 normal;
21+
attribute vec2 texCoord;
22+
23+
attribute vec4 ambient;
24+
attribute vec4 specular;
25+
attribute vec4 emissive;
26+
attribute float shininess;
27+
28+
varying vec4 vertColor;
29+
varying vec4 vertTexCoord;
30+
31+
const float zero_float = 0.0;
32+
const float one_float = 1.0;
33+
const vec3 zero_vec3 = vec3(0);
34+
35+
uniform float displaceStrength;
36+
uniform float time;
37+
38+
float falloffFactor(vec3 lightPos, vec3 vertPos, vec3 coeff) {
39+
vec3 lpv = lightPos - vertPos;
40+
vec3 dist = vec3(one_float);
41+
dist.z = dot(lpv, lpv);
42+
dist.y = sqrt(dist.z);
43+
return one_float / dot(dist, coeff);
44+
}
45+
46+
float spotFactor(vec3 lightPos, vec3 vertPos, vec3 lightNorm, float minCos, float spotExp) {
47+
vec3 lpv = normalize(lightPos - vertPos);
48+
vec3 nln = -one_float * lightNorm;
49+
float spotCos = dot(nln, lpv);
50+
return spotCos <= minCos ? zero_float : pow(spotCos, spotExp);
51+
}
52+
53+
float lambertFactor(vec3 lightDir, vec3 vecNormal) {
54+
return max(zero_float, dot(lightDir, vecNormal));
55+
}
56+
57+
float blinnPhongFactor(vec3 lightDir, vec3 vertPos, vec3 vecNormal, float shine) {
58+
vec3 np = normalize(vertPos);
59+
vec3 ldp = normalize(lightDir - np);
60+
return pow(max(zero_float, dot(ldp, vecNormal)), shine);
61+
}
62+
63+
// Source code for GLSL Perlin noise courtesy of:
64+
// https://github.com/ashima/webgl-noise/wiki
65+
66+
vec3 mod289(vec3 x) {
67+
return x - floor(x * (1.0 / 289.0)) * 289.0;
68+
}
69+
70+
vec2 mod289(vec2 x) {
71+
return x - floor(x * (1.0 / 289.0)) * 289.0;
72+
}
73+
74+
vec3 permute(vec3 x) {
75+
return mod289(((x*34.0)+1.0)*x);
76+
}
77+
78+
float snoise(vec2 v)
79+
{
80+
const vec4 C = vec4(0.211324865405187, // (3.0-sqrt(3.0))/6.0
81+
0.366025403784439, // 0.5*(sqrt(3.0)-1.0)
82+
-0.577350269189626, // -1.0 + 2.0 * C.x
83+
0.024390243902439); // 1.0 / 41.0
84+
// First corner
85+
vec2 i = floor(v + dot(v, C.yy) );
86+
vec2 x0 = v - i + dot(i, C.xx);
87+
88+
// Other corners
89+
vec2 i1;
90+
//i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
91+
//i1.y = 1.0 - i1.x;
92+
i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
93+
// x0 = x0 - 0.0 + 0.0 * C.xx ;
94+
// x1 = x0 - i1 + 1.0 * C.xx ;
95+
// x2 = x0 - 1.0 + 2.0 * C.xx ;
96+
vec4 x12 = x0.xyxy + C.xxzz;
97+
x12.xy -= i1;
98+
99+
// Permutations
100+
i = mod289(i); // Avoid truncation effects in permutation
101+
vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
102+
+ i.x + vec3(0.0, i1.x, 1.0 ));
103+
104+
vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
105+
m = m*m ;
106+
m = m*m ;
107+
108+
// Gradients: 41 points uniformly over a line, mapped onto a diamond.
109+
// The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
110+
111+
vec3 x = 2.0 * fract(p * C.www) - 1.0;
112+
vec3 h = abs(x) - 0.5;
113+
vec3 ox = floor(x + 0.5);
114+
vec3 a0 = x - ox;
115+
116+
// Normalise gradients implicitly by scaling m
117+
// Approximation of: m *= inversesqrt( a0*a0 + h*h );
118+
m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
119+
120+
// Compute final noise value at P
121+
vec3 g;
122+
g.x = a0.x * x0.x + h.x * x0.y;
123+
g.yz = a0.yz * x12.xz + h.yz * x12.yw;
124+
return 130.0 * dot(m, g);
125+
}
126+
127+
void main() {
128+
// Calculating texture coordinates, with r and q set both to one
129+
vertTexCoord = texMatrix * vec4(texCoord, 1.0, 1.0);
130+
131+
vec2 p = texCoord; // put coordinates into vec2 p for convenience
132+
p.x += time; // add time to make the noise and the subsequent displacement move
133+
float df = snoise( p ); // create displacement float value from shader-based 2D Perlin noise
134+
vec4 newVertexPos = vertex + vec4(normal * df * displaceStrength, 0.0); // regular vertex position + direction * displacementMap * displaceStrength
135+
136+
// Vertex in clip coordinates
137+
gl_Position = transform * newVertexPos;
138+
139+
// Vertex in eye coordinates
140+
vec3 ecVertex = vec3(modelview * vertex);
141+
142+
// Normal vector in eye coordinates
143+
vec3 ecNormal = normalize(normalMatrix * normal);
144+
145+
if (dot(-one_float * ecVertex, ecNormal) < zero_float) {
146+
// If normal is away from camera, choose its opposite.
147+
// If we add backface culling, this will be backfacing
148+
ecNormal *= -one_float;
149+
}
150+
151+
// Light calculations
152+
vec3 totalAmbient = vec3(0, 0, 0);
153+
vec3 totalDiffuse = vec3(0, 0, 0);
154+
vec3 totalSpecular = vec3(0, 0, 0);
155+
for (int i = 0; i < 8; i++) {
156+
if (lightCount == i) break;
157+
158+
vec3 lightPos = lightPosition[i].xyz;
159+
bool isDir = zero_float < lightPosition[i].w;
160+
float spotCos = lightSpot[i].x;
161+
float spotExp = lightSpot[i].y;
162+
163+
vec3 lightDir;
164+
float falloff;
165+
float spotf;
166+
167+
if (isDir) {
168+
falloff = one_float;
169+
lightDir = -one_float * lightNormal[i];
170+
} else {
171+
falloff = falloffFactor(lightPos, ecVertex, lightFalloff[i]);
172+
lightDir = normalize(lightPos - ecVertex);
173+
}
174+
175+
spotf = spotExp > zero_float ? spotFactor(lightPos, ecVertex, lightNormal[i],
176+
spotCos, spotExp)
177+
: one_float;
178+
179+
if (any(greaterThan(lightAmbient[i], zero_vec3))) {
180+
totalAmbient += lightAmbient[i] * falloff;
181+
}
182+
183+
if (any(greaterThan(lightDiffuse[i], zero_vec3))) {
184+
totalDiffuse += lightDiffuse[i] * falloff * spotf *
185+
lambertFactor(lightDir, ecNormal);
186+
}
187+
188+
if (any(greaterThan(lightSpecular[i], zero_vec3))) {
189+
totalSpecular += lightSpecular[i] * falloff * spotf *
190+
blinnPhongFactor(lightDir, ecVertex, ecNormal, shininess);
191+
}
192+
}
193+
194+
// Calculating final color as result of all lights (plus emissive term).
195+
// Transparency is determined exclusively by the diffuse component.
196+
vertColor = vec4(totalAmbient, 0) * ambient +
197+
vec4(totalDiffuse, 1) * color +
198+
vec4(totalSpecular, 0) * specular +
199+
vec4(emissive.rgb, 0);
200+
201+
}

data_path/data/lachoy.jpg

193 KB
Loading

0 commit comments

Comments
 (0)