| 1 |
/* Vertex shader utilities used by both Gouraud and Phong shading. |
| 2 |
|
| 3 |
Used by ../castlerendererinternalshader.pas to construct the final shader. |
| 4 |
When you change this file, rerun `make' and then recompile Pascal sources. |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifdef CASTLE_NEEDS_MIRROR_PLANE_TEX_COORDS |
| 8 |
uniform vec4 castle_NormalizedPlane; |
| 9 |
uniform vec3 castle_CameraPositionOnPlane; |
| 10 |
uniform vec3 castle_CameraSide; |
| 11 |
uniform vec3 castle_CameraUp; |
| 12 |
uniform vec4 castle_FrustumDimensions; |
| 13 |
|
| 14 |
/* Calculate texture coordinates matching ViewpointMirror texture projection. */ |
| 15 |
vec3 castle_generate_tex_coords_mirror_plane(const in vec4 vertex_world) |
| 16 |
{ |
| 17 |
/* The same implemented on CPU in Pascal (with Coord = vertex_world): |
| 18 |
|
| 19 |
PlaneCoord := PointOnPlaneClosestToPoint(Plane, Coord) - CameraPositionOnPlane; |
| 20 |
PlaneCoordProjected := Vector2( |
| 21 |
TVector3.DotProduct(PlaneCoord, CameraSide), |
| 22 |
TVector3.DotProduct(PlaneCoord, CameraUp) |
| 23 |
); |
| 24 |
Exit(Vector2( |
| 25 |
(PlaneCoordProjected.Data[0] - FrustumDimensions.Left) / FrustumDimensions.Width, |
| 26 |
(PlaneCoordProjected.Data[1] - FrustumDimensions.Bottom) / FrustumDimensions.Height |
| 27 |
)); |
| 28 |
*/ |
| 29 |
|
| 30 |
float plane_d = |
| 31 |
- dot(castle_NormalizedPlane.xyz, vertex_world.xyz) |
| 32 |
- castle_NormalizedPlane.w; |
| 33 |
vec3 vertex_on_plane = vertex_world.xyz + castle_NormalizedPlane.xyz * plane_d; |
| 34 |
|
| 35 |
vertex_on_plane -= castle_CameraPositionOnPlane; |
| 36 |
|
| 37 |
vec2 projected = vec2( |
| 38 |
dot(vertex_on_plane, castle_CameraSide), |
| 39 |
dot(vertex_on_plane, castle_CameraUp)); |
| 40 |
|
| 41 |
return vec3( |
| 42 |
(projected.x - castle_FrustumDimensions.x) / castle_FrustumDimensions.z, |
| 43 |
(projected.y - castle_FrustumDimensions.y) / castle_FrustumDimensions.w, |
| 44 |
0.0); |
| 45 |
} |
| 46 |
#endif |
| 47 |
|
| 48 |
/* Sphere mapping in GLSL adapted from |
| 49 |
http://www.ozone3d.net/tutorials/glsl_texturing_p04.php#part_41 |
| 50 |
by Jerome Guinot aka 'JeGX', many thanks! |
| 51 |
*/ |
| 52 |
vec2 castle_generate_tex_coords_sphere(const in vec4 vertex_eye, const in vec3 normal_eye) |
| 53 |
{ |
| 54 |
vec3 r = reflect( normalize(vec3(vertex_eye)), normal_eye ); |
| 55 |
float m = 2.0 * sqrt( r.x*r.x + r.y*r.y + (r.z+1.0)*(r.z+1.0) ); |
| 56 |
/* Using 1.0 / 2.0 instead of 0.5 to workaround fglrx bugs */ |
| 57 |
return r.xy / m + vec2(1.0, 1.0) / 2.0; |
| 58 |
} |