diff --git a/README.md b/README.md index 2d26873..4ae272f 100644 --- a/README.md +++ b/README.md @@ -1,362 +1,137 @@ ------------------------------------------------------------------------------- CIS565: Project 5: WebGL ------------------------------------------------------------------------------- -Fall 2014 -------------------------------------------------------------------------------- -Due Monday 11/03/2014 -------------------------------------------------------------------------------- - -------------------------------------------------------------------------------- -NOTE: -------------------------------------------------------------------------------- -This project requires any graphics card with support for a modern OpenGL -pipeline. Any AMD, NVIDIA, or Intel card from the past few years should work -fine, and every machine in the SIG Lab and Moore 100 is capable of running -this project. - -This project also requires a WebGL capable browser. The project is known to -have issues with Chrome on windows, but Firefox seems to run it fine. - ------------------------------------------------------------------------------- INTRODUCTION: ------------------------------------------------------------------------------- -In this project, you will get introduced to the world of GLSL in two parts: -vertex shading and fragment shading. The first part of this project is the -Image Processor, and the second part of this project is a Wave Vertex Shader. - -In the first part of this project, you will implement a GLSL vertex shader as -part of a WebGL demo. You will create a dynamic wave animation using code that -runs entirely on the GPU. - -In the second part of this project, you will implement a GLSL fragment shader -to render an interactive globe in WebGL. This will include texture blending, -bump mapping, specular masking, and adding a cloud layer to give your globe a -uniquie feel. - -------------------------------------------------------------------------------- -CONTENTS: -------------------------------------------------------------------------------- -The Project5 root directory contains the following subdirectories: - -* js/ contains the javascript files, including external libraries, necessary. -* assets/ contains the textures that will be used in the second half of the - assignment. -* resources/ contains the screenshots found in this readme file. - -------------------------------------------------------------------------------- -PART 1 REQUIREMENTS: -------------------------------------------------------------------------------- - -In Part 1, you are given code for: - -* Drawing a VBO through WebGL -* Javascript code for interfacing with WebGL -* Functions for generating simplex noise - -You are required to implement the following: -* A sin-wave based vertex shader: +There are two parts in this project. The first part is the Image Processor, and the second part is a Wave Vertex Shader. -![Example sin wave grid](resources/sinWaveGrid.png) +In the first part of this project, I implemented a GLSL vertex shader to create a dynamic wave animation using code that runs entirely on the GPU. -* One interesting vertex shader of your choice +In the second part of this project, I implemented a GLSL fragment shader to render an interactive globe in WebGL, including texture blending, bump mapping, specular masking, and a moving cloud layer. ------------------------------------------------------------------------------- -PART 1 WALKTHROUGH: +Demo: ------------------------------------------------------------------------------- -**Sin Wave** - -* For this assignment, you will need the latest version of Firefox. -* Begin by opening index.html. You should see a flat grid of black and white - lines on the xy plane: - -![Example boring grid](resources/emptyGrid.png) - -* In this assignment, you will animate the grid in a wave-like pattern using a - vertex shader, and determine each vertex’s color based on its height, as seen - in the example in the requirements. -* The vertex and fragment shader are located in script tags in `index.html`. -* The JavaScript code that needs to be modified is located in `index.js`. -* Required shader code modifications: - * Add a float uniform named u_time. - * Modify the vertex’s height using the following code: - - ```glsl - float s_contrib = sin(position.x*2.0*3.14159 + u_time); - float t_contrib = cos(position.y*2.0*3.14159 + u_time); - float height = s_contrib*t_contrib; - ``` - - * Use the GLSL mix function to blend together two colors of your choice based - on the vertex’s height. The lowest possible height should be assigned one - color (for example, `vec3(1.0, 0.2, 0.0)`) and the maximum height should be - another (`vec3(0.0, 0.8, 1.0)`). Use a varying variable to pass the color to - the fragment shader, where you will assign it `gl_FragColor`. - - * Using dat.gui, you will add color pickers to modify the max and min colors - via GUI. You will do this by adding the proper uniforms to the fragment - shader, and using the addColor function from dat.GUI. - -* Required JavaScript code modifications: - * A floating-point time value should be increased every animation step. - Hint: the delta should be less than one. - * To pass the time to the vertex shader as a uniform, first query the location - of `u_time` using `context.getUniformLocation` in `initializeShader()`. - Then, the uniform’s value can be set by calling `context.uniform1f` in - `animate()`. - -**Wave Of Your Choice** - -* Create another copy of `index.html`. Call it `index_custom.html`, or - something similar. -* Implement your own interesting vertex shader! In your README.md with your - submission, describe your custom vertex shader, what it does, and how it - works. +[![ScreenShot](https://github.com/liying3/Project5-WebGL/blob/master/result/part2/video%20cover.JPG)](http://youtu.be/DvqLw0I0swU) ------------------------------------------------------------------------------- -PART 2 REQUIREMENTS: +PART 1 ------------------------------------------------------------------------------- -In Part 2, you are given code for: +**Demo** -* Reading and loading textures -* Rendering a sphere with textures mapped on -* Basic passthrough fragment and vertex shaders -* A basic globe with Earth terrain color mapping -* Gamma correcting textures -* javascript to interact with the mouse - * left-click and drag moves the camera around - * right-click and drag moves the camera in and out +[![ScreenShot](https://github.com/liying3/Project5-WebGL/blob/master/result/part1/video%20cover.JPG)](http://youtu.be/xA6fc0ufyLs) -You are required to implement: +**Features** +There are four types of animation in this part. -* Bump mapped terrain -* Rim lighting to simulate atmosphere -* Night-time lights on the dark side of the globe -* Specular mapping -* Moving clouds - -You are also required to pick one open-ended effect to implement: - -* Procedural water rendering and animation using noise -* Shade based on altitude using the height map -* Cloud shadows via ray-tracing through the cloud map in the fragment shader -* Orbiting Moon with texture mapping and shadow casting onto Earth -* Draw a skybox around the entire scene for the stars. -* Your choice! Email Liam and Patrick to get approval first - -Finally in addition to your readme, you must also set up a gh-pages branch -(explained below) to expose your beautiful WebGL globe to the world. - -Some examples of what your completed globe renderer will look like: - -![Completed globe, day side](resources/globe_day.png) - -Figure 0. Completed globe renderer, daylight side. - -![Completed globe, twilight](resources/globe_twilight.png) - -Figure 1. Completed globe renderer, twilight border. - -![Completed globe, night side](resources/globe_night.png) - -Figure 2. Completed globe renderer, night side. - -------------------------------------------------------------------------------- -PART 2 WALKTHROUGH: -------------------------------------------------------------------------------- +* Sin wave -Open part2/frag_globe.html in Firefox to run it. You’ll see a globe -with Phong lighting like the one in Figure 3. All changes you need to make -will be in the fragment shader portion of this file. - -![Initial globe](resources/globe_initial.png) - -Figure 3. Initial globe with diffuse and specular lighting. - -**Night Lights** - -The backside of the globe not facing the sun is completely black in the -initial globe. Use the `diffuse` lighting component to detect if a fragment -is on this side of the globe, and, if so, shade it with the color from the -night light texture, `u_Night`. Do not abruptly switch from day to night; -instead use the `GLSL mix` function to smoothly transition from day to night -over a reasonable period. The resulting globe will look like Figure 4. -Consider brightening the night lights by multiplying the value by two. +```glsl +float s_contrib = sin(position.x*2.0*3.14159 + u_time); +float t_contrib = cos(position.y*2.0*3.14159 + u_time); +float height = s_contrib*t_contrib; +``` + +![ScreenShot](https://github.com/liying3/Project5-WebGL/blob/master/result/part1/sin.JPG) -The base code shows an example of how to gamma correct the nighttime texture: +* Sin wave 2 ```glsl -float gammaCorrect = 1/1.2; -vec4 nightColor = pow(texture2D(u_Night, v_Texcoord), vec4(gammaCorrect)); +float dist = distance(position, vec2(0.5,0.5)); +float height = sin(u_time - dist) / (u_time - dist); ``` -Feel free to play with gamma correcting the night and day textures if you -wish. Find values that you think look nice! +![ScreenShot](https://github.com/liying3/Project5-WebGL/blob/master/result/part1/sin2.JPG) -![Day/Night without specular mapping](resources/globe_nospecmap.png) +* Simple Butterfly -Figure 4. Globe with night lights and day/night blending at dusk/dawn. - -**Specular Map** +```glsl +float t_contrib = cos( (0.5-position.y) * u_time); +float height = t_contrib * 0.5 * (1.0 + abs(0.5-position.y)/0.5 ); +``` + +![ScreenShot](https://github.com/liying3/Project5-WebGL/blob/master/result/part1/bt.JPG) -Our day/night color still shows specular highlights on landmasses, which -should only be diffuse lit. Only the ocean should receive specular highlights. -Use `u_EarthSpec` to determine if a fragment is on ocean or land, and only -include the specular component if it is in ocean. +* Complex Butterfly -![Day/Night with specular mapping](resources/globe_specmap.png) +I use a butterfly function from Wolfram, http://mathworld.wolfram.com/ButterflyFunction.html -Figure 5. Globe with specular map. Compare to Figure 4. Here, the specular -component is not used when shading the land. +```glsl +float tmp = (pow(position.x,2.0) - pow(position.y,2.0)) / (pow(position.x,2.0) + pow(position.y,2.0)); +float height = tmp * sin((position.x + position.y) / u_time); +``` + +![ScreenShot](https://github.com/liying3/Project5-WebGL/blob/master/result/part1/bt2.JPG) -**Clouds** +* User Interface -In day time, clouds should be diffuse lit. Use `u_Cloud` to determine the -cloud color, and `u_CloudTrans` and `mix` to determine how much a daytime -fragment is affected by the day diffuse map or cloud color. See Figure 6. +User can user color picker to change the color of the grid and also the type of dynamic wave animation. -In night time, clouds should obscure city lights. Use `u_CloudTrans` and `mix` -to blend between the city lights and solid black. See Figure 7. +------------------------------------------------------------------------------- +PART 2 Features +------------------------------------------------------------------------------- +Website: http://liying3.github.io/Project5-WebGL/ -Animate the clouds by offseting the `s` component of `v_Texcoord` by `u_time` -when reading `u_Cloud` and `u_CloudTrans`. +Implemented the following basic features: +* Bump mapped terrain +* Rim lighting to simulate atmosphere +* Night-time lights on the dark side of the globe +* Specular mapping +* Moving clouds -![Day with clouds](resources/globe_daycloud.png) +Here are some rendered images: -Figure 6. Clouds with day time shading. +Bump mapped terrain -![Night with clouds](resources/globe_nightcloud.png) +![ScreenShot](https://github.com/liying3/Project5-WebGL/blob/master/result/part2/bump.JPG) -Figure 7. Clouds observing city nights on the dark side of the globe. +Night-time lights -**Bump Mapping** +![ScreenShot](https://github.com/liying3/Project5-WebGL/blob/master/result/part2/night.JPG) -Add the appearance of mountains by perturbing the normal used for diffuse -lighting the ground (not the clouds) by using the bump map texture, `u_Bump`. -This texture is 1024x512, and is zero when the fragment is at sea-level, and -one when the fragment is on the highest mountain. Read three texels from this -texture: once using `v_Texcoord`; once one texel to the right; and once one -texel above. Create a perturbed normal in tangent space: +Rim lighting and cloud layer -`normalize(vec3(center - right, center - top, 0.2))` +![ScreenShot](https://github.com/liying3/Project5-WebGL/blob/master/result/part2/cloud.JPG) -Use `eastNorthUpToEyeCoordinates` to transform this normal to eye coordinates, -normalize it, then use it for diffuse lighting the ground instead of the -original normal. +Extra Feature: +* Procedural water rendering and animation using perlin noise -![Globe with bump mapping](resources/globe_bumpmap.png) +Only the ocean should be animated here. So I use `u_EarthSpec` to determine if a fragment is on ocean or land. If it is in ocean, I use the perlin noise the get an offset position of it. And check the new position whether it is also in the ocean by 'u_EarthSpec'. If it is, I will replace the original texture with the offset one. -Figure 8. Bump mapping brings attention to mountains. +![ScreenShot](https://github.com/liying3/Project5-WebGL/blob/master/result/part2/ocean.JPG) -**Rim Lighting** +* user interface -Rim lighting is a simple post-processed lighting effect we can apply to make -the globe look as if it has an atmospheric layer catching light from the sun. -Implementing rim lighting is simple; we being by finding the dot product of -`v_Normal` and `v_Position`, and add 1 to the dot product. We call this value -our rim factor. If the rim factor is greater than 0, then we add a blue color -based on the rim factor to the current fragment color. You might use a color -something like `vec4(rim/4, rim/2, rim/2, 1)`. If our rim factor is not greater -than 0, then we leave the fragment color as is. Figures 0,1 and 2 show our -finished globe with rim lighting. +![ScreenShot](https://github.com/liying3/Project5-WebGL/blob/master/result/part2/gui.JPG) -For more information on rim lighting, -read http://www.fundza.com/rman_shaders/surface/rim_effects/index.html. +Users can check or unckeck certain effect, like cloud and ocean moving to get better view of other parts of the global. Also, there's FPS and Timing efficiency on the right of the screen to show current computation. ------------------------------------------------------------------------------- -GH-PAGES +Performance ------------------------------------------------------------------------------- -Since this assignment is in WebGL you will make your project easily viewable by -taking advantage of GitHub's project pages feature. - -Once you are done you will need to create a new branch named gh-pages: - -`git branch gh-pages` +From the following chart, we can tell that, it costs most time for shader initialization, and then is the sphere initialization. -Switch to your new branch: +As for animation each frame, it has average 48 FPS. -`git checkout gh-pages` - -Create an index.html file that is either your renamed frag_globe.html or -contains a link to it, commit, and then push as usual. Now you can go to - -`.github.io/` - -to see your beautiful globe from anywhere. +![ScreenShot](https://github.com/liying3/Project5-WebGL/blob/master/result/part2/fps.JPG) ------------------------------------------------------------------------------- -README +THIRD PARTY CODE POLICY ------------------------------------------------------------------------------- -All students must replace or augment the contents of this Readme.md in a clear -manner with the following: +* stas.js -* A brief description of the project and the specific features you implemented. -* At least one screenshot of your project running. -* A 30 second or longer video of your project running. To create the video you - can use http://www.microsoft.com/expression/products/Encoder4_Overview.aspx -* A performance evaluation (described in detail below). +https://github.com/mrdoob/stats.js/ -------------------------------------------------------------------------------- -PERFORMANCE EVALUATION -------------------------------------------------------------------------------- -The performance evaluation is where you will investigate how to make your -program more efficient using the skills you've learned in class. You must have -performed at least one experiment on your code to investigate the positive or -negative effects on performance. +It's a library to visualize realize fps and timing. -We encourage you to get creative with your tweaks. Consider places in your code -that could be considered bottlenecks and try to improve them. +* perlin noise -Each student should provide no more than a one page summary of their -optimizations along with tables and or graphs to visually explain any -performance differences. +https://github.com/ashima/webgl-noise/blob/master/src/noise3D.glsl -In this homework, we do not expect crazy performance evaluation in terms of -optimizations. However, it would be good to take performance benchmarks at -every step in this assignment to see how complicated fragment shaders affect the -overall speed. You can do this by using stats.js. +I use the perlin noise to get an offset position when implementing water animation. -------------------------------------------------------------------------------- -THIRD PARTY CODE POLICY -------------------------------------------------------------------------------- -* Use of any third-party code must be approved by asking on the Google groups. - If it is approved, all students are welcome to use it. Generally, we approve - use of third-party code that is not a core part of the project. For example, - for the ray tracer, we would approve using a third-party library for loading - models, but would not approve copying and pasting a CUDA function for doing - refraction. -* Third-party code must be credited in README.md. -* Using third-party code without its approval, including using another - student's code, is an academic integrity violation, and will result in you - receiving an F for the semester. - -------------------------------------------------------------------------------- -SELF-GRADING -------------------------------------------------------------------------------- -* On the submission date, email your grade, on a scale of 0 to 100, to Harmony, - harmoli+cis565@seas.upenn.com, with a one paragraph explanation. Be concise and - realistic. Recall that we reserve 30 points as a sanity check to adjust your - grade. Your actual grade will be (0.7 * your grade) + (0.3 * our grade). We - hope to only use this in extreme cases when your grade does not realistically - reflect your work - it is either too high or too low. In most cases, we plan - to give you the exact grade you suggest. -* Projects are not weighted evenly, e.g., Project 0 doesn't count as much as - the path tracer. We will determine the weighting at the end of the semester - based on the size of each project. - ---- -SUBMISSION ---- -As with the previous project, you should fork this project and work inside of -your fork. Upon completion, commit your finished project back to your fork, and -make a pull request to the master repository. You should include a README.md -file in the root directory detailing the following - -* A brief description of the project and specific features you implemented -* At least one screenshot of your project running. -* A link to a video of your project running. -* Instructions for building and running your project if they differ from the - base code. -* A performance writeup as detailed above. -* A list of all third-party code used. -* This Readme file edited as described above in the README section. +```glsl +float noise = perlinNoise(v_Position+u_time); +``` \ No newline at end of file diff --git a/frag_globe.html b/frag_globe.html index e074492..cbc91e3 100644 --- a/frag_globe.html +++ b/frag_globe.html @@ -40,6 +40,8 @@ +vec3 mod289(vec3 x) { + return x - floor(x * (1.0 / 289.0)) * 289.0; +} + +vec4 mod289(vec4 x) { + return x - floor(x * (1.0 / 289.0)) * 289.0; +} + +vec4 permute(vec4 x) { + return mod289(((x*34.0)+1.0)*x); +} + +vec4 taylorInvSqrt(vec4 r) +{ + return 1.79284291400159 - 0.85373472095314 * r; +} + +float snoise(vec3 v) + { + const vec2 C = vec2(1.0/6.0, 1.0/3.0) ; + const vec4 D = vec4(0.0, 0.5, 1.0, 2.0); + +// First corner + vec3 i = floor(v + dot(v, C.yyy) ); + vec3 x0 = v - i + dot(i, C.xxx) ; + +// Other corners + vec3 g = step(x0.yzx, x0.xyz); + vec3 l = 1.0 - g; + vec3 i1 = min( g.xyz, l.zxy ); + vec3 i2 = max( g.xyz, l.zxy ); + + // x0 = x0 - 0.0 + 0.0 * C.xxx; + // x1 = x0 - i1 + 1.0 * C.xxx; + // x2 = x0 - i2 + 2.0 * C.xxx; + // x3 = x0 - 1.0 + 3.0 * C.xxx; + vec3 x1 = x0 - i1 + C.xxx; + vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y + vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y + +// Permutations + i = mod289(i); + vec4 p = permute( permute( permute( + i.z + vec4(0.0, i1.z, i2.z, 1.0 )) + + i.y + vec4(0.0, i1.y, i2.y, 1.0 )) + + i.x + vec4(0.0, i1.x, i2.x, 1.0 )); + +// Gradients: 7x7 points over a square, mapped onto an octahedron. +// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) + float n_ = 0.142857142857; // 1.0/7.0 + vec3 ns = n_ * D.wyz - D.xzx; + + vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7) + + vec4 x_ = floor(j * ns.z); + vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N) + + vec4 x = x_ *ns.x + ns.yyyy; + vec4 y = y_ *ns.x + ns.yyyy; + vec4 h = 1.0 - abs(x) - abs(y); + + vec4 b0 = vec4( x.xy, y.xy ); + vec4 b1 = vec4( x.zw, y.zw ); + + //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0; + //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0; + vec4 s0 = floor(b0)*2.0 + 1.0; + vec4 s1 = floor(b1)*2.0 + 1.0; + vec4 sh = -step(h, vec4(0.0)); + + vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ; + vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ; + + vec3 p0 = vec3(a0.xy,h.x); + vec3 p1 = vec3(a0.zw,h.y); + vec3 p2 = vec3(a1.xy,h.z); + vec3 p3 = vec3(a1.zw,h.w); + +//Normalise gradients + vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3))); + p0 *= norm.x; + p1 *= norm.y; + p2 *= norm.z; + p3 *= norm.w; + +// Mix final noise value + vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0); + m = m * m; + return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1), + dot(p2,x2), dot(p3,x3) ) ); + } + + + diff --git a/js/frag_globe.js b/js/frag_globe.js index f37830d..f4815c9 100644 --- a/js/frag_globe.js +++ b/js/frag_globe.js @@ -55,7 +55,27 @@ var u_EarthSpecLocation; var u_BumpLocation; var u_timeLocation; - + + var cloudDisplay = true; + var u_cloudDisLocation; + var movingCloudDisplay = true; + var u_mvCloudDisLocation; + var movingOceanDisplay = true; + var u_mvOceanDisLocation; + var bumpDisplay = true; + var u_bumpDisLocation; + + //stas.js + var statsInitShader = new Stats(); + statsInitShader.setMode(1); // 0: fps, 1: ms + statsInitShader.domElement.style.position = 'absolute'; + statsInitShader.domElement.style.right = '0px'; + statsInitShader.domElement.style.top = '160px'; + document.body.appendChild(statsInitShader.domElement ); + var initS = document.createElement('div'); + initS.innerHTML = 'init shader'; + statsInitShader.domElement.appendChild(initS); + statsInitShader.begin(); (function initializeShader() { var vs = getShaderSource(document.getElementById("vs")); var fs = getShaderSource(document.getElementById("fs")); @@ -76,9 +96,15 @@ u_BumpLocation = gl.getUniformLocation(program,"u_Bump"); u_timeLocation = gl.getUniformLocation(program,"u_time"); u_CameraSpaceDirLightLocation = gl.getUniformLocation(program,"u_CameraSpaceDirLight"); + + u_cloudDisLocation = gl.getUniformLocation(program, "u_cloudDisplay"); + u_mvCloudDisLocation = gl.getUniformLocation(program, "u_movingCloudDisplay"); + u_mvOceanDisLocation = gl.getUniformLocation(program, "u_movingOceanDisplay"); + u_bumpDisLocation = gl.getUniformLocation(program, "u_bumpDisplay"); gl.useProgram(program); })(); + statsInitShader.end(); var dayTex = gl.createTexture(); var bumpTex = gl.createTexture(); @@ -87,6 +113,17 @@ var lightTex = gl.createTexture(); var specTex = gl.createTexture(); + //stas.js + var statsInitTexture = new Stats(); + statsInitTexture.setMode(1); // 0: fps, 1: ms + statsInitTexture.domElement.style.position = 'absolute'; + statsInitTexture.domElement.style.right = '0px'; + statsInitTexture.domElement.style.top = '240px'; + document.body.appendChild( statsInitTexture.domElement ); + var initT = document.createElement('div'); + initT.innerHTML = 'init texture'; + statsInitTexture.domElement.appendChild(initT); + statsInitTexture.begin(); function initLoadedTexture(texture){ gl.bindTexture(gl.TEXTURE_2D, texture); gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true); @@ -97,9 +134,22 @@ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); gl.bindTexture(gl.TEXTURE_2D, null); } + statsInitTexture.end(); var numberOfIndices; - + + //stas.js + var statsInitSphere = new Stats(); + statsInitSphere.setMode(1); // 0: fps, 1: ms + statsInitSphere.domElement.style.position = 'absolute'; + statsInitSphere.domElement.style.right = '0px'; + statsInitSphere.domElement.style.top = '320'; + document.body.appendChild( statsInitSphere.domElement ); + var initSphere = document.createElement('div'); + initSphere.innerHTML = 'init sphere'; + statsInitSphere.domElement.appendChild(initSphere); + statsInitSphere.begin(); + (function initializeSphere() { function uploadMesh(positions, texCoords, indices) { // Positions @@ -174,6 +224,7 @@ uploadMesh(positions, texCoords, indices); numberOfIndices = indicesIndex; })(); + statsInitSphere.end(); var time = 0; var mouseLeftDown = false; @@ -233,10 +284,21 @@ document.onmouseup = handleMouseUp; document.onmousemove = handleMouseMove; - + //stas.js + var statsAnimate = new Stats(); + statsAnimate.setMode(1); // 0: fps, 1: ms + statsAnimate.domElement.style.position = 'absolute'; + statsAnimate.domElement.style.right = '0'; + statsAnimate.domElement.style.top = '400px'; + document.body.appendChild( statsAnimate.domElement ); + var anim = document.createElement('div'); + anim.innerHTML = 'animate'; + statsAnimate.domElement.appendChild(anim); + function animate() { /////////////////////////////////////////////////////////////////////////// // Update + statsAnimate.begin(); var model = mat4.create(); mat4.identity(model); @@ -289,6 +351,15 @@ gl.drawElements(gl.TRIANGLES, numberOfIndices, gl.UNSIGNED_SHORT,0); time += 0.001; + gl.uniform1f(u_timeLocation, time); + + gl.uniform1i(u_cloudDisLocation, cloudDisplay); + gl.uniform1i(u_mvCloudDisLocation, movingCloudDisplay); + gl.uniform1i(u_mvOceanDisLocation, movingOceanDisplay); + gl.uniform1i(u_bumpDisLocation, bumpDisplay); + + statsAnimate.end(); + window.requestAnimFrame(animate); } @@ -306,6 +377,28 @@ } texture.image.src = src; } + + + function config() { + this.cloud = cloudDisplay; // RGB array + this.movingCloud = movingCloudDisplay; + this.movingOcean = movingOceanDisplay; + this.bump = bumpDisplay; + } + var gui = new dat.GUI(); + var cong = new config(); + gui.add(cong, 'cloud').onChange(function () { + cloudDisplay = cong.cloud; + }); + gui.add(cong, 'movingCloud').onChange(function () { + movingCloudDisplay = cong.movingCloud; + }); + gui.add(cong, 'movingOcean').onChange(function () { + movingOceanDisplay = cong.movingOcean; + }); + gui.add(cong, 'bump').onChange(function () { + bumpDisplay = cong.bump; + }); initializeTexture(dayTex, "assets/earthmap1024.png"); initializeTexture(bumpTex, "assets/earthbump1024.png"); diff --git a/js/lib/stats.js b/js/lib/stats.js new file mode 100644 index 0000000..90b2a27 --- /dev/null +++ b/js/lib/stats.js @@ -0,0 +1,149 @@ +/** + * @author mrdoob / http://mrdoob.com/ + */ + +var Stats = function () { + + var startTime = Date.now(), prevTime = startTime; + var ms = 0, msMin = Infinity, msMax = 0; + var fps = 0, fpsMin = Infinity, fpsMax = 0; + var frames = 0, mode = 0; + + var container = document.createElement( 'div' ); + container.id = 'stats'; + container.addEventListener( 'mousedown', function ( event ) { event.preventDefault(); setMode( ++ mode % 2 ) }, false ); + container.style.cssText = 'width:80px;opacity:0.9;cursor:pointer'; + + var fpsDiv = document.createElement( 'div' ); + fpsDiv.id = 'fps'; + fpsDiv.style.cssText = 'padding:0 0 3px 3px;text-align:left;background-color:#002'; + container.appendChild( fpsDiv ); + + var fpsText = document.createElement( 'div' ); + fpsText.id = 'fpsText'; + fpsText.style.cssText = 'color:#0ff;font-family:Helvetica,Arial,sans-serif;font-size:9px;font-weight:bold;line-height:15px'; + fpsText.innerHTML = 'FPS'; + fpsDiv.appendChild( fpsText ); + + var fpsGraph = document.createElement( 'div' ); + fpsGraph.id = 'fpsGraph'; + fpsGraph.style.cssText = 'position:relative;width:74px;height:30px;background-color:#0ff'; + fpsDiv.appendChild( fpsGraph ); + + while ( fpsGraph.children.length < 74 ) { + + var bar = document.createElement( 'span' ); + bar.style.cssText = 'width:1px;height:30px;float:left;background-color:#113'; + fpsGraph.appendChild( bar ); + + } + + var msDiv = document.createElement( 'div' ); + msDiv.id = 'ms'; + msDiv.style.cssText = 'padding:0 0 3px 3px;text-align:left;background-color:#020;display:none'; + container.appendChild( msDiv ); + + var msText = document.createElement( 'div' ); + msText.id = 'msText'; + msText.style.cssText = 'color:#0f0;font-family:Helvetica,Arial,sans-serif;font-size:9px;font-weight:bold;line-height:15px'; + msText.innerHTML = 'MS'; + msDiv.appendChild( msText ); + + var msGraph = document.createElement( 'div' ); + msGraph.id = 'msGraph'; + msGraph.style.cssText = 'position:relative;width:74px;height:30px;background-color:#0f0'; + msDiv.appendChild( msGraph ); + + while ( msGraph.children.length < 74 ) { + + var bar = document.createElement( 'span' ); + bar.style.cssText = 'width:1px;height:30px;float:left;background-color:#131'; + msGraph.appendChild( bar ); + + } + + var setMode = function ( value ) { + + mode = value; + + switch ( mode ) { + + case 0: + fpsDiv.style.display = 'block'; + msDiv.style.display = 'none'; + break; + case 1: + fpsDiv.style.display = 'none'; + msDiv.style.display = 'block'; + break; + } + + }; + + var updateGraph = function ( dom, value ) { + + var child = dom.appendChild( dom.firstChild ); + child.style.height = value + 'px'; + + }; + + return { + + REVISION: 12, + + domElement: container, + + setMode: setMode, + + begin: function () { + + startTime = Date.now(); + + }, + + end: function () { + + var time = Date.now(); + + ms = time - startTime; + msMin = Math.min( msMin, ms ); + msMax = Math.max( msMax, ms ); + + msText.textContent = ms + ' MS (' + msMin + '-' + msMax + ')'; + updateGraph( msGraph, Math.min( 30, 30 - ( ms / 200 ) * 30 ) ); + + frames ++; + + if ( time > prevTime + 1000 ) { + + fps = Math.round( ( frames * 1000 ) / ( time - prevTime ) ); + fpsMin = Math.min( fpsMin, fps ); + fpsMax = Math.max( fpsMax, fps ); + + fpsText.textContent = fps + ' FPS (' + fpsMin + '-' + fpsMax + ')'; + updateGraph( fpsGraph, Math.min( 30, 30 - ( fps / 100 ) * 30 ) ); + + prevTime = time; + frames = 0; + + } + + return time; + + }, + + update: function () { + + startTime = this.end(); + + } + + } + +}; + +if ( typeof module === 'object' ) { + + module.exports = Stats; + +} \ No newline at end of file diff --git a/js/webGLUtility.js b/js/webGLUtility.js index 134a93e..311dbdd 100644 --- a/js/webGLUtility.js +++ b/js/webGLUtility.js @@ -5,6 +5,7 @@ // University of Pennsylvania (c) 2014 (function(exports) { + "use strict"; exports = exports || window; diff --git a/result/part1/bt.JPG b/result/part1/bt.JPG new file mode 100644 index 0000000..898a5cd Binary files /dev/null and b/result/part1/bt.JPG differ diff --git a/result/part1/bt2.JPG b/result/part1/bt2.JPG new file mode 100644 index 0000000..16eae54 Binary files /dev/null and b/result/part1/bt2.JPG differ diff --git a/result/part1/part1.wmv b/result/part1/part1.wmv new file mode 100644 index 0000000..21854a4 Binary files /dev/null and b/result/part1/part1.wmv differ diff --git a/result/part1/sin.JPG b/result/part1/sin.JPG new file mode 100644 index 0000000..2ac74f7 Binary files /dev/null and b/result/part1/sin.JPG differ diff --git a/result/part1/sin2.JPG b/result/part1/sin2.JPG new file mode 100644 index 0000000..91f4efa Binary files /dev/null and b/result/part1/sin2.JPG differ diff --git a/result/part1/video cover.JPG b/result/part1/video cover.JPG new file mode 100644 index 0000000..d7b675d Binary files /dev/null and b/result/part1/video cover.JPG differ diff --git a/result/part2/bump.JPG b/result/part2/bump.JPG new file mode 100644 index 0000000..5dbf108 Binary files /dev/null and b/result/part2/bump.JPG differ diff --git a/result/part2/cloud.JPG b/result/part2/cloud.JPG new file mode 100644 index 0000000..e97b409 Binary files /dev/null and b/result/part2/cloud.JPG differ diff --git a/result/part2/fps.JPG b/result/part2/fps.JPG new file mode 100644 index 0000000..a866ed6 Binary files /dev/null and b/result/part2/fps.JPG differ diff --git a/result/part2/gui.JPG b/result/part2/gui.JPG new file mode 100644 index 0000000..9d5bb1b Binary files /dev/null and b/result/part2/gui.JPG differ diff --git a/result/part2/night.JPG b/result/part2/night.JPG new file mode 100644 index 0000000..a055a42 Binary files /dev/null and b/result/part2/night.JPG differ diff --git a/result/part2/ocean.JPG b/result/part2/ocean.JPG new file mode 100644 index 0000000..614d406 Binary files /dev/null and b/result/part2/ocean.JPG differ diff --git a/result/part2/part2.wmv b/result/part2/part2.wmv new file mode 100644 index 0000000..0f818f8 Binary files /dev/null and b/result/part2/part2.wmv differ diff --git a/result/part2/video cover.JPG b/result/part2/video cover.JPG new file mode 100644 index 0000000..ee7ce59 Binary files /dev/null and b/result/part2/video cover.JPG differ diff --git a/vert_wave.html b/vert_wave.html index 5c7495b..4aa85ba 100644 --- a/vert_wave.html +++ b/vert_wave.html @@ -17,21 +17,53 @@ attribute vec2 position; uniform mat4 u_modelViewPerspective; + + uniform float u_time; + + uniform int u_type; + + uniform vec3 u_minColor; + uniform vec3 u_maxColor; + + varying vec4 u_color; void main(void) { - // NOTE : according to the WebGL standard, 0.0f is not accepted - float height = 0.0; - - // NOTE : gl_Position is always a vec4 + // NOTE : according to the WebGL standard, 0.0f is not accepted + float height = 0.0; + + if (u_type == 0){ + float s_contrib = sin(position.x*2.0*3.14159 + u_time); + float t_contrib = cos(position.y*2.0*3.14159 + u_time); + height = s_contrib*t_contrib; + } + else if (u_type == 1) { + float dist = distance(position, vec2(0.5,0.5)) * 30.0; + float t = abs(u_time - dist) < 0.00001 ? 0.00001 : (u_time - dist); + height = clamp(sin(u_time - dist) / t, 0.0, 0.2); + } + else if (u_type == 2) { + float t_contrib = cos( (0.5-position.y) * u_time); + height = t_contrib * 0.5 * (1.0 + abs(0.5-position.y)/0.5 ); + } + else if (u_type == 3) { + float tmp = (pow(position.x,2.0) - pow(position.y,2.0)) / (pow(position.x,2.0) + pow(position.y,2.0)); + height = tmp * sin((position.x + position.y) / u_time); + } + + // NOTE : gl_Position is always a vec4 gl_Position = u_modelViewPerspective * vec4(vec3(position, height), 1.0); + + vec3 minColor = u_minColor / 255.0; + vec3 maxColor = u_maxColor / 255.0; + u_color = vec4( mix( minColor, maxColor, 1.0-2.0*height), 1.0 ); }