summaryrefslogtreecommitdiff
path: root/src/phong.vert
blob: 963c57b2e38d50fc9c7bc2936639c9fe3859c628 (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
uniform mat4 mvp; // eye -> screen
uniform mat4 mv;  // world -> eye
uniform mat4 m;   // object -> world

// These three are in object space
attribute vec4 normal;
attribute vec4 tangent;
attribute vec4 eye; // Eye position in object space

attribute vec2 uv;

varying vec3 normal_ws;
varying vec3 eye_direction_ws;

// Vector from the light to the vertex transformed to surface space.
varying vec3 light_ss;

// ???
varying vec3 eye_ss;

varying vec2 uv_coord = uv;

/* Position of the light in eye-space.
 */
const vec3 light_pos_es = vec3(0.0, 3.0, 0.0);

void main(void)
{
	gl_Position = mvp * gl_Vertex;

	vec3 tangent_es = vec3(mv * tangent);
	vec3 normal_es = vec3(mv * normal);
	vec3 bitangent_es = cross(normal_es, tangent_es);
	mat3 tbn = mat3(tangent_es, bitangent_es, normal_es);

	vec3 vertex_pos_es = vec3(mv * gl_Vertex);
	vec3 light_direction_es = light_pos_es - vertex_pos_es;

	/* Surface space vectors for lighting */
	light_ss = normalize(light_direction_es * tbn);
	eye_ss = -normalize(vertex_pos_es * tbn);

	/* World space vectors for environment mapping
	 * FIXME: eye should already be in world space, so I'm not sure why
	 *        I have to do m * eye here.  But it looks wrong otherwise.
	 */
	vec3 vertex_pos_ws = vec3(m * gl_Vertex);
	vec3 eye_pos_ws = vec3(m * eye);

	normal_ws = vec3(m * normal);
	eye_direction_ws = eye_pos_ws - vertex_pos_ws;
}