<!doctype html>
<head>
<title>cube5</title>
<script src="minMatrix.js"></script>
</head>
<body>
<canvas id="canvas" width="640" height="480"></canvas>
<div id="msg"></div>
<p>
move light: cursor key
</p>
<script id="vs" type="text/x-vertex">
// 頂点シェイダー
attribute vec3 position;
attribute vec3 normal;
attribute vec4 color;
uniform mat4 mvpMatrix;
uniform mat4 invMatrix;
uniform vec3 direction;
uniform vec4 ambientLightColor;
varying vec4 vColor;
void main(void) {
vec3 invLight = normalize(invMatrix * vec4(direction, 0.0)).xyz;
float diffuse = clamp(dot(normal, invLight), 0.2, 1.0);
vColor = color * vec4(vec3(diffuse), 1.0) + ambientLightColor;
gl_Position = mvpMatrix * vec4(position, 1.0);
}
</script>
<script id="fs" type="text/x-fragment">
// 断片シェイダー
precision mediump float;
varying vec4 vColor;
void main(void) {
gl_FragColor = vColor;
}
</script>
<script>
function Keyboard() {
var _keys = [];
this.isKeyDown = function(keyCode) {
return _keys[keyCode];
};
document.addEventListener("keydown", function(event) {
_keys[event.keyCode] = true;
});
document.addEventListener("keyup", function(event) {
_keys[event.keyCode] = false;
});
}
var keyboard = new Keyboard();
var gl; // webgl context
var mat = new matIV();
var pMatrix = mat.create(); // perspective
var indexCount;
var loc = {};
var lat = 0; // 緯度
var lon = 90; // 経度
var angle = 0;
var fps;
var fpsCount;
var secSave;
onload = function() {
gl = canvas.getContext("experimental-webgl");
var vShader = createShader("vs");
var fShader = createShader("fs");
var program = createProgram(vShader, fShader);
gl.useProgram(program);
var model = cube();
var vbo = createVbo(model.positions);
setAttribute(program, "position", vbo, 3);
var vbo = createVbo(model.normals);
setAttribute(program, "normal", vbo, 3);
var vbo = createVbo(model.colors);
setAttribute(program, "color", vbo, 4);
var ibo = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Int16Array(model.indices), gl.STATIC_DRAW);
indexCount = model.indices.length;
loc.mvpMatrix = gl.getUniformLocation(program, "mvpMatrix");
loc.invMatrix = gl.getUniformLocation(program, "invMatrix");
loc.direction = gl.getUniformLocation(program, "direction");
loc.ambientLightColor = gl.getUniformLocation(program, "ambientLightColor");
gl.enable(gl.CULL_FACE);
gl.frontFace(gl.CW);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
var ambientLightColor = [0.1, 0.1, 0.1, 1.0];
gl.uniform4fv(loc.ambientLightColor, ambientLightColor);
mat.perspective(45, canvas.width / canvas.height, 0.1, 10, pMatrix);
draw();
};
function draw() {
if (keyboard.isKeyDown(37)) lon = (lon + 2) % 360; // Left
if (keyboard.isKeyDown(38)) lat = Math.min(lat + 1, 90); // Up
if (keyboard.isKeyDown(39)) lon = (lon + 358) % 360; // Right
if (keyboard.isKeyDown(40)) lat = Math.max(lat - 1, -90); // Down
var sec = (new Date()).getSeconds();
if (secSave != sec) {
fps = fpsCount;
fpsCount = 0;
secSave = sec;
}
fpsCount++;
angle = (angle + 1) % 360;
msg.innerHTML = "lat:" + lat + " lon:" + lon + " fps:" + fps + " angle:" + angle;
// clear
gl.clearColor(0x64 / 0xff, 0x95 / 0xff, 0xed / 0xff, 1.0);
gl.clearDepth(1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// camera
var vMatrix = mat.create();
var vpMatrix = mat.create();
var mvpMatrix = mat.create();
mat.lookAt([0, 1, 3], [0, 0, 0], [0, 1, 0], vMatrix);
mat.multiply(pMatrix, vMatrix, vpMatrix);
// light
var rad = lat * Math.PI / 180;
var r = Math.cos(rad);
var y = Math.sin(rad);
var rad = lon * Math.PI / 180;
var x = Math.cos(rad) * r;
var z = Math.sin(rad) * r;
var direction = [x, y, z];
gl.uniform3fv(loc.direction, direction);
// model
var rad = angle * Math.PI / 180;
var mMatrix = mat.identity(mat.create());
mat.rotate(mMatrix, rad, [0, 1, 0], mMatrix);
var invMatrix = mat.create();
mat.inverse(mMatrix, invMatrix);
mat.multiply(vpMatrix, mMatrix, mvpMatrix);
gl.uniformMatrix4fv(loc.mvpMatrix, false, mvpMatrix);
gl.uniformMatrix4fv(loc.invMatrix, false, invMatrix);
gl.drawElements(gl.TRIANGLES, indexCount, gl.UNSIGNED_SHORT, 0);
gl.flush();
setTimeout(draw, 1000 / 30);
}
function createShader(id) {
var element = document.getElementById(id);
if (! element) return;
const types = {
"text/x-vertex": gl.VERTEX_SHADER,
"text/x-fragment": gl.FRAGMENT_SHADER,
};
var shader = gl.createShader(types[element.type]);
gl.shaderSource(shader, element.text);
gl.compileShader(shader);
if (! gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return;
}
return shader;
}
function createProgram(vs, fs) {
var program = gl.createProgram();
gl.attachShader(program, vs);
gl.attachShader(program, fs);
gl.linkProgram(program);
if (! gl.getProgramParameter(program, gl.LINK_STATUS)) {
alert(gl.getProgramInfoLog(program));
return;
}
return program;
}
function createVbo(data) {
var vbo = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
return vbo;
}
function setAttribute(program, name, vbo, stride) {
var location = gl.getAttribLocation(program, name);
gl.enableVertexAttribArray(location);
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
gl.vertexAttribPointer(location, stride, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
}
function cube() {
var x = 0.5;
var positions = [
x, -x, x, // x+
x, x, x,
x, x, -x,
x, -x, -x,
x, x, -x, // y+
x, x, x,
-x, x, x,
-x, x, -x,
-x, x, x, // z+
x, x, x,
x, -x, x,
-x, -x, x,
-x, x, x, // x-
-x, -x, x,
-x, -x, -x,
-x, x, -x,
x, -x, x, // y-
x, -x, -x,
-x, -x, -x,
-x, -x, x,
x, x, -x, // z-
-x, x, -x,
-x, -x, -x,
x, -x, -x,
];
var normals = [];
var colors = [];
for (var i = 0; i < 4; i++) { // x+
normals.push(1, 0, 0);
colors.push(1, 0, 0, 1);
}
for (var i = 0; i < 4; i++) { // y+
normals.push(0, 1, 0);
colors.push(0, 1, 0, 1);
}
for (var i = 0; i < 4; i++) { // z+
normals.push(0, 0, 1);
colors.push(0, 0, 1, 1);
}
for (var i = 0; i < 4; i++) { // x-
normals.push(-1, 0, 0);
colors.push(0, 1, 1, 1);
}
for (var i = 0; i < 4; i++) { // y-
normals.push(0, -1, 0);
colors.push(1, 0, 1, 1);
}
for (var i = 0; i < 4; i++) { // z-
normals.push(0, 0, -1);
colors.push(1, 1, 0, 1);
}
var indices = [];
for (var i = 0; i < 24; i += 4) {
indices.push(i, i+1, i+2, i+2, i+3, i);
}
return {"positions":positions, "normals":normals, "colors":colors, "indices":indices};
}
</script>
</body>