Answer To: 1 Project 4 WebGL 3D Project Overview In this project you will create a unique 3D animated scene...
Mohd answered on Apr 30 2021
completed solution/a.PNG
completed solution/Code/css/styles.css
* { margin: 0; padding: 0; }
html , body { width:100%; height:100%; }
canvas { display: block; }
completed solution/Code/index.html
Your browser doesn't appear to support the HTML5 eye: {
x:2.,
y:2.,
z:6.,
},
objects: [],
},
};
// Create a cube
function Cube(opts) {
var opts = opts || {};
this.id = uuid();
this.opts = opts;
// v0-v1-v2-v3 front
// v0-v3-v4-v5 right
// v0-v5-v6-v1 up
// v1-v6-v7-v2 left
// v7-v4-v3-v2 down
// v4-v7-v6-v5 back
this.attributes = {
aColor: {
size:3,
offset:0,
bufferData: new Float32Array([
2, 0, 0, 4, 0, 0, 5, 0, 0, 5, 0, 0,
2, 0, 0, 4, 0, 0, 5, 0, 0, 5, 0, 0,
2, 0, 0, 4, 0, 0, 5, 0, 0, 5, 0, 0,
2, 0, 0, 4, 0, 0, 5, 0, 0, 5, 0, 0,
2, 0, 0, 4, 0, 0, 5, 0, 0, 5, 0, 0,
2, 0, 0, 4, 0, 0, 5, 0, 0, 5, 0, 0,
]),
},
aNormal: {
size:3,
offset:0,
bufferData: new Float32Array([
0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
-1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0,
0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0,
0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0
]),
},
aPosition: {
size:3,
offset:0,
bufferData: new Float32Array([
1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0,-1.0, 1.0, 1.0,-1.0, 1.0,
1.0, 1.0, 1.0, 1.0,-1.0, 1.0, 1.0,-1.0,-1.0, 1.0, 1.0,-1.0,
1.0, 1.0, 1.0, 1.0, 1.0,-1.0, -1.0, 1.0,-1.0, -1.0, 1.0, 1.0,
-1.0, 1.0, 1.0, -1.0, 1.0,-1.0, -1.0,-1.0,-1.0, -1.0,-1.0, 1.0,
-1.0,-1.0,-1.0, 1.0,-1.0,-1.0, 1.0,-1.0, 1.0, -1.0,-1.0, 1.0,
1.0,-1.0,-1.0, -1.0,-1.0,-1.0, -1.0, 1.0,-1.0, 1.0, 1.0,-1.0
]),
},
};
this.indices = new Uint8Array([
0, 1, 2, 0, 2, 3,
4, 5, 6, 4, 6, 7,
8, 9,10, 8,10,11,
12,13,14, 12,14,15,
16,17,18, 16,18,19,
20,21,22, 20,22,23
]);
this.state = {
angle: opts.angle ? opts.angle : [0,0,0],
mm: mat4.create(),
nm: null,
};
this.selColor = opts.selColor ? opts.selColor : [255,12,255,210];
this.stride = opts.stride ? opts.stride : 0;
// Functionality
this.readState = function() {
this.attributes.aColorBackup = this.attributes.aColor;
this.attributes.aColor = this.attributes.aSelColor;
};
this.drawState = function() {
this.attributes.aColor = this.attributes.aColorBackup;
};
this.draw = function() {
var uMVPMatrix = state.gl.getUniformLocation(state.programs[state.program], 'uMVPMatrix');
var uModelMatrix = state.gl.getUniformLocation(state.programs[state.program], 'uModelMatrix');
var uNormalMatrix = state.gl.getUniformLocation(state.programs[state.program], 'uNormalMatrix');
var mvp = state.mvp;
state.programs[state.program].renderBuffers(this);
var n = this.indices.length;
var mm = mat4.create();
if (this.opts.translate) {
mat4.translate(mm, mm, this.opts.translate);
}
if (this.opts.scale) {
mat4.scale(mm, mm, this.opts.scale);
}
if (this.state.angle[0] || this.state.angle[1] || this.state.angle[2]) {
mat4.rotateX(mm, mm, this.state.angle[0]);
mat4.rotateY(mm, mm, this.state.angle[1]);
mat4.rotateZ(mm, mm, this.state.angle[2]);
}
state.gl.uniformMatrix4fv(uModelMatrix, false, mm);
mat4.copy(mvp, state.pm);
mat4.multiply(mvp, mvp, state.vm);
mat4.multiply(mvp, mvp, mm);
state.gl.uniformMatrix4fv(uMVPMatrix, false, mvp);
// Lighting
if (state.program === 'render') {
state.gl.uniform3f(state.uDiffuseLight, 1.0, 1.0, 1.0);
state.gl.uniform3f(state.uAmbientLight, 0.2, 0.2, 0.2);
// Set the light direction (in the world coordinate)
state.gl.uniform3f(state.uLightPosition, 1.0, 2.0, 1.7);
// Normalized invert
var nm = mat3.normalFromMat4(mat3.create(), mm);
state.gl.uniformMatrix3fv(uNormalMatrix, false, nm);
}
state.gl.drawElements(state.gl.TRIANGLES, n, state.gl.UNSIGNED_BYTE, 0);
};
// Initialization
this.init = function(_this) {
_this.selColor = _this.selColor.map(function(n) { return n/255; });
var arrays = [
_this.selColor, _this.selColor, _this.selColor, _this.selColor,
_this.selColor, _this.selColor, _this.selColor, _this.selColor,
_this.selColor, _this.selColor, _this.selColor, _this.selColor,
_this.selColor, _this.selColor, _this.selColor, _this.selColor,
_this.selColor, _this.selColor, _this.selColor, _this.selColor,
_this.selColor, _this.selColor, _this.selColor, _this.selColor,
];
_this.attributes.aSelColor = {
size:4,
offset:0,
bufferData: new Float32Array(
[].concat.apply([], arrays)
),
};
}(this);
};
glUtils.SL.init({ callback:function() { main(); } });
function main() {
state.canvas = document.getElementById("glcanvas");
state.gl = glUtils.checkWebGL(state.canvas, { preserveDrawingBuffer: true });
initCallbacks();
initShaders();
initGL();
initState();
draw();
if (state.app.doAnimate) {
animate();
}
}
function initCallbacks() {
document.onkeydown = keydown;
document.onkeyup = keyup;
state.canvas.onmousedown = mousedown;
state.canvas.onmouseup = mouseup;
state.canvas.onmousemove = mousemove;
}
function initShaders() {
var vertexShader = glUtils.getShader(state.gl, state.gl.VERTEX_SHADER, glUtils.SL.Shaders.v1.vertex),
vertexShader2 = glUtils.getShader(state.gl, state.gl.VERTEX_SHADER, glUtils.SL.Shaders.v2.vertex),
fragmentShader =...