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