-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceGolf.html
107 lines (87 loc) · 3.82 KB
/
SpaceGolf.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<html>
<head>
<title>SpaceGolf</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="./style/webgl.css" type="text/css">
<script type="text/javascript" src="./scripts/glMatrix-0.9.5.min.js"></script>
<script src="./scripts/physics.js" type="text/javascript"></script>
<script src="./scripts/objects.js" type="text/javascript"></script>
<script src="./scripts/SpaceGolf-main.js" type="text/javascript"></script>
<!-- Fragment shader program -->
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
const int MAX_LIGHTS = 7;
uniform int lightCount;
uniform vec3 uLightPosition[MAX_LIGHTS];
uniform vec3 uLightDiffuse[MAX_LIGHTS];
uniform vec3 uAmbientColor;
uniform sampler2D uSampler;
// variable for passing color from vertex shader to fragment shader
varying vec4 vColor;
varying vec2 vTextureCoord;
varying vec3 vTransformedNormal;
varying vec4 vPosition;
void main(void) {
vec3 lightWeighting[MAX_LIGHTS];
// calculate the vertex position
vec4 finalColor = vec4(0.0,0.0,0.0,1.0);
vec4 fragmentColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
for(int i = 0; i<MAX_LIGHTS; i++) {
vec3 normal = normalize(vTransformedNormal);
vec3 lightDirection = normalize(uLightPosition[i] - vPosition.xyz);
float directionalLightWeighting = max(dot(normal, lightDirection), 0.0);
float specular = 0.0;
if(directionalLightWeighting > 0.0) {
vec3 viewDirection = normalize(-vPosition.xyz);
vec3 halfDirection = normalize(lightDirection + viewDirection);
float specularAngle = max(dot(halfDirection, normal), 0.0);
specular = pow(specularAngle, 140.0);
}
lightWeighting[i] = uAmbientColor/vec3(MAX_LIGHTS, MAX_LIGHTS, MAX_LIGHTS) + uLightDiffuse[i] * directionalLightWeighting + uLightDiffuse[i] * specular;
finalColor += vec4(fragmentColor.rgb * lightWeighting[i] * vec3(1,1,1), fragmentColor.a);
}
gl_FragColor = finalColor;
}
</script>
<!-- Vertex shader program -->
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;
//attribute vec4 aVertexColor;
uniform mat4 uMVMatrix; // model-view matrix
uniform mat4 uPMatrix; // projection matrix
uniform mat3 uNMatrix; // normal matrix
// variable for passing texture coordinates and lighting weights
// from vertex shader to fragment shader
varying vec2 vTextureCoord;
varying vec3 vTransformedNormal;
varying vec4 vPosition;
varying vec4 vColor;
void main(void) {
vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
gl_Position = uPMatrix * vPosition;
vTextureCoord = aTextureCoord;
vTransformedNormal = uNMatrix * aVertexNormal;
vColor = vec4(1.0,1.0,1.0,1.0);
}
</script>
</head>
<body onload="start()">
<div id="uiOverlay-top">
<div class="uitext">Čas: <span id="casIgranja"></span></div>
<div class="uitext lower">Število udarcev: <span id="stUdarc"></span></div>
</div>
<div id="uiOverlay-right">
<div class="uitext">Moč udarca: <span id="mUdarc"></span></div>
</div>
<div id="uiOverlay-help">
<div ><span id="hImage" class="uiImage"></span></div>
</div>
<div id="content">
<canvas id="glcanvas" width="1366px" height="768px">
No <code><canvas></code> suppport in your browser.
</canvas>
</div>
</body>
</html>