Shader cleanup

This commit is contained in:
Atlante45 2015-11-23 16:11:23 -08:00
parent 28c8cf26f5
commit d08a4d77dc
2 changed files with 31 additions and 39 deletions

View file

@ -42,6 +42,14 @@ in vec2 inColor; // This is actual Lifetime + Seed
out vec4 varColor; out vec4 varColor;
out vec2 varTexcoord; out vec2 varTexcoord;
const int NUM_VERTICES_PER_PARTICLE = 4;
const vec4 UNIT_QUAD[NUM_VERTICES_PER_PARTICLE] = vec4[NUM_VERTICES_PER_PARTICLE](
vec4(-1.0, -1.0, 0.0, 1.0),
vec4(1.0, -1.0, 0.0, 1.0),
vec4(-1.0, 1.0, 0.0, 1.0),
vec4(1.0, 1.0, 0.0, 1.0)
);
float bezierInterpolate(float y1, float y2, float y3, float u) { float bezierInterpolate(float y1, float y2, float y3, float u) {
// https://en.wikipedia.org/wiki/Bezier_curve // https://en.wikipedia.org/wiki/Bezier_curve
return (1.0 - u) * (1.0 - u) * y1 + 2.0 * (1.0 - u) * u * y2 + u * u * y3; return (1.0 - u) * (1.0 - u) * y1 + 2.0 * (1.0 - u) * u * y2 + u * u * y3;
@ -100,45 +108,29 @@ vec4 interpolate3Vec4(vec4 y1, vec4 y2, vec4 y3, float u) {
void main(void) { void main(void) {
const int NUM_VERTICES_PER_PARTICLE = 4;
const vec4 UNIT_QUAD[NUM_VERTICES_PER_PARTICLE] = vec4[NUM_VERTICES_PER_PARTICLE](
vec4(-1.0, -1.0, 0.0, 1.0),
vec4(1.0, -1.0, 0.0, 1.0),
vec4(-1.0, 1.0, 0.0, 1.0),
vec4(1.0, 1.0, 0.0, 1.0)
);
float age = inColor.x / particle.lifespan;
float seed = inColor.y;
// anchor point in eye space
vec4 anchorPoint = vec4(inPosition.xyz, 1.0);
float radius = interpolate3Floats(particle.radius.start, particle.radius.middle, particle.radius.finish , age);
TransformCamera cam = getTransformCamera(); TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject(); TransformObject obj = getTransformObject();
<$transformModelToEyePos(cam, obj, anchorPoint, anchorPoint)$>
// Which icon are we dealing with ? // Which icon are we dealing with ?
int particleID = gl_VertexID / NUM_VERTICES_PER_PARTICLE; int particleID = gl_VertexID / NUM_VERTICES_PER_PARTICLE;
// Which quad vertex pos? // Which quad vertex pos?
int twoTriID = gl_VertexID - particleID * NUM_VERTICES_PER_PARTICLE; int twoTriID = gl_VertexID - particleID * NUM_VERTICES_PER_PARTICLE;
vec4 quadPos = radius * UNIT_QUAD[twoTriID];
// Particle properties
vec4 clipPos; float age = inColor.x / particle.lifespan;
vec4 eyePos = vec4(anchorPoint.xyz + quadPos.xyz, 1.0); float seed = inColor.y;
<$transformEyeToClipPos(cam, eyePos, clipPos)$>
gl_Position = clipPos;
// Pass the texcoord and the z texcoord is representing the texture icon // Pass the texcoord and the z texcoord is representing the texture icon
varTexcoord = vec2((UNIT_QUAD[twoTriID].xy + 1.0) * 0.5); varTexcoord = vec2((UNIT_QUAD[twoTriID].xy + 1.0) * 0.5);
varColor = interpolate3Vec4(particle.color.start, particle.color.middle, particle.color.finish, age); varColor = interpolate3Vec4(particle.color.start, particle.color.middle, particle.color.finish, age);
// if (inColor.x == 0.0) { // anchor point in eye space
// varColor = vec4(0, 1, 0, 1); float radius = interpolate3Floats(particle.radius.start, particle.radius.middle, particle.radius.finish , age);
// } else { vec4 quadPos = radius * UNIT_QUAD[twoTriID];
// varColor = vec4(1, 0, 0, 1);
// } vec4 anchorPoint;
<$transformModelToEyePos(cam, obj, inPosition, anchorPoint)$>
vec4 eyePos = anchorPoint + quadPos;
<$transformEyeToClipPos(cam, eyePos, gl_Position)$>
} }

View file

@ -74,15 +74,6 @@ TransformCamera getTransformCamera() {
} }
<@endfunc@> <@endfunc@>
<@func $transformModelToEyePos(cameraTransform, objectTransform, modelPos, eyePos)@>
<!// Equivalent to the following but hoppefully a tad more accurate
//return camera._view * object._model * pos; !>
{ // transformModelToEyePos
vec4 _worldpos = (<$objectTransform$>._model * vec4(<$modelPos$>.xyz, 1.0));
<$eyePos$> = (<$cameraTransform$>._view * _worldpos);
}
<@endfunc@>
<@func $transformInstancedModelToEyeAndClipPos(cameraTransform, objectTransform, modelPos, eyePos, clipPos)@> <@func $transformInstancedModelToEyeAndClipPos(cameraTransform, objectTransform, modelPos, eyePos, clipPos)@>
<!// Equivalent to the following but hoppefully a tad more accurate <!// Equivalent to the following but hoppefully a tad more accurate
//return camera._projection * camera._view * object._model * pos; !> //return camera._projection * camera._view * object._model * pos; !>
@ -148,6 +139,15 @@ TransformCamera getTransformCamera() {
} }
<@endfunc@> <@endfunc@>
<@func $transformModelToEyePos(cameraTransform, objectTransform, modelPos, eyePos)@>
<!// Equivalent to the following but hoppefully a tad more accurate
//return camera._view * object._model * pos; !>
{ // transformModelToEyePos
vec4 _worldpos = (<$objectTransform$>._model * vec4(<$modelPos$>.xyz, 1.0));
<$eyePos$> = (<$cameraTransform$>._view * _worldpos);
}
<@endfunc@>
<@func transformEyeToClipPos(cameraTransform, eyePos, clipPos)@> <@func transformEyeToClipPos(cameraTransform, eyePos, clipPos)@>
{ // transformEyeToClipPos { // transformEyeToClipPos
<$clipPos$> = <$cameraTransform$>._projection * vec4(<$eyePos$>.xyz, 1.0); <$clipPos$> = <$cameraTransform$>._projection * vec4(<$eyePos$>.xyz, 1.0);