-
Notifications
You must be signed in to change notification settings - Fork 44
/
index.html
155 lines (128 loc) · 4.58 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
<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="util/DataReader.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="CollisionDetection.js"></script>
<script type="text/javascript" src="MapParser.js"></script>
<script type="text/javascript" src="MapRender.js"></script>
<script type="text/javascript" src="Map.js"></script>
<script type="text/javascript" src="PlayerParser.js"></script>
<script type="text/javascript" src="ModelRender.js"></script>
<script type="text/javascript" src="Player.js"></script>
<script type="text/javascript" src="lib/keyboard.js"></script>
<script type="text/javascript">
//Define namespace
window.cs = window.cs || { };
//Webgl context
var gl;
//Modelview matrix
cs.mvMatrix = mat4.create();
//Projection matrix
cs.pMatrix = mat4.create();
function initGL(canvas) {
try {
gl = canvas.getContext("experimental-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 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(cs.pMatrix, cs.config.FIELD_OF_VIEW*Math.PI/180,
gl.viewportWidth / gl.viewportHeight,
cs.config.NEAR_CLIPPING, cs.config.FAR_CLIPPING);
mat4.identity(cs.mvMatrix);
//Draw player
cs.player.render();
mat4.identity(cs.mvMatrix);
mat4.rotateX(cs.mvMatrix, cs.mvMatrix, player.xAngle);
mat4.rotateY(cs.mvMatrix, cs.mvMatrix, player.yAngle);
//Move the player by moving the map in the reverse direction
mat4.translate(cs.mvMatrix, cs.mvMatrix,
[player.y, -player.z - cs.config.PLAYER_HEIGHT, player.x]);
cs.map.render(player.position());
}
function mainLoop() {
requestAnimFrame(mainLoop);
cs.player.move();
render();
}
//Download the bsp file and call callback on the result
function download(path, callback) {
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);
download(cs.config.MAP_PATH, function(data) {
//Parse map
cs.map = new cs.Map(gl, data);
download(cs.config.PLAYER_PATH, function(data) {
//Hardcoded cs_assault position. TODO: Read this from the entity set
cs.player = new cs.Player(gl, -186, 2597, 165, 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() {
//is the mouse not currently locked?
if(!PointerLock.pointerLockElement()) {
//Nope. Request locking
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>