Last active
August 29, 2015 13:56
-
-
Save atduskgreg/8905776 to your computer and use it in GitHub Desktop.
bounce ball shader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fragment shader | |
#version 150 | |
uniform float time; | |
uniform vec2 mouse; | |
uniform vec2 startingPosition; | |
uniform float gravity; | |
uniform vec2 initialVelocity; | |
uniform float ballRadius; | |
uniform float rebound; | |
uniform int numBounces; | |
out vec4 outputColor; | |
void main() | |
{ | |
float x = startingPosition.x + initialVelocity.x * time; | |
float y = startingPosition.y; | |
float prevBounce = 0; | |
for(int n = 0; n < numBounces; n++){ | |
float thisBounce = (initialVelocity.y * pow(rebound,n-1))/gravity + prevBounce; | |
if(time > prevBounce && time < thisBounce){ | |
y = startingPosition.y + initialVelocity.y*pow(rebound,n-1) * (time - prevBounce) - gravity*pow(time-prevBounce,2); | |
} | |
prevBounce = thisBounce; | |
} | |
vec2 ball = vec2(x,y); | |
vec2 pos = (gl_FragCoord.xy - ball); | |
float dist_squared = dot(pos, pos); | |
if (dist_squared < ballRadius){ | |
outputColor = vec4(1,1,1,1); | |
} else { | |
outputColor = vec4(0,0,0,1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vertex shader (passthrough) | |
#version 150 | |
uniform mat4 modelViewProjectionMatrix; | |
in vec4 position; | |
void main(){ | |
gl_Position = modelViewProjectionMatrix * position; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment