2022-10-22 18:08:26 +00:00
|
|
|
"use strict";
|
|
|
|
(() => {
|
2022-10-23 18:36:16 +00:00
|
|
|
const makeCylinder = (meridians = 3, parallels = 3) => {
|
|
|
|
// The number of meridians dividing the sphere's surface.
|
|
|
|
// Two meridians would make an XZ plane.
|
|
|
|
if (meridians < 3) {
|
|
|
|
throw "meridians must be ≥ 3";
|
|
|
|
}
|
|
|
|
|
|
|
|
// The number of parallels, including the poles, dividing the sphere's surface.
|
|
|
|
// Two parallels would make a vertical line segment.
|
|
|
|
if (parallels < 3) {
|
2022-10-23 21:43:13 +00:00
|
|
|
// throw "parallels must be ≥ 3";
|
2022-10-23 18:36:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 00:51:27 +00:00
|
|
|
const radius = 1;
|
2022-10-23 22:21:08 +00:00
|
|
|
|
|
|
|
// The seam needs two of each vertex so we can map the texture correctly.
|
|
|
|
const vertices = new Float32Array((meridians + 1) * parallels * 5);
|
|
|
|
const vertexStride = 5 * vertices.constructor.BYTES_PER_ELEMENT;
|
|
|
|
const positionOffset = 0;
|
|
|
|
const textureCoordOffset = 3 * vertices.constructor.BYTES_PER_ELEMENT;
|
|
|
|
let verticesIdx = 0;
|
2022-10-23 18:36:16 +00:00
|
|
|
for (let p = 0; p < parallels; p++) {
|
2022-10-24 00:51:27 +00:00
|
|
|
const lat = Math.PI * (p / (parallels - 1) - 0.5);
|
|
|
|
const y = Math.sin(lat);
|
2022-10-23 22:21:08 +00:00
|
|
|
for (let m = 0; m < meridians + 1; m++) {
|
2022-10-24 00:51:27 +00:00
|
|
|
const long = m * 2 * Math.PI / meridians;
|
|
|
|
const x = Math.cos(lat) * Math.sin(long) * radius;
|
|
|
|
const z = Math.cos(lat) * Math.cos(long) * radius;
|
2022-10-23 22:21:08 +00:00
|
|
|
const u = m / meridians;
|
|
|
|
const v = p / (parallels - 1);
|
|
|
|
|
|
|
|
vertices[verticesIdx++] = x;
|
|
|
|
vertices[verticesIdx++] = y;
|
|
|
|
vertices[verticesIdx++] = z;
|
|
|
|
vertices[verticesIdx++] = u;
|
|
|
|
vertices[verticesIdx++] = v;
|
2022-10-23 18:36:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const indices = new Uint16Array(meridians * (parallels - 1) * 6);
|
|
|
|
let indicesIdx = 0;
|
|
|
|
for (let p = 0; p < parallels - 1; p++) {
|
|
|
|
for (let m = 0; m < meridians; m++) {
|
2022-10-23 22:21:08 +00:00
|
|
|
const lowerLeft = p * (meridians + 1) + m;
|
|
|
|
const lowerRight = p * (meridians + 1) + (m + 1);
|
|
|
|
const upperLeft = (p + 1) * (meridians + 1) + m;
|
|
|
|
const upperRight = (p + 1) * (meridians + 1) + (m + 1);
|
2022-10-23 18:36:16 +00:00
|
|
|
const currIndices = [
|
|
|
|
// Lower triangle
|
|
|
|
lowerLeft, lowerRight, upperRight,
|
|
|
|
|
|
|
|
// Upper triangle
|
|
|
|
lowerLeft, upperRight, upperLeft,
|
|
|
|
];
|
|
|
|
for (const index of currIndices) {
|
|
|
|
indices[indicesIdx++] = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 21:43:13 +00:00
|
|
|
return {
|
2022-10-23 22:21:08 +00:00
|
|
|
vertices, indices,
|
|
|
|
vertexStride, positionOffset, textureCoordOffset,
|
2022-10-23 21:43:13 +00:00
|
|
|
};
|
2022-10-23 18:36:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 19:41:00 +00:00
|
|
|
const initShaderProgram = (gl) => {
|
2022-10-22 18:08:26 +00:00
|
|
|
const vShaderSrc = `
|
|
|
|
attribute vec3 position;
|
2022-10-23 15:10:11 +00:00
|
|
|
attribute vec2 textureCoord;
|
2022-10-22 18:08:26 +00:00
|
|
|
|
2022-10-23 15:10:11 +00:00
|
|
|
uniform mat4 modelMatrix;
|
2022-10-24 05:30:15 +00:00
|
|
|
uniform mat4 projectionMatrix;
|
|
|
|
// uniform mat4 viewMatrix;
|
2022-10-23 15:10:11 +00:00
|
|
|
|
|
|
|
varying highp vec2 vTextureCoord;
|
2022-10-22 19:41:00 +00:00
|
|
|
|
2022-10-22 18:08:26 +00:00
|
|
|
void main() {
|
2022-10-24 05:30:15 +00:00
|
|
|
gl_Position = projectionMatrix * modelMatrix * vec4(position, 1);
|
2022-10-23 15:10:11 +00:00
|
|
|
vTextureCoord = textureCoord;
|
2022-10-22 18:08:26 +00:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const fShaderSrc = `
|
2022-10-23 15:10:11 +00:00
|
|
|
varying highp vec2 vTextureCoord;
|
|
|
|
uniform sampler2D sampler;
|
|
|
|
|
2022-10-22 18:08:26 +00:00
|
|
|
void main() {
|
2022-10-24 00:51:27 +00:00
|
|
|
gl_FragColor = texture2D(sampler, vTextureCoord);
|
2022-10-22 18:08:26 +00:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const compileShader = (src, type) => {
|
|
|
|
let shader = gl.createShader(type);
|
|
|
|
gl.shaderSource(shader, src);
|
|
|
|
gl.compileShader(shader);
|
|
|
|
let success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
|
|
|
|
if (!success) {
|
2022-10-23 15:10:11 +00:00
|
|
|
throw gl.getShaderInfoLog(shader);
|
2022-10-22 18:08:26 +00:00
|
|
|
}
|
|
|
|
return shader;
|
2022-10-22 15:53:10 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 18:08:26 +00:00
|
|
|
let vShader = compileShader(vShaderSrc, gl.VERTEX_SHADER);
|
|
|
|
let fShader = compileShader(fShaderSrc, gl.FRAGMENT_SHADER);
|
2022-10-22 15:53:10 +00:00
|
|
|
|
2022-10-22 18:08:26 +00:00
|
|
|
let program = gl.createProgram();
|
|
|
|
gl.attachShader(program, vShader);
|
|
|
|
gl.attachShader(program, fShader);
|
|
|
|
gl.linkProgram(program);
|
|
|
|
let success = gl.getProgramParameter(program, gl.LINK_STATUS);
|
2022-10-22 15:53:10 +00:00
|
|
|
if (!success) {
|
2022-10-22 18:08:26 +00:00
|
|
|
throw gl.getProgramInfoLog(program);
|
2022-10-22 15:53:10 +00:00
|
|
|
}
|
2022-10-22 19:41:00 +00:00
|
|
|
return program;
|
|
|
|
};
|
|
|
|
|
2022-10-23 15:10:11 +00:00
|
|
|
const loadTexture = (gl, image, textureUnit = 0) => {
|
|
|
|
const textureUnitName = `TEXTURE${textureUnit}`
|
|
|
|
const texture = gl.createTexture();
|
|
|
|
const samplerLocation = gl.getUniformLocation(program, "sampler");
|
|
|
|
image.addEventListener("load", () => {
|
|
|
|
gl.activeTexture(gl[textureUnitName]);
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
|
|
gl.uniform1i(samplerLocation, textureUnit);
|
|
|
|
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
2022-10-23 21:43:13 +00:00
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
2022-10-23 15:10:11 +00:00
|
|
|
});
|
|
|
|
return texture;
|
|
|
|
};
|
|
|
|
|
2022-10-22 19:41:00 +00:00
|
|
|
let viewer = document.getElementById("viewer");
|
2022-10-24 00:59:52 +00:00
|
|
|
let gl = viewer.getContext("webgl2");
|
2022-10-22 19:41:00 +00:00
|
|
|
let program = initShaderProgram(gl);
|
2022-10-23 15:10:11 +00:00
|
|
|
let modelMatrixLocation = gl.getUniformLocation(program, "modelMatrix");
|
2022-10-24 05:30:15 +00:00
|
|
|
let projectionMatrixLocation = gl.getUniformLocation(program, "projectionMatrix");
|
|
|
|
const viewMatrixLocation = gl.getUniformLocation(program, "viewMatrix");
|
2022-10-22 19:41:00 +00:00
|
|
|
|
2022-10-22 21:32:28 +00:00
|
|
|
// Set up mouse-panning state machine.
|
|
|
|
let θy = 0;
|
2022-10-23 21:43:13 +00:00
|
|
|
let θx = 0;
|
2022-10-22 21:32:28 +00:00
|
|
|
const PanState = Object.freeze({
|
|
|
|
idle: (event) => {
|
|
|
|
if (event.type === "mousedown") {
|
|
|
|
return PanState.panning;
|
|
|
|
}
|
|
|
|
return PanState.idle;
|
|
|
|
},
|
|
|
|
panning: (event) => {
|
|
|
|
switch (event.type) {
|
|
|
|
case "mouseup":
|
|
|
|
return PanState.idle;
|
|
|
|
case "mouseover":
|
|
|
|
if ((event.buttons & 1) == 0) {
|
|
|
|
return PanState.idle;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "mousemove":
|
2022-10-24 00:51:27 +00:00
|
|
|
θy += event.movementX * 0.005;
|
|
|
|
θx += event.movementY * 0.005;
|
2022-10-24 04:40:08 +00:00
|
|
|
if (θx < -Math.PI / 2) {
|
|
|
|
θx = -Math.PI / 2;
|
|
|
|
} else if (θx > Math.PI / 2) {
|
|
|
|
θx = Math.PI / 2;
|
|
|
|
}
|
2022-10-22 21:32:28 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
return PanState.panning;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const runPanState = (mouseEvent) => {
|
|
|
|
panState = panState(mouseEvent);
|
|
|
|
}
|
|
|
|
var panState = PanState.idle;
|
|
|
|
viewer.addEventListener("mousedown", runPanState);
|
|
|
|
viewer.addEventListener("mouseup", runPanState);
|
|
|
|
viewer.addEventListener("mouseover", runPanState);
|
|
|
|
viewer.addEventListener("mousemove", runPanState);
|
|
|
|
|
2022-10-24 17:38:30 +00:00
|
|
|
const minZoom = 1;
|
|
|
|
let zoom = 3;
|
|
|
|
viewer.addEventListener("wheel", (evt) => {
|
|
|
|
console.log(evt.deltaY);
|
2024-07-22 21:18:56 +00:00
|
|
|
zoom = Math.max(zoom * (1 - evt.deltaY * 0.005), minZoom);
|
2022-10-24 17:38:30 +00:00
|
|
|
evt.preventDefault();
|
|
|
|
});
|
|
|
|
|
2022-10-23 15:56:44 +00:00
|
|
|
gl.useProgram(program);
|
2022-10-24 00:51:27 +00:00
|
|
|
const cylinder = makeCylinder(24, 24);
|
2022-10-23 21:43:13 +00:00
|
|
|
|
2022-10-23 22:21:08 +00:00
|
|
|
let vertexBuffer = gl.createBuffer();
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
|
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, cylinder.vertices, gl.STATIC_DRAW);
|
2022-10-23 15:56:44 +00:00
|
|
|
const positionLocation = gl.getAttribLocation(program, "position");
|
2022-10-23 16:03:32 +00:00
|
|
|
const textureCoordLocation = gl.getAttribLocation(program, "textureCoord");
|
2022-10-23 15:56:44 +00:00
|
|
|
|
2022-10-23 16:17:35 +00:00
|
|
|
let indexBuffer = gl.createBuffer();
|
|
|
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
|
2022-10-23 21:43:13 +00:00
|
|
|
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, cylinder.indices, gl.STATIC_DRAW);
|
|
|
|
|
|
|
|
gl.enable(gl.CULL_FACE);
|
2022-10-23 16:17:35 +00:00
|
|
|
|
2022-10-23 15:10:11 +00:00
|
|
|
// Load texture
|
|
|
|
let image = document.getElementById("image");
|
2022-10-23 15:56:44 +00:00
|
|
|
loadTexture(gl, image);
|
2022-10-23 15:10:11 +00:00
|
|
|
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
|
|
|
|
|
2022-10-22 21:32:28 +00:00
|
|
|
const render = () => {
|
2022-10-22 18:08:26 +00:00
|
|
|
gl.useProgram(program);
|
|
|
|
|
2022-10-22 19:41:00 +00:00
|
|
|
// Create the transformation matrix.
|
2022-10-24 05:30:15 +00:00
|
|
|
const modelMatrix = mat4.create();
|
2022-10-23 15:10:11 +00:00
|
|
|
mat4.rotateX(modelMatrix, modelMatrix, θx);
|
|
|
|
mat4.rotateY(modelMatrix, modelMatrix, θy);
|
|
|
|
gl.uniformMatrix4fv(modelMatrixLocation, false, modelMatrix);
|
2022-10-22 19:41:00 +00:00
|
|
|
|
2022-10-24 05:30:15 +00:00
|
|
|
const aspect = gl.drawingBufferWidth / gl.drawingBufferHeight;
|
|
|
|
const projectionMatrix = mat4.ortho(mat4.create(), -aspect / zoom, aspect / zoom, -1 / zoom, 1 / zoom, -1, 1);
|
|
|
|
gl.uniformMatrix4fv(projectionMatrixLocation, false, projectionMatrix);
|
|
|
|
|
|
|
|
// const viewMatrix = mat4.lookAt(mat4.create(), [0, 0, 0], [0, 0, 1], [0, 1, 0]);
|
|
|
|
// gl.uniformMatrix4fv(viewMatrixLocation, false, viewMatrix);
|
|
|
|
|
2022-10-23 22:21:08 +00:00
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
|
2022-10-22 18:08:26 +00:00
|
|
|
gl.enableVertexAttribArray(positionLocation);
|
2022-10-23 22:21:08 +00:00
|
|
|
gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, cylinder.vertexStride, cylinder.positionOffset);
|
2022-10-23 15:56:44 +00:00
|
|
|
gl.enableVertexAttribArray(textureCoordLocation);
|
2022-10-23 22:21:08 +00:00
|
|
|
gl.vertexAttribPointer(textureCoordLocation, 2, gl.FLOAT, false, cylinder.vertexStride, cylinder.textureCoordOffset);
|
2022-10-23 15:56:44 +00:00
|
|
|
|
2022-10-23 21:43:13 +00:00
|
|
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
|
2022-10-23 22:21:08 +00:00
|
|
|
gl.drawElements(gl.TRIANGLES, cylinder.indices.length, gl.UNSIGNED_SHORT, 0);
|
2022-10-22 19:41:00 +00:00
|
|
|
requestAnimationFrame(render);
|
2022-10-22 18:08:26 +00:00
|
|
|
};
|
2022-10-22 19:41:00 +00:00
|
|
|
|
|
|
|
requestAnimationFrame(render);
|
2022-10-22 18:08:26 +00:00
|
|
|
})();
|