-
Notifications
You must be signed in to change notification settings - Fork 45
/
index.html
251 lines (204 loc) · 7.23 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript" src="config.js"></script>
<script type="text/javascript" src="util/PointerLock.js"></script>
<script type="text/javascript" src="lib/gl-matrix.js"></script>
<script type="text/javascript" src="lib/webgl-utils.js"></script>
<script type="text/javascript" src="MapParser.js"></script>
<script type="text/javascript" src="MapRender.js"></script>
<script type="text/javascript" src="CollisionDetection.js"></script>
<script type="text/javascript" src="Map.js"></script>
<script type="text/javascript" src="Player.js"></script>
<script type="text/javascript" src="lib/keyboard.js"></script>
<script id="shader-fs" type="x-shader/x-fragment">
//Shader for doing color stuff
precision mediump float;
varying vec3 forFragColor;
void main(void) {
gl_FragColor = vec4(forFragColor, 1.0);
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
//Shader for calculating positions of each vertex
attribute vec3 aVertexPosition;
attribute vec3 aVertexColor;
varying vec3 forFragColor;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
//uniform mat3 uNMatrix;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
forFragColor = aVertexColor;
}
</script>
<script type="text/javascript">
//Define namespace
window.cs = window.cs || { };
//Webgl context
var gl;
//For sending information to the shaders
var shaderProgram;
//Modelview matrix
var mvMatrix = mat4.create();
//Projection matrix
var pMatrix = mat4.create();
//Normal matrix not yet needed
//var uNMatrix = mat3.create();
//Hardcoded cs_assault position. TODO: Read this from the entity set
cs.player = new cs.Player(-320, 352, 48, 5);
//http://forums.steampowered.com/forums/showthread.php?t=1613876
//Boilerplate code begin
function initGL(canvas) {
try {
gl = canvas.getContext("webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.CULL_FACE);
gl.cullFace(gl.FRONT);
} catch (e) { }
if (!gl) {
console.log("Could not initialise WebGL");
}
}
function getShader(gl, id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
function initShaders() {
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");
shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
gl.useProgram(shaderProgram);
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
//shaderProgram.uNMatrixUniform = gl.getUniformLocation(shaderProgram, "uNMatrix");
}
function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
//gl.uniformMatrix3fv(shaderProgram.uNMatrixUniform, false, uNMatrix);
}
//Boilerplate code end
function render() {
var player = cs.player;
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
mat4.perspective(pMatrix, cs.config.FIELD_OF_VIEW*Math.PI/180,
gl.viewportWidth / gl.viewportHeight, cs.config.NEAR_CLIPPING, cs.config.FAR_CLIPPING);
//Rotate the player
mat4.identity(mvMatrix);
mat4.rotateX(mvMatrix, mvMatrix, player.xAngle);
mat4.rotateY(mvMatrix, mvMatrix, player.yAngle);
//Move the player by moving the map in the reverse direction
mat4.translate(mvMatrix, mvMatrix, [player.y, -player.z - cs.config.PLAYER_HEIGHT, player.x]);
//Rotate the map so it matches the first person shooter style
mat4.rotateX(mvMatrix, mvMatrix, -Math.PI/2);
mat4.rotateZ(mvMatrix, mvMatrix, Math.PI/2);
//mat4.invert(uNMatrix, mvMatrix);
setMatrixUniforms();
//Render the entire map
cs.map.render(player.position());
}
function mainLoop() {
requestAnimFrame(mainLoop);
cs.player.move();
render();
}
//Download the bsp file and call callback on the result
function getMapData(callback) {
var path = cs.config.MAP_PATH;
var req = new XMLHttpRequest();
req.open("GET", path, true);
req.responseType = "arraybuffer";
req.onreadystatechange = function () {
if(req.readyState === 4) {
if(req.status === 200 || req.status == 0) {
callback(new Uint8Array(req.response));
}
}
}
req.send(null);
}
function webGLStart() {
var canvas = document.getElementById("canvas");
initGL(canvas);
initShaders();
getMapData(function(data) {
//Parse map
cs.map = new cs.Map(shaderProgram, gl, data);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
//Set event handler for resizing the screen every time
//the window changes size
var resizeCallback = function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
gl.viewportWidth = window.innerWidth;
gl.viewportHeight = window.innerHeight;
};
resizeCallback();
window.addEventListener("resize", resizeCallback, false);
//Listen for clicks on the canvas
canvas.addEventListener("click", function() {
PointerLock.requestPointerLock(canvas);
}, false);
//Listen for pointer locking
PointerLock.addPointerLockExchangeEventListener(document, function(e) {
//Did the pointer just go from unlocked to locked?
if(!!PointerLock.pointerLockElement()) {
//Yep! Add mousemove listener
PointerLock.addMouseMoveEventListener(document, rotatePlayer, false);
}
else { //Nope. Remove mouse move listener
PointerLock.removeMouseMoveEventListener(document, rotatePlayer);
}
}, false);
mainLoop();
});
}
//Rotate the player when the mouse is moved
function rotatePlayer(e) {
cs.player.rotate(e.movementX, e.movementY);
}
</script>
</head>
<body onload="webGLStart();">
<canvas id="canvas" style="display: block; margin:0; padding:0"></canvas>
</body>
</html>