-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevelBuilderGL.js
178 lines (135 loc) · 7.12 KB
/
levelBuilderGL.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
//levelBuilderGL.js
import { calculateBoundingBox } from './groundMechanicsGL.js';
let prevPosMuzzle = null;
export const groundPolygons = [];
export function placeObj(gl, obj, translation, rotation, scale, locations, isGround = false) {
const modelMatrix = mat4.create();
mat4.translate(modelMatrix, modelMatrix, translation);
mat4.scale(modelMatrix, modelMatrix, scale);
mat4.rotate(modelMatrix, modelMatrix, rotation.angle, rotation.axis);//performed first // Rotate around object's local origin
const normalMatrix = mat3.create();
mat3.normalFromMat4(normalMatrix, modelMatrix);
gl.uniformMatrix4fv(locations.uniforms.modelMatrix, false, modelMatrix);
gl.uniformMatrix3fv(locations.uniforms.normalMatrix, false, normalMatrix);
gl.bindBuffer(gl.ARRAY_BUFFER, obj.vertexBuffer);
gl.enableVertexAttribArray(locations.attributes.position);
gl.vertexAttribPointer(locations.attributes.position, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, obj.normalBuffer);
gl.enableVertexAttribArray(locations.attributes.normal);
gl.vertexAttribPointer(locations.attributes.normal, 3, gl.FLOAT, false, 0, 0);
if (isGround) {
gl.bindBuffer(gl.ARRAY_BUFFER, obj.texCoordBuffer);
gl.enableVertexAttribArray(locations.attributes.texCoord);
gl.vertexAttribPointer(locations.attributes.texCoord, 2, gl.FLOAT, false, 0, 0);
const transformedVertices = [];
for (const vertex of obj.vertices) {
const vec = vec4.fromValues(vertex[0], vertex[1], vertex[2], 1);
const transformedVec = vec4.create();
vec4.transformMat4(transformedVec, vec, modelMatrix);
transformedVertices.push([transformedVec[0], transformedVec[1], transformedVec[2]]);
}
const boundingBox = calculateBoundingBox(transformedVertices);
groundPolygons.push({ boundingBox });
}
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, obj.indexBuffer);
gl.drawElements(gl.TRIANGLES, obj.vertexCount, gl.UNSIGNED_SHORT, 0);
}
export function placeWeapon(gl, obj, modelMatrix, rotation, locations, hasTex = false, inWorldSpace = true) {
const normalMatrix = mat3.create();
if (inWorldSpace) {
mat4.scale(modelMatrix, modelMatrix, [1, -1, 1]);
}
mat4.rotate(modelMatrix, modelMatrix, rotation.angle, rotation.axis);
mat3.normalFromMat4(normalMatrix, modelMatrix);
gl.uniformMatrix4fv(locations.uniforms.modelMatrix, false, modelMatrix);
gl.uniformMatrix3fv(locations.uniforms.normalMatrix, false, normalMatrix);
gl.bindBuffer(gl.ARRAY_BUFFER, obj.vertexBuffer);
gl.enableVertexAttribArray(locations.attributes.position);
gl.vertexAttribPointer(locations.attributes.position, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, obj.normalBuffer);
gl.enableVertexAttribArray(locations.attributes.normal);
gl.vertexAttribPointer(locations.attributes.normal, 3, gl.FLOAT, false, 0, 0);
if (hasTex) {
gl.bindBuffer(gl.ARRAY_BUFFER, obj.texCoordBuffer);
gl.enableVertexAttribArray(locations.attributes.texCoord);
gl.vertexAttribPointer(locations.attributes.texCoord, 2, gl.FLOAT, false, 0, 0);
}
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, obj.indexBuffer);
gl.drawElements(gl.TRIANGLES, obj.vertexCount, gl.UNSIGNED_SHORT, 0);
}
export function drawRepeatingObj(gl, obj, locations, startZ, endZ, repeatInterval, translation, rotation, scale, isGround = 0) {
for (let z = startZ; z >= endZ; z -= repeatInterval) {
placeObj(gl, obj, [translation[0], translation[1], translation[2] + z], rotation, scale, locations, isGround);
}
}
let currentTextureUnit = null;
let currentTexture = null;
export function bindTexture(gl, textureUnit, texture, parameters = {}) {
if (currentTextureUnit !== textureUnit || currentTexture !== texture) {
gl.activeTexture(textureUnit);
gl.bindTexture(gl.TEXTURE_2D, texture);
currentTextureUnit = textureUnit;
currentTexture = texture;
}
if (parameters.wrapS !== undefined) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, parameters.wrapS);
if (parameters.wrapT !== undefined) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, parameters.wrapT);
if (parameters.minFilter !== undefined) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, parameters.minFilter);
if (parameters.magFilter !== undefined) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, parameters.magFilter);
}
//old version that saved position of the muzzle objects
// export function genFloatingMuzzle(obj, baseRotationAxis, baseColor, numObjects, speedMultiplier, loopTime) {
// if (prevPosMuzzle === null) {
// prevPosMuzzle = speedMultiplier * loopTime;
// }
// let posMuzzle = (speedMultiplier === 0) ? prevPosMuzzle : prevPosMuzzle + (speedMultiplier * (loopTime - prevPosMuzzle / speedMultiplier));
// const objects = [];
// for (let i = 0; i < numObjects; i++) {
// const angle = posMuzzle + (i * 2 * Math.PI / numObjects);
// objects.push({
// obj: obj,
// rotation: { angle: angle, axis: baseRotationAxis },
// color: baseColor,
// });
// }
// if (speedMultiplier !== 0) {
// prevPosMuzzle = posMuzzle;
// }
// return objects;
// }
export function genFloatingMuzzle(obj, baseRotationAxis, baseColor, numObjects, speedMultiplier, loopTime) {
const posMuzzle = speedMultiplier * loopTime;
const objects = [];
for (let i = 0; i < numObjects; i++) {
const angle = posMuzzle + (i * 2 * Math.PI / numObjects);
objects.push({
obj: obj,
rotation: { angle: angle, axis: baseRotationAxis },
color: baseColor,
});
}
return objects;
}
export function placeMuzzle(gl, obj, modelMatrix, translation, rotation, locations, hasTex = false, inWorldSpace = true) {
const normalMatrix = mat3.create();
if (inWorldSpace) {
mat4.scale(modelMatrix, modelMatrix, [1, -1, 1]);
}
mat4.rotate(modelMatrix, modelMatrix, rotation.angle, rotation.axis);
mat4.translate(modelMatrix, modelMatrix, translation);
mat3.normalFromMat4(normalMatrix, modelMatrix);
gl.uniformMatrix4fv(locations.uniforms.modelMatrix, false, modelMatrix);
gl.uniformMatrix3fv(locations.uniforms.normalMatrix, false, normalMatrix);
gl.bindBuffer(gl.ARRAY_BUFFER, obj.vertexBuffer);
gl.enableVertexAttribArray(locations.attributes.position);
gl.vertexAttribPointer(locations.attributes.position, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, obj.normalBuffer);
gl.enableVertexAttribArray(locations.attributes.normal);
gl.vertexAttribPointer(locations.attributes.normal, 3, gl.FLOAT, false, 0, 0);
if (hasTex) {
gl.bindBuffer(gl.ARRAY_BUFFER, obj.texCoordBuffer);
gl.enableVertexAttribArray(locations.attributes.texCoord);
gl.vertexAttribPointer(locations.attributes.texCoord, 2, gl.FLOAT, false, 0, 0);
}
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, obj.indexBuffer);
gl.drawElements(gl.TRIANGLES, obj.vertexCount, gl.UNSIGNED_SHORT, 0);
}