-
Notifications
You must be signed in to change notification settings - Fork 11
/
MapRender.js
284 lines (234 loc) · 8.36 KB
/
MapRender.js
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
This file contains all the code needed in order to render a bsp version 30
map.
TODO: Render WAD textures and textures defined in the bsp file
**/
define(["GameInfo", "lib/gl-matrix"], function(GameInfo, glMatrix) {
return function(gl, map) {
//Shaders
var fragmentShader =
" precision mediump float;" +
" varying vec3 forFragColor;" +
" void main(void) {" +
" gl_FragColor = vec4(forFragColor, 1.0);" +
" }";
var vertexShader =
" attribute vec3 aVertexPosition;" +
" attribute vec3 aVertexColor;" +
" varying vec3 forFragColor;" +
" uniform mat4 uMVMatrix;" +
" uniform mat4 uPMatrix;" +
" void main(void) {" +
" gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);" +
" forFragColor = aVertexColor;" +
" }";
this.map = map;
this.gl = gl;
function getShader(gl, shaderCode, shaderType) {
var shader = gl.createShader(shaderType);
gl.shaderSource(shader, shaderCode);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var shaderProgram = (function() {
var sFragmentShader = getShader(gl, fragmentShader, gl.FRAGMENT_SHADER);
var sVertexShader = getShader(gl, vertexShader, gl.VERTEX_SHADER);
var program = gl.createProgram();
gl.attachShader(program, sVertexShader);
gl.attachShader(program, sFragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
gl.useProgram(program);
program.vertexPositionAttribute = gl.getAttribLocation(program, "aVertexPosition");
program.vertexColorAttribute = gl.getAttribLocation(program, "aVertexColor");
program.pMatrixUniform = gl.getUniformLocation(program, "uPMatrix");
program.mvMatrixUniform = gl.getUniformLocation(program, "uMVMatrix");
return program;
})();
//Create buffers
var indexBuffer = gl.createBuffer();
var vertexBuffer = gl.createBuffer();
var colorBuffer = gl.createBuffer();
//Array of already drawn faces
var renderedFaces = [];
//Check if point lies within min and max
var pointInBox = function(point, min, max) {
var x = point[0],
y = point[1],
z = point[2];
return (min[0] <= x && x <= max[0] &&
min[1] <= y && y <= max[1] &&
min[2] <= z && z <= max[2]);
}
//Search for which leaf the vector "pos" is in. i is the i'th
//child of the parent node.
var getLeafForPositionHelper = function(pos, iNode, i) {
var node = map.nodes[iNode];
//If the child index is positive it's an index into the node array
//Otherwise it's an index into the leaf array
if(node.iChildren[i] >= 0) {
var min = map.nodes[node.iChildren[i]].nMins;
var max = map.nodes[node.iChildren[i]].nMaxs;
if(pointInBox(pos, min, max)) {
return getLeafForPosition(pos, node.iChildren[i]);
}
}
else if(~node.iChildren[i] != 0) {
var min = map.leaves[~node.iChildren[i]].nMins;
var max = map.leaves[~node.iChildren[i]].nMaxs;
if(pointInBox(pos, min, max)) {
//Bitwise inversion according to the specification
return ~node.iChildren[i];
}
}
return -1;
}
//Search for which leaf the vector "pos" is in.
var getLeafForPosition = function(pos, iNode) {
var first = getLeafForPositionHelper(pos, iNode, 0);
//Was it in the first one?
if(first != -1) {
//Yep! Return that leaf
return first;
}
else {
//Nope. Check the other child
return getLeafForPositionHelper(pos, iNode, 1);
}
};
var getIndex = function(i, face) {
var iEdge = map.surfedges[face.iFirstEdge + i];
var index;
if(iEdge > 0) {
var edge = map.edges[iEdge];
index = edge[0];
}
else {
var edge = map.edges[-iEdge];
index = edge[1];
}
return index;
}
var renderFace = function(iFace, index_array) {
//If this face has already been drawn just return
if(!!renderedFaces[iFace]) {
return;
}
//Remember that we have drawn this face
renderedFaces[iFace] = true;
var face = map.faces[iFace];
//No need to render it if it has no light
if(face.nStyles[0] == 0xFF) return;
//We need to convert from triangle fans to triangles to allow
//for a single draw call
//Thus a sequence of indices describing a triangle fan:
//0 1 2 3 4 5 6
//Should be converted to triangles, which in this example is:
//0 1 2 0 2 3 0 3 4 0 4 5 0 5 6
//Hardcode the first triangle since we need to reuse this vertex
var index = getIndex(0, face);
var center = index;
index_array.push(index);
index = getIndex(1, face);
index_array.push(index);
var previous = index;
index = getIndex(2, face);
index_array.push(index);
previous = index;
for(var i = 3; i < face.nEdges; ++i) {
index = getIndex(i, face);
index_array.push(center);
index_array.push(previous);
index_array.push(index);
previous = index;
}
}
var renderLeaf = function(iLeaf, index_array) {
var leaf = map.leaves[iLeaf];
var n = leaf.nMarkSurfaces;
for(var i = 0; i < n; ++i) {
renderFace(map.markSurfaces[leaf.iFirstMarkSurface + i], index_array);
}
}
var render = function(iNode, iLeaf, pos, index_array) {
//If iNode points to a leaf
if(iNode < 0) {
if(iNode == -1) return;
//If this node is not visible, don't draw it
if(iLeaf > 0 && (map.visibility[iLeaf-1] &&
!map.visibility[iLeaf-1][~iNode - 1])) {
return;
}
return renderLeaf(~iNode, index_array);
}
var location;
var plane_index = map.nodes[iNode].iPlane;
var plane = map.planes.planes[plane_index];
//If the plane is perpendicular to an axis it's either 0, 1 or 2
switch (plane.nType) {
case 0:
location = pos[0] - plane.distance;
case 1:
location = pos[1] - plane.distance;
case 2:
location = pos[2] - plane.distance;
default:
//Not perpendicular. Calculate the location the hard way using:
//location = dot(normal, pos) - distance
//(from http://en.wikipedia.org/wiki/Hesse_normal_form)
location = (map.planes.normals[3*plane_index] * pos[0] +
map.planes.normals[3*plane_index+1] * pos[1] +
map.planes.normals[3*plane_index+2] * pos[2]) - plane.distance;
}
//Is the player behind this node or in front?
if (location > 0.0) {
//In front: Render the leaves furthest behind first
render(map.nodes[iNode].iChildren[1], iLeaf, pos, index_array);
render(map.nodes[iNode].iChildren[0], iLeaf, pos, index_array);
}
else {
render(map.nodes[iNode].iChildren[0], iLeaf, pos, index_array);
render(map.nodes[iNode].iChildren[1], iLeaf, pos, index_array);
}
};
this.render = function(pos) {
gl.useProgram(shaderProgram);
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
//Rotate the map
glMatrix.mat4.rotateX(GameInfo.mvMatrix, GameInfo.mvMatrix, -Math.PI/2);
glMatrix.mat4.rotateZ(GameInfo.mvMatrix, GameInfo.mvMatrix, Math.PI/2);
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, GameInfo.pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, GameInfo.mvMatrix);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, map.vertices, gl.STATIC_DRAW);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, 3,
gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, map.lighting, gl.STATIC_DRAW);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, 3,
gl.FLOAT, false, 0, 0);
//Clear the array that tells us which faces we've already drawn
renderedFaces.length = 0;
//Find the leaf that the vector "pos" is locate in
var iLeaf = getLeafForPosition(pos, 0);
//Get indices of the required vertices
var index_array = [];
render(0, iLeaf, pos, index_array);
//Bind index buffer
var buffer = new Uint16Array(index_array);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, buffer, gl.STATIC_DRAW);
//Finally draw the map!
gl.drawElements(gl.TRIANGLES, buffer.length, gl.UNSIGNED_SHORT, 0);
gl.disableVertexAttribArray(shaderProgram.vertexPositionAttribute);
gl.disableVertexAttribArray(shaderProgram.vertexColorAttribute);
};
};
});