mirror of
https://github.com/RustyRaptor/renderagenericsphereinwebgl.git
synced 2026-12-03 15:15:21 -07:00
more attempts
This commit is contained in:
1 parent
1ae1bffa60
commit
ae7baa64e1
6 files changed
+1034
-182
No files matched your search
+122
-182
@@ -26,213 +26,153 @@
|
||||
*/
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<head>
|
||||
<title>Earth and Mars</title>
|
||||
<script type="application/javascript" src="webgl-utils.js"></script>
|
||||
<script type="application/javascript" src="webgl-debug.js"></script>
|
||||
<script src="J3DI.js"> </script>
|
||||
<script src="J3DIMath.js" type="application/javascript"> </script>
|
||||
<script src="J3DI.js"></script>
|
||||
<script src="J3DIMath.js" type="application/javascript"></script>
|
||||
<script language="JavaScript" type="text/javascript" src="./renderer.js"></script>
|
||||
<script language="JavaScript" type="text/javascript">
|
||||
var renderer = 0;
|
||||
|
||||
function run() {
|
||||
var vertSrc = document.getElementById("code_vert").value;
|
||||
var fracSrc = document.getElementById("code_frag").value;
|
||||
|
||||
renderer = new Renderer("myWebGLCanvas", vertSrc, fracSrc);
|
||||
renderer.init();
|
||||
renderer.display();
|
||||
}
|
||||
|
||||
function updateRenderer() {
|
||||
var vertSrc = document.getElementById("code_vert").value;
|
||||
var fragSrc = document.getElementById("code_frag").value;
|
||||
|
||||
renderer.updateShader(vertSrc, fragSrc);
|
||||
renderer.display();
|
||||
}
|
||||
|
||||
var interval = setInterval(timerFunc, 40);
|
||||
|
||||
function timerFunc() {
|
||||
var offset = 1.0;
|
||||
renderer.t += offset;
|
||||
renderer.display();
|
||||
}
|
||||
|
||||
function modeChanged() {
|
||||
var d = document.getElementById("select_id").value;
|
||||
renderer.modeVal = d;
|
||||
}
|
||||
|
||||
function modelChanged() {
|
||||
var d = document.getElementById("select_id2").value;
|
||||
renderer.updateModel(d);
|
||||
renderer.display();
|
||||
}
|
||||
|
||||
function exampleChanged() {
|
||||
var d = document.getElementById("select_example_id").value;
|
||||
switch(parseInt(d)){
|
||||
case 1:
|
||||
document.getElementById("code_vert").value = document.getElementById("PhongBrdfPerVertVert").value;
|
||||
document.getElementById("code_frag").value = document.getElementById("PhongBrdfPerVertFrag").value;
|
||||
break;
|
||||
case 2:
|
||||
document.getElementById("code_vert").value = document.getElementById("PhongBrdfPerFragVert").value;
|
||||
document.getElementById("code_frag").value = document.getElementById("PhongBrdfPerFragFrag").value;
|
||||
break;
|
||||
case 3:
|
||||
document.getElementById("code_vert").value = document.getElementById("PhongShadingVert").value;
|
||||
document.getElementById("code_frag").value = document.getElementById("PhongShadingFrag").value;
|
||||
break;
|
||||
case 4:
|
||||
document.getElementById("code_vert").value = document.getElementById("BlinnPhongShadingVert").value;
|
||||
document.getElementById("code_frag").value = document.getElementById("BlinnPhongShadingFrag").value;
|
||||
break;
|
||||
}
|
||||
|
||||
updateRenderer();
|
||||
}
|
||||
|
||||
</script>
|
||||
<script id="vshader" type="x-shader/x-vertex">
|
||||
uniform mat4 u_modelViewProjMatrix;
|
||||
uniform mat4 u_normalMatrix;
|
||||
uniform vec3 lightDir;
|
||||
|
||||
attribute vec3 vNormal;
|
||||
attribute vec4 vTexCoord;
|
||||
attribute vec4 vPosition;
|
||||
attribute vec3 inputPosition;
|
||||
attribute vec2 inputTexCoord;
|
||||
attribute vec3 inputNormal;
|
||||
|
||||
varying float v_Dot;
|
||||
varying vec2 v_texCoord;
|
||||
uniform mat4 projection, modelview, normalMat;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = u_modelViewProjMatrix * vPosition;
|
||||
v_texCoord = vTexCoord.st;
|
||||
vec4 transNormal = u_normalMatrix * vec4(vNormal,1);
|
||||
v_Dot = max(dot(transNormal.xyz, lightDir), 0.0);
|
||||
varying vec3 normalInterp;
|
||||
varying vec3 vertPos;
|
||||
|
||||
void main(){
|
||||
gl_Position = projection * modelview * vec4(inputPosition, 1.0);
|
||||
vec4 vertPos4 = modelview * vec4(inputPosition, 1.0);
|
||||
vertPos = vec3(vertPos4) / vertPos4.w;
|
||||
normalInterp = vec3(normalMat * vec4(inputNormal, 0.0));
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
</script>
|
||||
<script id="fshader" type="x-shader/x-fragment">
|
||||
precision mediump float;
|
||||
|
||||
uniform sampler2D sampler2d;
|
||||
varying vec3 normalInterp;
|
||||
varying vec3 vertPos;
|
||||
|
||||
varying float v_Dot;
|
||||
varying vec2 v_texCoord;
|
||||
uniform int mode;
|
||||
|
||||
const vec3 lightPos = vec3(1.0,1.0,1.0);
|
||||
const vec3 ambientColor = vec3(0.3, 0.0, 0.0);
|
||||
const vec3 diffuseColor = vec3(0.5, 0.0, 0.0);
|
||||
const vec3 specColor = vec3(1.0, 1.0, 1.0);
|
||||
|
||||
void main() {
|
||||
vec3 normal = normalize(normalInterp);
|
||||
vec3 lightDir = normalize(lightPos - vertPos);
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
vec3 viewDir = normalize(-vertPos);
|
||||
|
||||
float lambertian = max(dot(lightDir,normal), 0.0);
|
||||
float specular = 0.0;
|
||||
|
||||
if(lambertian > 0.0) {
|
||||
float specAngle = max(dot(reflectDir, viewDir), 0.0);
|
||||
specular = pow(specAngle, 4.0);
|
||||
}
|
||||
gl_FragColor = vec4(ambientColor +
|
||||
lambertian*diffuseColor +
|
||||
specular*specColor, 1.0);
|
||||
|
||||
// only ambient
|
||||
if(mode == 2) gl_FragColor = vec4(ambientColor, 1.0);
|
||||
// only diffuse
|
||||
if(mode == 3) gl_FragColor = vec4(lambertian*diffuseColor, 1.0);
|
||||
// only specular
|
||||
if(mode == 4) gl_FragColor = vec4(specular*specColor, 1.0);
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 color = texture2D(sampler2d,v_texCoord);
|
||||
color += vec4(0.1,0.1,0.1,1);
|
||||
gl_FragColor = vec4(color.xyz * v_Dot, color.a);
|
||||
}
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
<script>
|
||||
var g = {}; // globals
|
||||
function init()
|
||||
{
|
||||
var gl = initWebGL("example");
|
||||
if (!gl) {
|
||||
return;
|
||||
}
|
||||
g.program = simpleSetup(gl, "vshader", "fshader",
|
||||
[ "vNormal", "vTexCoord", "vPosition"],
|
||||
[ 0, 0, 0, 1 ], 10000);
|
||||
|
||||
gl.uniform3f(gl.getUniformLocation(g.program, "lightDir"), 0, 0, 1);
|
||||
gl.uniform1i(gl.getUniformLocation(g.program, "sampler2d"), 0);
|
||||
|
||||
if (g.program) {
|
||||
g.u_normalMatrixLoc = gl.getUniformLocation(g.program, "u_normalMatrix");
|
||||
g.u_modelViewProjMatrixLoc = gl.getUniformLocation(g.program, "u_modelViewProjMatrix");
|
||||
}
|
||||
|
||||
g.sphere = makeSphere(gl, 1, 30, 30);
|
||||
|
||||
// get the images
|
||||
earthTexture = loadImageTexture(gl, "earthmap1k.jpg");
|
||||
marsTexture = loadImageTexture(gl, "mars500x250.png");
|
||||
|
||||
return gl;
|
||||
}
|
||||
|
||||
var width = -1;
|
||||
var height = -1;
|
||||
var framerate;
|
||||
var requestId;
|
||||
|
||||
function reshape(ctx)
|
||||
{
|
||||
var canvas = document.getElementById('example');
|
||||
if (canvas.width == width && canvas.height == height)
|
||||
return;
|
||||
|
||||
width = canvas.width;
|
||||
height = canvas.height;
|
||||
|
||||
ctx.viewport(0, 0, width, height);
|
||||
|
||||
g.perspectiveMatrix = new J3DIMatrix4();
|
||||
g.perspectiveMatrix.perspective(30, width/height, 1, 10000);
|
||||
g.perspectiveMatrix.lookat(0,0,6, 0, 0, 0, 0, 1, 0);
|
||||
}
|
||||
|
||||
function drawOne(ctx, angle, x, y, z, scale, texture)
|
||||
{
|
||||
// setup VBOs
|
||||
ctx.enableVertexAttribArray(0);
|
||||
ctx.enableVertexAttribArray(1);
|
||||
ctx.enableVertexAttribArray(2);
|
||||
|
||||
ctx.bindBuffer(ctx.ARRAY_BUFFER, g.sphere.vertexObject);
|
||||
ctx.vertexAttribPointer(2, 3, ctx.FLOAT, false, 0, 0);
|
||||
|
||||
ctx.bindBuffer(ctx.ARRAY_BUFFER, g.sphere.normalObject);
|
||||
ctx.vertexAttribPointer(0, 3, ctx.FLOAT, false, 0, 0);
|
||||
|
||||
ctx.bindBuffer(ctx.ARRAY_BUFFER, g.sphere.texCoordObject);
|
||||
ctx.vertexAttribPointer(1, 2, ctx.FLOAT, false, 0, 0);
|
||||
|
||||
ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, g.sphere.indexObject);
|
||||
|
||||
// generate the model-view matrix
|
||||
var mvMatrix = new J3DIMatrix4();
|
||||
mvMatrix.translate(x,y,z);
|
||||
mvMatrix.rotate(30, 1,0,0);
|
||||
mvMatrix.rotate(angle, 0,1,0);
|
||||
mvMatrix.scale(scale, scale, scale);
|
||||
|
||||
// construct the normal matrix from the model-view matrix
|
||||
var normalMatrix = new J3DIMatrix4(mvMatrix);
|
||||
normalMatrix.invert();
|
||||
normalMatrix.transpose();
|
||||
normalMatrix.setUniform(ctx, g.u_normalMatrixLoc, false);
|
||||
|
||||
// construct the model-view * projection matrix
|
||||
var mvpMatrix = new J3DIMatrix4(g.perspectiveMatrix);
|
||||
mvpMatrix.multiply(mvMatrix);
|
||||
mvpMatrix.setUniform(ctx, g.u_modelViewProjMatrixLoc, false);
|
||||
|
||||
ctx.bindTexture(ctx.TEXTURE_2D, texture);
|
||||
ctx.drawElements(ctx.TRIANGLES, g.sphere.numIndices, ctx.UNSIGNED_SHORT, 0);
|
||||
}
|
||||
|
||||
function drawPicture(ctx)
|
||||
{
|
||||
reshape(ctx);
|
||||
ctx.clear(ctx.COLOR_BUFFER_BIT | ctx.DEPTH_BUFFER_BIT);
|
||||
|
||||
drawOne(ctx, currentAngle, -1, 0, 0, 1, earthTexture);
|
||||
drawOne(ctx, -currentAngle, 1, 0, 0, 0.6, marsTexture);
|
||||
|
||||
framerate.snapshot();
|
||||
|
||||
currentAngle += incAngle;
|
||||
if (currentAngle > 360)
|
||||
currentAngle -= 360;
|
||||
}
|
||||
|
||||
function start()
|
||||
{
|
||||
var c = document.getElementById("example");
|
||||
var w = Math.floor(window.innerWidth * 0.9);
|
||||
var h = Math.floor(window.innerHeight * 0.9);
|
||||
|
||||
//c = WebGLDebugUtils.makeLostContextSimulatingCanvas(c);
|
||||
// tell the simulator when to lose context.
|
||||
//c.loseContextInNCalls(1500);
|
||||
|
||||
c.addEventListener('webglcontextlost', handleContextLost, false);
|
||||
c.addEventListener('webglcontextrestored', handleContextRestored, false);
|
||||
|
||||
c.width = w;
|
||||
c.height = h;
|
||||
|
||||
var ctx = init();
|
||||
if (!ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
framerate = new Framerate("framerate");
|
||||
currentAngle = 0;
|
||||
incAngle = 0.2;
|
||||
|
||||
function f() {
|
||||
drawPicture(ctx)
|
||||
requestId = window.requestAnimFrame(f, c);
|
||||
};
|
||||
f();
|
||||
|
||||
function handleContextLost(e) {
|
||||
e.preventDefault();
|
||||
clearLoadingImages();
|
||||
if (requestId !== undefined) {
|
||||
window.cancelAnimFrame(requestId);
|
||||
requestId = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function handleContextRestored() {
|
||||
init();
|
||||
f();
|
||||
}
|
||||
|
||||
}
|
||||
</script>
|
||||
<style type="text/css">
|
||||
canvas {
|
||||
border: 2px solid black;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body onload="start()">
|
||||
<canvas id="example">
|
||||
</head>
|
||||
<body onload="run();">
|
||||
<canvas id="example">
|
||||
There is supposed to be an example drawing here, but it's not important.
|
||||
</canvas>
|
||||
<div id="framerate"></div>
|
||||
<div id="console"></div>
|
||||
</body>
|
||||
</canvas>
|
||||
<div id="framerate"></div>
|
||||
<div id="console"></div>
|
||||
</body>
|
||||
</html>
|
||||
Whitespace-only changes.
@@ -0,0 +1,523 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>WebGL Example: Phong / Blinn Phong Shading</title>
|
||||
<script language="JavaScript" type="text/javascript" src="./renderer.js"></script>
|
||||
<script>
|
||||
var g = {}; // globals
|
||||
|
||||
function init()
|
||||
{
|
||||
// Initialize
|
||||
var gl = initWebGL(
|
||||
// The id of the Canvas Element
|
||||
"example");
|
||||
if (!gl) {
|
||||
return;
|
||||
}
|
||||
g.program = simpleSetup(
|
||||
gl,
|
||||
// The ids of the vertex and fragment shaders
|
||||
"vshader", "fshader",
|
||||
// The vertex attribute names used by the shaders.
|
||||
// The order they appear here corresponds to their index
|
||||
// used later.
|
||||
[ "vNormal", "vColor", "vPosition"],
|
||||
// The clear color and depth values
|
||||
[ 0, 0, 0, 1 ], 10000);
|
||||
|
||||
// Set up a uniform variable for the shaders
|
||||
gl.uniform3f(gl.getUniformLocation(g.program, "lightDir"), 0, 0, 1);
|
||||
|
||||
// Create a box. On return 'gl' contains a 'box' property with
|
||||
// the BufferObjects containing the arrays for vertices,
|
||||
// normals, texture coords, and indices.
|
||||
g.box = makeBox(gl);
|
||||
|
||||
// Set up the array of colors for the cube's faces
|
||||
var colors = new Uint8Array(
|
||||
[ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, // v0-v1-v2-v3 front
|
||||
1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, // v0-v3-v4-v5 right
|
||||
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, // v0-v5-v6-v1 top
|
||||
1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, // v1-v6-v7-v2 left
|
||||
1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, // v7-v4-v3-v2 bottom
|
||||
0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1 ] // v4-v7-v6-v5 back
|
||||
);
|
||||
|
||||
// Set up the vertex buffer for the colors
|
||||
g.box.colorObject = gl.createBuffer();
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, g.box.colorObject);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
|
||||
|
||||
// Create some matrices to use later and save their locations in the shaders
|
||||
g.mvMatrix = new J3DIMatrix4();
|
||||
g.u_normalMatrixLoc = gl.getUniformLocation(g.program, "u_normalMatrix");
|
||||
g.normalMatrix = new J3DIMatrix4();
|
||||
g.u_modelViewProjMatrixLoc =
|
||||
gl.getUniformLocation(g.program, "u_modelViewProjMatrix");
|
||||
g.mvpMatrix = new J3DIMatrix4();
|
||||
|
||||
// Enable all of the vertex attribute arrays.
|
||||
gl.enableVertexAttribArray(0);
|
||||
gl.enableVertexAttribArray(1);
|
||||
gl.enableVertexAttribArray(2);
|
||||
|
||||
// Set up all the vertex attributes for vertices, normals and colors
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, g.box.vertexObject);
|
||||
gl.vertexAttribPointer(2, 3, gl.FLOAT, false, 0, 0);
|
||||
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, g.box.normalObject);
|
||||
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
|
||||
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, g.box.colorObject);
|
||||
gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, false, 0, 0);
|
||||
|
||||
// Bind the index array
|
||||
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, g.box.indexObject);
|
||||
|
||||
return gl;
|
||||
}
|
||||
|
||||
width = -1;
|
||||
height = -1;
|
||||
var requestId;
|
||||
|
||||
function reshape(gl)
|
||||
{
|
||||
var canvas = document.getElementById('example');
|
||||
var windowWidth = window.innerWidth - 20;
|
||||
var windowHeight = window.innerHeight - 40;
|
||||
if (windowWidth == width && windowHeight == height)
|
||||
return;
|
||||
|
||||
width = windowWidth;
|
||||
height = windowHeight;
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
|
||||
// Set the viewport and projection matrix for the scene
|
||||
gl.viewport(0, 0, width, height);
|
||||
g.perspectiveMatrix = new J3DIMatrix4();
|
||||
g.perspectiveMatrix.perspective(30, width/height, 1, 10000);
|
||||
g.perspectiveMatrix.lookat(0, 0, 7, 0, 0, 0, 0, 1, 0);
|
||||
}
|
||||
|
||||
function drawPicture(gl)
|
||||
{
|
||||
// Make sure the canvas is sized correctly.
|
||||
reshape(gl);
|
||||
|
||||
// Clear the canvas
|
||||
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
// Make a model/view matrix.
|
||||
g.mvMatrix.makeIdentity();
|
||||
g.mvMatrix.rotate(20, 1,0,0);
|
||||
g.mvMatrix.rotate(currentAngle, 0,1,0);
|
||||
|
||||
// Construct the normal matrix from the model-view matrix and pass it in
|
||||
g.normalMatrix.load(g.mvMatrix);
|
||||
g.normalMatrix.invert();
|
||||
g.normalMatrix.transpose();
|
||||
g.normalMatrix.setUniform(gl, g.u_normalMatrixLoc, false);
|
||||
|
||||
// Construct the model-view * projection matrix and pass it in
|
||||
g.mvpMatrix.load(g.perspectiveMatrix);
|
||||
g.mvpMatrix.multiply(g.mvMatrix);
|
||||
g.mvpMatrix.setUniform(gl, g.u_modelViewProjMatrixLoc, false);
|
||||
|
||||
// Draw the cube
|
||||
gl.drawElements(gl.TRIANGLES, g.box.numIndices, gl.UNSIGNED_BYTE, 0);
|
||||
|
||||
// Show the framerate
|
||||
framerate.snapshot();
|
||||
|
||||
currentAngle += incAngle;
|
||||
if (currentAngle > 360)
|
||||
currentAngle -= 360;
|
||||
}
|
||||
|
||||
function start()
|
||||
{
|
||||
var c = document.getElementById("example");
|
||||
var w = Math.floor(window.innerWidth * 0.9);
|
||||
var h = Math.floor(window.innerHeight * 0.9);
|
||||
|
||||
//c = WebGLDebugUtils.makeLostContextSimulatingCanvas(c);
|
||||
// tell the simulator when to lose context.
|
||||
//c.loseContextInNCalls(15);
|
||||
|
||||
c.addEventListener('webglcontextlost', handleContextLost, false);
|
||||
c.addEventListener('webglcontextrestored', handleContextRestored, false);
|
||||
|
||||
c.width = w;
|
||||
c.height = h;
|
||||
|
||||
var gl = init();
|
||||
if (!gl) {
|
||||
return;
|
||||
}
|
||||
currentAngle = 0;
|
||||
incAngle = 0.5;
|
||||
framerate = new Framerate("framerate");
|
||||
var f = function() {
|
||||
drawPicture(gl);
|
||||
requestId = window.requestAnimFrame(f, c);
|
||||
};
|
||||
f();
|
||||
|
||||
function handleContextLost(e) {
|
||||
e.preventDefault();
|
||||
if (requestId !== undefined) {
|
||||
window.cancelAnimFrame(requestId);
|
||||
requestId = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function handleContextRestored() {
|
||||
init();
|
||||
f();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body onload="run();">
|
||||
|
||||
<h1>WebGL Example: Phong / Blinn Phong Shading</h1>
|
||||
<div style="max-width: 550px;">
|
||||
<p>
|
||||
The JavaScript source of this example is: <a href="./renderer.js">renderer.js</a><br>
|
||||
If you do not see an output below, your browser does not support <a href="http://www.khronos.org/webgl/">WebGL</a>. The WebGL shader code is slightly different from the GLSL code (version 1.4 ) that is used in the <a href="http://www.uni-marburg.de/fb12/grafikmultimedia/lehre/grafik1">lecture</a>.
|
||||
This is because WebGL shaders must conform to the OpenGL ES Shading Language (Version 1.0) which can be found <a href="http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf">here</a>.
|
||||
</div>
|
||||
</p>
|
||||
</div>
|
||||
<canvas id="myWebGLCanvas" width="640" height="480">Your browser does not support the canvas element</canvas>
|
||||
|
||||
<div style="max-width: 550px;">Edit the shader code below and click on the button to see the result:
|
||||
<button type="button" onclick="updateRenderer()">Reload Shader Code</button></div>
|
||||
|
||||
<br>
|
||||
<select onchange="modeChanged()" id="select_id">
|
||||
<option value="1">mode = 1</option>
|
||||
<option value="2">mode = 2</option>
|
||||
<option value="3">mode = 3</option>
|
||||
<option value="4">mode = 4</option>
|
||||
<option value="5">mode = 5</option>
|
||||
</select>
|
||||
|
||||
<select onchange="modelChanged()" id="select_id2">
|
||||
<option value="./knot.txt">Knot</option>
|
||||
<option value="./cube.txt">Cube</option>
|
||||
<option value="./sphere.txt">Sphere</option>
|
||||
<option value="./hose.txt">Hose</option>
|
||||
<option value="./teapot.txt">Teapot</option>
|
||||
</select>
|
||||
|
||||
<select onchange="exampleChanged()" id="select_example_id">
|
||||
<option value="1">Phong BRDF per Vertex</option>
|
||||
<option value="2">Phong BRDF per Fragment</option>
|
||||
<option value="3">Phong Shading</option>
|
||||
<option value="4">Blinn Phong Shading</option>
|
||||
</select>
|
||||
|
||||
<table border="0" class="maintable" cellpadding="3px" cellspacing="3px">
|
||||
<tr style="vertical-align: top;">
|
||||
<td>Vertex shader: <br><textarea cols="40" rows="10" class="code_input_vert" id="code_vert" wrap="logical">
|
||||
attribute vec3 inputPosition;
|
||||
attribute vec2 inputTexCoord;
|
||||
attribute vec3 inputNormal;
|
||||
|
||||
uniform mat4 projection, modelview, normalMat;
|
||||
uniform int mode;
|
||||
|
||||
varying vec4 forFragColor;
|
||||
|
||||
const vec3 lightPos = vec3(1.0, 1.0, 1.0);
|
||||
const vec3 diffuseColor = vec3(0.5, 0.0, 0.0);
|
||||
const vec3 specColor = vec3(1.0, 1.0, 1.0);
|
||||
|
||||
void main(){
|
||||
gl_Position = projection * modelview * vec4(inputPosition, 1.0);
|
||||
|
||||
// all following gemetric computations are performed in the
|
||||
// camera coordinate system (aka eye coordinates)
|
||||
vec3 normal = vec3(normalMat * vec4(inputNormal, 0.0));
|
||||
vec4 vertPos4 = modelview * vec4(inputPosition, 1.0);
|
||||
vec3 vertPos = vec3(vertPos4) / vertPos4.w;
|
||||
vec3 lightDir = normalize(lightPos - vertPos);
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
vec3 viewDir = normalize(-vertPos);
|
||||
|
||||
float lambertian = max(dot(lightDir,normal), 0.0);
|
||||
float specular = 0.0;
|
||||
|
||||
if(lambertian > 0.0) {
|
||||
float specAngle = max(dot(reflectDir, viewDir), 0.0);
|
||||
specular = pow(specAngle, 4.0);
|
||||
|
||||
// the exponent controls the shininess (try mode 2)
|
||||
if(mode == 2) specular = pow(specAngle, 16.0);
|
||||
|
||||
// according to the rendering equation we would need to multiply
|
||||
// with the the "lambertian", but this has little visual effect
|
||||
if(mode == 3) specular *= lambertian;
|
||||
// switch to mode 4 to turn off the specular component
|
||||
if(mode == 4) specular *= 0.0;
|
||||
}
|
||||
|
||||
forFragColor = vec4(lambertian*diffuseColor + specular*specColor, 1.0);
|
||||
}
|
||||
</textarea>
|
||||
<p id="code_vert_error" style="width: 200px;"></p>
|
||||
</td>
|
||||
|
||||
<td>Fragment shader: <br><textarea cols="40" rows="10" class="code_input" id="code_frag" wrap="logical">
|
||||
precision mediump float;
|
||||
varying vec4 forFragColor;
|
||||
|
||||
void main() {
|
||||
gl_FragColor = forFragColor;
|
||||
}
|
||||
</textarea>
|
||||
<p id="code_frag_error" style="width: 200px;"></p>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<textarea id="PhongBrdfPerVertVert" style="display: none;">
|
||||
attribute vec3 inputPosition;
|
||||
attribute vec2 inputTexCoord;
|
||||
attribute vec3 inputNormal;
|
||||
|
||||
uniform mat4 projection, modelview, normalMat;
|
||||
uniform int mode;
|
||||
|
||||
varying vec4 forFragColor;
|
||||
|
||||
const vec3 lightPos = vec3(1.0, 1.0, 1.0);
|
||||
const vec3 diffuseColor = vec3(0.5, 0.0, 0.0);
|
||||
const vec3 specColor = vec3(1.0, 1.0, 1.0);
|
||||
|
||||
void main(){
|
||||
gl_Position = projection * modelview * vec4(inputPosition, 1.0);
|
||||
|
||||
// all following gemetric computations are performed in the
|
||||
// camera coordinate system (aka eye coordinates)
|
||||
vec3 normal = vec3(normalMat * vec4(inputNormal, 0.0));
|
||||
vec4 vertPos4 = modelview * vec4(inputPosition, 1.0);
|
||||
vec3 vertPos = vec3(vertPos4) / vertPos4.w;
|
||||
vec3 lightDir = normalize(lightPos - vertPos);
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
vec3 viewDir = normalize(-vertPos);
|
||||
|
||||
float lambertian = max(dot(lightDir,normal), 0.0);
|
||||
float specular = 0.0;
|
||||
|
||||
if(lambertian > 0.0) {
|
||||
float specAngle = max(dot(reflectDir, viewDir), 0.0);
|
||||
specular = pow(specAngle, 4.0);
|
||||
|
||||
// the exponent controls the shininess (try mode 2)
|
||||
if(mode == 2) specular = pow(specAngle, 16.0);
|
||||
|
||||
// according to the rendering equation we would need to multiply
|
||||
// with the the "lambertian", but this has little visual effect
|
||||
if(mode == 3) specular *= lambertian;
|
||||
// switch to mode 4 to turn off the specular component
|
||||
if(mode == 4) specular *= 0.0;
|
||||
}
|
||||
forFragColor = vec4(lambertian*diffuseColor + specular*specColor, 1.0);
|
||||
}
|
||||
</textarea>
|
||||
<textarea id="PhongBrdfPerVertFrag" style="display: none;">
|
||||
precision mediump float;
|
||||
varying vec4 forFragColor;
|
||||
|
||||
void main() {
|
||||
gl_FragColor = forFragColor;
|
||||
}
|
||||
</textarea>
|
||||
<textarea id="PhongBrdfPerFragVert" style="display: none;">
|
||||
attribute vec3 inputPosition;
|
||||
attribute vec2 inputTexCoord;
|
||||
attribute vec3 inputNormal;
|
||||
|
||||
uniform mat4 projection, modelview, normalMat;
|
||||
|
||||
varying vec3 normalInterp;
|
||||
varying vec3 vertPos;
|
||||
|
||||
void main(){
|
||||
gl_Position = projection * modelview * vec4(inputPosition, 1.0);
|
||||
vec4 vertPos4 = modelview * vec4(inputPosition, 1.0);
|
||||
vertPos = vec3(vertPos4) / vertPos4.w;
|
||||
normalInterp = vec3(normalMat * vec4(inputNormal, 0.0));
|
||||
}
|
||||
</textarea>
|
||||
<textarea id="PhongBrdfPerFragFrag" style="display: none;">
|
||||
precision mediump float;
|
||||
|
||||
varying vec3 normalInterp;
|
||||
varying vec3 vertPos;
|
||||
|
||||
uniform int mode;
|
||||
|
||||
const vec3 lightPos = vec3(1.0,1.0,1.0);
|
||||
const vec3 diffuseColor = vec3(0.5, 0.0, 0.0);
|
||||
const vec3 specColor = vec3(1.0, 1.0, 1.0);
|
||||
|
||||
void main() {
|
||||
|
||||
vec3 normal = normalize(normalInterp);
|
||||
vec3 lightDir = normalize(lightPos - vertPos);
|
||||
|
||||
float lambertian = max(dot(lightDir,normal), 0.0);
|
||||
float specular = 0.0;
|
||||
|
||||
if(lambertian > 0.0) {
|
||||
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
vec3 viewDir = normalize(-vertPos);
|
||||
|
||||
float specAngle = max(dot(reflectDir, viewDir), 0.0);
|
||||
specular = pow(specAngle, 4.0);
|
||||
|
||||
// the exponent controls the shininess (try mode 2)
|
||||
if(mode == 2) specular = pow(specAngle, 16.0);
|
||||
|
||||
// according to the rendering equation we would need to multiply
|
||||
// with the the "lambertian", but this has little visual effect
|
||||
if(mode == 3) specular *= lambertian;
|
||||
|
||||
// switch to mode 4 to turn off the specular component
|
||||
if(mode == 4) specular *= 0.0;
|
||||
|
||||
}
|
||||
|
||||
gl_FragColor = vec4( lambertian*diffuseColor +
|
||||
specular*specColor, 1.0);
|
||||
}
|
||||
</textarea>
|
||||
<textarea id="PhongShadingVert" style="display: none;">
|
||||
attribute vec3 inputPosition;
|
||||
attribute vec2 inputTexCoord;
|
||||
attribute vec3 inputNormal;
|
||||
|
||||
uniform mat4 projection, modelview, normalMat;
|
||||
|
||||
varying vec3 normalInterp;
|
||||
varying vec3 vertPos;
|
||||
|
||||
void main(){
|
||||
gl_Position = projection * modelview * vec4(inputPosition, 1.0);
|
||||
vec4 vertPos4 = modelview * vec4(inputPosition, 1.0);
|
||||
vertPos = vec3(vertPos4) / vertPos4.w;
|
||||
normalInterp = vec3(normalMat * vec4(inputNormal, 0.0));
|
||||
}
|
||||
</textarea>
|
||||
<textarea id="PhongShadingFrag" style="display: none;">
|
||||
precision mediump float;
|
||||
|
||||
varying vec3 normalInterp;
|
||||
varying vec3 vertPos;
|
||||
|
||||
uniform int mode;
|
||||
|
||||
const vec3 lightPos = vec3(1.0,1.0,1.0);
|
||||
const vec3 ambientColor = vec3(0.3, 0.0, 0.0);
|
||||
const vec3 diffuseColor = vec3(0.5, 0.0, 0.0);
|
||||
const vec3 specColor = vec3(1.0, 1.0, 1.0);
|
||||
|
||||
void main() {
|
||||
vec3 normal = normalize(normalInterp);
|
||||
vec3 lightDir = normalize(lightPos - vertPos);
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
vec3 viewDir = normalize(-vertPos);
|
||||
|
||||
float lambertian = max(dot(lightDir,normal), 0.0);
|
||||
float specular = 0.0;
|
||||
|
||||
if(lambertian > 0.0) {
|
||||
float specAngle = max(dot(reflectDir, viewDir), 0.0);
|
||||
specular = pow(specAngle, 4.0);
|
||||
}
|
||||
gl_FragColor = vec4(ambientColor +
|
||||
lambertian*diffuseColor +
|
||||
specular*specColor, 1.0);
|
||||
|
||||
// only ambient
|
||||
if(mode == 2) gl_FragColor = vec4(ambientColor, 1.0);
|
||||
// only diffuse
|
||||
if(mode == 3) gl_FragColor = vec4(lambertian*diffuseColor, 1.0);
|
||||
// only specular
|
||||
if(mode == 4) gl_FragColor = vec4(specular*specColor, 1.0);
|
||||
|
||||
}
|
||||
</textarea>
|
||||
|
||||
<textarea id="BlinnPhongShadingVert" style="display: none;">
|
||||
attribute vec3 inputPosition;
|
||||
attribute vec2 inputTexCoord;
|
||||
attribute vec3 inputNormal;
|
||||
|
||||
uniform mat4 projection, modelview, normalMat;
|
||||
|
||||
varying vec3 normalInterp;
|
||||
varying vec3 vertPos;
|
||||
|
||||
void main(){
|
||||
gl_Position = projection * modelview * vec4(inputPosition, 1.0);
|
||||
vec4 vertPos4 = modelview * vec4(inputPosition, 1.0);
|
||||
vertPos = vec3(vertPos4) / vertPos4.w;
|
||||
normalInterp = vec3(normalMat * vec4(inputNormal, 0.0));
|
||||
}
|
||||
</textarea>
|
||||
<textarea id="BlinnPhongShadingFrag" style="display: none;">
|
||||
precision mediump float;
|
||||
|
||||
varying vec3 normalInterp;
|
||||
varying vec3 vertPos;
|
||||
|
||||
uniform int mode;
|
||||
|
||||
const vec3 lightPos = vec3(1.0,1.0,1.0);
|
||||
const vec3 ambientColor = vec3(0.1, 0.0, 0.0);
|
||||
const vec3 diffuseColor = vec3(0.5, 0.0, 0.0);
|
||||
const vec3 specColor = vec3(1.0, 1.0, 1.0);
|
||||
|
||||
void main() {
|
||||
|
||||
vec3 normal = normalize(normalInterp);
|
||||
vec3 lightDir = normalize(lightPos - vertPos);
|
||||
|
||||
float lambertian = max(dot(lightDir,normal), 0.0);
|
||||
float specular = 0.0;
|
||||
|
||||
if(lambertian > 0.0) {
|
||||
|
||||
vec3 viewDir = normalize(-vertPos);
|
||||
|
||||
// this is blinn phong
|
||||
vec3 halfDir = normalize(lightDir + viewDir);
|
||||
float specAngle = max(dot(halfDir, normal), 0.0);
|
||||
specular = pow(specAngle, 16.0);
|
||||
|
||||
// this is phong (for comparison)
|
||||
if(mode == 2) {
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
specAngle = max(dot(reflectDir, viewDir), 0.0);
|
||||
// note that the exponent is different here
|
||||
specular = pow(specAngle, 4.0);
|
||||
}
|
||||
}
|
||||
|
||||
gl_FragColor = vec4(ambientColor +
|
||||
lambertian * diffuseColor +
|
||||
specular * specColor, 1.0);
|
||||
}
|
||||
</textarea>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 336 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 170 KiB |
@@ -0,0 +1,389 @@
|
||||
// created by Thorsten Thormaehlen for educational purpose
|
||||
|
||||
function Renderer(canvasName, vertSrc, fragSrc)
|
||||
{
|
||||
// public member
|
||||
this.t = 0.0;
|
||||
this.modeVal = 1;
|
||||
this.lightPos = new Float32Array(3);
|
||||
this.lightVec = new Float32Array(3);
|
||||
this.attenuation = 0.01;
|
||||
|
||||
// private members (inside closure)
|
||||
var canvasName = canvasName;
|
||||
var vertSrc = vertSrc;
|
||||
var fragSrc = fragSrc;
|
||||
var canvas;
|
||||
var gl;
|
||||
var sceneVertNo = 0;
|
||||
var bufID;
|
||||
var progID = 0;
|
||||
var vertID = 0;
|
||||
var fragID = 0;
|
||||
var vertexLoc = 0;
|
||||
var texCoordLoc = 0;
|
||||
var normalLoc = 0;
|
||||
var projectionLoc = 0;
|
||||
var modelviewLoc = 0;
|
||||
var normalMatrixLoc = 0;
|
||||
var modeLoc = 0;
|
||||
var lightPosLoc = 0;
|
||||
var lightVecLoc = 0;
|
||||
var attenuationLoc = 0;
|
||||
var projection = new Float32Array(16);
|
||||
var modelview = new Float32Array(16);
|
||||
var currentFileName = "./knot.txt";
|
||||
|
||||
// public
|
||||
this.updateShader = function (newvertSrc, newfragSrc) {
|
||||
vertSrc = newvertSrc;
|
||||
fragSrc = newfragSrc;
|
||||
|
||||
gl.deleteProgram(progID);
|
||||
gl.deleteShader(vertID);
|
||||
gl.deleteShader(fragID);
|
||||
|
||||
setupShaders();
|
||||
}
|
||||
|
||||
|
||||
// public
|
||||
this.updateModel = function (newFileName) {
|
||||
currentFileName = newFileName;
|
||||
|
||||
gl.deleteProgram(progID);
|
||||
gl.deleteShader(vertID);
|
||||
gl.deleteShader(fragID);
|
||||
gl.deleteBuffer(bufID);
|
||||
|
||||
this.init();
|
||||
}
|
||||
|
||||
// public
|
||||
this.init = function () {
|
||||
this.canvas = window.document.getElementById(canvasName);
|
||||
try {
|
||||
gl = this.canvas.getContext("experimental-webgl");
|
||||
} catch (e) {}
|
||||
if (!gl) {
|
||||
window.alert("Error: Could not retrieve WebGL Context");
|
||||
return;
|
||||
}
|
||||
|
||||
gl.enable(gl.DEPTH_TEST);
|
||||
|
||||
setupShaders();
|
||||
|
||||
// generate a Vertex Buffer Object (VBO)
|
||||
bufID = gl.createBuffer();
|
||||
|
||||
var sceneVertexData = loadVertexData(currentFileName);
|
||||
|
||||
sceneVertNo = sceneVertexData.length / (3+2+3);
|
||||
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, bufID);
|
||||
gl.bufferData(gl.ARRAY_BUFFER, sceneVertexData, gl.STATIC_DRAW);
|
||||
|
||||
if(vertexLoc != -1) {
|
||||
// position
|
||||
var offset = 0;
|
||||
var stride = (3+2+3)*Float32Array.BYTES_PER_ELEMENT;
|
||||
gl.vertexAttribPointer(vertexLoc, 3, gl.FLOAT, false, stride, offset);
|
||||
gl.enableVertexAttribArray(vertexLoc);
|
||||
}
|
||||
if(texCoordLoc != -1) {
|
||||
// texCoord
|
||||
offset = 0 + 3*Float32Array.BYTES_PER_ELEMENT;
|
||||
gl.vertexAttribPointer(texCoordLoc, 2, gl.FLOAT, false, stride, offset);
|
||||
gl.enableVertexAttribArray(texCoordLoc);
|
||||
}
|
||||
|
||||
if(normalLoc != -1) {
|
||||
// normal
|
||||
offset = 0 + (3+2)*Float32Array.BYTES_PER_ELEMENT;
|
||||
gl.vertexAttribPointer(normalLoc, 3, gl.FLOAT, false, stride, offset);
|
||||
gl.enableVertexAttribArray(normalLoc);
|
||||
}
|
||||
this.resize(this.canvas.width, this.canvas.height);
|
||||
}
|
||||
|
||||
function loadVertexData(filename) {
|
||||
var data = new Float32Array(0);
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('GET', filename, false);
|
||||
request.send(); //"false" above, will block
|
||||
|
||||
if (request.status != 200) {
|
||||
alert("can not load file " + filename);
|
||||
|
||||
}else{
|
||||
var floatVals = request.responseText.split('\n');
|
||||
var numFloats = parseInt(floatVals[0]);
|
||||
if(numFloats % (3+2+3) != 0) return data;
|
||||
data = new Float32Array(numFloats);
|
||||
for(var k = 0; k < numFloats; k++) {
|
||||
data[k] = floatVals[k+1];
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
//public
|
||||
this.resize = function (w, h) {
|
||||
gl.viewport(0, 0, w, h);
|
||||
|
||||
// this function replaces gluPerspective
|
||||
mat4Perspective(projection, 45.0, w/h, 0.5, 4.0);
|
||||
//mat4Print(projection);
|
||||
}
|
||||
|
||||
//public
|
||||
this.display = function () {
|
||||
gl.clearColor(0.0, 0.0, 0.0, 1.0);
|
||||
gl.clear(gl.COLOR_BUFFER_BIT);
|
||||
|
||||
// camera orbits in the z=1.5 plane
|
||||
// and looks at the origin
|
||||
// mat4LookAt replaces gluLookAt
|
||||
var rad = Math.PI / 180.0 * this.t;
|
||||
|
||||
mat4LookAt(modelview,
|
||||
1.5*Math.cos(rad), 1.5*Math.sin(rad), 1.5, // eye
|
||||
0.0, 0.0, 0.0, // look at
|
||||
0.0, 0.0, 1.0); // up
|
||||
|
||||
//mat4Print(modelview);
|
||||
|
||||
var modelviewInv = new Float32Array(16);
|
||||
var normalmatrix = new Float32Array(16);
|
||||
mat4Invert(modelview, modelviewInv);
|
||||
mat4Transpose(modelviewInv, normalmatrix);
|
||||
|
||||
gl.useProgram(progID);
|
||||
|
||||
// load the current projection and modelview matrix into the
|
||||
// corresponding UNIFORM variables of the shader
|
||||
gl.uniformMatrix4fv(projectionLoc, false, projection);
|
||||
gl.uniformMatrix4fv(modelviewLoc, false, modelview);
|
||||
if(normalMatrixLoc != -1) gl.uniformMatrix4fv(normalMatrixLoc, false, normalmatrix);
|
||||
if(modeLoc != -1) gl.uniform1i(modeLoc, this.modeVal);
|
||||
if(lightPosLoc != -1) gl.uniform3fv(lightPosLoc, this.lightPos);
|
||||
if(lightVecLoc != -1) gl.uniform3fv(lightVecLoc, this.lightVec);
|
||||
if(attenuationLoc != -1) gl.uniform1f(attenuationLoc, this.attenuation);
|
||||
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, bufID);
|
||||
gl.drawArrays(gl.TRIANGLES, 0, sceneVertNo);
|
||||
}
|
||||
|
||||
// private
|
||||
function setupShaders() {
|
||||
|
||||
// create shader
|
||||
vertID = gl.createShader(gl.VERTEX_SHADER);
|
||||
fragID = gl.createShader(gl.FRAGMENT_SHADER);
|
||||
|
||||
// specify shader source
|
||||
gl.shaderSource(vertID, vertSrc);
|
||||
gl.shaderSource(fragID, fragSrc);
|
||||
|
||||
// compile the shader
|
||||
gl.compileShader(vertID);
|
||||
gl.compileShader(fragID);
|
||||
|
||||
var error = false;
|
||||
// check for errors
|
||||
if(!gl.getShaderParameter(vertID, gl.COMPILE_STATUS)) {
|
||||
document.getElementById("code_vert_error").innerHTML = "invalid vertex shader : " + gl.getShaderInfoLog(vertID);
|
||||
error = true;
|
||||
}
|
||||
else{
|
||||
document.getElementById("code_vert_error").innerHTML = "";
|
||||
}
|
||||
if(!gl.getShaderParameter(fragID, gl.COMPILE_STATUS)) {
|
||||
document.getElementById("code_frag_error").innerHTML = "invalid fragment shader : " + gl.getShaderInfoLog(fragID);
|
||||
error = true;
|
||||
}else{
|
||||
document.getElementById("code_frag_error").innerHTML = "";
|
||||
}
|
||||
|
||||
if(error) return;
|
||||
|
||||
// create program and attach shaders
|
||||
progID = gl.createProgram();
|
||||
gl.attachShader(progID, vertID);
|
||||
gl.attachShader(progID, fragID);
|
||||
|
||||
// link the program
|
||||
gl.linkProgram(progID);
|
||||
if (!gl.getProgramParameter(progID, gl.LINK_STATUS)) {
|
||||
alert(gl.getProgramInfoLog(progID));
|
||||
return;
|
||||
}
|
||||
|
||||
// retrieve the location of the IN variables of the vertex shader
|
||||
vertexLoc = gl.getAttribLocation(progID,"inputPosition");
|
||||
texCoordLoc = gl.getAttribLocation(progID,"inputTexCoord");
|
||||
normalLoc = gl.getAttribLocation(progID, "inputNormal");
|
||||
|
||||
// retrieve the location of the UNIFORM variables of the shader
|
||||
projectionLoc = gl.getUniformLocation(progID, "projection");
|
||||
modelviewLoc = gl.getUniformLocation(progID, "modelview");
|
||||
normalMatrixLoc = gl.getUniformLocation(progID, "normalMat");
|
||||
modeLoc = gl.getUniformLocation(progID, "mode");
|
||||
lightPosLoc = gl.getUniformLocation(progID, "lightPos");
|
||||
lightVecLoc = gl.getUniformLocation(progID, "lightVec");
|
||||
attenuationLoc = gl.getUniformLocation(progID, "attenuationVal");
|
||||
|
||||
}
|
||||
|
||||
// the following functions are some matrix and vector helpers
|
||||
// they work for this demo but in general it is recommended
|
||||
// to use more advanced matrix libraries
|
||||
function vec3Dot(a, b) {
|
||||
return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
|
||||
}
|
||||
|
||||
function vec3Cross(a, b, res) {
|
||||
res[0] = a[1] * b[2] - b[1] * a[2];
|
||||
res[1] = a[2] * b[0] - b[2] * a[0];
|
||||
res[2] = a[0] * b[1] - b[0] * a[1];
|
||||
}
|
||||
|
||||
function vec3Normalize(a) {
|
||||
var mag = Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
|
||||
a[0] /= mag; a[1] /= mag; a[2] /= mag;
|
||||
}
|
||||
|
||||
function mat4Identity(a) {
|
||||
a.length = 16;
|
||||
for (var i = 0; i < 16; ++i) a[i] = 0.0;
|
||||
for (var i = 0; i < 4; ++i) a[i + i * 4] = 1.0;
|
||||
}
|
||||
|
||||
function mat4Multiply(a, b, res) {
|
||||
for (var i = 0; i < 4; ++i) {
|
||||
for (var j = 0; j < 4; ++j) {
|
||||
res[j*4 + i] = 0.0;
|
||||
for (var k = 0; k < 4; ++k) {
|
||||
res[j*4 + i] += a[k*4 + i] * b[j*4 + k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function mat4Perspective(a, fov, aspect, zNear, zFar) {
|
||||
var f = 1.0 / Math.tan (fov/2.0 * (Math.PI / 180.0));
|
||||
mat4Identity(a);
|
||||
a[0] = f / aspect;
|
||||
a[1 * 4 + 1] = f;
|
||||
a[2 * 4 + 2] = (zFar + zNear) / (zNear - zFar);
|
||||
a[3 * 4 + 2] = (2.0 * zFar * zNear) / (zNear - zFar);
|
||||
a[2 * 4 + 3] = -1.0;
|
||||
a[3 * 4 + 3] = 0.0;
|
||||
}
|
||||
|
||||
function mat4LookAt(viewMatrix,
|
||||
eyeX, eyeY, eyeZ,
|
||||
centerX, centerY, centerZ,
|
||||
upX, upY, upZ) {
|
||||
|
||||
var dir = new Float32Array(3);
|
||||
var right = new Float32Array(3);
|
||||
var up = new Float32Array(3);
|
||||
var eye = new Float32Array(3);
|
||||
|
||||
up[0]=upX; up[1]=upY; up[2]=upZ;
|
||||
eye[0]=eyeX; eye[1]=eyeY; eye[2]=eyeZ;
|
||||
|
||||
dir[0]=centerX-eyeX; dir[1]=centerY-eyeY; dir[2]=centerZ-eyeZ;
|
||||
vec3Normalize(dir);
|
||||
vec3Cross(dir,up,right);
|
||||
vec3Normalize(right);
|
||||
vec3Cross(right,dir,up);
|
||||
vec3Normalize(up);
|
||||
// first row
|
||||
viewMatrix[0] = right[0];
|
||||
viewMatrix[4] = right[1];
|
||||
viewMatrix[8] = right[2];
|
||||
viewMatrix[12] = -vec3Dot(right, eye);
|
||||
// second row
|
||||
viewMatrix[1] = up[0];
|
||||
viewMatrix[5] = up[1];
|
||||
viewMatrix[9] = up[2];
|
||||
viewMatrix[13] = -vec3Dot(up, eye);
|
||||
// third row
|
||||
viewMatrix[2] = -dir[0];
|
||||
viewMatrix[6] = -dir[1];
|
||||
viewMatrix[10] = -dir[2];
|
||||
viewMatrix[14] = vec3Dot(dir, eye);
|
||||
// forth row
|
||||
viewMatrix[3] = 0.0;
|
||||
viewMatrix[7] = 0.0;
|
||||
viewMatrix[11] = 0.0;
|
||||
viewMatrix[15] = 1.0;
|
||||
}
|
||||
|
||||
function mat4Print(a) {
|
||||
// opengl uses column major order
|
||||
var out = "";
|
||||
for (var i = 0; i < 4; ++i) {
|
||||
for (var j = 0; j < 4; ++j) {
|
||||
out += a[j * 4 + i] + " ";
|
||||
}
|
||||
out += "\n";
|
||||
}
|
||||
alert(out);
|
||||
}
|
||||
|
||||
function mat4Transpose(a, transposed) {
|
||||
var t = 0;
|
||||
for (var i = 0; i < 4; ++i) {
|
||||
for (var j = 0; j < 4; ++j) {
|
||||
transposed[t++] = a[j * 4 + i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function mat4Invert(m, inverse) {
|
||||
var inv = new Float32Array(16);
|
||||
inv[0] = m[5]*m[10]*m[15]-m[5]*m[11]*m[14]-m[9]*m[6]*m[15]+
|
||||
m[9]*m[7]*m[14]+m[13]*m[6]*m[11]-m[13]*m[7]*m[10];
|
||||
inv[4] = -m[4]*m[10]*m[15]+m[4]*m[11]*m[14]+m[8]*m[6]*m[15]-
|
||||
m[8]*m[7]*m[14]-m[12]*m[6]*m[11]+m[12]*m[7]*m[10];
|
||||
inv[8] = m[4]*m[9]*m[15]-m[4]*m[11]*m[13]-m[8]*m[5]*m[15]+
|
||||
m[8]*m[7]*m[13]+m[12]*m[5]*m[11]-m[12]*m[7]*m[9];
|
||||
inv[12]= -m[4]*m[9]*m[14]+m[4]*m[10]*m[13]+m[8]*m[5]*m[14]-
|
||||
m[8]*m[6]*m[13]-m[12]*m[5]*m[10]+m[12]*m[6]*m[9];
|
||||
inv[1] = -m[1]*m[10]*m[15]+m[1]*m[11]*m[14]+m[9]*m[2]*m[15]-
|
||||
m[9]*m[3]*m[14]-m[13]*m[2]*m[11]+m[13]*m[3]*m[10];
|
||||
inv[5] = m[0]*m[10]*m[15]-m[0]*m[11]*m[14]-m[8]*m[2]*m[15]+
|
||||
m[8]*m[3]*m[14]+m[12]*m[2]*m[11]-m[12]*m[3]*m[10];
|
||||
inv[9] = -m[0]*m[9]*m[15]+m[0]*m[11]*m[13]+m[8]*m[1]*m[15]-
|
||||
m[8]*m[3]*m[13]-m[12]*m[1]*m[11]+m[12]*m[3]*m[9];
|
||||
inv[13]= m[0]*m[9]*m[14]-m[0]*m[10]*m[13]-m[8]*m[1]*m[14]+
|
||||
m[8]*m[2]*m[13]+m[12]*m[1]*m[10]-m[12]*m[2]*m[9];
|
||||
inv[2] = m[1]*m[6]*m[15]-m[1]*m[7]*m[14]-m[5]*m[2]*m[15]+
|
||||
m[5]*m[3]*m[14]+m[13]*m[2]*m[7]-m[13]*m[3]*m[6];
|
||||
inv[6] = -m[0]*m[6]*m[15]+m[0]*m[7]*m[14]+m[4]*m[2]*m[15]-
|
||||
m[4]*m[3]*m[14]-m[12]*m[2]*m[7]+m[12]*m[3]*m[6];
|
||||
inv[10]= m[0]*m[5]*m[15]-m[0]*m[7]*m[13]-m[4]*m[1]*m[15]+
|
||||
m[4]*m[3]*m[13]+m[12]*m[1]*m[7]-m[12]*m[3]*m[5];
|
||||
inv[14]= -m[0]*m[5]*m[14]+m[0]*m[6]*m[13]+m[4]*m[1]*m[14]-
|
||||
m[4]*m[2]*m[13]-m[12]*m[1]*m[6]+m[12]*m[2]*m[5];
|
||||
inv[3] = -m[1]*m[6]*m[11]+m[1]*m[7]*m[10]+m[5]*m[2]*m[11]-
|
||||
m[5]*m[3]*m[10]-m[9]*m[2]*m[7]+m[9]*m[3]*m[6];
|
||||
inv[7] = m[0]*m[6]*m[11]-m[0]*m[7]*m[10]-m[4]*m[2]*m[11]+
|
||||
m[4]*m[3]*m[10]+m[8]*m[2]*m[7]-m[8]*m[3]*m[6];
|
||||
inv[11]= -m[0]*m[5]*m[11]+m[0]*m[7]*m[9]+m[4]*m[1]*m[11]-
|
||||
m[4]*m[3]*m[9]-m[8]*m[1]*m[7]+m[8]*m[3]*m[5];
|
||||
inv[15]= m[0]*m[5]*m[10]-m[0]*m[6]*m[9]-m[4]*m[1]*m[10]+
|
||||
m[4]*m[2]*m[9]+m[8]*m[1]*m[6]-m[8]*m[2]*m[5];
|
||||
|
||||
var det = m[0]*inv[0]+m[1]*inv[4]+m[2]*inv[8]+m[3]*inv[12];
|
||||
if (det == 0) return false;
|
||||
det = 1.0 / det;
|
||||
for (var i = 0; i < 16; i++) inverse[i] = inv[i] * det;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user