Initial transfer of larger repo from git. IF full history and branches are needed must go to github at https://github.com/DulcisL/CCUCoding.git
This commit is contained in:
commit
c09ce40be0
4048 files changed
+603256
No files matched your search
@@ -0,0 +1,15 @@
|
||||
Chapter 5 Programs
|
||||
|
||||
The first four programs allow you to resize the object and the rotate and move the camera through buttons. Note that because the clipping volume is measured from the camera, it easy to clip out the entire object by moving the camera. You may also get odd views if you move the camera inside the object.
|
||||
|
||||
hat: Display of sombrero function using both filled triangles and line loops
|
||||
|
||||
hata: Display of the sombero function using line strips in two directions
|
||||
|
||||
ortho1: Interactive orthographic viewing of cube
|
||||
|
||||
perspective1: Interactive perspective viewing of cube
|
||||
|
||||
shadow: projective shadow of a square onto y = 0 plane with moving light
|
||||
|
||||
ortho2 and perspective2 use slide bars instead of buttons
|
||||
@@ -0,0 +1,67 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<p> </p>
|
||||
<button id = "Button1">Increase Z</button>
|
||||
<button id = "Button2">Decrease Z</button>
|
||||
<button id = "Button3">Increase R</button>
|
||||
<button id = "Button4">Decrease R</button>
|
||||
|
||||
<p> </p>
|
||||
<button id = "Button5">Increase theta</button>
|
||||
<button id = "Button6">Decrease theta</button>
|
||||
<button id = "Button7">Increase phi</button>
|
||||
<button id = "Button8">Decrease phi</button>
|
||||
<p> </p>
|
||||
<button id = "Button9">Wider</button>
|
||||
<button id = "Button10">Narrower</button>
|
||||
<button id = "Button11">Higher</button>
|
||||
<button id = "Button12">Shorter</button>
|
||||
|
||||
|
||||
<p> </p>
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
|
||||
in vec4 aPosition;
|
||||
|
||||
uniform mat4 uModelViewMatrix;
|
||||
uniform mat4 uProjectionMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uProjectionMatrix*uModelViewMatrix*aPosition;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
uniform vec4 uColor;
|
||||
|
||||
out vec4 fColor;
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
fColor = uColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
||||
<script type="text/javascript" src="hat.js"></script>
|
||||
|
||||
<body>
|
||||
|
||||
|
||||
<canvas id="gl-canvas" width="512" height="512">
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,152 @@
|
||||
"use strict";
|
||||
|
||||
var hat = function() {
|
||||
|
||||
var gl;
|
||||
|
||||
var nRows = 50;
|
||||
var nColumns = 50;
|
||||
|
||||
// data for radial hat function: sin(Pi*r)/(Pi*r)
|
||||
|
||||
var data = [];
|
||||
for(var i = 0; i < nRows; ++i) {
|
||||
data.push([]);
|
||||
var x = Math.PI*(4*i/nRows-2.0);
|
||||
|
||||
for(var j = 0; j < nColumns; ++j) {
|
||||
var y = Math.PI*(4*j/nRows-2.0);
|
||||
var r = Math.sqrt(x*x+y*y);
|
||||
|
||||
// take care of 0/0 for r = 0
|
||||
|
||||
data[i][j] = r ? Math.sin(r) / r : 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
var positionsArray = [];
|
||||
|
||||
var colorLoc;
|
||||
|
||||
var near = -10;
|
||||
var far = 10;
|
||||
var radius = 1.0;
|
||||
var theta = 0.0;
|
||||
var phi = 0.0;
|
||||
var dr = 5.0 * Math.PI/180.0;
|
||||
|
||||
const black = vec4(0.0, 0.0, 0.0, 1.0);
|
||||
const red = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
|
||||
const at = vec3(0.0, 0.0, 0.0);
|
||||
const up = vec3(0.0, 1.0, 0.0);
|
||||
|
||||
var left = -2.0;
|
||||
var right = 2.0;
|
||||
var top = 2.0;
|
||||
var bottom = -2.0;
|
||||
|
||||
var modeViewMatrix, projectionMatrix;
|
||||
var modelViewMatrixLoc, projectionMatrixLoc;
|
||||
|
||||
window.onload = function init()
|
||||
{
|
||||
var canvas = document.getElementById("gl-canvas");
|
||||
|
||||
gl = canvas.getContext('webgl2');
|
||||
if (!gl) alert( "WebGL 2.0 isn't available");
|
||||
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
|
||||
gl.clearColor(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
// enable depth testing and polygon offset
|
||||
// so lines will be in front of filled triangles
|
||||
|
||||
gl.enable(gl.DEPTH_TEST);
|
||||
gl.depthFunc(gl.LEQUAL);
|
||||
gl.enable(gl.POLYGON_OFFSET_FILL);
|
||||
gl.polygonOffset(1.0, 2.0);
|
||||
|
||||
// vertex array of nRows*nColumns quadrilaterals
|
||||
// (two triangles/quad) from data
|
||||
|
||||
for(var i=0; i<nRows-1; i++) {
|
||||
for(var j=0; j<nColumns-1;j++) {
|
||||
positionsArray.push( vec4(2*i/nRows-1, data[i][j], 2*j/nColumns-1, 1.0));
|
||||
positionsArray.push( vec4(2*(i+1)/nRows-1, data[i+1][j], 2*j/nColumns-1, 1.0));
|
||||
positionsArray.push( vec4(2*(i+1)/nRows-1, data[i+1][j+1], 2*(j+1)/nColumns-1, 1.0));
|
||||
positionsArray.push( vec4(2*i/nRows-1, data[i][j+1], 2*(j+1)/nColumns-1, 1.0) );
|
||||
}
|
||||
}
|
||||
//
|
||||
// Load shaders and initialize attribute buffers
|
||||
//
|
||||
var program = initShaders(gl, "vertex-shader", "fragment-shader");
|
||||
gl.useProgram(program);
|
||||
|
||||
|
||||
var vBufferId = gl.createBuffer();
|
||||
gl.bindBuffer( gl.ARRAY_BUFFER, vBufferId);
|
||||
gl.bufferData( gl.ARRAY_BUFFER, flatten(positionsArray), gl.STATIC_DRAW);
|
||||
|
||||
var positionLoc = gl.getAttribLocation( program, "aPosition");
|
||||
gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0 );
|
||||
gl.enableVertexAttribArray( positionLoc);
|
||||
|
||||
colorLoc = gl.getUniformLocation(program, "uColor");
|
||||
|
||||
modelViewMatrixLoc = gl.getUniformLocation(program, "uModelViewMatrix");
|
||||
projectionMatrixLoc = gl.getUniformLocation(program, "uProjectionMatrix");
|
||||
|
||||
// buttons for moving viewer and changing size
|
||||
|
||||
document.getElementById("Button1").onclick = function(){near *= 1.1; far *= 1.1;};
|
||||
document.getElementById("Button2").onclick = function(){near *= 0.9; far *= 0.9;};
|
||||
document.getElementById("Button3").onclick = function(){radius *= 2.0;};
|
||||
document.getElementById("Button4").onclick = function(){radius *= 0.5;};
|
||||
document.getElementById("Button5").onclick = function(){theta += dr;};
|
||||
document.getElementById("Button6").onclick = function(){theta -= dr;};
|
||||
document.getElementById("Button7").onclick = function(){phi += dr;};
|
||||
document.getElementById("Button8").onclick = function(){phi -= dr;};
|
||||
document.getElementById("Button9").onclick = function(){left *= 0.9; right *= 0.9;};
|
||||
document.getElementById("Button10").onclick = function(){left *= 1.1; right *= 1.1;};
|
||||
document.getElementById("Button11").onclick = function(){top *= 0.9; bottom *= 0.9;};
|
||||
document.getElementById("Button12").onclick = function(){top *= 1.1; bottom *= 1.1;};
|
||||
|
||||
render();
|
||||
|
||||
}
|
||||
|
||||
|
||||
function render()
|
||||
{
|
||||
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
var eye = vec3(radius*Math.sin(theta)*Math.cos(phi),
|
||||
radius*Math.sin(theta)*Math.sin(phi),
|
||||
radius*Math.cos(theta));
|
||||
|
||||
var modelViewMatrix = lookAt(eye, at, up);
|
||||
var projectionMatrix = ortho(left, right, bottom, top, near, far);
|
||||
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
|
||||
gl.uniformMatrix4fv(projectionMatrixLoc, false, flatten(projectionMatrix));
|
||||
|
||||
// draw each quad as two filled red triangles
|
||||
// and then as two black line loops
|
||||
|
||||
for(var i=0; i<positionsArray.length; i+=4) {
|
||||
gl.uniform4fv(colorLoc, red);
|
||||
gl.drawArrays( gl.TRIANGLE_FAN, i, 4 );
|
||||
gl.uniform4fv(colorLoc, black);
|
||||
gl.drawArrays( gl.LINE_LOOP, i, 4 );
|
||||
}
|
||||
|
||||
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
hat();
|
||||
@@ -0,0 +1,66 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
|
||||
<p> </p>
|
||||
<button id = "Button1">Increase Z</button>
|
||||
<button id = "Button2">Decrease Z</button>
|
||||
<button id = "Button3">Increase R</button>
|
||||
<button id = "Button4">Decrease R</button>
|
||||
|
||||
<p> </p>
|
||||
<button id = "Button5">Increase theta</button>
|
||||
<button id = "Button6">Decrease theta</button>
|
||||
<button id = "Button7">Increase phi</button>
|
||||
<button id = "Button8">Decrease phi</button>
|
||||
<p> </p>
|
||||
<button id = "Button9">Wider</button>
|
||||
<button id = "Button10">Narrower</button>
|
||||
<button id = "Button11">Higher</button>
|
||||
<button id = "Button12">Shorter</button>
|
||||
|
||||
|
||||
<p> </p>
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
|
||||
in vec4 aPosition;
|
||||
|
||||
uniform mat4 uModelViewMatrix;
|
||||
uniform mat4 uProjectionMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uProjectionMatrix*uModelViewMatrix*aPosition;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
out vec4 fColor;
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
fColor = vec4(0.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
||||
<script type="text/javascript" src="hata.js"></script>
|
||||
|
||||
<body>
|
||||
|
||||
|
||||
<canvas id="gl-canvas" width="512" height="512">
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,136 @@
|
||||
"use strict";
|
||||
|
||||
var hata = function() {
|
||||
|
||||
var nRows = 50;
|
||||
var nColumns = 50;
|
||||
|
||||
|
||||
// data for radial hat function: sin(Pi*r)/(Pi*r)
|
||||
|
||||
|
||||
var data = new Array(nRows);
|
||||
for(var i =0; i<nRows; i++) data[i]=new Array(nColumns);
|
||||
|
||||
for(var i=0; i<nRows; i++) {
|
||||
var x = Math.PI*(4*i/nRows-2.0);
|
||||
for(var j=0; j<nColumns; j++) {
|
||||
var y = Math.PI*(4*j/nRows-2.0);
|
||||
var r = Math.sqrt(x*x+y*y)
|
||||
|
||||
// take care of 0/0 for r = 0
|
||||
|
||||
|
||||
if(r) data[i][j] = Math.sin(r)/r;
|
||||
else data[i][j] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
var positionsArray = [];
|
||||
|
||||
var canvas;
|
||||
var gl;
|
||||
|
||||
var near = -10;
|
||||
var far = 10;
|
||||
var radius = 1.0;
|
||||
var theta = 0.0;
|
||||
var phi = 0.0;
|
||||
var dr = 5.0 * Math.PI/180.0;
|
||||
|
||||
var left = -2.0;
|
||||
var right = 2.0;
|
||||
var top = 2.0;
|
||||
var bottom = -2.0;
|
||||
|
||||
var modelViewMatrix, projectionMatrix;
|
||||
var modelViewMatrixLoc, projectionMatrixLoc;
|
||||
var eye;
|
||||
|
||||
const at = vec3(0.0, 0.0, 0.0);
|
||||
const up = vec3(0.0, 1.0, 0.0);
|
||||
|
||||
window.onload = function init() {
|
||||
|
||||
canvas = document.getElementById("gl-canvas");
|
||||
|
||||
gl = canvas.getContext('webgl2');
|
||||
if (!gl) alert("WebGL 2.0 isn't available");
|
||||
|
||||
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
|
||||
gl.clearColor(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
// vertex array of data for nRows and nColumns of line strips
|
||||
|
||||
for(var i=0; i<nRows-1; i++) for(var j=0; j<nColumns-1;j++) {
|
||||
positionsArray.push(vec4(2*i/nRows-1, data[i][j], 2*j/nColumns-1, 1.0));
|
||||
}
|
||||
for(var j=0; j<nColumns-1; j++) for(var i=0; i<nRows-1;i++) {
|
||||
positionsArray.push(vec4(2*i/nRows-1, data[i][j], 2*j/nColumns-1, 1.0));
|
||||
}
|
||||
//
|
||||
// Load shaders and initialize attribute buffers
|
||||
//
|
||||
var program = initShaders(gl, "vertex-shader", "fragment-shader");
|
||||
gl.useProgram(program);
|
||||
|
||||
|
||||
var vBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(positionsArray), gl.STATIC_DRAW);
|
||||
|
||||
var positionLoc = gl.getAttribLocation( program, "aPosition");
|
||||
gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(positionLoc);
|
||||
|
||||
modelViewMatrixLoc = gl.getUniformLocation(program, "uModelViewMatrix");
|
||||
projectionMatrixLoc = gl.getUniformLocation(program, "uProjectionMatrix");
|
||||
|
||||
// buttons for moving viewer and changing size
|
||||
|
||||
document.getElementById("Button1").onclick = function(){near *= 1.1; far *= 1.1;};
|
||||
document.getElementById("Button2").onclick = function(){near *= 0.9; far *= 0.9;};
|
||||
document.getElementById("Button3").onclick = function(){radius *= 2.0;};
|
||||
document.getElementById("Button4").onclick = function(){radius *= 0.5;};
|
||||
document.getElementById("Button5").onclick = function(){theta += dr;};
|
||||
document.getElementById("Button6").onclick = function(){theta -= dr;};
|
||||
document.getElementById("Button7").onclick = function(){phi += dr;};
|
||||
document.getElementById("Button8").onclick = function(){phi -= dr;};
|
||||
document.getElementById("Button9").onclick = function(){left *= 0.9; right *= 0.9;};
|
||||
document.getElementById("Button10").onclick = function(){left *= 1.1; right *= 1.1;};
|
||||
document.getElementById("Button11").onclick = function(){top *= 0.9; bottom *= 0.9;};
|
||||
document.getElementById("Button12").onclick = function(){top *= 1.1; bottom *= 1.1;};
|
||||
|
||||
render();
|
||||
}
|
||||
|
||||
|
||||
var render = function() {
|
||||
gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
eye = vec3(radius*Math.sin(theta)*Math.cos(phi),
|
||||
radius*Math.sin(theta)*Math.sin(phi), radius*Math.cos(theta));
|
||||
|
||||
modelViewMatrix = lookAt(eye, at , up);
|
||||
projectionMatrix = ortho(left, right, bottom, top, near, far);
|
||||
|
||||
gl.uniformMatrix4fv( modelViewMatrixLoc, false, flatten(modelViewMatrix) );
|
||||
gl.uniformMatrix4fv( projectionMatrixLoc, false, flatten(projectionMatrix) );
|
||||
|
||||
// render columns of data then rows
|
||||
|
||||
for(var i=0; i<nRows-1; i++) gl.drawArrays( gl.LINE_STRIP, i*(nColumns-1), nColumns-1 );
|
||||
for(var i=0; i<nColumns-1; i++) gl.drawArrays( gl.LINE_STRIP, i*(nRows-1)+positionsArray.length/2, nRows-1 );
|
||||
|
||||
|
||||
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
hata();
|
||||
@@ -0,0 +1,58 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<p> </p>
|
||||
<button id = "Button1">Increase Z</button>
|
||||
<button id = "Button2">Decrease Z</button>
|
||||
<button id = "Button3">Increase R</button>
|
||||
<button id = "Button4">Decrease R</button>
|
||||
|
||||
<p> </p>
|
||||
<button id = "Button5">Increase theta</button>
|
||||
<button id = "Button6">Decrease theta</button>
|
||||
<button id = "Button7">Increase phi</button>
|
||||
<button id = "Button8">Decrease phi</button>
|
||||
<p> </p>
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
|
||||
in vec4 aPosition;
|
||||
in vec4 aColor;
|
||||
out vec4 vColor;
|
||||
|
||||
uniform mat4 uModelViewMatrix;
|
||||
uniform mat4 uProjectionMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uProjectionMatrix*uModelViewMatrix*aPosition;
|
||||
vColor = aColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
in vec4 vColor;
|
||||
out vec4 fColor;
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
fColor = vColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
||||
<script type="text/javascript" src="ortho.js"></script>
|
||||
|
||||
<body>
|
||||
<canvas id="gl-canvas" width="512" height="512">
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,156 @@
|
||||
|
||||
"use strict";
|
||||
|
||||
function orthoExample() {
|
||||
|
||||
var canvas;
|
||||
var gl;
|
||||
|
||||
var numVertices = 36;
|
||||
|
||||
var positionsArray = [];
|
||||
var colorsArray = [];
|
||||
|
||||
var vertices = [
|
||||
vec4(-0.5, -0.5, 0.5, 1.0),
|
||||
vec4(-0.5, 0.5, 0.5, 1.0),
|
||||
vec4(0.5, 0.5, 0.5, 1.0),
|
||||
vec4(0.5, -0.5, 0.5, 1.0),
|
||||
vec4(-0.5, -0.5, -0.5, 1.0),
|
||||
vec4(-0.5, 0.5, -0.5, 1.0),
|
||||
vec4(0.5, 0.5, -0.5, 1.0),
|
||||
vec4(0.5, -0.5, -0.5, 1.0),
|
||||
];
|
||||
|
||||
var vertexColors = [
|
||||
vec4(0.0, 0.0, 0.0, 1.0), // black
|
||||
vec4(1.0, 0.0, 0.0, 1.0), // red
|
||||
vec4(1.0, 1.0, 0.0, 1.0), // yellow
|
||||
vec4(0.0, 1.0, 0.0, 1.0), // green
|
||||
vec4(0.0, 0.0, 1.0, 1.0), // blue
|
||||
vec4(1.0, 0.0, 1.0, 1.0), // magenta
|
||||
vec4(0.0, 1.0, 1.0, 1.0), // cyan
|
||||
vec4(1.0, 1.0, 1.0, 1.0), // white
|
||||
];
|
||||
|
||||
var near = -1;
|
||||
var far = 1;
|
||||
var radius = 1.0;
|
||||
var theta = 0.0;
|
||||
var phi = 0.0;
|
||||
var dr = 5.0 * Math.PI/180.0;
|
||||
|
||||
var left = -1.0;
|
||||
var right = 1.0;
|
||||
var top = 1.0;
|
||||
var bottom = -1.0;
|
||||
|
||||
var modelViewMatrixLoc, projectionMatrixLoc;
|
||||
var modelViewMatrix, projectionMatrix;
|
||||
var eye;
|
||||
|
||||
const at = vec3(0.0, 0.0, 0.0);
|
||||
const up = vec3(0.0, 1.0, 0.0);
|
||||
|
||||
// quad uses first index to set color for face
|
||||
|
||||
function quad(a, b, c, d) {
|
||||
positionsArray.push(vertices[a]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[b]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[c]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[a]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[c]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[d]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
}
|
||||
|
||||
// Each face determines two triangles
|
||||
|
||||
function colorCube()
|
||||
{
|
||||
quad(1, 0, 3, 2);
|
||||
quad(2, 3, 7, 6);
|
||||
quad(3, 0, 4, 7);
|
||||
quad(6, 5, 1, 2);
|
||||
quad(4, 5, 6, 7);
|
||||
quad(5, 4, 0, 1);
|
||||
}
|
||||
|
||||
|
||||
window.onload = function init() {
|
||||
canvas = document.getElementById("gl-canvas");
|
||||
|
||||
gl = canvas.getContext('webgl2');
|
||||
if (!gl) alert("WebGL 2.0 isn't available");
|
||||
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
|
||||
gl.clearColor(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
gl.enable(gl.DEPTH_TEST);
|
||||
|
||||
//
|
||||
// Load shaders and initialize attribute buffers
|
||||
//
|
||||
var program = initShaders(gl, "vertex-shader", "fragment-shader");
|
||||
gl.useProgram(program);
|
||||
|
||||
colorCube();
|
||||
|
||||
var cBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(colorsArray), gl.STATIC_DRAW);
|
||||
|
||||
var colorLoc = gl.getAttribLocation(program, "aColor");
|
||||
gl.vertexAttribPointer(colorLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(colorLoc);
|
||||
|
||||
var vBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(positionsArray), gl.STATIC_DRAW);
|
||||
|
||||
var positionLoc = gl.getAttribLocation(program, "aPosition");
|
||||
gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(positionLoc);
|
||||
|
||||
modelViewMatrixLoc = gl.getUniformLocation(program, "uModelViewMatrix");
|
||||
projectionMatrixLoc = gl.getUniformLocation(program, "uProjectionMatrix");
|
||||
|
||||
// buttons to change viewing parameters
|
||||
|
||||
document.getElementById("Button1").onclick = function(){near *= 1.1; far *= 1.1;};
|
||||
document.getElementById("Button2").onclick = function(){near *= 0.9; far *= 0.9;};
|
||||
document.getElementById("Button3").onclick = function(){radius *= 1.1;};
|
||||
document.getElementById("Button4").onclick = function(){radius *= 0.9;};
|
||||
document.getElementById("Button5").onclick = function(){theta += dr;};
|
||||
document.getElementById("Button6").onclick = function(){theta -= dr;};
|
||||
document.getElementById("Button7").onclick = function(){phi += dr;};
|
||||
document.getElementById("Button8").onclick = function(){phi -= dr;};
|
||||
|
||||
render();
|
||||
}
|
||||
|
||||
|
||||
var render = function() {
|
||||
gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
eye = vec3(radius*Math.sin(phi), radius*Math.sin(theta),
|
||||
radius*Math.cos(phi));
|
||||
|
||||
modelViewMatrix = lookAt(eye, at , up);
|
||||
projectionMatrix = ortho(left, right, bottom, top, near, far);
|
||||
|
||||
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
|
||||
gl.uniformMatrix4fv(projectionMatrixLoc, false, flatten(projectionMatrix));
|
||||
|
||||
gl.drawArrays(gl.TRIANGLES, 0, numVertices);
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
}
|
||||
orthoExample();
|
||||
@@ -0,0 +1,58 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<p> </p>
|
||||
<button id = "Button1">Increase Z</button>
|
||||
<button id = "Button2">Decrease Z</button>
|
||||
<button id = "Button3">Increase R</button>
|
||||
<button id = "Button4">Decrease R</button>
|
||||
|
||||
<p> </p>
|
||||
<button id = "Button5">Increase theta</button>
|
||||
<button id = "Button6">Decrease theta</button>
|
||||
<button id = "Button7">Increase phi</button>
|
||||
<button id = "Button8">Decrease phi</button>
|
||||
<p> </p>
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
|
||||
in vec4 aPosition;
|
||||
in vec4 aColor;
|
||||
out vec4 vColor;
|
||||
|
||||
uniform mat4 uModelViewMatrix;
|
||||
uniform mat4 uProjectionMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uProjectionMatrix*uModelViewMatrix*aPosition;
|
||||
vColor = aColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
in vec4 vColor;
|
||||
out vec4 fColor;
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
fColor = vColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
||||
<script type="text/javascript" src="ortho1.js"></script>
|
||||
|
||||
<body>
|
||||
<canvas id="gl-canvas" width="512" height="512">
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,157 @@
|
||||
"use strict";
|
||||
|
||||
var ortho1 = function() {
|
||||
|
||||
var canvas;
|
||||
var gl;
|
||||
|
||||
var numPositions = 36;
|
||||
|
||||
var positionsArray = [];
|
||||
var colorsArray = [];
|
||||
|
||||
var vertices = [
|
||||
vec4(-0.5, -0.5, 0.5, 1.0),
|
||||
vec4(-0.5, 0.5, 0.5, 1.0),
|
||||
vec4(0.5, 0.5, 0.5, 1.0),
|
||||
vec4(0.5, -0.5, 0.5, 1.0),
|
||||
vec4(-0.5, -0.5, -0.5, 1.0),
|
||||
vec4(-0.5, 0.5, -0.5, 1.0),
|
||||
vec4(0.5, 0.5, -0.5, 1.0),
|
||||
vec4(0.5, -0.5, -0.5, 1.0),
|
||||
];
|
||||
|
||||
var vertexColors = [
|
||||
vec4(0.0, 0.0, 0.0, 1.0), // black
|
||||
vec4(1.0, 0.0, 0.0, 1.0), // red
|
||||
vec4(1.0, 1.0, 0.0, 1.0), // yellow
|
||||
vec4(0.0, 1.0, 0.0, 1.0), // green
|
||||
vec4(0.0, 0.0, 1.0, 1.0), // blue
|
||||
vec4(1.0, 0.0, 1.0, 1.0), // magenta
|
||||
vec4(0.0, 1.0, 1.0, 1.0), // cyan
|
||||
vec4(1.0, 1.0, 1.0, 1.0), // white
|
||||
];
|
||||
|
||||
var near = -1;
|
||||
var far = 1;
|
||||
var radius = 1.0;
|
||||
var theta = 0.0;
|
||||
var phi = 0.0;
|
||||
var dr = 5.0 * Math.PI/180.0;
|
||||
|
||||
var left = -1.0;
|
||||
var right = 1.0;
|
||||
var top = 1.0;
|
||||
var bottom = -1.0;
|
||||
|
||||
|
||||
var modelViewMatrix, projectionMatrix;
|
||||
var modelViewMatrixLoc, projectionMatrixLoc;
|
||||
var eye;
|
||||
|
||||
const at = vec3(0.0, 0.0, 0.0);
|
||||
const up = vec3(0.0, 1.0, 0.0);
|
||||
|
||||
// quad uses first index to set color for face
|
||||
|
||||
function quad(a, b, c, d) {
|
||||
positionsArray.push(vertices[a]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[b]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[c]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[a]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[c]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[d]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
}
|
||||
|
||||
// Each face determines two triangles
|
||||
|
||||
function colorCube()
|
||||
{
|
||||
quad(1, 0, 3, 2);
|
||||
quad(2, 3, 7, 6);
|
||||
quad(3, 0, 4, 7);
|
||||
quad(6, 5, 1, 2);
|
||||
quad(4, 5, 6, 7);
|
||||
quad(5, 4, 0, 1);
|
||||
}
|
||||
|
||||
|
||||
window.onload = function init() {
|
||||
canvas = document.getElementById("gl-canvas");
|
||||
|
||||
gl = canvas.getContext('webgl2');
|
||||
if (!gl) alert("WebGL 2.0 isn't available");
|
||||
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
|
||||
gl.clearColor(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
gl.enable(gl.DEPTH_TEST);
|
||||
|
||||
//
|
||||
// Load shaders and initialize attribute buffers
|
||||
//
|
||||
var program = initShaders(gl, "vertex-shader", "fragment-shader");
|
||||
gl.useProgram(program);
|
||||
|
||||
colorCube();
|
||||
|
||||
var cBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(colorsArray), gl.STATIC_DRAW);
|
||||
|
||||
var colorLoc = gl.getAttribLocation(program, "aColor");
|
||||
gl.vertexAttribPointer(colorLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(colorLoc);
|
||||
|
||||
var vBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(positionsArray), gl.STATIC_DRAW);
|
||||
|
||||
var positionLoc = gl.getAttribLocation(program, "aPosition");
|
||||
gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(positionLoc);
|
||||
|
||||
modelViewMatrixLoc = gl.getUniformLocation(program, "uModelViewMatrix");
|
||||
projectionMatrixLoc = gl.getUniformLocation(program, "uProjectionMatrix");
|
||||
|
||||
// buttons to change viewing parameters
|
||||
|
||||
document.getElementById("Button1").onclick = function(){near *= 1.1; far *= 1.1;};
|
||||
document.getElementById("Button2").onclick = function(){near *= 0.9; far *= 0.9;};
|
||||
document.getElementById("Button3").onclick = function(){radius *= 1.1;};
|
||||
document.getElementById("Button4").onclick = function(){radius *= 0.9;};
|
||||
document.getElementById("Button5").onclick = function(){theta += dr;};
|
||||
document.getElementById("Button6").onclick = function(){theta -= dr;};
|
||||
document.getElementById("Button7").onclick = function(){phi += dr;};
|
||||
document.getElementById("Button8").onclick = function(){phi -= dr;};
|
||||
|
||||
render();
|
||||
}
|
||||
|
||||
|
||||
var render = function() {
|
||||
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
eye = vec3(radius*Math.sin(phi), radius*Math.sin(theta),
|
||||
radius*Math.cos(phi));
|
||||
|
||||
modelViewMatrix = lookAt(eye, at , up);
|
||||
projectionMatrix = ortho(left, right, bottom, top, near, far);
|
||||
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
|
||||
gl.uniformMatrix4fv(projectionMatrixLoc, false, flatten(projectionMatrix));
|
||||
|
||||
gl.drawArrays(gl.TRIANGLES, 0, numPositions);
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ortho1();
|
||||
@@ -0,0 +1,76 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<div>
|
||||
depth .05<input id="depthSlider" type="range"
|
||||
min=".05" max="3" step="0.1" value="2" />
|
||||
3
|
||||
</div>
|
||||
<div>
|
||||
radius 0.05<input id="radiusSlider" type="range"
|
||||
min="0.05" max="2" step="0.1" value="1" />
|
||||
2
|
||||
</div>
|
||||
<div>
|
||||
theta -90<input id="thetaSlider" type="range"
|
||||
min="-90" max="90" step="5" value="0" />
|
||||
90
|
||||
</div>
|
||||
<div>
|
||||
phi -90<input id="phiSlider" type="range"
|
||||
min="-90" max="90" step="5" value="0" />
|
||||
90
|
||||
</div>
|
||||
<div>
|
||||
width 0<input id="widthSlider" type="range"
|
||||
min="0" max="20" step="1" value="10" />
|
||||
20
|
||||
</div>
|
||||
<div>
|
||||
height 0<input id="heightSlider" type="range"
|
||||
min="0" max="20" step="1" value="10" />
|
||||
20
|
||||
</div>
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
|
||||
in vec4 aPosition;
|
||||
in vec4 aColor;
|
||||
out vec4 vColor;
|
||||
|
||||
uniform mat4 uModelViewMatrix;
|
||||
uniform mat4 uProjectionMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uProjectionMatrix*uModelViewMatrix*aPosition;
|
||||
vColor = aColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
in vec4 vColor;
|
||||
out vec4 fColor;
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
fColor = vColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
||||
<script type="text/javascript" src="ortho2.js"></script>
|
||||
|
||||
<body>
|
||||
<canvas id="gl-canvas" width="512" height="512">
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,169 @@
|
||||
"use strict";
|
||||
|
||||
var ortho2 = function() {
|
||||
|
||||
var canvas;
|
||||
var gl;
|
||||
|
||||
var numPositions = 36;
|
||||
|
||||
var positionsArray = [];
|
||||
var colorsArray = [];
|
||||
|
||||
var vertices = [
|
||||
vec4(-0.5, -0.5, 0.5, 1.0),
|
||||
vec4(-0.5, 0.5, 0.5, 1.0),
|
||||
vec4(0.5, 0.5, 0.5, 1.0),
|
||||
vec4(0.5, -0.5, 0.5, 1.0),
|
||||
vec4(-0.5, -0.5, -0.5, 1.0),
|
||||
vec4(-0.5, 0.5, -0.5, 1.0),
|
||||
vec4(0.5, 0.5, -0.5, 1.0),
|
||||
vec4(0.5, -0.5, -0.5, 1.0),
|
||||
];
|
||||
|
||||
var vertexColors = [
|
||||
vec4(0.0, 0.0, 0.0, 1.0), // black
|
||||
vec4(1.0, 0.0, 0.0, 1.0), // red
|
||||
vec4(1.0, 1.0, 0.0, 1.0), // yellow
|
||||
vec4(0.0, 1.0, 0.0, 1.0), // green
|
||||
vec4(0.0, 0.0, 1.0, 1.0), // blue
|
||||
vec4(1.0, 0.0, 1.0, 1.0), // magenta
|
||||
vec4(0.0, 1.0, 1.0, 1.0), // cyan
|
||||
vec4(1.0, 1.0, 1.0, 1.0), // white
|
||||
];
|
||||
|
||||
var near = -1;
|
||||
var far = 1;
|
||||
var radius = 1;
|
||||
var theta = 0.0;
|
||||
var phi = 0.0;
|
||||
var dr = 5.0 * Math.PI/180.0;
|
||||
|
||||
var left = -1.0;
|
||||
var right = 1.0;
|
||||
var top = 1.0;
|
||||
var bottom = -1.0;
|
||||
|
||||
|
||||
var modelViewMatrix, projectionMatrix;
|
||||
var modelViewMatrixLoc, projectionMatrixLoc;
|
||||
var eye;
|
||||
const at = vec3(0.0, 0.0, 0.0);
|
||||
const up = vec3(0.0, 1.0, 0.0);
|
||||
|
||||
function quad(a, b, c, d) {
|
||||
positionsArray.push(vertices[a]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[b]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[c]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[a]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[c]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[d]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
}
|
||||
|
||||
|
||||
function colorCube()
|
||||
{
|
||||
quad( 1, 0, 3, 2 );
|
||||
quad( 2, 3, 7, 6 );
|
||||
quad( 3, 0, 4, 7 );
|
||||
quad( 6, 5, 1, 2 );
|
||||
quad( 4, 5, 6, 7 );
|
||||
quad( 5, 4, 0, 1 );
|
||||
}
|
||||
|
||||
|
||||
window.onload = function init() {
|
||||
canvas = document.getElementById("gl-canvas");
|
||||
|
||||
gl = canvas.getContext('webgl2');
|
||||
if (!gl) alert("WebGL 2.0 isn't available");
|
||||
|
||||
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
|
||||
gl.clearColor(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
gl.enable(gl.DEPTH_TEST);
|
||||
|
||||
|
||||
//
|
||||
// Load shaders and initialize attribute buffers
|
||||
//
|
||||
var program = initShaders(gl, "vertex-shader", "fragment-shader");
|
||||
gl.useProgram(program);
|
||||
|
||||
colorCube();
|
||||
|
||||
var cBufferId = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, cBufferId);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(colorsArray), gl.STATIC_DRAW);
|
||||
|
||||
var colorLoc = gl.getAttribLocation(program, "aColor");
|
||||
gl.vertexAttribPointer(colorLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(colorLoc);
|
||||
|
||||
var vBufferId = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, vBufferId);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(positionsArray), gl.STATIC_DRAW);
|
||||
|
||||
var positionLoc = gl.getAttribLocation(program, "aPosition");
|
||||
gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(positionLoc);
|
||||
|
||||
modelViewMatrixLoc = gl.getUniformLocation(program, "uModelViewMatrix");
|
||||
projectionMatrixLoc = gl.getUniformLocation(program, "uProjectionMatrix");
|
||||
|
||||
// sliders for viewing parameters
|
||||
|
||||
document.getElementById("depthSlider").onchange = function(event) {
|
||||
far = event.target.value/2;
|
||||
near = -event.target.value/2;
|
||||
};
|
||||
|
||||
document.getElementById("radiusSlider").onchange = function(event) {
|
||||
radius = event.target.value;
|
||||
};
|
||||
document.getElementById("thetaSlider").onchange = function(event) {
|
||||
theta = event.target.value* Math.PI/180.0;
|
||||
};
|
||||
document.getElementById("phiSlider").onchange = function(event) {
|
||||
phi = event.target.value* Math.PI/180.0;
|
||||
};
|
||||
document.getElementById("heightSlider").onchange = function(event) {
|
||||
top = event.target.value/2;
|
||||
bottom = -event.target.value/2;
|
||||
};
|
||||
document.getElementById("widthSlider").onchange = function(event) {
|
||||
right = event.target.value/2;
|
||||
left = -event.target.value/2;
|
||||
};
|
||||
|
||||
render();
|
||||
}
|
||||
|
||||
|
||||
var render = function() {
|
||||
gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
eye = vec3(radius*Math.sin(phi), radius*Math.sin(theta),
|
||||
radius*Math.cos(phi));
|
||||
|
||||
modelViewMatrix = lookAt(eye, at , up);
|
||||
projectionMatrix = ortho(left, right, bottom, top, near, far);
|
||||
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
|
||||
gl.uniformMatrix4fv(projectionMatrixLoc, false, flatten(projectionMatrix));
|
||||
|
||||
gl.drawArrays(gl.TRIANGLES, 0, numPositions);
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ortho2();
|
||||
@@ -0,0 +1,60 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<p> </p>
|
||||
<button id = "Button1">Increase Z</button>
|
||||
<button id = "Button2">Decrease Z</button>
|
||||
<button id = "Button3">Increase R</button>
|
||||
<button id = "Button4">Decrease R</button>
|
||||
|
||||
<p> </p>
|
||||
<button id = "Button5">Increase theta</button>
|
||||
<button id = "Button6">Decrease theta</button>
|
||||
<button id = "Button7">Increase phi</button>
|
||||
<button id = "Button8">Decrease phi</button>
|
||||
<p> </p>
|
||||
|
||||
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
|
||||
in vec4 aPosition;
|
||||
in vec4 aColor;
|
||||
out vec4 vColor;
|
||||
|
||||
uniform mat4 uModelViewMatrix;
|
||||
uniform mat4 uProjectionMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uProjectionMatrix*uModelViewMatrix*aPosition;
|
||||
vColor = aColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
in vec4 vColor;
|
||||
out vec4 fColor;
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
fColor = vColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
||||
<script type="text/javascript" src="perspective.js"></script>
|
||||
|
||||
<body>
|
||||
<canvas id="gl-canvas" width="512" height="512">
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,152 @@
|
||||
"use strict";
|
||||
|
||||
var perspectiveExample = function(){
|
||||
var canvas;
|
||||
var gl;
|
||||
|
||||
var numPositions = 36;
|
||||
|
||||
var positionsArray = [];
|
||||
var colorsArray = [];
|
||||
|
||||
var vertices = [
|
||||
vec4(-0.5, -0.5, 1.5, 1.0),
|
||||
vec4(-0.5, 0.5, 1.5, 1.0),
|
||||
vec4(0.5, 0.5, 1.5, 1.0),
|
||||
vec4(0.5, -0.5, 1.5, 1.0),
|
||||
vec4(-0.5, -0.5, 0.5, 1.0),
|
||||
vec4(-0.5, 0.5, 0.5, 1.0),
|
||||
vec4(0.5, 0.5, 0.5, 1.0),
|
||||
vec4(0.5, -0.5, 0.5, 1.0)
|
||||
];
|
||||
|
||||
var vertexColors = [
|
||||
vec4(0.0, 0.0, 0.0, 1.0), // black
|
||||
vec4(1.0, 0.0, 0.0, 1.0), // red
|
||||
vec4(1.0, 1.0, 0.0, 1.0), // yellow
|
||||
vec4(0.0, 1.0, 0.0, 1.0), // green
|
||||
vec4(0.0, 0.0, 1.0, 1.0), // blue
|
||||
vec4(1.0, 0.0, 1.0, 1.0), // magenta
|
||||
vec4(0.0, 1.0, 1.0, 1.0), // cyan
|
||||
vec4(1.0, 1.0, 1.0, 1.0), // white
|
||||
];
|
||||
|
||||
|
||||
var near = 0.3;
|
||||
var far = 3.0;
|
||||
var radius = 4.0;
|
||||
var theta = 0.0;
|
||||
var phi = 0.0;
|
||||
var dr = 5.0 * Math.PI/180.0;
|
||||
|
||||
var fovy = 45.0; // Field-of-view in Y direction angle (in degrees)
|
||||
var aspect; // Viewport aspect ratio
|
||||
|
||||
var modelViewMatrixLoc, projectionMatrixLoc;
|
||||
var modelViewMatrix, projectionMatrix;
|
||||
var eye;
|
||||
const at = vec3(0.0, 0.0, 0.0);
|
||||
const up = vec3(0.0, 1.0, 0.0);
|
||||
|
||||
function quad(a, b, c, d) {
|
||||
positionsArray.push(vertices[a]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[b]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[c]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[a]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[c]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[d]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
}
|
||||
|
||||
|
||||
function colorCube()
|
||||
{
|
||||
quad(1, 0, 3, 2);
|
||||
quad(2, 3, 7, 6);
|
||||
quad(3, 0, 4, 7);
|
||||
quad(6, 5, 1, 2);
|
||||
quad(4, 5, 6, 7);
|
||||
quad(5, 4, 0, 1);
|
||||
}
|
||||
|
||||
|
||||
window.onload = function init() {
|
||||
|
||||
canvas = document.getElementById("gl-canvas");
|
||||
|
||||
gl = canvas.getContext('webgl2');
|
||||
if (!gl) alert("WebGL 2.0 isn't available" );
|
||||
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
|
||||
aspect = canvas.width/canvas.height;
|
||||
|
||||
gl.clearColor(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
gl.enable(gl.DEPTH_TEST);
|
||||
|
||||
|
||||
//
|
||||
// Load shaders and initialize attribute buffers
|
||||
//
|
||||
var program = initShaders(gl, "vertex-shader", "fragment-shader");
|
||||
gl.useProgram(program);
|
||||
|
||||
colorCube();
|
||||
|
||||
var cBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(colorsArray), gl.STATIC_DRAW);
|
||||
|
||||
var colorLoc = gl.getAttribLocation(program, "aColor");
|
||||
gl.vertexAttribPointer(colorLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(colorLoc);
|
||||
|
||||
var vBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(positionsArray), gl.STATIC_DRAW);
|
||||
|
||||
var positionLoc = gl.getAttribLocation(program, "aPosition");
|
||||
gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(positionLoc);
|
||||
|
||||
modelViewMatrixLoc = gl.getUniformLocation(program, "uModelViewMatrix");
|
||||
projectionMatrixLoc = gl.getUniformLocation(program, "uProjectionMatrix");
|
||||
|
||||
// buttons for viewing parameters
|
||||
|
||||
document.getElementById("Button1").onclick = function(){near *= 1.1; far *= 1.1;};
|
||||
document.getElementById("Button2").onclick = function(){near *= 0.9; far *= 0.9;};
|
||||
document.getElementById("Button3").onclick = function(){radius *= 2.0;};
|
||||
document.getElementById("Button4").onclick = function(){radius *= 0.5;};
|
||||
document.getElementById("Button5").onclick = function(){theta += dr;};
|
||||
document.getElementById("Button6").onclick = function(){theta -= dr;};
|
||||
document.getElementById("Button7").onclick = function(){phi += dr;};
|
||||
document.getElementById("Button8").onclick = function(){phi -= dr;};
|
||||
|
||||
render();
|
||||
}
|
||||
|
||||
|
||||
var render = function(){
|
||||
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
eye = vec3(radius*Math.sin(theta)*Math.cos(phi),
|
||||
radius*Math.sin(theta)*Math.sin(phi), radius*Math.cos(theta));
|
||||
modelViewMatrix = lookAt(eye, at , up);
|
||||
projectionMatrix = perspective(fovy, aspect, near, far);
|
||||
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
|
||||
gl.uniformMatrix4fv(projectionMatrixLoc, false, flatten(projectionMatrix));
|
||||
|
||||
gl.drawArrays(gl.TRIANGLES, 0, numPositions);
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
|
||||
}
|
||||
perspectiveExample();
|
||||
@@ -0,0 +1,60 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<p> </p>
|
||||
<button id = "Button1">Increase Z</button>
|
||||
<button id = "Button2">Decrease Z</button>
|
||||
<button id = "Button3">Increase R</button>
|
||||
<button id = "Button4">Decrease R</button>
|
||||
|
||||
<p> </p>
|
||||
<button id = "Button5">Increase theta</button>
|
||||
<button id = "Button6">Decrease theta</button>
|
||||
<button id = "Button7">Increase phi</button>
|
||||
<button id = "Button8">Decrease phi</button>
|
||||
<p> </p>
|
||||
|
||||
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
|
||||
in vec4 aPosition;
|
||||
in vec4 aColor;
|
||||
out vec4 vColor;
|
||||
|
||||
uniform mat4 uModelViewMatrix;
|
||||
uniform mat4 uProjectionMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uProjectionMatrix*uModelViewMatrix*aPosition;
|
||||
vColor = aColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
in vec4 vColor;
|
||||
out vec4 fColor;
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
fColor = vColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
||||
<script type="text/javascript" src="perspective1.js"></script>
|
||||
|
||||
<body>
|
||||
<canvas id="gl-canvas" width="512" height="512">
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,154 @@
|
||||
var perspectiveExample1 = function() {
|
||||
|
||||
"use strict";
|
||||
|
||||
var canvas;
|
||||
var gl;
|
||||
|
||||
var numPositions = 36;
|
||||
|
||||
var positionsArray = [];
|
||||
var colorsArray = [];
|
||||
|
||||
var vertices = [
|
||||
vec4(-0.5, -0.5, 1.5, 1.0),
|
||||
vec4(-0.5, 0.5, 1.5, 1.0),
|
||||
vec4(0.5, 0.5, 1.5, 1.0),
|
||||
vec4(0.5, -0.5, 1.5, 1.0),
|
||||
vec4(-0.5, -0.5, 0.5, 1.0),
|
||||
vec4(-0.5, 0.5, 0.5, 1.0),
|
||||
vec4(0.5, 0.5, 0.5, 1.0),
|
||||
vec4( 0.5, -0.5, 0.5, 1.0)
|
||||
];
|
||||
|
||||
var vertexColors = [
|
||||
vec4(0.0, 0.0, 0.0, 1.0), // black
|
||||
vec4(1.0, 0.0, 0.0, 1.0), // red
|
||||
vec4(1.0, 1.0, 0.0, 1.0), // yellow
|
||||
vec4(0.0, 1.0, 0.0, 1.0), // green
|
||||
vec4(0.0, 0.0, 1.0, 1.0), // blue
|
||||
vec4(1.0, 0.0, 1.0, 1.0), // magenta
|
||||
vec4(0.0, 1.0, 1.0, 1.0), // cyan
|
||||
vec4(1.0, 1.0, 1.0, 1.0), // white
|
||||
];
|
||||
|
||||
|
||||
var near = 0.3;
|
||||
var far = 3.0;
|
||||
var radius = 4.0;
|
||||
var theta = 0.0;
|
||||
var phi = 0.0;
|
||||
var dr = 5.0 * Math.PI/180.0;
|
||||
|
||||
var fovy = 45.0; // Field-of-view in Y direction angle (in degrees)
|
||||
var aspect; // Viewport aspect ratio
|
||||
|
||||
var modelViewMatrix, projectionMatrix;
|
||||
var modelViewMatrixLoc, projectionMatrixLoc;
|
||||
var eye;
|
||||
const at = vec3(0.0, 0.0, 0.0);
|
||||
const up = vec3(0.0, 1.0, 0.0);
|
||||
|
||||
function quad(a, b, c, d) {
|
||||
positionsArray.push(vertices[a]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[b]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[c]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[a]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[c]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[d]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
}
|
||||
|
||||
|
||||
function colorCube()
|
||||
{
|
||||
quad(1, 0, 3, 2);
|
||||
quad(2, 3, 7, 6);
|
||||
quad(3, 0, 4, 7);
|
||||
quad(6, 5, 1, 2);
|
||||
quad(4, 5, 6, 7);
|
||||
quad(5, 4, 0, 1);
|
||||
}
|
||||
|
||||
|
||||
window.onload = function init() {
|
||||
|
||||
canvas = document.getElementById("gl-canvas");
|
||||
|
||||
gl = canvas.getContext('webgl2');
|
||||
if (!gl) alert("WebGL 2.0 isn't available");
|
||||
|
||||
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
|
||||
aspect = canvas.width/canvas.height;
|
||||
|
||||
gl.clearColor(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
gl.enable(gl.DEPTH_TEST);
|
||||
|
||||
|
||||
//
|
||||
// Load shaders and initialize attribute buffers
|
||||
//
|
||||
var program = initShaders(gl, "vertex-shader", "fragment-shader");
|
||||
gl.useProgram(program);
|
||||
|
||||
colorCube();
|
||||
|
||||
var cBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(colorsArray), gl.STATIC_DRAW);
|
||||
|
||||
var colorLoc = gl.getAttribLocation(program, "aColor");
|
||||
gl.vertexAttribPointer(colorLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(colorLoc);
|
||||
|
||||
var vBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(positionsArray), gl.STATIC_DRAW);
|
||||
|
||||
var positionLoc = gl.getAttribLocation(program, "aPosition");
|
||||
gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(positionLoc);
|
||||
|
||||
modelViewMatrixLoc = gl.getUniformLocation(program, "uModelViewMatrix");
|
||||
projectionMatrixLoc = gl.getUniformLocation(program, "uProjectionMatrix");
|
||||
|
||||
// buttons for viewing parameters
|
||||
|
||||
document.getElementById("Button1").onclick = function(){near *= 1.1; far *= 1.1;};
|
||||
document.getElementById("Button2").onclick = function(){near *= 0.9; far *= 0.9;};
|
||||
document.getElementById("Button3").onclick = function(){radius *= 2.0;};
|
||||
document.getElementById("Button4").onclick = function(){radius *= 0.5;};
|
||||
document.getElementById("Button5").onclick = function(){theta += dr;};
|
||||
document.getElementById("Button6").onclick = function(){theta -= dr;};
|
||||
document.getElementById("Button7").onclick = function(){phi += dr;};
|
||||
document.getElementById("Button8").onclick = function(){phi -= dr;};
|
||||
|
||||
render();
|
||||
}
|
||||
|
||||
|
||||
var render = function(){
|
||||
gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
eye = vec3(radius*Math.sin(theta)*Math.cos(phi),
|
||||
radius*Math.sin(theta)*Math.sin(phi), radius*Math.cos(theta));
|
||||
modelViewMatrix = lookAt(eye, at , up);
|
||||
projectionMatrix = perspective(fovy, aspect, near, far);
|
||||
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
|
||||
gl.uniformMatrix4fv(projectionMatrixLoc, false, flatten(projectionMatrix));
|
||||
|
||||
gl.drawArrays(gl.TRIANGLES, 0, numPositions);
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
|
||||
}
|
||||
perspectiveExample1();
|
||||
@@ -0,0 +1,84 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<p> </p>
|
||||
<div>
|
||||
zNear .01<input id="zNearSlider" type="range"
|
||||
min=".01" max="3" step="0.1" value="0.3" />
|
||||
3
|
||||
</div>
|
||||
<div>
|
||||
zFar 3<input id="zFarSlider" type="range"
|
||||
min="3" max="10" step="3.0" value="3" />
|
||||
10
|
||||
</div>
|
||||
<div>
|
||||
radius 0.05<input id="radiusSlider" type="range"
|
||||
min="0.05" max="10" step="0.1" value="4" />
|
||||
10
|
||||
</div>
|
||||
<div>
|
||||
theta -90<input id="thetaSlider" type="range"
|
||||
min="-90" max="90" step="5" value="0" />
|
||||
90
|
||||
</div>
|
||||
<div>
|
||||
phi -90<input id="phiSlider" type="range"
|
||||
min="-90" max="90" step="5" value="0" />
|
||||
90
|
||||
</div>
|
||||
<div>
|
||||
fov 10<input id="fovSlider" type="range"
|
||||
min="10" max="120" step="5" value="45" />
|
||||
120
|
||||
</div>
|
||||
<div>
|
||||
aspect 0.5<input id="aspectSlider" type="range"
|
||||
min="0.5" max="2" step="0.1" value="1" />
|
||||
2
|
||||
</div>
|
||||
|
||||
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
|
||||
in vec4 aPosition;
|
||||
in vec4 aColor;
|
||||
out vec4 vColor;
|
||||
|
||||
uniform mat4 uModelViewMatrix;
|
||||
uniform mat4 uProjectionMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uProjectionMatrix*uModelViewMatrix*aPosition;
|
||||
vColor = aColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
|
||||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
in vec4 vColor;
|
||||
out vec4 fColor;
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
fColor = vColor;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
||||
<script type="text/javascript" src="perspective2.js"></script>
|
||||
|
||||
<body>
|
||||
<canvas id="gl-canvas" width="512" height="512">
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,166 @@
|
||||
var perspectiveExample2 = function() {
|
||||
|
||||
"use strict";
|
||||
|
||||
var canvas;
|
||||
var gl;
|
||||
|
||||
var numPositions = 36;
|
||||
|
||||
var positionsArray = [];
|
||||
var colorsArray = [];
|
||||
|
||||
var vertices = [
|
||||
vec4(-0.5, -0.5, 1.5, 1.0),
|
||||
vec4(-0.5, 0.5, 1.5, 1.0),
|
||||
vec4(0.5, 0.5, 1.5, 1.0),
|
||||
vec4(0.5, -0.5, 1.5, 1.0),
|
||||
vec4(-0.5, -0.5, 0.5, 1.0),
|
||||
vec4(-0.5, 0.5, 0.5, 1.0),
|
||||
vec4(0.5, 0.5, 0.5, 1.0),
|
||||
vec4( 0.5, -0.5, 0.5, 1.0)
|
||||
];
|
||||
|
||||
var vertexColors = [
|
||||
vec4(0.0, 0.0, 0.0, 1.0), // black
|
||||
vec4(1.0, 0.0, 0.0, 1.0), // red
|
||||
vec4(1.0, 1.0, 0.0, 1.0), // yellow
|
||||
vec4(0.0, 1.0, 0.0, 1.0), // green
|
||||
vec4(0.0, 0.0, 1.0, 1.0), // blue
|
||||
vec4(1.0, 0.0, 1.0, 1.0), // magenta
|
||||
vec4(0.0, 1.0, 1.0, 1.0), // cyan
|
||||
vec4(1.0, 1.0, 1.0, 1.0), // white
|
||||
];
|
||||
|
||||
|
||||
var near = 0.3;
|
||||
var far = 3.0;
|
||||
var radius = 4.0;
|
||||
var theta = 0.0;
|
||||
var phi = 0.0;
|
||||
var dr = 5.0 * Math.PI/180.0;
|
||||
|
||||
var fovy = 45.0; // Field-of-view in Y direction angle (in degrees)
|
||||
var aspect = 1.0; // Viewport aspect ratio
|
||||
|
||||
var modelViewMatrix, projectionMatrix;
|
||||
var modelViewMatrixLoc, projectionMatrixLoc;
|
||||
var eye;
|
||||
const at = vec3(0.0, 0.0, 0.0);
|
||||
const up = vec3(0.0, 1.0, 0.0);
|
||||
|
||||
|
||||
|
||||
function quad(a, b, c, d) {
|
||||
positionsArray.push(vertices[a]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[b]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[c]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[a]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[c]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
positionsArray.push(vertices[d]);
|
||||
colorsArray.push(vertexColors[a]);
|
||||
}
|
||||
|
||||
function colorCube()
|
||||
{
|
||||
quad(1, 0, 3, 2);
|
||||
quad(2, 3, 7, 6);
|
||||
quad(3, 0, 4, 7);
|
||||
quad(6, 5, 1, 2);
|
||||
quad(4, 5, 6, 7);
|
||||
quad(5, 4, 0, 1);
|
||||
}
|
||||
|
||||
|
||||
window.onload = function init() {
|
||||
canvas = document.getElementById("gl-canvas");
|
||||
|
||||
gl = canvas.getContext('webgl2');
|
||||
if (!gl) alert("WebGL 2.0 isn't available");
|
||||
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
|
||||
aspect = canvas.width/canvas.height;
|
||||
|
||||
gl.clearColor(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
gl.enable(gl.DEPTH_TEST);
|
||||
|
||||
//
|
||||
// Load shaders and initialize attribute buffers
|
||||
//
|
||||
var program = initShaders(gl, "vertex-shader", "fragment-shader");
|
||||
gl.useProgram(program);
|
||||
|
||||
colorCube();
|
||||
|
||||
var cBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(colorsArray), gl.STATIC_DRAW);
|
||||
|
||||
var colorLoc = gl.getAttribLocation(program, "aColor");
|
||||
gl.vertexAttribPointer(colorLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(colorLoc);
|
||||
|
||||
var vBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(positionsArray), gl.STATIC_DRAW);
|
||||
|
||||
var positionLoc = gl.getAttribLocation( program, "aPosition");
|
||||
gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(positionLoc);
|
||||
|
||||
modelViewMatrixLoc = gl.getUniformLocation(program, "uModelViewMatrix");
|
||||
projectionMatrixLoc = gl.getUniformLocation(program, "uProjectionMatrix");
|
||||
|
||||
// sliders for viewing parameters
|
||||
|
||||
document.getElementById("zFarSlider").onchange = function(event) {
|
||||
far = event.target.value;
|
||||
};
|
||||
document.getElementById("zNearSlider").onchange = function(event) {
|
||||
near = event.target.value;
|
||||
};
|
||||
document.getElementById("radiusSlider").onchange = function(event) {
|
||||
radius = event.target.value;
|
||||
};
|
||||
document.getElementById("thetaSlider").onchange = function(event) {
|
||||
theta = event.target.value* Math.PI/180.0;
|
||||
};
|
||||
document.getElementById("phiSlider").onchange = function(event) {
|
||||
phi = event.target.value* Math.PI/180.0;
|
||||
};
|
||||
document.getElementById("aspectSlider").onchange = function(event) {
|
||||
aspect = event.target.value;
|
||||
};
|
||||
document.getElementById("fovSlider").onchange = function(event) {
|
||||
fovy = event.target.value;
|
||||
};
|
||||
|
||||
render();
|
||||
}
|
||||
|
||||
|
||||
var render = function(){
|
||||
|
||||
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
eye = vec3(radius*Math.sin(theta)*Math.cos(phi),
|
||||
radius*Math.sin(theta)*Math.sin(phi), radius*Math.cos(theta));
|
||||
modelViewMatrix = lookAt(eye, at, up);
|
||||
projectionMatrix = perspective(fovy, aspect, near, far);
|
||||
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
|
||||
gl.uniformMatrix4fv(projectionMatrixLoc, false, flatten(projectionMatrix));
|
||||
|
||||
gl.drawArrays(gl.TRIANGLES, 0, numPositions);
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
|
||||
}
|
||||
perspectiveExample2();
|
||||
@@ -0,0 +1,44 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<script id="vertex-shader" type="x-shader/x-vertex">
|
||||
#version 300 es
|
||||
|
||||
in vec4 aPosition;
|
||||
|
||||
uniform mat4 uModelViewMatrix;
|
||||
uniform mat4 uProjectionMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uProjectionMatrix*uModelViewMatrix*aPosition;
|
||||
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id="fragment-shader" type="x-shader/x-fragment">
|
||||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
uniform vec4 uColor;
|
||||
out vec4 fColor;
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
fColor = uColor;
|
||||
|
||||
}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
||||
<script type="text/javascript" src="shadow.js"></script>
|
||||
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
||||
|
||||
<body>
|
||||
<canvas id="gl-canvas" width="512" height="512">
|
||||
Oops ... your browser doesn't support the HTML5 canvas element
|
||||
</canvas>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,142 @@
|
||||
"use strict";
|
||||
|
||||
var shadow = function () {
|
||||
|
||||
var canvas;
|
||||
var gl;
|
||||
|
||||
var positionsArray = [];
|
||||
|
||||
var near = -4;
|
||||
var far = 4;
|
||||
|
||||
var theta = 0.0;
|
||||
|
||||
var left = -2.0;
|
||||
var right = 2.0;
|
||||
var top = 2.0;
|
||||
var bottom = -2.0;
|
||||
|
||||
var modelViewMatrix, projectionMatrix;
|
||||
var modelViewMatrixLoc, projectionMatrixLoc;
|
||||
|
||||
var colorLoc;
|
||||
|
||||
var eye, at, up;
|
||||
var light;
|
||||
|
||||
var m;
|
||||
|
||||
var red;
|
||||
var black;
|
||||
|
||||
|
||||
window.onload = function init() {
|
||||
canvas = document.getElementById("gl-canvas");
|
||||
|
||||
gl = canvas.getContext('webgl2');
|
||||
if (!gl) alert("WebGL 2.0 isn't available");
|
||||
|
||||
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
|
||||
gl.clearColor(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
gl.enable(gl.DEPTH_TEST);
|
||||
|
||||
light = vec3(0.0, 2.0, 0.0);
|
||||
|
||||
// matrix for shadow projection
|
||||
|
||||
m = mat4();
|
||||
m[3][3] = 0;
|
||||
m[3][1] = -1 / light[1];
|
||||
|
||||
//console.log("m");
|
||||
//printm(m);
|
||||
|
||||
at = vec3(0.0, 0.0, 0.0);
|
||||
up = vec3(0.0, 1.0, 0.0);
|
||||
eye = vec3(1.0, 1.0, 1.0);
|
||||
|
||||
// color square red and shadow black
|
||||
|
||||
red = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
black = vec4(0.0, 0.0, 0.0, 1.0);
|
||||
|
||||
// square
|
||||
|
||||
positionsArray.push(vec4(-0.5, 0.5, -0.5, 1.0));
|
||||
positionsArray.push(vec4(-0.5, 0.5, 0.5, 1.0));
|
||||
positionsArray.push(vec4(0.5, 0.5, 0.5, 1.0));
|
||||
positionsArray.push(vec4(0.5, 0.5, -0.5, 1.0));
|
||||
|
||||
//
|
||||
// Load shaders and initialize attribute buffers
|
||||
//
|
||||
var program = initShaders(gl, "vertex-shader", "fragment-shader");
|
||||
gl.useProgram(program);
|
||||
|
||||
var vBuffer = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, flatten(positionsArray), gl.STATIC_DRAW);
|
||||
|
||||
var positionLoc = gl.getAttribLocation(program, "aPosition");
|
||||
gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0);
|
||||
gl.enableVertexAttribArray(positionLoc);
|
||||
|
||||
colorLoc = gl.getUniformLocation(program, "uColor");
|
||||
|
||||
modelViewMatrixLoc = gl.getUniformLocation(program, "uModelViewMatrix");
|
||||
projectionMatrixLoc = gl.getUniformLocation(program, "uProjectionMatrix");
|
||||
|
||||
projectionMatrix = ortho(left, right, bottom, top, near, far);
|
||||
gl.uniformMatrix4fv(projectionMatrixLoc, false, flatten(projectionMatrix));
|
||||
|
||||
render();
|
||||
|
||||
}
|
||||
|
||||
|
||||
var render = function () {
|
||||
|
||||
theta += 0.1;
|
||||
if (theta > 2 * Math.PI) theta -= 2 * Math.PI;
|
||||
|
||||
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
// model-view matrix for square
|
||||
|
||||
modelViewMatrix = lookAt(eye, at, up);
|
||||
|
||||
// send color and matrix for square then render
|
||||
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
|
||||
gl.uniform4fv(colorLoc, red);
|
||||
gl.drawArrays(gl.TRIANGLE_FAN, 0, 4);
|
||||
|
||||
// rotate light source
|
||||
|
||||
light[0] = Math.sin(theta);
|
||||
light[2] = Math.cos(theta);
|
||||
|
||||
|
||||
modelViewMatrix = mult(modelViewMatrix, translate(light[0], light[1], light[2]));
|
||||
|
||||
modelViewMatrix = mult(modelViewMatrix, m);
|
||||
|
||||
modelViewMatrix = mult(modelViewMatrix, translate(-light[0], -light[1],
|
||||
-light[2]));
|
||||
|
||||
// send color and matrix for shadow
|
||||
|
||||
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
|
||||
gl.uniform4fv(colorLoc, black);
|
||||
gl.drawArrays(gl.TRIANGLE_FAN, 0, 4);
|
||||
|
||||
requestAnimationFrame(render);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
shadow();
|
||||
Reference in new issue
Block a user