<!DOCTYPE html>
<html>
<head>
<title>Shadows in WebGL</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
}
h3 {
margin: 0;
}
canvas {
width: 400px;
height: 300px;
border: 1px solid black;
}
</style>
</head>
<body>
<h3>Basic Shadow with Shadow Mapping</h3>
<canvas id="webglCanvas"></canvas>
<script src=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/gl-matrix.js"></script>
<script>
const canvas = document.getElementById('webglCanvas');
const gl = canvas.getContext('webgl');
if (!gl) {
alert('WebGL not supported');
throw new Error('WebGL not supported');
}
const vertexShaderSource = `
attribute vec4 a_position;
attribute vec4 a_color;
uniform mat4 u_modelViewProjectionMatrix;
varying vec4 v_color;
void main() {
gl_Position = u_modelViewProjectionMatrix * a_position;
v_color = a_color;
}
`;
const fragmentShaderSource = `
precision mediump float;
varying vec4 v_color;
void main() {
gl_FragColor = v_color;
}
`;
function createShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.error('Shader compilation failed:',
gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
function createProgram(gl, vertexShaderSource,
fragmentShaderSource) {
const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.error('Program linking failed:', gl.getProgramInfoLog(program));
return null;
}
return program;
}
const program = createProgram(gl, vertexShaderSource,
fragmentShaderSource);
gl.useProgram(program);
const vertices = new Float32Array([
0.0, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0,
-0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 1.0,
0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 1.0
]);
const positionLocation = gl.getAttribLocation(program, 'a_position');
const colorLocation = gl.getAttribLocation(program, 'a_color');
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 28, 0);
gl.enableVertexAttribArray(colorLocation);
gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 28, 8);
const modelViewProjectionMatrix = mat4.create();
mat4.ortho(modelViewProjectionMatrix, -1, 1, -1, 1, -1, 1);
const uModelViewProjectionMatrix = gl.getUniformLocation(program,
'u_modelViewProjectionMatrix');
gl.uniformMatrix4fv(uModelViewProjectionMatrix, false,
modelViewProjectionMatrix);
function drawScene() {
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 3);
}
drawScene();
</script>
</body>
</html>