-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp2.html
86 lines (64 loc) · 2.8 KB
/
mp2.html
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
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Flight Sim</title>
<meta charset="utf-8">
</head>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexNormal;
attribute vec3 aVertexPosition;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat3 uNMatrix;
uniform vec3 uLightPosition;
uniform vec3 uAmbientLightColor;
uniform vec3 uDiffuseLightColor;
uniform vec3 uSpecularLightColor;
const float shininess = 32.0;
varying vec4 vColor;
void main(void) {
// Get the vertex position in eye coordinates
vec4 vertexPositionEye4 = uMVMatrix * vec4(aVertexPosition, 1.0);
vec3 vertexPositionEye3 = vertexPositionEye4.xyz / vertexPositionEye4.w;
// Calculate the vector (l) to the light source
vec3 vectorToLightSource = normalize(uLightPosition - vertexPositionEye3);
// Transform the normal (n) to eye coordinates
vec3 normalEye = normalize(uNMatrix * aVertexNormal);
// Calculate n dot l for diffuse lighting
float diffuseLightWeightning = max(dot(normalEye,
vectorToLightSource), 0.0);
// Calculate the reflection vector (r) that is needed for specular light
vec3 reflectionVector = normalize(reflect(-vectorToLightSource,
normalEye));
// The camera in eye coordinates is located in the origin and is pointing
// along the negative z-axis. Calculate viewVector (v)
// in eye coordinates as:
// (0.0, 0.0, 0.0) - vertexPositionEye3
vec3 viewVectorEye = -normalize(vertexPositionEye3);
float rdotv = max(dot(reflectionVector, viewVectorEye), 0.0);
float specularLightWeightning = pow(rdotv, shininess);
// Sum up all three reflection components and send to the fragment shader
vColor = vec4((uAmbientLightColor
+ uDiffuseLightColor * diffuseLightWeightning
+ uSpecularLightColor * specularLightWeightning),1.0);
gl_Position = uPMatrix*uMVMatrix*vec4(aVertexPosition, 1.0);
}
</script>
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec4 vColor;
void main(void) {
gl_FragColor = vColor;
}
</script>
<script src="gl-matrix-min.js"></script>
<script src="webgl-utils.js"></script>
<script src="terrainModeling.js"></script>
<script src="mp2.js"></script>
<body onload="startup();">
<p> Use the arrow keys to control the plane </p>
<p> W or up arrow to pitch up, S or down arrow to pitch down</p>
<p> A or left arrow to roll left, D or right arrow to roll right</p>
<canvas id="myGLCanvas" width="1024" height="768"></canvas>
</body>
</html>