210 likes | 217 Views
Announcement. Final project proposal due in one week. Demo on Jan. 14, 2018?. Final Projects. Individual or Team of 2. Voting during the demo. Do NOT use THREE.js Themes for this semester: Interactive Art in WebGL Animating objects with changing colors, positions, shapes, …etc.
E N D
Announcement • Final project proposal due in one week. • Demo on Jan. 14, 2018?
Final Projects • Individual or Team of 2. • Voting during the demo. • Do NOT use THREE.js • Themes for this semester: Interactive Art in WebGL • Animating objects with changing colors, positions, shapes, …etc. • Interacts with mouse input, audio input (music), or gyro input.
Recap for the Past 10 Weeks • Response to the review comments • Answers to the Midterm Exam questions • Final Project • More 3D models!
Comments from the Reviews • 逐步講解程式碼 • 更詳細講解每個函式的用法 • 增加實作的比重 • 數學好難,希望老師講仔細一點 • 可以大聲一點
Midterm Results • 100分:18人 • 80-90分:15人 • 60-70分:15人 • <60分:6人
Answers to the Midterm Qs Q1: Fix the path to the common folder, Change vertexColors, or change the shaders, gl.clearColor() Q2: Change the texCoord Q3: Init(): timeLoc = gl.getUniformLocation(program, “time"); render(): gl.uniform1f(timeLoc, time);
Answers to the Midterm Qs Q4: materialAmbient, materialDiffuse, and materialSpecular Q5: gl.drawArrays( gl.TRIANGLES, 0, numCubeVertices ); modeling = rotate(0, 1, 0, 0); gl.uniformMatrix4fv( modelingLoc, 0, flatten(modeling) ); gl.drawArrays( gl.TRIANGLES, numCubeVertices, numFloorVertices );
Final Projects • It is about time to think about your final projects. • Themes for this semester: Interactive Art in WebGL • Animating objects with changing colors, positions, shapes, …etc. • Interacts with mouse input, mobile phone gyro sensor input, or audio input (music).
Inspiration • http://srchea.com/experimenting-with-web-audio-api-three-js-webgl • http://threejsplaygnd.brangerbriz.net/ • http://w-labs.at/experiments/audioviz/ • http://airtightinteractive.com/demos/js/reactive/ • http://airtightinteractive.com/demos/ • http://www.michaelbromley.co.uk/blog/42/audio-visualization-with-web-audio-canvas-and-the-soundcloud-api • https://developer.mozilla.org/en-US/demos/detail/3d-audio-spectrum-visualizer/launch
3D Objects An “Object” Oriented Approach
Vertex Shader in HTML (HTML code) <script id="vertex-shader" type="x-shader/x-vertex"> // vertex attributes attribute vec4 vPosition; attribute vec4 vColor; attribute vec4 vNormal; varying vec4 fColor; uniform mat4 modelingMatrix; uniform mat4 viewingMatrix; uniform mat4 projectionMatrix; uniform vec4 eyePosition; uniform vec4 lightPosition; uniform vec4 materialAmbient; uniform vec4 materialDiffuse; uniform vec4 materialSpecular; uniform float shininess;
(HTML code) void main() { vec4 L = normalize( lightPosition - modelingMatrix * vPosition ); vec4 N = normalize( modelingMatrix * vNormal ); vec4 V = normalize( eyePosition - modelingMatrix * vPosition ); vec4 H = normalize( L + V ); // Halfway vector // Compute terms in the illumination equation vec4 ambient = materialAmbient; float Kd = max( dot(L, N), 0.0 ); vec4 diffuse = Kd * materialDiffuse; float Ks = pow( max(dot(N, H), 0.0), shininess ); vec4 specular = Ks * materialSpecular; fColor = (ambient + diffuse) * vColor + specular; gl_Position = ... }
Sphere as a JS “Object”(JavaScript code) function sphere() { this.numVertices = 0; this.pointsArray = []; this.colorsArray = []; this.normalsArray = []; this.spherePoint = function (theta, phi) { var V = vec4(Math.cos(theta)*Math.cos(phi), ... var smallV = scalev(0.5, V); // scale to [-0.5, 0.5] this.pointsArray.push(smallV); V[3]=0.0; // convert point to vector normalize(V, 1); this.normalsArray.push(V); this.colorsArray.push(vec4(1.0, 0.0, 0.0, 1.0)); } ... }
(JavaScript code) var ball = new sphere(); window.onload = function init() { ... var vBuffer = gl.createBuffer(); gl.bindBuffer( gl.ARRAY_BUFFER, vBuffer ); gl.bufferData( gl.ARRAY_BUFFER, flatten(ball.pointsArray), ...); ... var cBuffer = gl.createBuffer(); gl.bindBuffer( gl.ARRAY_BUFFER, cBuffer ); gl.bufferData( gl.ARRAY_BUFFER, flatten(ball.colorsArray), ...); ... var nBuffer = gl.createBuffer(); gl.bindBuffer( gl.ARRAY_BUFFER, nBuffer ); gl.bufferData( gl.ARRAY_BUFFER, flatten(ball.normalsArray), ...); ... }
(JavaScript code) function render() { modeling = mult(rotate(theta[xAxis], 1, 0, 0), mult(rotate(theta[yAxis], 0, 1, 0), rotate(theta[zAxis], 0, 0, 1))); viewing = ... projection = ... ... gl.drawArrays( gl.TRIANGLES, 0, ball.numVertices ); requestAnimFrame( render ); }
A Quick Summary • numVertices, pointsArray, colorsArray, and normalsArray are now members of the sphere object. • The variable ball is an instance of the sphere object, created by “new sphere()”
3D Objects Using the basic-objects-IFS.js Utility
Cube, uvSphere, uvTorus, Teapot, … and more • new cube(side) • new uvSphere(radius, slices, stacks) • teapotModel return { vertexPositions: ..., vertexNormals: ..., vertexTextureCoords: ..., indices: ... }
Lab Time • Try to draw at least 2 different objects (e.g., a sphere and a torus or a teapot)