| 1 |
// |
| 2 |
// Input |
| 3 |
// |
| 4 |
#version 400 |
| 5 |
precision highp float; |
| 6 |
// sphere position: model coordinates |
| 7 |
in vec3 a_pos; |
| 8 |
// sphere corner: [-r, -r], [r, -r], [r, r], [-r, r] |
| 9 |
in vec2 a_corner; |
| 10 |
// offset for the center of the sphere's AO map texture tile |
| 11 |
in vec2 a_tileOffset; |
| 12 |
// color: RGB |
| 13 |
in vec3 a_color; |
| 14 |
|
| 15 |
// |
| 16 |
// Output |
| 17 |
// |
| 18 |
|
| 19 |
// normalized corner |
| 20 |
out vec2 v_corner; |
| 21 |
// color |
| 22 |
out vec3 v_color; |
| 23 |
// position in eye-coordinate space |
| 24 |
out vec4 v_eyePos; |
| 25 |
// sphere radius |
| 26 |
out float v_radius; |
| 27 |
// AO tile offset |
| 28 |
out vec2 v_tileOffset; |
| 29 |
|
| 30 |
// |
| 31 |
// Uniforms |
| 32 |
// |
| 33 |
|
| 34 |
// model-view matrix |
| 35 |
uniform mat4 u_modelView; |
| 36 |
// projection matrix |
| 37 |
uniform mat4 u_projection; |
| 38 |
|
| 39 |
#define CONTOUR_LINES |
| 40 |
#ifdef CONTOUR_LINES |
| 41 |
const float contourWidth = 0.3; |
| 42 |
#endif |
| 43 |
|
| 44 |
|
| 45 |
void main() |
| 46 |
{ |
| 47 |
// pass through AO tile offset & color |
| 48 |
v_tileOffset = a_tileOffset; |
| 49 |
v_color = a_color; |
| 50 |
// extract radius from unnormalized corner attribute |
| 51 |
v_radius = abs(a_corner.x); |
| 52 |
// normalize corner to be in [-1, 1] range |
| 53 |
#ifdef CONTOUR_LINES |
| 54 |
v_corner = a_corner / v_radius + sign(a_corner) * vec2(contourWidth, contourWidth); |
| 55 |
#else |
| 56 |
v_corner = a_corner / v_radius; |
| 57 |
#endif |
| 58 |
// compute eye-coordinate sphere position |
| 59 |
v_eyePos = u_modelView * vec4(a_pos, 1.0); |
| 60 |
|
| 61 |
// test if the closest point on the sphere would be clipped |
| 62 |
vec4 clipTestNear = v_eyePos; |
| 63 |
// z-axis points to eye (camera), add radius to sphere position |
| 64 |
clipTestNear.z += v_radius; |
| 65 |
// project the eye-coordinate sphere position |
| 66 |
clipTestNear = u_projection * clipTestNear; |
| 67 |
if (clipTestNear.z > -clipTestNear.w) { |
| 68 |
// not clipped, calculate clip coordinate |
| 69 |
gl_Position = v_eyePos; |
| 70 |
#ifdef CONTOUR_LINES |
| 71 |
gl_Position.xy += a_corner + sign(a_corner) * vec2(contourWidth, contourWidth); |
| 72 |
//gl_Position.xy += a_corner; |
| 73 |
#else |
| 74 |
gl_Position.xy += a_corner; |
| 75 |
#endif |
| 76 |
gl_Position = u_projection * gl_Position; |
| 77 |
} else { |
| 78 |
// clipped, invalidate the clip coordinate to ensure that it will be clipped |
| 79 |
gl_Position.w = 0.0; |
| 80 |
} |
| 81 |
} |