Skip to content

Commit c131dfd

Browse files
tctrdanoli3
authored andcommitted
Android example : SHADER WORKS
forgot to commit the data folder no need to put paths to folders under bin/data (cherry picked from commit f258569)
1 parent ce62fc8 commit c131dfd

11 files changed

Lines changed: 158 additions & 13 deletions

File tree

examples/android/androidEmptyExample/ofApp/src/main/bin/data/.gitkeep

Whitespace-only changes.
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifdef GL_ES
2+
// define default precision for float, vec, mat.
3+
precision highp float;
4+
#endif
5+
6+
uniform vec4 globalColor;
7+
8+
void main(){
9+
//this is the fragment shader
10+
//this is where the pixel level drawing happens
11+
//gl_FragCoord gives us the x and y of the current pixel its drawing
12+
13+
//we grab the x and y and store them in an int
14+
float xVal = gl_FragCoord.x;
15+
float yVal = gl_FragCoord.y;
16+
17+
//we use the mod function to only draw pixels if they are every 2 in x or every 4 in y
18+
if( mod(xVal, 2.0) == 0.5 && mod(yVal, 4.0) == 0.5 ){
19+
gl_FragColor = globalColor;
20+
}else{
21+
discard;
22+
}
23+
24+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#ifdef GL_ES
2+
// define default precision for float, vec, mat.
3+
precision highp float;
4+
#endif
5+
6+
attribute vec4 position;
7+
8+
uniform mat4 modelViewProjectionMatrix;
9+
10+
uniform float timeValX;
11+
uniform float timeValY;
12+
uniform vec2 mouse;
13+
14+
//generate a random value from four points
15+
vec4 rand(vec2 A,vec2 B,vec2 C,vec2 D){
16+
17+
vec2 s=vec2(12.9898,78.233);
18+
vec4 tmp=vec4(dot(A,s),dot(B,s),dot(C,s),dot(D,s));
19+
20+
return fract(sin(tmp) * 43758.5453)* 2.0 - 1.0;
21+
}
22+
23+
//this is similar to a perlin noise function
24+
float noise(vec2 coord,float d){
25+
26+
vec2 C[4];
27+
28+
float d1 = 1.0/d;
29+
30+
C[0]=floor(coord*d)*d1;
31+
32+
C[1]=C[0]+vec2(d1,0.0);
33+
34+
C[2]=C[0]+vec2(d1,d1);
35+
36+
C[3]=C[0]+vec2(0.0,d1);
37+
38+
39+
vec2 p=fract(coord*d);
40+
41+
vec2 q=1.0-p;
42+
43+
vec4 w=vec4(q.x*q.y,p.x*q.y,p.x*p.y,q.x*p.y);
44+
45+
return dot(vec4(rand(C[0],C[1],C[2],C[3])),w);
46+
}
47+
48+
49+
void main(){
50+
51+
//get our current vertex position so we can modify it
52+
vec4 pos = modelViewProjectionMatrix * position;
53+
54+
//generate some noise values based on vertex position and the time value which comes in from our OF app
55+
float noiseAmntX = noise( vec2(-timeValX + pos.x / 1000.0, 100.0), 20.0 );
56+
float noiseAmntY = noise( vec2(timeValY + pos.y / 1000.0, pos.x / 2000.0), 20.0 );
57+
58+
//generate noise for our blue pixel value
59+
float noiseB = noise( vec2(timeValY * 0.25, pos.y / 2000.0), 20.0 );
60+
61+
//lets also figure out the distance between the mouse and the vertex and apply a repelling force away from the mouse
62+
vec2 d = vec2(pos.x, pos.y) - mouse;
63+
float len = sqrt(d.x*d.x + d.y*d.y);
64+
if( len < 300.0 && len > 0.0 ){
65+
66+
//lets get the distance into 0-1 ranges
67+
float pct = len / 300.0;
68+
69+
//this turns our linear 0-1 value into a curved 0-1 value
70+
pct *= pct;
71+
72+
//flip it so the closer we are the greater the repulsion
73+
pct = 1.0 - pct;
74+
75+
//normalize our repulsion vector
76+
d /= len;
77+
78+
//apply the repulsion to our position
79+
pos.x += d.x * pct * 90.0;
80+
pos.y += d.y * pct * 90.0;
81+
}
82+
83+
//modify our position with the smooth noise
84+
pos.x += noiseAmntX * 20.0;
85+
pos.y += noiseAmntY * 10.0;
86+
87+
//finally set the pos to be that actual position rendered
88+
gl_Position = pos;
89+
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

examples/android/androidEmptyExample/ofApp/src/main/cpp/ofApp.cpp

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,26 @@ void ofApp::setup(){
77
ofSetVerticalSync(false);
88
ofEnableAlphaBlending();
99

10-
bool loadok = font.load("verdana.ttf", 100, true, false, true, 0.4, 72);
10+
bool loadok = font.load("verdana.ttf", 200, true, false, true, 0.4, 72);
11+
shader.load("shaders/noise.vert", "shaders/noise.frag");
1112

13+
auto textShapes = font.getStringAsPoints("openFrameworks");
14+
for (auto glyph : textShapes) {
15+
text.append(glyph);
16+
}
1217

18+
ofRectangle boundingBox;
19+
for (auto outline : text.getOutline()) {
20+
boundingBox = boundingBox.getUnion(outline.getBoundingBox());
21+
}
22+
23+
boundingBox.alignTo(ofGetCurrentViewport(), OF_ALIGN_HORZ_CENTER, OF_ALIGN_VERT_CENTER);
24+
glm::vec2 textPos(boundingBox.getX(), boundingBox.getMaxY());
25+
26+
text.translate(textPos);
27+
text.setFillColor(ofColor(245, 58, 135));
28+
29+
doShader = false;
1330
}
1431

1532
void ofApp::exit(){
@@ -24,20 +41,34 @@ void ofApp::update(){
2441
//--------------------------------------------------------------
2542
void ofApp::draw(){
2643

27-
int r = 128 + 128 * cosf(ofGetElapsedTimef());
44+
int r = 128 + 50 * cosf(ofGetElapsedTimef());
2845
int g = 0;
29-
int b = 128 + 128 * sinf(ofGetElapsedTimef());
46+
int b = 128 + 50 * sinf(ofGetElapsedTimef());
3047

3148
ofBackground(r,g,b);
49+
50+
//ofSetColor(245, 58, 135);
51+
//ofFill();
52+
//font.drawStringAsShapes("meditate", 300, 400);
3253

33-
ofSetColor(225);
34-
ofDrawBitmapString("ANDROID WORKING", 200, 300);
54+
if( doShader ){
55+
shader.begin();
56+
//we want to pass in some varrying values to animate our type / color
57+
shader.setUniform1f("timeValX", ofGetElapsedTimef() * 0.1 );
58+
shader.setUniform1f("timeValY", -ofGetElapsedTimef() * 0.18 );
59+
60+
//we also pass in the mouse position
61+
//we have to transform the coords to what the shader is expecting which is 0,0 in the center and y axis flipped.
62+
shader.setUniform2f("mouse", ofGetMouseX() - ofGetWidth()/2, ofGetHeight()/2-ofGetMouseY() );
3563

64+
}
3665

37-
ofSetColor(245, 58, 135);
38-
ofFill();
66+
//finally draw our text
67+
text.draw();
3968

40-
font.drawStringAsShapes("meditate", 300, 400);
69+
if( doShader ){
70+
shader.end();
71+
}
4172

4273

4374

@@ -60,7 +91,7 @@ void ofApp::windowResized(int w, int h){
6091

6192
//--------------------------------------------------------------
6293
void ofApp::touchDown(int x, int y, int id){
63-
94+
doShader = true;
6495
}
6596

6697
//--------------------------------------------------------------
@@ -70,6 +101,7 @@ void ofApp::touchMoved(int x, int y, int id){
70101

71102
//--------------------------------------------------------------
72103
void ofApp::touchUp(int x, int y, int id){
104+
doShader = false;
73105

74106
}
75107

0 commit comments

Comments
 (0)