/
project.js
565 lines (489 loc) · 17.9 KB
/
project.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// WebGL context
var gl;
var canvas;
var program;
// Camera Matrices
var modelViewMatrix;
var ProjectionMatrix;
// Framebuffers
var pickFrameBuffer;
// Shader Attribute Pointers
var vPosition;
// Shader Uniform Pointers
var uColor;
var uNormal;
var uLight;
var uInstance;
var uNormalMatrix;
var uModelView;
var uProjection;
var uRenderMode;
var uPickColor;
// Geometry data structures
var meshes = [];
var selected = null;
var wireColor = new Float32Array([0, 0, 0, 1]);
var selectedColor = new Float32Array([1, 1, 1, 1]);
var picked = null;
var boolGeo1, boolGeo2;
window.onload = function init() {
// Set up the WebGL canvas and context.
canvas = document.getElementById("gl-canvas");
gl = WebGLUtils.setupWebGL(canvas);
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0.5, 0.5, 0.5, 1.0);
// Depth testing and polygon offset.
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.CULL_FACE);
//gl.depthFunc(gl.LEQUAL);
gl.enable(gl.POLYGON_OFFSET_FILL);
gl.polygonOffset(1.0, 2.0);
// Allocate the color picking framebuffer.
pickFrameBuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, pickFrameBuffer);
// Set up color attachment texture for the color picking framebuffer.
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, canvas.width, canvas.height, 0,
gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.generateMipmap(gl.TEXTURE_2D);
// Create a rendering buffer with depth information.
var pickRenderbuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, pickRenderbuffer);
gl.renderbufferStorage(
gl.RENDERBUFFER,
gl.DEPTH_COMPONENT16,
canvas.width, canvas.height);
// Attach color buffer
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D,
texture, 0);
// Attach the render buffer
gl.framebufferRenderbuffer(
gl.FRAMEBUFFER,
gl.DEPTH_ATTACHMENT,
gl.RENDERBUFFER,
pickRenderbuffer);
// Rebind original framebuffer
gl.bindTexture(gl.TEXTURE_2D, null);
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
// Load shaders.
program = initShaders(gl, "vertex.glsl", "fragment.glsl");
gl.useProgram(program);
// Get shader attribute locations.
vPosition = gl.getAttribLocation(program, "vPosition");
// Get shader uniform locations.
uColor = gl.getUniformLocation(program, "Color");
uNormal = gl.getUniformLocation(program, "Normal");
uLight = gl.getUniformLocation(program, "Light");
uInstance = gl.getUniformLocation(program, "Instance");
uNormalMatrix = gl.getUniformLocation(program, "NormalMatrix");
uModelView = gl.getUniformLocation(program, "ModelView");
uProjection = gl.getUniformLocation(program, "Projection");
uRenderMode = gl.getUniformLocation(program, "RenderMode");
uPickColor = gl.getUniformLocation(program, "PickColor");
// Set up camera.
modelViewMatrix = identityMatrix();
ProjectionMatrix = ortho(-2, 2, -2, 2, -5, 5);
// Set up the light source.
gl.uniform4fv(uLight, flatten(vec4(-1, 1, 1, 0)));
AttachEventHandlers();
render();
};
function render(timeStamp, renderMode, clickEvent) {
// Set the rendering mode uniform. 0 is normal, 1 is for picking.
if (!renderMode) renderMode = 0;
gl.uniform1i(uRenderMode, renderMode);
if (renderMode === 1) gl.bindFramebuffer(gl.FRAMEBUFFER, pickFrameBuffer);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Set up the camera uniforms.
gl.uniformMatrix4fv(uModelView, gl.FALSE, flatten(modelViewMatrix));
gl.uniformMatrix4fv(uProjection, gl.FALSE, flatten(ProjectionMatrix));
// Render each mesh.
for (var i = 0; i < meshes.length; i++) {
var curMesh = meshes[i];
// Pass in instance transform.
gl.uniformMatrix4fv(
uInstance,
gl.FALSE,
flatten(curMesh.geometry.instance)
);
// Pass in normal transform.
gl.uniformMatrix4fv(
uNormalMatrix,
gl.FALSE,
flatten(curMesh.geometry.normalMatrix)
);
// Pass in the pick color for this mesh.
gl.uniform1i(uPickColor, curMesh.id);
// Bind the mesh's buffer and attributes to the buffer.
gl.bindBuffer(gl.ARRAY_BUFFER, curMesh.vbo);
gl.vertexAttribPointer(vPosition, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vPosition);
// Render each polygon
for (var j = 0; j < curMesh.geometry.polygons.length; j++) {
curPoly = curMesh.geometry.polygons[j];
// Pass in the vertex's normal (actually the polygon's)
var n = curPoly.plane.normal;
var norm = new Float32Array([n.x, n.y, n.z, 0]);
gl.uniform4fv(uNormal, norm);
// Solid
gl.uniform4fv(uColor, flatten(curPoly.color));
gl.drawArrays(
gl.TRIANGLE_FAN,
curMesh.offsets[j],
curMesh.lengths[j]
);
// Wireframe
if (renderMode === 0) {
if (curMesh === selected) {
gl.uniform4fv(uColor, selectedColor);
} else {
gl.uniform4fv(uColor, wireColor);
}
gl.drawArrays(
gl.LINE_LOOP,
curMesh.offsets[j],
curMesh.lengths[j]
);
}
}
}
// If we were doing a pick, figure out what was selected and pick it, then
// rebind the default buffer for normal rendering.
if (renderMode === 1) {
var rect = canvas.getBoundingClientRect();
var screenX = clickEvent.clientX - rect.left;
var screenY = canvas.height - (clickEvent.clientY - rect.top);
var color = new Uint8Array(4);
gl.readPixels(screenX, screenY, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, color);
getPickedMesh(color[0]);
console.log(color[0]);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
// Only re-render if we are doing normal rendering.
if (renderMode === 0) requestAnimFrame(render);
}
/**
* Attaches event handlers for HTML5 elements and interactions.
*/
function AttachEventHandlers() {
canvas.onmousedown = handleMouseDown;
canvas.onclick = handlePickClick;
document.onmouseup = handleMouseUp;
document.onmousemove = handleMouseMove;
// Create buttons
document.getElementById("cubeButton").onmousedown = createCube;
document.getElementById("sphereButton").onmousedown = createSphere;
document.getElementById("cylinderButton").onmousedown = createCylinder;
document.getElementById("coneButton").onmousedown = createCone;
// Interaction controls
document.getElementById("transX").onchange = onFieldChange;
document.getElementById("transXSlider").oninput = onSliderChange;
document.getElementById("transY").onchange = onFieldChange;
document.getElementById("transYSlider").oninput = onSliderChange;
document.getElementById("transZ").onchange = onFieldChange;
document.getElementById("transZSlider").oninput = onSliderChange;
document.getElementById("scaleX").onchange = onFieldChange;
document.getElementById("scaleXSlider").oninput = onSliderChange;
document.getElementById("scaleY").onchange = onFieldChange;
document.getElementById("scaleYSlider").oninput = onSliderChange;
document.getElementById("scaleZ").onchange = onFieldChange;
document.getElementById("scaleZSlider").oninput = onSliderChange;
document.getElementById("rotX").onchange = onFieldChange;
document.getElementById("rotXSlider").oninput = onSliderChange;
document.getElementById("rotY").onchange = onFieldChange;
document.getElementById("rotYSlider").oninput = onSliderChange;
document.getElementById("rotZ").onchange = onFieldChange;
document.getElementById("rotZSlider").oninput = onSliderChange;
document.getElementById("sceneList").onchange = function(event) {
selected = event.srcElement.selectedOptions[0].mesh;
updateControls(selected);
};
document.getElementById("geo1SetButton").onclick = function(event) {
boolGeo1 = selected;
document.getElementById("geo1Field").value =
selected ? selected.name : null;
};
document.getElementById("geo2SetButton").onclick = function(event) {
boolGeo2 = selected;
document.getElementById("geo2Field").value =
selected ? selected.name : null;
};
// Other Buttons
document.getElementById("colorButton").onmousedown = function(event) {
var r = document.getElementById("colR").value;
var g = document.getElementById("colG").value;
var b = document.getElementById("colB").value;
var a = document.getElementById("colA").value;
selected.geometry.setColor([r, g, b, a]);
};
document.getElementById("deleteButton").onmousedown = deleteMesh;
document.getElementById("copyButton").onmousedown = createCopy;
// Boolean Buttons
document.getElementById("unionButton").onmousedown = function() {
var newGeo = createBooleanGeometry(boolGeo1, boolGeo2, BSPNode.Union);
boolGeo1 = newGeo;
boolGeo2 = null;
document.getElementById("geo1Field").value = boolGeo1.name;
document.getElementById("geo2Field").value = null;
};
document.getElementById("intersectButton").onmousedown = function() {
var newGeo = createBooleanGeometry(boolGeo1, boolGeo2, BSPNode.Intersect);
boolGeo1 = newGeo;
boolGeo2 = null;
document.getElementById("geo1Field").value = boolGeo1.name;
document.getElementById("geo2Field").value = null;
};
document.getElementById("subtractButton").onmousedown = function() {
var newGeo = createBooleanGeometry(boolGeo1, boolGeo2, BSPNode.Subtract);
boolGeo1 = newGeo;
boolGeo2 = null;
document.getElementById("geo1Field").value = boolGeo1.name;
document.getElementById("geo2Field").value = null;
};
}
/**
* Handles left clicks on the canvas element. Renders 1 frame of pick coloring,
* finds the selected mesh, and puts it in the picked global.
* @param {type} event
* @returns {undefined}
*/
function handlePickClick(event) {
render(null, 1, event);
selected = picked;
updateControls(picked);
}
/**
* Picks a mesh based on id.
* @param {int} id The ID of the mesh to pick (also its pick color)
*/
function getPickedMesh(id) {
for (var i = 0; i < meshes.length; i++) {
if (meshes[i].id === id) {
picked = meshes[i];
return;
}
}
}
//*************************************************
// SCENE OBJECT CREATION EVENTS
//*************************************************
var MeshIDs = 0;
function createCube(event) {
var mesh = new Mesh(gl, Cube(null, null, null, vec4(0, 0.25, 1, 1)));
mesh.name = "Cube " + MeshIDs++;
mesh.id = MeshIDs;
createEvent(mesh);
}
function createSphere(event) {
var mesh = new Mesh(gl, Sphere(null, null, null, vec4(1, 0.5, 0, 1)));
mesh.name = "Sphere " + MeshIDs++;
mesh.id = MeshIDs;
createEvent(mesh);
}
function createCylinder(event) {
var mesh = new Mesh(gl, Cylinder(null, null, null, vec4(0.75, 0, 0.75, 1)));
mesh.name = "Cylinder" + MeshIDs++;
mesh.id = MeshIDs;
createEvent(mesh);
}
function createCone(event) {
var mesh = new Mesh(gl, Cone(null, null, null, vec4(0, 1, 0, 1)));
mesh.name = "Cone" + MeshIDs++;
mesh.id = MeshIDs;
createEvent(mesh);
}
function createBooleanGeometry(mesh1, mesh2, operation) {
var mesh = new Mesh(gl, operation(
mesh1.geometry.transformedGeometry(),
mesh2.geometry.transformedGeometry()));
mesh.name = "Geometry " + MeshIDs++;
mesh.id = MeshIDs;
deleteMesh(null, mesh1);
deleteMesh(null, mesh2);
createEvent(mesh);
return mesh;
}
function createCopy(event) {
var mesh = selected.clone(gl);
mesh.name = "Geometry " + MeshIDs++;
mesh.id = MeshIDs;
createEvent(mesh);
}
/**
* Does postprocessing necessary to sync the program with the new object.
* @param {Mesh} mesh The new mesh.
*/
function createEvent(mesh) {
meshes.push(mesh);
// Create a new option for the scene list.
var option = document.createElement("option");
option.text = mesh.name;
option.id = mesh.name;
option.mesh = mesh;
// Add the new option and select it immediately.
var list = document.getElementById("sceneList");
list.add(option);
list.selectedIndex = list.length - 1;
// Select the mesh in the scene and synchronize the controls.
selected = mesh;
updateControls(selected);
}
function deleteMesh(event, mesh) {
if (!mesh) {
if (!selected) {
return;
} else {
mesh = selected;
}
}
// Remove the mesh from the scene.
var index = meshes.indexOf(mesh);
meshes.splice(index, 1);
// Remove the mesh from the list.
var list = document.getElementById("sceneList");
for (var i = 0; i < list.childNodes.length; i++) {
if (list.childNodes[i].id === mesh.name) {
list.remove(i);
break;
}
}
selected = null;
}
//*************************************************
// HTML INPUT CONTROLS EVENTS
//*************************************************
function onFieldChange(event) {
// Get the value of the complement control and update it.
var fieldID = event.target.id;
var sliderID = fieldID + "Slider";
var value = event.target.value;
var complement = document.getElementById(sliderID);
if (complement !== null) complement.value = value;
// Update the corresponding object's properties.
updateObject(fieldID, value, selected);
}
function onSliderChange(event) {
// Get the value of the complement control and update it.
var sliderID = event.target.id;
var fieldID = sliderID.substring(0, sliderID.length - 6);
var value = event.target.value;
document.getElementById(fieldID).value = value;
// Update the corresponding object's properties.
updateObject(sliderID, value, selected);
}
//*************************************************
// OBJECT / CONTROL UPDATE FUNCTIONS
//*************************************************
/**
* Updates the selected object given a changed control.
*
* @param {string} inputID The id of the control that changed.
* @param {number} value The value of that control.
* @param {Mesh} mesh The mesh to update.
*/
function updateObject(inputID, value, mesh) {
if (!mesh) return;
var geo = mesh.geometry;
switch (inputID) {
case "transX":
case "transXSlider":
geo.pos[0] = value;
break;
case "transY":
case "transYSlider":
geo.pos[1] = value;
break;
case "transZ":
case "transZSlider":
geo.pos[2] = value;
break;
case "scaleX":
case "scaleXSlider":
geo.scale[0] = value;
break;
case "scaleY":
case "scaleYSlider":
geo.scale[1] = value;
break;
case "scaleZ":
case "scaleZSlider":
geo.scale[2] = value;
break;
case "rotX":
case "rotXSlider":
geo.rot[0] = deg2rad(value);
break;
case "rotY":
case "rotYSlider":
geo.rot[1] = deg2rad(value);
break;
case "rotZ":
case "rotZSlider":
geo.rot[2] = deg2rad(value);
break;
case "colR":
geo.color[0] = value;
break;
case "colG":
geo.color[1] = value;
break;
case "colB":
geo.color[2] = value;
break;
case "colA":
geo.color[3] = value;
break;
}
// Update the instance matrices to reflect the changes.
geo.updateTransforms();
}
/**
* Updates the user controls with the values of the mesh.
*
* @param {Mesh} mesh The mesh to update against.
*/
function updateControls(mesh) {
if (!mesh) return;
var geo = mesh.geometry;
// Update the scene list in case the update was caused by a pick.
var list = document.getElementById("sceneList");
for (var i = 0; i < list.childNodes.length; i++) {
if (list.childNodes[i].id === mesh.name) {
list.selectedIndex = i;
break;
}
}
// Translation
document.getElementById("transX").value = geo.pos[0];
document.getElementById("transY").value = geo.pos[1];
document.getElementById("transZ").value = geo.pos[2];
document.getElementById("transXSlider").value = geo.pos[0];
document.getElementById("transYSlider").value = geo.pos[1];
document.getElementById("transZSlider").value = geo.pos[2];
// Scale
document.getElementById("scaleX").value = geo.scale[0];
document.getElementById("scaleY").value = geo.scale[1];
document.getElementById("scaleZ").value = geo.scale[2];
document.getElementById("scaleXSlider").value = geo.scale[0];
document.getElementById("scaleYSlider").value = geo.scale[1];
document.getElementById("scaleZSlider").value = geo.scale[2];
// Rotation
document.getElementById("rotX").value = rad2deg(geo.rot[0]);
document.getElementById("rotY").value = rad2deg(geo.rot[1]);
document.getElementById("rotZ").value = rad2deg(geo.rot[2]);
document.getElementById("rotXSlider").value = rad2deg(geo.rot[0]);
document.getElementById("rotYSlider").value = rad2deg(geo.rot[1]);
document.getElementById("rotZSlider").value = rad2deg(geo.rot[2]);
}
function deg2rad(deg) {
return deg * (Math.PI / 180);
}
function rad2deg(rad) {
return rad * (360 / (2 * Math.PI));
}