summaryrefslogtreecommitdiff
path: root/drivers/gles2/shaders/cube_to_dp.glsl
blob: 0b3f53a870a66966a1f827fb7e10724062334cc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
[vertex]

#ifdef USE_GLES_OVER_GL
#define mediump
#define highp
#else
precision mediump float;
precision mediump int;
#endif

attribute highp vec4 vertex_attrib; // attrib:0
attribute vec2 uv_in; // attrib:4

varying vec2 uv_interp;

void main() {

	uv_interp = uv_in;
	gl_Position = vertex_attrib;
}

[fragment]

#ifdef USE_GLES_OVER_GL
#define mediump
#define highp
#else
precision mediump float;
precision mediump int;
#endif

uniform highp samplerCube source_cube; //texunit:0
varying vec2 uv_interp;

uniform bool z_flip;
uniform highp float z_far;
uniform highp float z_near;
uniform highp float bias;

void main() {

	highp vec3 normal = vec3( uv_interp * 2.0 - 1.0, 0.0 );
/*
	if(z_flip) {
		normal.z = 0.5 - 0.5*((normal.x * normal.x) + (normal.y * normal.y));
	} else {
		normal.z = -0.5 + 0.5*((normal.x * normal.x) + (normal.y * normal.y));
	}
*/

	//normal.z = sqrt(1.0-dot(normal.xy,normal.xy));
	//normal.xy*=1.0+normal.z;

	normal.z = 0.5 - 0.5*((normal.x * normal.x) + (normal.y * normal.y));
	normal = normalize(normal);

/*
	normal.z=0.5;
	normal=normalize(normal);
*/
	if (!z_flip) {
		normal.z=-normal.z;
	}

	//normal = normalize(vec3( uv_interp * 2.0 - 1.0, 1.0 ));
	float depth = textureCube(source_cube,normal).r;

	// absolute values for direction cosines, bigger value equals closer to basis axis
	vec3 unorm = abs(normal);

	if ( (unorm.x >= unorm.y) && (unorm.x >= unorm.z) ) {
	    // x code
	    unorm = normal.x > 0.0 ?  vec3( 1.0, 0.0, 0.0 ) : vec3( -1.0, 0.0, 0.0 ) ;
	} else if ( (unorm.y > unorm.x) && (unorm.y >= unorm.z) ) {
	    // y code
	    unorm = normal.y > 0.0 ?  vec3( 0.0, 1.0, 0.0 ) :  vec3( 0.0, -1.0, 0.0 ) ;
	} else if ( (unorm.z > unorm.x) && (unorm.z > unorm.y) ) {
	    // z code
	    unorm = normal.z > 0.0 ?  vec3( 0.0, 0.0, 1.0 ) :  vec3( 0.0, 0.0, -1.0 ) ;
	} else {
	    // oh-no we messed up code
	    // has to be
	    unorm = vec3( 1.0, 0.0, 0.0 );
	}

	float depth_fix = 1.0 / dot(normal,unorm);


	depth = 2.0 * depth - 1.0;
	float linear_depth = 2.0 * z_near * z_far / (z_far + z_near - depth * (z_far - z_near));
	gl_FragDepth = (linear_depth*depth_fix+bias) / z_far;
}