Skip to content

Instantly share code, notes, and snippets.

@atduskgreg
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atduskgreg/8905776 to your computer and use it in GitHub Desktop.
Save atduskgreg/8905776 to your computer and use it in GitHub Desktop.
bounce ball shader
// 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);
}
}
// 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