1+ #version 120
2+
3+ attribute vec3 vertexPosition;
4+ attribute vec2 vertexTexCoord;
5+ attribute vec3 vertexNormal;
6+ attribute vec4 vertexColor;
7+
8+ uniform mat4 mvp;
9+ uniform mat4 matModel;
10+
11+ varying vec3 fragPosition;
12+ varying vec2 fragTexCoord;
13+ varying vec4 fragColor;
14+ varying vec3 fragNormal;
15+
16+ // inverse() and transpose() are not built-in until GLSL 1.40
17+ mat3 inverse(mat3 m)
18+ {
19+ float a00 = m[0 ][0 ], a01 = m[0 ][1 ], a02 = m[0 ][2 ];
20+ float a10 = m[1 ][0 ], a11 = m[1 ][1 ], a12 = m[1 ][2 ];
21+ float a20 = m[2 ][0 ], a21 = m[2 ][1 ], a22 = m[2 ][2 ];
22+ float b01 = a22* a11 - a12* a21;
23+ float b11 = - a22* a10 + a12* a20;
24+ float b21 = a21* a10 - a11* a20;
25+ float det = a00* b01 + a01* b11 + a02* b21;
26+ return mat3 (b01, (- a22* a01 + a02* a21), ( a12* a01 - a02* a11),
27+ b11, ( a22* a00 - a02* a20), (- a12* a00 + a02* a10),
28+ b21, (- a21* a00 + a01* a20), ( a11* a00 - a01* a10)) / det;
29+ }
30+
31+ mat3 transpose (mat3 m)
32+ {
33+ return mat3 (m[0 ][0 ], m[1 ][0 ], m[2 ][0 ],
34+ m[0 ][1 ], m[1 ][1 ], m[2 ][1 ],
35+ m[0 ][2 ], m[1 ][2 ], m[2 ][2 ]);
36+ }
37+
38+ void main()
39+ {
40+ fragPosition = vec3 (matModel * vec4 (vertexPosition, 1.0 ));
41+ fragTexCoord = vertexTexCoord;
42+ fragColor = vertexColor;
43+
44+ mat3 normalMatrix = transpose (inverse(mat3 (matModel)));
45+ fragNormal = normalize (normalMatrix * vertexNormal);
46+
47+ gl_Position = mvp * vec4 (vertexPosition, 1.0 );
48+ }
0 commit comments