diff --git a/lab1/Cours IHM Labo 1.pdf b/lab1/Cours IHM Labo 1.pdf new file mode 100644 index 0000000000000000000000000000000000000000..d0bdd86bf3606bfef95bdf6e6a9cd8297a256af2 Binary files /dev/null and b/lab1/Cours IHM Labo 1.pdf differ diff --git a/lab1/README.txt b/lab1/README.txt new file mode 100644 index 0000000000000000000000000000000000000000..256490cb25bbaefd74b427e2f3821796ca9ca894 --- /dev/null +++ b/lab1/README.txt @@ -0,0 +1 @@ +Tp r�alis� durant l'ann�e 2018/2019 \ No newline at end of file diff --git a/lab1/REAME.md b/lab1/REAME.md deleted file mode 100644 index f93a378562ffb18f21872c8c5a941f21403caa48..0000000000000000000000000000000000000000 --- a/lab1/REAME.md +++ /dev/null @@ -1,9 +0,0 @@ -# Lab1 IHM - -Ce dossier contient un template pour le lab1. - -Vous devez compléter les 4 sections **TODO** dans le fichier `lab1.js`: - -- Dans le vertex shader (ligne 4) -- Dans le fragment shader (ligne 10) -- Dans la fonction `initVertexBuffers` (ligne 50 et 66) \ No newline at end of file diff --git a/lab1/labo1.html b/lab1/labo1.html new file mode 100644 index 0000000000000000000000000000000000000000..6f4ee3a8f7f1c8cbd7da20f10afb4e0463f5b516 --- /dev/null +++ b/lab1/labo1.html @@ -0,0 +1,27 @@ +<!DOCTYPE html> +<!-- + Cedric Dos Reis + IHM + 31.10.2018 +--> + +<html lang="en"> + <head> + <meta charset="utf-8" /> + <title>TP1</title> + <style> + #webgl{border:1px solid black;} + </style> + </head> + <body onload="main()"> + <canvas id="webgl" width="500" height="500"> + Please use a browser that supports "canvas" + </canvas> + + <script src="lib/webgl-utils.js"></script> + <script src="lib/webgl-debug.js"></script> + <script src="lib/cuon-utils.js"></script> + <script src="labo1.js"></script> + + </body> +</html> diff --git a/lab1/labo1.js b/lab1/labo1.js new file mode 100644 index 0000000000000000000000000000000000000000..fca14ddb43267a3fa9b237f3aaf16f2428c69c82 --- /dev/null +++ b/lab1/labo1.js @@ -0,0 +1,157 @@ +// Cedric Dos Reis +// IHM +// 31.10.2018 + +// Vertex shader program +var VSHADER_SOURCE = + 'attribute vec3 coordinates;'+ + 'attribute vec3 color;'+ + 'varying vec3 vColor;'+ + 'void main(void) {' + + 'gl_Position = vec4(coordinates, 1.0);' + + 'vColor = color;'+ + '}'; + +// Fragment shader program +var FSHADER_SOURCE = + 'precision mediump float;'+ + 'varying vec3 vColor;'+ + 'void main(void) {'+ + 'gl_FragColor = vec4(vColor, 1.0);'+ + '}'; + +function main() { + // Retrieve <canvas> element + var canvas = document.getElementById('webgl'); + + // Get the rendering context for WebGL + var gl = getWebGLContext(canvas); + if (!gl) { + console.log('Failed to get the rendering context for WebGL'); + return; + } + + // Initialize shaders + if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) { + console.log('Failed to intialize shaders.'); + return; + } + + // Write the positions, colors & indices of vertices to buffers + initVertexBuffers(gl); + var nbIndices = initIndexBuffers(gl); + initColorBuffers(gl); + + + // Specify the color for clearing <canvas> + gl.clearColor(1, 1, 1, 1); + + // Enable the depth test + gl.enable(gl.DEPTH_TEST); + + // Clear <canvas> + gl.clear(gl.COLOR_BUFFER_BIT); + + // Set the view port + gl.viewport(0,0,canvas.width,canvas.height); + + // Draw the rectangle + gl.drawElements(gl.TRIANGLES, nbIndices, gl.UNSIGNED_SHORT,0); +} + +function initColorBuffers(gl) { + var colors = new Float32Array([ + 1,0.8,0, 1,0.8,0, 1,0.8,0, 1,0.8,0, + 0,0.5,1, 0,0.5,1, 0,0.5,1, 0,0.5,1, + 0,1,0.5, 0,1,0.5, 0,1,0.5, 0,1,0.5, + 0.8,0,0.8, 0.8,0,0.8, 0.8,0,0.8, 0.8,0,0.8, + + ]); + + // Create a buffer object + var colorBuffer = gl.createBuffer(); + + // Bind the buffer object to target + gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); + // Write date into the buffer object + gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW); + + var color = gl.getAttribLocation(gl.program, "color"); + // point an attribute to the color + gl.vertexAttribPointer(color,3,gl.FLOAT, false,0,0); + //enable the color attribute + gl.enableVertexAttribArray(color); +} + +function initIndexBuffers(gl) { + var indices = new Uint16Array([ + 3,2,1, // index in vertices array for triangle vertex + 3,1,0, + + 4,5,6, + 4,6,7, + + 8,9,10, + 8,10,11, + + 12,13,14, + 13,14,15, + ]); + + // Create a buffer object + var indexBuffer = gl.createBuffer(); + + // Bind the buffer object to target + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer); + // Write date into the buffer object + gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW); + + return indices.length; +} + +function initVertexBuffers(gl) { + var vertices = new Float32Array([ + // top left + -1,1,0, + -1,0.2,0, + -0.2,0.2,0, + -0.2,1,0, + + //bottom left + -0.2,0.2,0, + -1,0.2,0, + -1,-0.6,0, + -0.2,-0.6,0, + + //top right + -0.2,0.2,0, + -0.2,1,0, + 1,1,0, + 1,0.2,0, + + //bottom right + 0.2,0.2,0, + 1,0.2,0, + 0.2,-0.6,0, + 1,-0.6,0 + ]); + + // Create a buffer object + var vertexBuffer = gl.createBuffer(); + + // Bind the buffer object to target + gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); + // Write date into the buffer object + gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); + + var coord = gl.getAttribLocation(gl.program, "coordinates"); + //point an attribute to the buffer + gl.vertexAttribPointer(coord, 3, gl.FLOAT, false,0,0); + // enable the attribute + gl.enableVertexAttribArray(coord); +} + + + + + diff --git a/lab2/src/lib/cuon-matrix.js b/lab1/lib/cuon-matrix.js similarity index 100% rename from lab2/src/lib/cuon-matrix.js rename to lab1/lib/cuon-matrix.js diff --git a/lab1/src/lib/cuon-utils.js b/lab1/lib/cuon-utils.js similarity index 100% rename from lab1/src/lib/cuon-utils.js rename to lab1/lib/cuon-utils.js diff --git a/lab1/src/lib/webgl-debug.js b/lab1/lib/webgl-debug.js similarity index 100% rename from lab1/src/lib/webgl-debug.js rename to lab1/lib/webgl-debug.js diff --git a/lab1/src/lib/webgl-utils.js b/lab1/lib/webgl-utils.js similarity index 100% rename from lab1/src/lib/webgl-utils.js rename to lab1/lib/webgl-utils.js diff --git a/lab1/src/lab1.html b/lab1/src/lab1.html deleted file mode 100644 index a657a5c628bc47acb6afd3b84c1e81b2dc52af5c..0000000000000000000000000000000000000000 --- a/lab1/src/lab1.html +++ /dev/null @@ -1,16 +0,0 @@ -<!DOCTYPE html> -<html lang="en"> -<head> - <meta charset="UTF-8"> - <title>Simple model</title> -</head> -<body onload="main()"> -<canvas width="600" height="600" id="my_Canvas"> - Please use a browser that supports "canvas" -</canvas> -<script src="lib/webgl-utils.js"></script> -<script src="lib/webgl-debug.js"></script> -<script src="lib/cuon-utils.js"></script> -<script src="lab1.js"></script> -</body> -</html> \ No newline at end of file diff --git a/lab1/src/lab1.js b/lab1/src/lab1.js deleted file mode 100644 index f61b5431141f7c4750653973947614ee0a6ccca1..0000000000000000000000000000000000000000 --- a/lab1/src/lab1.js +++ /dev/null @@ -1,75 +0,0 @@ -// Vertex shader program -const VSHADER_SOURCE = - '\n' + - // TODO: Implement your vertex shader code here - '\n'; - -// Fragment shader program -const FSHADER_SOURCE = - '\n' + - // TODO: Implement your fragment shader code here - '\n'; - -function main() { - // Retrieve <canvas> element - const canvas = document.getElementById('my_Canvas'); - - // Get the rendering context for WebGL - const gl = getWebGLContext(canvas); - if (!gl) { - console.log('Failed to get the rendering context for WebGL'); - return; - } - - // Initialize shaders - if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) { - console.log('Failed to intialize shaders.'); - return; - } - - // Write the positions of vertices to a vertex shader - const n = initVertexBuffers(gl); - if (n < 0) { - console.log('Failed to set the positions of the vertices'); - return; - } - - // Specify the color for clearing <canvas> - gl.clearColor(0, 0, 0, 1); - - // Clear <canvas> - gl.clear(gl.COLOR_BUFFER_BIT); - - // Draw the rectangle - gl.drawArrays(gl.TRIANGLES, 0, n); -} - -function initVertexBuffers(gl) { - // This is the model - const vertices = new Float32Array([ - // TODO: Create your model here - ]); - const n = 3; // The number of vertices - - // Create a buffer object - const vertexBuffer = gl.createBuffer(); - if (!vertexBuffer) { - console.log('Failed to create the buffer object'); - return -1; - } - - // Bind the buffer object to target - gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); - // Write date into the buffer object - gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); - - // TODO: - // 1. Bind your attribute variable in the vertex shader to a JavaScript varibale using gl.getAttribLocation(...) function - // 2. Assign the buffer object to this attribute variable using gl.vertexAttribPointer(...) function - // 3. Enable the assignment to this attribute variable using gl.enableVertexAttribArray(...) function - - // Unbind the buffer object - gl.bindBuffer(gl.ARRAY_BUFFER, null); - - return n; -} diff --git a/lab1/doc/enonce.pdf b/lab2/Cours IHM Labo 2.pdf similarity index 60% rename from lab1/doc/enonce.pdf rename to lab2/Cours IHM Labo 2.pdf index bf1d751aaf9afd0ffeec97148aecd0f9921439dc..23b837652a8e3a9d9a6ae5112aab64f5134294f4 100644 Binary files a/lab1/doc/enonce.pdf and b/lab2/Cours IHM Labo 2.pdf differ diff --git a/lab2/README.md b/lab2/README.md deleted file mode 100644 index 83f22b66a359e5918d39d4bdb30d0820616dafd5..0000000000000000000000000000000000000000 --- a/lab2/README.md +++ /dev/null @@ -1,63 +0,0 @@ -# Lab2 IHM - -This folder contains a template for the **lab2** of IHM course. - ---- - -***This template is optional. You are allowed to realize your lab from scratch***. - ---- - -* Execute this command line : `git pull base master` under your local repository where you did the **lab1** - - > Before doing so, check that you have defined this repository as a second remote repo by executing this command line: - > - > ```shell - > git remote -v - > ``` - > - > You should see this line in the output: - > - > ``` - > base ssh://git@ssh.hesge.ch:10572/cours-ihm/labs_ihm_2019_preparation.git - > ``` - > - > Otherwise, execute this command line: - > - > ```bash - > git remote add base ssh://git@ssh.hesge.ch:10572/cours-ihm/labs_ihm_2019.git - > ``` - -* Read carefully this README file, it will help you modeling your 3D Letter - -* If you want to use this template, there are 3 files to complete. You should start understanding and completing first the `WebglInstance.js` file, then completing `YourLetter.js` file, and finally you complete the `lab2.js` file. - -* Here is the list of the **TODO**s: - -  - -# Modeling a letter: explanation - - - -* Modeling letters: - * Letter base size: `(2, 2, 1)` - - * Vertex $P0$ has coordinates `(1,1,0.5)`, $P1$ has coordinates `(0.25,1, 0.5)`... (*see figure 1*) - - * Defining faces: - - **Front face** - - * Vertices: `P0, P1, P2, P3, P4, P5, P6, P7, P16, P17, P18` (*see figure 2*) - - * Indices: `0,1,8, 0,8,6, 0,6,7, 1,8,9, 1,9,2, 2,9,10, 2,3,10, 3,10,5, 3,4,5, 5,6,10, 6,8,10` - - Indices `0,1,8` represent the triangle `(P0,P1,P16)` (*green triangle in the figure 2*) - - Indices `0,8,6` represent the triangle `(P0,P16,P6)` (*yellow triangle in the figure 2*) - - **An other face** (*see figure 3*) - - * Vertices: `P0, P8, P9, P1` - * Indices: `22,23,24, 22,24,25` (representing two triangles) diff --git a/lab2/README.txt b/lab2/README.txt new file mode 100644 index 0000000000000000000000000000000000000000..280ec12ad384cf4217f1730147c6825b7a4d83ca --- /dev/null +++ b/lab2/README.txt @@ -0,0 +1 @@ +tp realis� durant le cour IHM de 2018/2019 \ No newline at end of file diff --git a/lab2/doc/enonce.pdf b/lab2/doc/enonce.pdf deleted file mode 100644 index 9d5709231a651c9b0a9b93137d63eaacbb63cead..0000000000000000000000000000000000000000 Binary files a/lab2/doc/enonce.pdf and /dev/null differ diff --git a/lab2/labo2.html b/lab2/labo2.html new file mode 100644 index 0000000000000000000000000000000000000000..ebafed408698d34709bbff3dea35b8474b0c400c --- /dev/null +++ b/lab2/labo2.html @@ -0,0 +1,39 @@ +<!DOCTYPE html> +<!-- + Cedric Dos Reis + Novembre 2018 + Hepia - IHM +--> + +<html lang="en"> + <head> + <meta charset="utf-8" /> + <title>TP2</title> + <style> + #webgl{border:1px solid black;} + </style> + </head> + <body onload="main()"> + <canvas id="webgl" width="500" height="500"> + Please use a browser that supports "canvas" + </canvas> + + <script src="lib/webgl-utils.js"></script> + <script src="lib/webgl-debug.js"></script> + <script src="lib/cuon-utils.js"></script> + <script src="lib/cuon-matrix.js"></script> + <script src="labo2.js"></script> + <br> + <p><input type="checkbox" name="light1" checked=true onclick="handleCheckBox(this)">Ambient Light<br> + <p><input type="checkbox" name="light2" checked=true onclick="handleCheckBox(this)">Light 2<br> + <p> Use key w,a,s,d to move the camera</p> + <p> Use arrows to rotate the camera</p> + <!-- light + http://rodger.global-linguist.com/webgl/ch08/LightedCube.html + http://rodger.global-linguist.com/webgl/ch08/LightedCube_ambient.html + http://rodger.global-linguist.com/webgl/ch08/PointLightedCube.html + --> + + + </body> +</html> diff --git a/lab2/labo2.js b/lab2/labo2.js new file mode 100644 index 0000000000000000000000000000000000000000..db618a194db24e1783e0d456f20ae196892e0dc3 --- /dev/null +++ b/lab2/labo2.js @@ -0,0 +1,358 @@ +// Cedric Dos Reis +// Novembre 2018 +// Hepia - IHM + + +// Vertex shader program +var VSHADER_SOURCE = + 'attribute vec4 a_Position;\n' + + 'attribute vec4 a_Color;\n' + + 'attribute vec4 a_Normal;\n' + + 'uniform mat4 u_MvpMatrix;\n' + + 'uniform mat4 u_ModelMatrix;\n' + // Model matrix + 'uniform mat4 u_NormalMatrix;\n' + // Transformation matrix of the normal + 'uniform vec3 u_LightColor;\n' + // Light color + 'uniform vec3 u_LightPosition;\n' + // Position of the light source (in the world coordinate system) + 'uniform vec3 u_AmbientLight;\n' + // Ambient light color + 'varying vec4 v_Color;\n' + + 'void main() {\n' + + ' gl_Position = u_MvpMatrix * a_Position;\n' + + // Recalculate the normal based on the model matrix and make its length 1. + ' vec3 normal = normalize(vec3(u_NormalMatrix * a_Normal));\n' + + // Calculate world coordinate of vertex + ' vec4 vertexPosition = u_ModelMatrix * a_Position;\n' + + // Calculate the light direction and make it 1.0 in length + ' vec3 lightDirection = normalize(u_LightPosition - vec3(vertexPosition));\n' + + // The dot product of the light direction and the normal + ' float nDotL = max(dot(lightDirection, normal), 0.0);\n' + + // Calculate the color due to diffuse reflection + ' vec3 diffuse = u_LightColor * a_Color.rgb * nDotL;\n' + + // Calculate the color due to ambient reflection + ' vec3 ambient = u_AmbientLight * a_Color.rgb;\n' + + // Add the surface colors due to diffuse reflection and ambient reflection + ' v_Color = vec4(diffuse + ambient, a_Color.a);\n' + + '}\n'; + +// Fragment shader program +var FSHADER_SOURCE = + '#ifdef GL_ES\n' + + 'precision mediump float;\n' + + '#endif\n' + + 'varying vec4 v_Color;\n' + + 'void main() {\n' + + ' gl_FragColor = v_Color;\n' + + '}\n'; + +var camHeight = 0, camSide = 0, camRotateSide = 0, camRotateFront =0; + +var displayLight1 = true, displayLight2 = true; + +function main() { + // Retrieve <canvas> element + var canvas = document.getElementById('webgl'); + + // Get the rendering context for WebGL + var gl = getWebGLContext(canvas); + if (!gl) { + console.log('Failed to get the rendering context for WebGL'); + return; + } + + // Initialize shaders + if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) { + console.log('Failed to intialize shaders.'); + return; + } + + // + var n = initVertexBuffers(gl); + if (n < 0) { + console.log('Failed to set the vertex information'); + return; + } + + // Set the clear color and enable the depth test + gl.clearColor(0, 0, 0, 1); + gl.enable(gl.DEPTH_TEST); + + // Get the storage locations of uniform variables and so on + var u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix'); + var u_MvpMatrix = gl.getUniformLocation(gl.program, 'u_MvpMatrix'); + var u_NormalMatrix = gl.getUniformLocation(gl.program, 'u_NormalMatrix'); + var u_LightColor = gl.getUniformLocation(gl.program, 'u_LightColor'); + var u_LightPosition = gl.getUniformLocation(gl.program, 'u_LightPosition'); + var u_DiffuseLight = gl.getUniformLocation(gl.program, 'u_DiffuseLight'); + var u_LightDirection = gl.getUniformLocation(gl.program, 'u_LightDirection'); + var u_AmbientLight = gl.getUniformLocation(gl.program, 'u_AmbientLight'); + if (!u_MvpMatrix || !u_NormalMatrix || !u_LightColor || !u_LightPosition || !u_AmbientLight) { + console.log('Failed to get the storage location'); + return; + } + + + var modelMatrix = new Matrix4(); // Model matrix + var mvpMatrix = new Matrix4(); // Model view projection matrix + var normalMatrix = new Matrix4(); // Transformation matrix for normals + + + + + + // Set the light direction (in the world coordinate) + var lightDirection = new Vector3([5.0, 0.0, -5.0]); + + draw(gl, n, u_MvpMatrix, mvpMatrix, u_LightDirection, lightDirection, u_AmbientLight, u_DiffuseLight, u_LightColor, u_LightPosition, modelMatrix, u_ModelMatrix, normalMatrix, u_NormalMatrix); + + window.onkeydown = function(ev){ + keydown(ev, gl, n, u_MvpMatrix, mvpMatrix, u_LightDirection, lightDirection, u_AmbientLight, u_DiffuseLight, u_LightColor, u_LightPosition, modelMatrix, u_ModelMatrix, normalMatrix, u_NormalMatrix); + }; + +} + +// draw scene +function draw(gl, n, u_MvpMatrix, mvpMatrix, u_LightDirection, lightDirection, u_AmbientLight, u_DiffuseLight, u_LightColor, u_LightPosition, modelMatrix, u_ModelMatrix, normalMatrix, u_NormalMatrix){ + + + lightDirection.normalize(); // Normalize + gl.uniform3fv(u_LightDirection, lightDirection.elements); + // Set the light color (white) + if(displayLight2){ + gl.uniform3f(u_LightColor, 1.0, 1.0, 1.0); + }else{ + gl.uniform3f(u_LightColor, 0, 0, 0); + } + + // Set the light direction (in the world coordinate) + gl.uniform3f(u_LightPosition, 2.3, 4.0, 3.5); + // Set the ambient light + if(displayLight1){ + gl.uniform3f(u_AmbientLight, 0.3, 0.3, 0.3); + }else{ + gl.uniform3f(u_AmbientLight, 0.0, 0.0, 0.0); + } + + // Calculate the model matrix + //modelMatrix.setRotate(90, 0, 1, 0); // Rotate around the y-axis + // Pass the model matrix to u_ModelMatrix + gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements); + + // Define camera view and position + mvpMatrix.setPerspective(80, 1, 1, 100); + mvpMatrix.lookAt(camSide, camHeight, 5, camSide + camRotateSide, camHeight + camRotateFront, 0, 0, 1, 0); + mvpMatrix.multiply(modelMatrix); + gl.uniformMatrix4fv(u_MvpMatrix, false, mvpMatrix.elements); + + // Pass the matrix to transform the normal based on the model matrix to u_NormalMatrix + normalMatrix.setInverseOf(modelMatrix); + normalMatrix.transpose(); + gl.uniformMatrix4fv(u_NormalMatrix, false, normalMatrix.elements); + + // Pass the model view projection matrix to u_MvpMatrix + gl.uniformMatrix4fv(u_MvpMatrix, false, mvpMatrix.elements); + + + // Clear color and depth buffer + gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); + // Draw the cube + gl.drawElements(gl.TRIANGLES, n, gl.UNSIGNED_BYTE, 0); +} + +function initVertexBuffers(gl) { + // Create a cube + // v6----- v5 + // /| /| + // v1------v0| + // | | | | + // | |v7---|-|v4 + // |/ |/ + // v2------v3 + // Coordinates + var vertices = new Float32Array([ + 5.0, 1.0, 1.0, 3.0, 1.0, 1.0, 3.0,-1.0, 1.0, 5.0,-1.0, 1.0, // v0-v1-v2-v3 front + 5.0, 1.0, 1.0, 5.0,-1.0, 1.0, 5.0,-1.0,-1.0, 5.0, 1.0,-1.0, // v0-v3-v4-v5 right + 5.0, 1.0, 1.0, 5.0, 1.0,-1.0, 3.0, 1.0,-1.0, 3.0, 1.0, 1.0, // v0-v5-v6-v1 up + 3.0, 1.0, 1.0, 3.0, 1.0,-1.0, 3.0,-1.0,-1.0, 3.0,-1.0, 1.0, // v1-v6-v7-v2 left + 3.0,-1.0,-1.0, 5.0,-1.0,-1.0, 5.0,-1.0, 1.0, 3.0,-1.0, 1.0, // v7-v4-v3-v2 down + 5.0,-1.0,-1.0, 3.0,-1.0,-1.0, 3.0, 1.0,-1.0, 5.0, 1.0,-1.0, // v4-v7-v6-v5 back + + 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, + + -3.0, 1.0, 1.0, -5.0, 1.0, 1.0, -5.0,-1.0, 1.0, -3.0,-1.0, 1.0, + -3.0, 1.0, 1.0, -3.0,-1.0, 1.0, -3.0,-1.0,-1.0, -3.0, 1.0,-1.0, + -3.0, 1.0, 1.0, -3.0, 1.0,-1.0, -5.0, 1.0,-1.0, -5.0, 1.0, 1.0, + -5.0, 1.0, 1.0, -5.0, 1.0,-1.0, -5.0,-1.0,-1.0, -5.0,-1.0, 1.0, + -5.0,-1.0,-1.0, -3.0,-1.0,-1.0, -3.0,-1.0, 1.0, -5.0,-1.0, 1.0, + -3.0,-1.0,-1.0, -5.0,-1.0,-1.0, -5.0, 1.0,-1.0, -3.0, 1.0,-1.0, + ]); + + // Colors + var colors = new Float32Array([ + 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v1-v2-v3 front + 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v3-v4-v5 right + 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v5-v6-v1 up + 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v1-v6-v7-v2 left + 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v7-v4-v3-v2 down + 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v4-v7-v6-v5 back + + 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, + 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, + 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, + 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, + 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, + 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, + + + 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, + 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, + 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, + 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, + 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, + 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, + + ]); + + // Normal + var normals = 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, // v0-v1-v2-v3 front + 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, // v0-v3-v4-v5 right + 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, // v0-v5-v6-v1 up + -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, // v1-v6-v7-v2 left + 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, // v7-v4-v3-v2 down + 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, // v4-v7-v6-v5 back + + 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, // v0-v1-v2-v3 front + 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, // v0-v3-v4-v5 right + 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, // v0-v5-v6-v1 up + -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, // v1-v6-v7-v2 left + 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, // v7-v4-v3-v2 down + 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, // v4-v7-v6-v5 back + + 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, // v0-v1-v2-v3 front + 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, // v0-v3-v4-v5 right + 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, // v0-v5-v6-v1 up + -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, // v1-v6-v7-v2 left + 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, // v7-v4-v3-v2 down + 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, 0.0, 0.0,-1.0, // v4-v7-v6-v5 back + + ]); + + // Indices of the vertices + var indices = new Uint8Array([ + 0, 1, 2, 0, 2, 3, // front + 4, 5, 6, 4, 6, 7, // right + 8, 9,10, 8,10,11, // up + 12,13,14, 12,14,15, // left + 16,17,18, 16,18,19, // down + 20,21,22, 20,22,23, // back + + 24,25,26, 24,26,27, + 28,29,30, 28,30,31, + 32,33,34, 32,34,35, + 36,37,38, 36,38,39, + 40,41,42, 40,42,43, + 44,45,46, 44,46,47, + + 48,49,50, 48,50,51, + 52,53,54, 52,54,55, + 56,57,58, 56,58,59, + 60,61,62, 60,62,63, + 64,65,66, 64,66,67, + 68,69,70, 68,70,71, + ]); + + // Write the vertex property to buffers (coordinates, colors and normals) + if (!initArrayBuffer(gl, 'a_Position', vertices, 3)) return -1; + if (!initArrayBuffer(gl, 'a_Color', colors, 3)) return -1; + if (!initArrayBuffer(gl, 'a_Normal', normals, 3)) return -1; + + // Unbind the buffer object + gl.bindBuffer(gl.ARRAY_BUFFER, null); + + // Write the indices to the buffer object + var indexBuffer = gl.createBuffer(); + if (!indexBuffer) { + console.log('Failed to create the buffer object'); + return false; + } + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer); + gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW); + + return indices.length; +} + +function initArrayBuffer(gl, attribute, data, num) { + // Create a buffer object + var buffer = gl.createBuffer(); + if (!buffer) { + console.log('Failed to create the buffer object'); + return false; + } + // Write date into the buffer object + gl.bindBuffer(gl.ARRAY_BUFFER, buffer); + gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW); + // Assign the buffer object to the attribute variable + var a_attribute = gl.getAttribLocation(gl.program, attribute); + if (a_attribute < 0) { + console.log('Failed to get the storage location of ' + attribute); + return false; + } + gl.vertexAttribPointer(a_attribute, num, gl.FLOAT, false, 0, 0); + // Enable the assignment of the buffer object to the attribute variable + gl.enableVertexAttribArray(a_attribute); + + return true; +} + +// Key down +function keydown(ev, gl, n, u_MvpMatrix, mvpMatrix, u_LightDirection, lightDirection, u_AmbientLight, u_DiffuseLight, u_LightColor, u_LightPosition, modelMatrix, u_ModelMatrix, normalMatrix, u_NormalMatrix) { + switch(ev.keyCode){ + case 65 : + camSide-=0.1; + break; + case 87 : + camHeight+=0.1; + break; + case 68: + camSide+=0.1; + break; + case 83 : + camHeight-=0.1; + break; + case 38 : + //arrow up + camRotateFront+=0.1; + break; + case 37: + // arrow left + camRotateSide-=0.1; + break; + case 40: + //arrow bottom + camRotateFront-=0.1; + break; + case 39: // arrow right + camRotateSide+=0.1; + break; + } + draw(gl, n, u_MvpMatrix, mvpMatrix, u_LightDirection, lightDirection, u_AmbientLight, u_DiffuseLight, u_LightColor, u_LightPosition, modelMatrix, u_ModelMatrix, normalMatrix, u_NormalMatrix); +} + +function handleCheckBox(cb){ + console.log(cb.name); + console.log(cb.checked); + + if(cb.name =="light1"){ + displayLight1 = cb.checked; + console.log(displayLight1); + } + + if(cb.name == "light2"){ + displayLight2 = cb.checked; + console.log(displayLight2); + } +} \ No newline at end of file diff --git a/lab2/lib/cuon-matrix.js b/lab2/lib/cuon-matrix.js new file mode 100644 index 0000000000000000000000000000000000000000..b67a5dd1b998eba54f585cf3849b8c17052d2a35 --- /dev/null +++ b/lab2/lib/cuon-matrix.js @@ -0,0 +1,741 @@ +// cuon-matrix.js (c) 2012 kanda and matsuda +/** + * This is a class treating 4x4 matrix. + * This class contains the function that is equivalent to OpenGL matrix stack. + * The matrix after conversion is calculated by multiplying a conversion matrix from the right. + * The matrix is replaced by the calculated result. + */ + +/** + * Constructor of Matrix4 + * If opt_src is specified, new matrix is initialized by opt_src. + * Otherwise, new matrix is initialized by identity matrix. + * @param opt_src source matrix(option) + */ +var Matrix4 = function(opt_src) { + var i, s, d; + if (opt_src && typeof opt_src === 'object' && opt_src.hasOwnProperty('elements')) { + s = opt_src.elements; + d = new Float32Array(16); + for (i = 0; i < 16; ++i) { + d[i] = s[i]; + } + this.elements = d; + } else { + this.elements = new Float32Array([1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]); + } +}; + +/** + * Set the identity matrix. + * @return this + */ +Matrix4.prototype.setIdentity = function() { + var e = this.elements; + e[0] = 1; e[4] = 0; e[8] = 0; e[12] = 0; + e[1] = 0; e[5] = 1; e[9] = 0; e[13] = 0; + e[2] = 0; e[6] = 0; e[10] = 1; e[14] = 0; + e[3] = 0; e[7] = 0; e[11] = 0; e[15] = 1; + return this; +}; + +/** + * Copy matrix. + * @param src source matrix + * @return this + */ +Matrix4.prototype.set = function(src) { + var i, s, d; + + s = src.elements; + d = this.elements; + + if (s === d) { + return; + } + + for (i = 0; i < 16; ++i) { + d[i] = s[i]; + } + + return this; +}; + +/** + * Multiply the matrix from the right. + * @param other The multiply matrix + * @return this + */ +Matrix4.prototype.concat = function(other) { + var i, e, a, b, ai0, ai1, ai2, ai3; + + // Calculate e = a * b + e = this.elements; + a = this.elements; + b = other.elements; + + // If e equals b, copy b to temporary matrix. + if (e === b) { + b = new Float32Array(16); + for (i = 0; i < 16; ++i) { + b[i] = e[i]; + } + } + + for (i = 0; i < 4; i++) { + ai0=a[i]; ai1=a[i+4]; ai2=a[i+8]; ai3=a[i+12]; + e[i] = ai0 * b[0] + ai1 * b[1] + ai2 * b[2] + ai3 * b[3]; + e[i+4] = ai0 * b[4] + ai1 * b[5] + ai2 * b[6] + ai3 * b[7]; + e[i+8] = ai0 * b[8] + ai1 * b[9] + ai2 * b[10] + ai3 * b[11]; + e[i+12] = ai0 * b[12] + ai1 * b[13] + ai2 * b[14] + ai3 * b[15]; + } + + return this; +}; +Matrix4.prototype.multiply = Matrix4.prototype.concat; + +/** + * Multiply the three-dimensional vector. + * @param pos The multiply vector + * @return The result of multiplication(Float32Array) + */ +Matrix4.prototype.multiplyVector3 = function(pos) { + var e = this.elements; + var p = pos.elements; + var v = new Vector3(); + var result = v.elements; + + result[0] = p[0] * e[0] + p[1] * e[4] + p[2] * e[ 8] + e[12]; + result[1] = p[0] * e[1] + p[1] * e[5] + p[2] * e[ 9] + e[13]; + result[2] = p[0] * e[2] + p[1] * e[6] + p[2] * e[10] + e[14]; + + return v; +}; + +/** + * Multiply the four-dimensional vector. + * @param pos The multiply vector + * @return The result of multiplication(Float32Array) + */ +Matrix4.prototype.multiplyVector4 = function(pos) { + var e = this.elements; + var p = pos.elements; + var v = new Vector4(); + var result = v.elements; + + result[0] = p[0] * e[0] + p[1] * e[4] + p[2] * e[ 8] + p[3] * e[12]; + result[1] = p[0] * e[1] + p[1] * e[5] + p[2] * e[ 9] + p[3] * e[13]; + result[2] = p[0] * e[2] + p[1] * e[6] + p[2] * e[10] + p[3] * e[14]; + result[3] = p[0] * e[3] + p[1] * e[7] + p[2] * e[11] + p[3] * e[15]; + + return v; +}; + +/** + * Transpose the matrix. + * @return this + */ +Matrix4.prototype.transpose = function() { + var e, t; + + e = this.elements; + + t = e[ 1]; e[ 1] = e[ 4]; e[ 4] = t; + t = e[ 2]; e[ 2] = e[ 8]; e[ 8] = t; + t = e[ 3]; e[ 3] = e[12]; e[12] = t; + t = e[ 6]; e[ 6] = e[ 9]; e[ 9] = t; + t = e[ 7]; e[ 7] = e[13]; e[13] = t; + t = e[11]; e[11] = e[14]; e[14] = t; + + return this; +}; + +/** + * Calculate the inverse matrix of specified matrix, and set to this. + * @param other The source matrix + * @return this + */ +Matrix4.prototype.setInverseOf = function(other) { + var i, s, d, inv, det; + + s = other.elements; + d = this.elements; + inv = new Float32Array(16); + + inv[0] = s[5]*s[10]*s[15] - s[5] *s[11]*s[14] - s[9] *s[6]*s[15] + + s[9]*s[7] *s[14] + s[13]*s[6] *s[11] - s[13]*s[7]*s[10]; + inv[4] = - s[4]*s[10]*s[15] + s[4] *s[11]*s[14] + s[8] *s[6]*s[15] + - s[8]*s[7] *s[14] - s[12]*s[6] *s[11] + s[12]*s[7]*s[10]; + inv[8] = s[4]*s[9] *s[15] - s[4] *s[11]*s[13] - s[8] *s[5]*s[15] + + s[8]*s[7] *s[13] + s[12]*s[5] *s[11] - s[12]*s[7]*s[9]; + inv[12] = - s[4]*s[9] *s[14] + s[4] *s[10]*s[13] + s[8] *s[5]*s[14] + - s[8]*s[6] *s[13] - s[12]*s[5] *s[10] + s[12]*s[6]*s[9]; + + inv[1] = - s[1]*s[10]*s[15] + s[1] *s[11]*s[14] + s[9] *s[2]*s[15] + - s[9]*s[3] *s[14] - s[13]*s[2] *s[11] + s[13]*s[3]*s[10]; + inv[5] = s[0]*s[10]*s[15] - s[0] *s[11]*s[14] - s[8] *s[2]*s[15] + + s[8]*s[3] *s[14] + s[12]*s[2] *s[11] - s[12]*s[3]*s[10]; + inv[9] = - s[0]*s[9] *s[15] + s[0] *s[11]*s[13] + s[8] *s[1]*s[15] + - s[8]*s[3] *s[13] - s[12]*s[1] *s[11] + s[12]*s[3]*s[9]; + inv[13] = s[0]*s[9] *s[14] - s[0] *s[10]*s[13] - s[8] *s[1]*s[14] + + s[8]*s[2] *s[13] + s[12]*s[1] *s[10] - s[12]*s[2]*s[9]; + + inv[2] = s[1]*s[6]*s[15] - s[1] *s[7]*s[14] - s[5] *s[2]*s[15] + + s[5]*s[3]*s[14] + s[13]*s[2]*s[7] - s[13]*s[3]*s[6]; + inv[6] = - s[0]*s[6]*s[15] + s[0] *s[7]*s[14] + s[4] *s[2]*s[15] + - s[4]*s[3]*s[14] - s[12]*s[2]*s[7] + s[12]*s[3]*s[6]; + inv[10] = s[0]*s[5]*s[15] - s[0] *s[7]*s[13] - s[4] *s[1]*s[15] + + s[4]*s[3]*s[13] + s[12]*s[1]*s[7] - s[12]*s[3]*s[5]; + inv[14] = - s[0]*s[5]*s[14] + s[0] *s[6]*s[13] + s[4] *s[1]*s[14] + - s[4]*s[2]*s[13] - s[12]*s[1]*s[6] + s[12]*s[2]*s[5]; + + inv[3] = - s[1]*s[6]*s[11] + s[1]*s[7]*s[10] + s[5]*s[2]*s[11] + - s[5]*s[3]*s[10] - s[9]*s[2]*s[7] + s[9]*s[3]*s[6]; + inv[7] = s[0]*s[6]*s[11] - s[0]*s[7]*s[10] - s[4]*s[2]*s[11] + + s[4]*s[3]*s[10] + s[8]*s[2]*s[7] - s[8]*s[3]*s[6]; + inv[11] = - s[0]*s[5]*s[11] + s[0]*s[7]*s[9] + s[4]*s[1]*s[11] + - s[4]*s[3]*s[9] - s[8]*s[1]*s[7] + s[8]*s[3]*s[5]; + inv[15] = s[0]*s[5]*s[10] - s[0]*s[6]*s[9] - s[4]*s[1]*s[10] + + s[4]*s[2]*s[9] + s[8]*s[1]*s[6] - s[8]*s[2]*s[5]; + + det = s[0]*inv[0] + s[1]*inv[4] + s[2]*inv[8] + s[3]*inv[12]; + if (det === 0) { + return this; + } + + det = 1 / det; + for (i = 0; i < 16; i++) { + d[i] = inv[i] * det; + } + + return this; +}; + +/** + * Calculate the inverse matrix of this, and set to this. + * @return this + */ +Matrix4.prototype.invert = function() { + return this.setInverseOf(this); +}; + +/** + * Set the orthographic projection matrix. + * @param left The coordinate of the left of clipping plane. + * @param right The coordinate of the right of clipping plane. + * @param bottom The coordinate of the bottom of clipping plane. + * @param top The coordinate of the top top clipping plane. + * @param near The distances to the nearer depth clipping plane. This value is minus if the plane is to be behind the viewer. + * @param far The distances to the farther depth clipping plane. This value is minus if the plane is to be behind the viewer. + * @return this + */ +Matrix4.prototype.setOrtho = function(left, right, bottom, top, near, far) { + var e, rw, rh, rd; + + if (left === right || bottom === top || near === far) { + throw 'null frustum'; + } + + rw = 1 / (right - left); + rh = 1 / (top - bottom); + rd = 1 / (far - near); + + e = this.elements; + + e[0] = 2 * rw; + e[1] = 0; + e[2] = 0; + e[3] = 0; + + e[4] = 0; + e[5] = 2 * rh; + e[6] = 0; + e[7] = 0; + + e[8] = 0; + e[9] = 0; + e[10] = -2 * rd; + e[11] = 0; + + e[12] = -(right + left) * rw; + e[13] = -(top + bottom) * rh; + e[14] = -(far + near) * rd; + e[15] = 1; + + return this; +}; + +/** + * Multiply the orthographic projection matrix from the right. + * @param left The coordinate of the left of clipping plane. + * @param right The coordinate of the right of clipping plane. + * @param bottom The coordinate of the bottom of clipping plane. + * @param top The coordinate of the top top clipping plane. + * @param near The distances to the nearer depth clipping plane. This value is minus if the plane is to be behind the viewer. + * @param far The distances to the farther depth clipping plane. This value is minus if the plane is to be behind the viewer. + * @return this + */ +Matrix4.prototype.ortho = function(left, right, bottom, top, near, far) { + return this.concat(new Matrix4().setOrtho(left, right, bottom, top, near, far)); +}; + +/** + * Set the perspective projection matrix. + * @param left The coordinate of the left of clipping plane. + * @param right The coordinate of the right of clipping plane. + * @param bottom The coordinate of the bottom of clipping plane. + * @param top The coordinate of the top top clipping plane. + * @param near The distances to the nearer depth clipping plane. This value must be plus value. + * @param far The distances to the farther depth clipping plane. This value must be plus value. + * @return this + */ +Matrix4.prototype.setFrustum = function(left, right, bottom, top, near, far) { + var e, rw, rh, rd; + + if (left === right || top === bottom || near === far) { + throw 'null frustum'; + } + if (near <= 0) { + throw 'near <= 0'; + } + if (far <= 0) { + throw 'far <= 0'; + } + + rw = 1 / (right - left); + rh = 1 / (top - bottom); + rd = 1 / (far - near); + + e = this.elements; + + e[ 0] = 2 * near * rw; + e[ 1] = 0; + e[ 2] = 0; + e[ 3] = 0; + + e[ 4] = 0; + e[ 5] = 2 * near * rh; + e[ 6] = 0; + e[ 7] = 0; + + e[ 8] = (right + left) * rw; + e[ 9] = (top + bottom) * rh; + e[10] = -(far + near) * rd; + e[11] = -1; + + e[12] = 0; + e[13] = 0; + e[14] = -2 * near * far * rd; + e[15] = 0; + + return this; +}; + +/** + * Multiply the perspective projection matrix from the right. + * @param left The coordinate of the left of clipping plane. + * @param right The coordinate of the right of clipping plane. + * @param bottom The coordinate of the bottom of clipping plane. + * @param top The coordinate of the top top clipping plane. + * @param near The distances to the nearer depth clipping plane. This value must be plus value. + * @param far The distances to the farther depth clipping plane. This value must be plus value. + * @return this + */ +Matrix4.prototype.frustum = function(left, right, bottom, top, near, far) { + return this.concat(new Matrix4().setFrustum(left, right, bottom, top, near, far)); +}; + +/** + * Set the perspective projection matrix by fovy and aspect. + * @param fovy The angle between the upper and lower sides of the frustum. + * @param aspect The aspect ratio of the frustum. (width/height) + * @param near The distances to the nearer depth clipping plane. This value must be plus value. + * @param far The distances to the farther depth clipping plane. This value must be plus value. + * @return this + */ +Matrix4.prototype.setPerspective = function(fovy, aspect, near, far) { + var e, rd, s, ct; + + if (near === far || aspect === 0) { + throw 'null frustum'; + } + if (near <= 0) { + throw 'near <= 0'; + } + if (far <= 0) { + throw 'far <= 0'; + } + + fovy = Math.PI * fovy / 180 / 2; + s = Math.sin(fovy); + if (s === 0) { + throw 'null frustum'; + } + + rd = 1 / (far - near); + ct = Math.cos(fovy) / s; + + e = this.elements; + + e[0] = ct / aspect; + e[1] = 0; + e[2] = 0; + e[3] = 0; + + e[4] = 0; + e[5] = ct; + e[6] = 0; + e[7] = 0; + + e[8] = 0; + e[9] = 0; + e[10] = -(far + near) * rd; + e[11] = -1; + + e[12] = 0; + e[13] = 0; + e[14] = -2 * near * far * rd; + e[15] = 0; + + return this; +}; + +/** + * Multiply the perspective projection matrix from the right. + * @param fovy The angle between the upper and lower sides of the frustum. + * @param aspect The aspect ratio of the frustum. (width/height) + * @param near The distances to the nearer depth clipping plane. This value must be plus value. + * @param far The distances to the farther depth clipping plane. This value must be plus value. + * @return this + */ +Matrix4.prototype.perspective = function(fovy, aspect, near, far) { + return this.concat(new Matrix4().setPerspective(fovy, aspect, near, far)); +}; + +/** + * Set the matrix for scaling. + * @param x The scale factor along the X axis + * @param y The scale factor along the Y axis + * @param z The scale factor along the Z axis + * @return this + */ +Matrix4.prototype.setScale = function(x, y, z) { + var e = this.elements; + e[0] = x; e[4] = 0; e[8] = 0; e[12] = 0; + e[1] = 0; e[5] = y; e[9] = 0; e[13] = 0; + e[2] = 0; e[6] = 0; e[10] = z; e[14] = 0; + e[3] = 0; e[7] = 0; e[11] = 0; e[15] = 1; + return this; +}; + +/** + * Multiply the matrix for scaling from the right. + * @param x The scale factor along the X axis + * @param y The scale factor along the Y axis + * @param z The scale factor along the Z axis + * @return this + */ +Matrix4.prototype.scale = function(x, y, z) { + var e = this.elements; + e[0] *= x; e[4] *= y; e[8] *= z; + e[1] *= x; e[5] *= y; e[9] *= z; + e[2] *= x; e[6] *= y; e[10] *= z; + e[3] *= x; e[7] *= y; e[11] *= z; + return this; +}; + +/** + * Set the matrix for translation. + * @param x The X value of a translation. + * @param y The Y value of a translation. + * @param z The Z value of a translation. + * @return this + */ +Matrix4.prototype.setTranslate = function(x, y, z) { + var e = this.elements; + e[0] = 1; e[4] = 0; e[8] = 0; e[12] = x; + e[1] = 0; e[5] = 1; e[9] = 0; e[13] = y; + e[2] = 0; e[6] = 0; e[10] = 1; e[14] = z; + e[3] = 0; e[7] = 0; e[11] = 0; e[15] = 1; + return this; +}; + +/** + * Multiply the matrix for translation from the right. + * @param x The X value of a translation. + * @param y The Y value of a translation. + * @param z The Z value of a translation. + * @return this + */ +Matrix4.prototype.translate = function(x, y, z) { + var e = this.elements; + e[12] += e[0] * x + e[4] * y + e[8] * z; + e[13] += e[1] * x + e[5] * y + e[9] * z; + e[14] += e[2] * x + e[6] * y + e[10] * z; + e[15] += e[3] * x + e[7] * y + e[11] * z; + return this; +}; + +/** + * Set the matrix for rotation. + * The vector of rotation axis may not be normalized. + * @param angle The angle of rotation (degrees) + * @param x The X coordinate of vector of rotation axis. + * @param y The Y coordinate of vector of rotation axis. + * @param z The Z coordinate of vector of rotation axis. + * @return this + */ +Matrix4.prototype.setRotate = function(angle, x, y, z) { + var e, s, c, len, rlen, nc, xy, yz, zx, xs, ys, zs; + + angle = Math.PI * angle / 180; + e = this.elements; + + s = Math.sin(angle); + c = Math.cos(angle); + + if (0 !== x && 0 === y && 0 === z) { + // Rotation around X axis + if (x < 0) { + s = -s; + } + e[0] = 1; e[4] = 0; e[ 8] = 0; e[12] = 0; + e[1] = 0; e[5] = c; e[ 9] =-s; e[13] = 0; + e[2] = 0; e[6] = s; e[10] = c; e[14] = 0; + e[3] = 0; e[7] = 0; e[11] = 0; e[15] = 1; + } else if (0 === x && 0 !== y && 0 === z) { + // Rotation around Y axis + if (y < 0) { + s = -s; + } + e[0] = c; e[4] = 0; e[ 8] = s; e[12] = 0; + e[1] = 0; e[5] = 1; e[ 9] = 0; e[13] = 0; + e[2] =-s; e[6] = 0; e[10] = c; e[14] = 0; + e[3] = 0; e[7] = 0; e[11] = 0; e[15] = 1; + } else if (0 === x && 0 === y && 0 !== z) { + // Rotation around Z axis + if (z < 0) { + s = -s; + } + e[0] = c; e[4] =-s; e[ 8] = 0; e[12] = 0; + e[1] = s; e[5] = c; e[ 9] = 0; e[13] = 0; + e[2] = 0; e[6] = 0; e[10] = 1; e[14] = 0; + e[3] = 0; e[7] = 0; e[11] = 0; e[15] = 1; + } else { + // Rotation around another axis + len = Math.sqrt(x*x + y*y + z*z); + if (len !== 1) { + rlen = 1 / len; + x *= rlen; + y *= rlen; + z *= rlen; + } + nc = 1 - c; + xy = x * y; + yz = y * z; + zx = z * x; + xs = x * s; + ys = y * s; + zs = z * s; + + e[ 0] = x*x*nc + c; + e[ 1] = xy *nc + zs; + e[ 2] = zx *nc - ys; + e[ 3] = 0; + + e[ 4] = xy *nc - zs; + e[ 5] = y*y*nc + c; + e[ 6] = yz *nc + xs; + e[ 7] = 0; + + e[ 8] = zx *nc + ys; + e[ 9] = yz *nc - xs; + e[10] = z*z*nc + c; + e[11] = 0; + + e[12] = 0; + e[13] = 0; + e[14] = 0; + e[15] = 1; + } + + return this; +}; + +/** + * Multiply the matrix for rotation from the right. + * The vector of rotation axis may not be normalized. + * @param angle The angle of rotation (degrees) + * @param x The X coordinate of vector of rotation axis. + * @param y The Y coordinate of vector of rotation axis. + * @param z The Z coordinate of vector of rotation axis. + * @return this + */ +Matrix4.prototype.rotate = function(angle, x, y, z) { + return this.concat(new Matrix4().setRotate(angle, x, y, z)); +}; + +/** + * Set the viewing matrix. + * @param eyeX, eyeY, eyeZ The position of the eye point. + * @param centerX, centerY, centerZ The position of the reference point. + * @param upX, upY, upZ The direction of the up vector. + * @return this + */ +Matrix4.prototype.setLookAt = function(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ) { + var e, fx, fy, fz, rlf, sx, sy, sz, rls, ux, uy, uz; + + fx = centerX - eyeX; + fy = centerY - eyeY; + fz = centerZ - eyeZ; + + // Normalize f. + rlf = 1 / Math.sqrt(fx*fx + fy*fy + fz*fz); + fx *= rlf; + fy *= rlf; + fz *= rlf; + + // Calculate cross product of f and up. + sx = fy * upZ - fz * upY; + sy = fz * upX - fx * upZ; + sz = fx * upY - fy * upX; + + // Normalize s. + rls = 1 / Math.sqrt(sx*sx + sy*sy + sz*sz); + sx *= rls; + sy *= rls; + sz *= rls; + + // Calculate cross product of s and f. + ux = sy * fz - sz * fy; + uy = sz * fx - sx * fz; + uz = sx * fy - sy * fx; + + // Set to this. + e = this.elements; + e[0] = sx; + e[1] = ux; + e[2] = -fx; + e[3] = 0; + + e[4] = sy; + e[5] = uy; + e[6] = -fy; + e[7] = 0; + + e[8] = sz; + e[9] = uz; + e[10] = -fz; + e[11] = 0; + + e[12] = 0; + e[13] = 0; + e[14] = 0; + e[15] = 1; + + // Translate. + return this.translate(-eyeX, -eyeY, -eyeZ); +}; + +/** + * Multiply the viewing matrix from the right. + * @param eyeX, eyeY, eyeZ The position of the eye point. + * @param centerX, centerY, centerZ The position of the reference point. + * @param upX, upY, upZ The direction of the up vector. + * @return this + */ +Matrix4.prototype.lookAt = function(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ) { + return this.concat(new Matrix4().setLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ)); +}; + +/** + * Multiply the matrix for project vertex to plane from the right. + * @param plane The array[A, B, C, D] of the equation of plane "Ax + By + Cz + D = 0". + * @param light The array which stored coordinates of the light. if light[3]=0, treated as parallel light. + * @return this + */ +Matrix4.prototype.dropShadow = function(plane, light) { + var mat = new Matrix4(); + var e = mat.elements; + + var dot = plane[0] * light[0] + plane[1] * light[1] + plane[2] * light[2] + plane[3] * light[3]; + + e[ 0] = dot - light[0] * plane[0]; + e[ 1] = - light[1] * plane[0]; + e[ 2] = - light[2] * plane[0]; + e[ 3] = - light[3] * plane[0]; + + e[ 4] = - light[0] * plane[1]; + e[ 5] = dot - light[1] * plane[1]; + e[ 6] = - light[2] * plane[1]; + e[ 7] = - light[3] * plane[1]; + + e[ 8] = - light[0] * plane[2]; + e[ 9] = - light[1] * plane[2]; + e[10] = dot - light[2] * plane[2]; + e[11] = - light[3] * plane[2]; + + e[12] = - light[0] * plane[3]; + e[13] = - light[1] * plane[3]; + e[14] = - light[2] * plane[3]; + e[15] = dot - light[3] * plane[3]; + + return this.concat(mat); +} + +/** + * Multiply the matrix for project vertex to plane from the right.(Projected by parallel light.) + * @param normX, normY, normZ The normal vector of the plane.(Not necessary to be normalized.) + * @param planeX, planeY, planeZ The coordinate of arbitrary points on a plane. + * @param lightX, lightY, lightZ The vector of the direction of light.(Not necessary to be normalized.) + * @return this + */ +Matrix4.prototype.dropShadowDirectionally = function(normX, normY, normZ, planeX, planeY, planeZ, lightX, lightY, lightZ) { + var a = planeX * normX + planeY * normY + planeZ * normZ; + return this.dropShadow([normX, normY, normZ, -a], [lightX, lightY, lightZ, 0]); +}; + +/** + * Constructor of Vector3 + * If opt_src is specified, new vector is initialized by opt_src. + * @param opt_src source vector(option) + */ +var Vector3 = function(opt_src) { + var v = new Float32Array(3); + if (opt_src && typeof opt_src === 'object') { + v[0] = opt_src[0]; v[1] = opt_src[1]; v[2] = opt_src[2]; + } + this.elements = v; +} + +/** + * Normalize. + * @return this + */ +Vector3.prototype.normalize = function() { + var v = this.elements; + var c = v[0], d = v[1], e = v[2], g = Math.sqrt(c*c+d*d+e*e); + if(g){ + if(g == 1) + return this; + } else { + v[0] = 0; v[1] = 0; v[2] = 0; + return this; + } + g = 1/g; + v[0] = c*g; v[1] = d*g; v[2] = e*g; + return this; +}; + +/** + * Constructor of Vector4 + * If opt_src is specified, new vector is initialized by opt_src. + * @param opt_src source vector(option) + */ +var Vector4 = function(opt_src) { + var v = new Float32Array(4); + if (opt_src && typeof opt_src === 'object') { + v[0] = opt_src[0]; v[1] = opt_src[1]; v[2] = opt_src[2]; v[3] = opt_src[3]; + } + this.elements = v; +} diff --git a/lab2/src/lib/cuon-utils.js b/lab2/lib/cuon-utils.js similarity index 95% rename from lab2/src/lib/cuon-utils.js rename to lab2/lib/cuon-utils.js index dc08b2ef1ee96304e68dafa7caed57f7e80894bb..471d98cd0c41a50ed30058f30f6226a4fc93465e 100644 --- a/lab2/src/lib/cuon-utils.js +++ b/lab2/lib/cuon-utils.js @@ -94,13 +94,13 @@ function loadShader(gl, type, source) { } /** - * Initialize and get the rendering for WebglInstance + * Initialize and get the rendering for WebGL * @param canvas <cavnas> element * @param opt_debug flag to initialize the context for debugging - * @return the rendering context for WebglInstance + * @return the rendering context for WebGL */ function getWebGLContext(canvas, opt_debug) { - // Get the rendering context for WebglInstance + // Get the rendering context for WebGL var gl = WebGLUtils.setupWebGL(canvas); if (!gl) return null; diff --git a/lab2/src/lib/webgl-debug.js b/lab2/lib/webgl-debug.js similarity index 92% rename from lab2/src/lib/webgl-debug.js rename to lab2/lib/webgl-debug.js index 685868dc61347ea10c0376520f1e20ed57a765c7..8e27d54aa7f3592ed72cd40d0e7c67de558f52b4 100644 --- a/lab2/src/lib/webgl-debug.js +++ b/lab2/lib/webgl-debug.js @@ -2,7 +2,7 @@ //Use of this source code is governed by a BSD-style license that can be //found in the LICENSE file. -// Various functions for helping debug WebglInstance apps. +// Various functions for helping debug WebGL apps. WebGLDebugUtils = function() { @@ -105,7 +105,7 @@ var glEnums = null; /** * Initializes this module. Safe to call more than once. - * @param {!WebGLRenderingContext} ctx A WebglInstance context. If + * @param {!WebGLRenderingContext} ctx A WebGL context. If * you have more than one context it doesn't matter which one * you pass in, it is only used to pull out constants. */ @@ -130,9 +130,9 @@ function checkInit() { } /** - * Returns true or false if value matches any WebglInstance enum + * Returns true or false if value matches any WebGL enum * @param {*} value Value to check if it might be an enum. - * @return {boolean} True if value matches one of the WebglInstance defined enums + * @return {boolean} True if value matches one of the WebGL defined enums */ function mightBeEnum(value) { checkInit(); @@ -140,7 +140,7 @@ function mightBeEnum(value) { } /** - * Gets an string version of an WebglInstance enum. + * Gets an string version of an WebGL enum. * * Example: * var str = WebGLDebugUtil.glEnumToString(ctx.getError()); @@ -152,13 +152,13 @@ function glEnumToString(value) { checkInit(); var name = glEnums[value]; return (name !== undefined) ? name : - ("*UNKNOWN WebglInstance ENUM (0x" + value.toString(16) + ")"); + ("*UNKNOWN WebGL ENUM (0x" + value.toString(16) + ")"); } /** - * Returns the string version of a WebglInstance argument. + * Returns the string version of a WebGL argument. * Attempts to convert enum arguments to strings. - * @param {string} functionName the name of the WebglInstance function. + * @param {string} functionName the name of the WebGL function. * @param {number} argumentIndx the index of the argument. * @param {*} value The value of the argument. * @return {string} The value as a string. @@ -174,7 +174,7 @@ function glFunctionArgToString(functionName, argumentIndex, value) { } /** - * Given a WebglInstance context returns a wrapped context that calls + * Given a WebGL context returns a wrapped context that calls * gl.getError after every command and calls a function if the * result is not gl.NO_ERROR. * @@ -194,7 +194,7 @@ function makeDebugContext(ctx, opt_onErrorFunc) { argStr += ((ii == 0) ? '' : ', ') + glFunctionArgToString(functionName, ii, args[ii]); } - log("WebglInstance error "+ glEnumToString(err) + " in "+ functionName + + log("WebGL error "+ glEnumToString(err) + " in "+ functionName + "(" + argStr + ")"); }; @@ -202,7 +202,7 @@ function makeDebugContext(ctx, opt_onErrorFunc) { // we can still return it to the client app. var glErrorShadow = { }; - // Makes a function that calls a WebglInstance function and then calls getError. + // Makes a function that calls a WebGL function and then calls getError. function makeErrorWrapper(ctx, functionName) { return function() { var result = ctx[functionName].apply(ctx, arguments); @@ -215,7 +215,7 @@ function makeDebugContext(ctx, opt_onErrorFunc) { }; } - // Make a an object that has a copy of every property of the WebglInstance context + // Make a an object that has a copy of every property of the WebGL context // but wraps all functions. var wrapper = {}; for (var propertyName in ctx) { @@ -344,7 +344,7 @@ function makeLostContextSimulatingContext(ctx) { } } - // Makes a function that simulates WebglInstance when out of context. + // Makes a function that simulates WebGL when out of context. function makeLostContextWrapper(ctx, functionName) { var f = ctx[functionName]; return function() { @@ -577,21 +577,21 @@ function makeLostContextSimulatingContext(ctx) { return { /** * Initializes this module. Safe to call more than once. - * @param {!WebGLRenderingContext} ctx A WebglInstance context. If + * @param {!WebGLRenderingContext} ctx A WebGL context. If * you have more than one context it doesn't matter which one * you pass in, it is only used to pull out constants. */ 'init': init, /** - * Returns true or false if value matches any WebglInstance enum + * Returns true or false if value matches any WebGL enum * @param {*} value Value to check if it might be an enum. - * @return {boolean} True if value matches one of the WebglInstance defined enums + * @return {boolean} True if value matches one of the WebGL defined enums */ 'mightBeEnum': mightBeEnum, /** - * Gets an string version of an WebglInstance enum. + * Gets an string version of an WebGL enum. * * Example: * WebGLDebugUtil.init(ctx); @@ -603,7 +603,7 @@ return { 'glEnumToString': glEnumToString, /** - * Converts the argument of a WebglInstance function to a string. + * Converts the argument of a WebGL function to a string. * Attempts to convert enum arguments to strings. * * Example: @@ -612,7 +612,7 @@ return { * * would return 'TEXTURE_2D' * - * @param {string} functionName the name of the WebglInstance function. + * @param {string} functionName the name of the WebGL function. * @param {number} argumentIndx the index of the argument. * @param {*} value The value of the argument. * @return {string} The value as a string. @@ -620,7 +620,7 @@ return { 'glFunctionArgToString': glFunctionArgToString, /** - * Given a WebglInstance context returns a wrapped context that calls + * Given a WebGL context returns a wrapped context that calls * gl.getError after every command and calls a function if the * result is not NO_ERROR. * @@ -643,7 +643,7 @@ return { 'makeDebugContext': makeDebugContext, /** - * Given a WebglInstance context returns a wrapped context that adds 4 + * Given a WebGL context returns a wrapped context that adds 4 * functions. * * ctx.loseContext: diff --git a/lab2/src/lib/webgl-utils.js b/lab2/lib/webgl-utils.js similarity index 95% rename from lab2/src/lib/webgl-utils.js rename to lab2/lib/webgl-utils.js index 26ed37e0d6adf469edabfa17b0c9e3d00199e744..7a6b54616559301f33b6f9bd4e7c41af77b224a0 100644 --- a/lab2/src/lib/webgl-utils.js +++ b/lab2/lib/webgl-utils.js @@ -40,7 +40,7 @@ * * gl = WebGLUtils.setupWebGL(canvas); * - * For animated WebglInstance apps use of setTimeout or setInterval are + * For animated WebGL apps use of setTimeout or setInterval are * discouraged. It is recommended you structure your rendering * loop like this. * @@ -82,7 +82,7 @@ var makeFailHTML = function(msg) { * @type {string} */ var GET_A_WEBGL_BROWSER = '' + - 'This page requires a browser that supports WebglInstance.<br/>' + + 'This page requires a browser that supports WebGL.<br/>' + '<a href="http://get.webgl.org">Click here to upgrade your browser.</a>'; /** @@ -90,13 +90,13 @@ var GET_A_WEBGL_BROWSER = '' + * @type {string} */ var OTHER_PROBLEM = '' + - "It doesn't appear your computer can support WebglInstance.<br/>" + + "It doesn't appear your computer can support WebGL.<br/>" + '<a href="http://get.webgl.org">Click here for more information.</a>'; /** * Creates a webgl context. If creation fails it will * change the contents of the container of the <canvas> - * tag to an error message with the correct links for WebglInstance. + * tag to an error message with the correct links for WebGL. * @param {Element} canvas. The canvas element to create a * context from. * @param {WebGLContextCreationAttirbutes} opt_attribs Any diff --git a/lab2/readme_assets/A.png b/lab2/readme_assets/A.png deleted file mode 100644 index 14e007b8e5b2bb39b6ddbd8e0cfce1a2e83465e3..0000000000000000000000000000000000000000 Binary files a/lab2/readme_assets/A.png and /dev/null differ diff --git a/lab2/readme_assets/todos.png b/lab2/readme_assets/todos.png deleted file mode 100644 index d892c821268efb755cf9e319319288c6eba5325b..0000000000000000000000000000000000000000 Binary files a/lab2/readme_assets/todos.png and /dev/null differ diff --git a/lab2/src/WebglInstance.js b/lab2/src/WebglInstance.js deleted file mode 100644 index 6997e352db3a893ae5df456b3e34dd6bca06ca1a..0000000000000000000000000000000000000000 --- a/lab2/src/WebglInstance.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * Object that contains the WebGl context and some utilities - * @param {Element} canvas HTML Canvas object where our 3D scene will be drawn - * @constructor - * - Get WebGl context and store it in the attribute "gl" - * - Set the clear color of the canvas - * - Enable the depth test - */ -function WebglInstance(canvas) { - this.gl = getWebGLContext(canvas); - if (!this.gl) - throw 'Failed to get the rendering context for WebglInstance'; - - // Set the clear color and enable the depth test - this.gl.clearColor(0.0, 0.0, 0.0, 1.0); - this.gl.enable(this.gl.DEPTH_TEST); -} - -/** - * Initialize and build the shader program based on the vertex and fragment shader - * @param {string} vertexShader Containing the vertex shader - * @param {string} fragmentShader Containing the fragment shader - */ -WebglInstance.prototype.initShaders = function(vertexShader, fragmentShader) { - if (!initShaders(this.gl, vertexShader, fragmentShader)) - throw 'Failed to initialize shaders.'; -}; - -/** - * Write the vertex coordinates and colors to the buffer object - * @param {YourLetter} shape Object containing 3 attributes: vertices, indices and colors (see YourLetter.js) - */ -WebglInstance.prototype.writeObject = function(shape) { - // Pass the shape positions to the vertex shader - this.writeArrayBuffer(new Float32Array([...shape.vertices]), 3, this.gl.FLOAT, 'a_Position'); - - // Pass the colors to the vertex shader - this.writeArrayBuffer(new Float32Array([...shape.colors]), 3, this.gl.FLOAT, 'a_Color'); - - // Write the indices to the buffer object - const indexBuffer = this.gl.createBuffer(); - if (!indexBuffer) - throw 'Failed to createBuffer'; - - // Write the indices to the buffer object - this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, indexBuffer); - this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER, new Uint8Array([...shape.indices]), this.gl.STATIC_DRAW); - - this.indicesCount = shape.indices.length; -}; - -/** - * Write array to the vertex object buffer - * @param {Float32Array} array Data to write - * @param {GLint} size Size of each vertex (3 in our case xyz for positions and rgb for colors - * @param {GLenum} type Type of each value in the array - * @param {string} attribute Attribute variable name - */ -WebglInstance.prototype.writeArrayBuffer = function(array, size, type, attribute) { - const buffer = this.gl.createBuffer(); // Create a buffer object - if (!buffer) - throw 'Failed to create the buffer object'; - - // Write date into the buffer object - this.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer); - this.gl.bufferData(this.gl.ARRAY_BUFFER, array, this.gl.STATIC_DRAW); - // Assign the buffer object to the attribute variable - const a_attribute = this.gl.getAttribLocation(this.gl.program, attribute); - if (a_attribute < 0) - throw 'Failed to get the storage location of ' + attribute; - - this.gl.vertexAttribPointer(a_attribute, size, type, false, 0, 0); - // Enable the assignment of the buffer object to the attribute variable - this.gl.enableVertexAttribArray(a_attribute); -}; - -/** - * Bind and pass a matrix to a uniform varibale of the vertex shader - * @param {Matrix4} matrix Data to be passed to the uniform - * @param {string} u_uniform Uniform variable name - */ -WebglInstance.prototype.writeUniformMatrix = function(matrix, u_uniform) { - // TODO: - // - Get the location of the uniform variable in the shader program. Hint: use getUniformLocation() function) - // - Pass the matrix to the uniform variable. Hint: use uniformMatrix4fv() function) -}; - -/** - * Clear the output buffer - */ -WebglInstance.prototype.clear = function() { - this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT); -}; - -/** - * Render from array buffer data - */ -WebglInstance.prototype.draw = function() { - this.gl.drawElements(this.gl.TRIANGLES, this.indicesCount, this.gl.UNSIGNED_BYTE, 0); -}; \ No newline at end of file diff --git a/lab2/src/YourLetter.js b/lab2/src/YourLetter.js deleted file mode 100644 index 262b30da31f8367dc2025d5bec3bea09adc68034..0000000000000000000000000000000000000000 --- a/lab2/src/YourLetter.js +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Model 3D letter (see README file of lab 2 folder for explanation) - * TODO: Change the file and the function name to go with your assigned letter. - * E.g if your letter is "G", this JavaScript must be renamed to "G.js" and - * the function "YourLetter()" must be renamed to "G()" - * @constructor - * - Define vertices - * - Define indices - * - Define colors - */ -function YourLetter() { - // Define vertices - const v0 = [1, -1, .5], v1 = [1, -1, -.5]; - // v2, v3... TODO: Complete with your points' positions - - this.vertices = new Float32Array([ - v0, v1, - // v2, v3, v4... TODO: Complete with your vertices - ].flat(2)); - - // Define vertices indices - this.indices = new Uint8Array([ // Indices of the vertices - 0,1,8, 0,8,6, - // 0,6,7,... TODO: Complete with your indices - ]); - - // Define colors - this.colors = new Float32Array( - // TODO: Complete with your colors - ); -} \ No newline at end of file diff --git a/lab2/src/lab2.html b/lab2/src/lab2.html deleted file mode 100644 index f356e42eb0c5c6dfdd7263400bb07d135302710d..0000000000000000000000000000000000000000 --- a/lab2/src/lab2.html +++ /dev/null @@ -1,19 +0,0 @@ -<!DOCTYPE html> -<html lang="en"> -<head> - <meta charset="UTF-8"> - <title>Lab 2</title> -</head> -<body onload="main()"> - <canvas height="500" width="500" id="my_Canvas"> - Please use a browser that supports "canvas" - </canvas> -<script src="lib/webgl-utils.js"></script> -<script src="lib/webgl-debug.js"></script> -<script src="lib/cuon-utils.js"></script> -<script src="lib/cuon-matrix.js"></script> -<script src="YourLetter.js"></script> -<script src="WebglInstance.js"></script> -<script src="lab2.js"></script> -</body> -</html> \ No newline at end of file diff --git a/lab2/src/lab2.js b/lab2/src/lab2.js deleted file mode 100644 index f3718c614313bb00bb2ec3c877c7c0c194d808f6..0000000000000000000000000000000000000000 --- a/lab2/src/lab2.js +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Vertex shader program - * @type {string} - */ -const VSHADER_SOURCE = - '\n' + - // TODO: Write your vertex shader - '\n'; - -/** - * Fragment shader program - * @type {string} - */ -const FSHADER_SOURCE = - '\n' + - // TODO: write your fragment shader - '\n'; - -/** - * Global object of type WebglInstance - */ -let webGL; - -/** - * This is the main entry point of your JavaScript program - * called in the "onload" event of the body tag in HTML - */ -function main() { - // Retrieve <canvas> element - const canvas = document.getElementById('my_Canvas'); - - // Get the rendering context for WebglInstance - webGL = new WebglInstance(canvas); - - // Initialize shaders - webGL.initShaders(VSHADER_SOURCE, FSHADER_SOURCE); - - // TODO: - // - Instantiate your letter object - // - Defining the transform matrix (Model) - // - Defining the camera view and projection matrix - - //********************************************************************************** - // Rendering - //********************************************************************************** - webGL.clear(); - // TODO: Write the vertex coordinates and colors to the buffer object - // TODO: Write the Model, View, Projection matrices to the buffer object - webGL.draw(); -} \ No newline at end of file