| 1 |
/* Parallax bump mapping shader effect. |
| 2 |
This is added right after bump_mapping.fs. |
| 3 |
Included by EnableShaderBumpMapping in ../castlerendererinternalshader.pas unit. |
| 4 |
*/ |
| 5 |
|
| 6 |
/* Make castleTranspose() available. |
| 7 |
|
| 8 |
For desktop OpenGL, we could use "#version 120" and get transpose(). |
| 9 |
|
| 10 |
But on OpenGLES, it seems easiest to just implement your own transpose(). |
| 11 |
Or we could require version 3.00, |
| 12 |
https://www.khronos.org/registry/OpenGL-Refpages/es3.1/html/transpose.xhtml , |
| 13 |
but this also requires other GLSL changes, |
| 14 |
and it seems that iOS would be troublesome anyway: |
| 15 |
https://stackoverflow.com/questions/18034677/transpose-a-mat4-in-opengl-es-2-0-glsl |
| 16 |
*/ |
| 17 |
mat3 castleTranspose(const in mat3 m) |
| 18 |
{ |
| 19 |
return mat3( |
| 20 |
vec3(m[0].x, m[1].x, m[2].x), |
| 21 |
vec3(m[0].y, m[1].y, m[2].y), |
| 22 |
vec3(m[0].z, m[1].z, m[2].z) |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
// avoid redeclaring variables when no "separate compilation units" available (OpenGLES) |
| 27 |
#ifndef GL_ES |
| 28 |
uniform mat4 castle_ModelViewMatrix; |
| 29 |
#endif |
| 30 |
|
| 31 |
varying vec3 castle_vertex_to_eye_in_tangent_space; |
| 32 |
|
| 33 |
void PLUG_bump_mapping_tangent_space_calculations( |
| 34 |
const in vec4 vertex_eye, const in mat3 tangent_to_object_space) |
| 35 |
{ |
| 36 |
mat3 object_to_tangent_space = castleTranspose(tangent_to_object_space); |
| 37 |
mat3 eye_to_object_space = mat3( |
| 38 |
castle_ModelViewMatrix[0][0], castle_ModelViewMatrix[1][0], castle_ModelViewMatrix[2][0], |
| 39 |
castle_ModelViewMatrix[0][1], castle_ModelViewMatrix[1][1], castle_ModelViewMatrix[2][1], |
| 40 |
castle_ModelViewMatrix[0][2], castle_ModelViewMatrix[1][2], castle_ModelViewMatrix[2][2]); |
| 41 |
mat3 eye_to_tangent_space = object_to_tangent_space * eye_to_object_space; |
| 42 |
/* Theoretically faster implementation below, not fully correct --- |
| 43 |
assume that transpose is enough to invert this matrix. Tests proved: |
| 44 |
- results seem the same |
| 45 |
- but it's not really faster. */ |
| 46 |
// mat3 eye_to_tangent_space = castleTranspose(castle_tangent_to_eye_space); |
| 47 |
castle_vertex_to_eye_in_tangent_space = normalize(eye_to_tangent_space * (-vec3(vertex_eye)) ); |
| 48 |
|
| 49 |
/* PLUG: bump_mapping_parallax_tangent_space_calculations (vertex_eye, eye_to_tangent_space) */ |
| 50 |
} |