Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const canvas = document.getElementById('glCanvas');
+ const gl = canvas.getContext('webgl');
+
+ if (!gl) {
+ alert('WebGL not supported');
+ return;
+ }
+
+ // Vertex shader program
+ const vsSource = `
+ attribute vec4 aVertexPosition;
+ attribute vec4 aVertexColor;
+ uniform mat4 uModelViewMatrix;
+ uniform mat4 uProjectionMatrix;
+ varying lowp vec4 vColor;
+ void main() {
+ gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
+ vColor = aVertexColor;
+ }
+ `;
+
+ // Fragment shader program
+ const fsSource = `
+ varying lowp vec4 vColor;
+ void main() {
+ gl_FragColor = vColor;
+ }
+ `;
+
+ // Initialize shaders
+ function initShaderProgram(gl, vs, fs) {
+ const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vs);
+ const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fs);
+
+ const shaderProgram = gl.createProgram();
+ gl.attachShader(shaderProgram, vertexShader);
+ gl.attachShader(shaderProgram, fragmentShader);
+ gl.linkProgram(shaderProgram);
+
+ if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
+ console.error('Shader program initialization failed');
+ return null;
+ }
+
+ return shaderProgram;
+ }
+
+ function loadShader(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.deleteShader(shader);
+ return null;
+ }
+
+ return shader;
+ }
+
+ // Initialize buffers and create cube
+ let rotation = [0, 0, 0];
+ let zoom = -6.0;
+ let isDragging = false;
+ let lastMouseX = 0;
+ let lastMouseY = 0;
+
+ function initBuffers(gl) {
+ const positions = [
+ // Front face
+ -1.0, -1.0, 1.0,
+ 1.0, -1.0, 1.0,
+ 1.0, 1.0, 1.0,
+ -1.0, 1.0, 1.0,
+ // Back face
+ -1.0, -1.0, -1.0,
+ -1.0, 1.0, -1.0,
+ 1.0, 1.0, -1.0,
+ 1.0, -1.0, -1.0,
+ ];
+
+ const colors = [
+ 1.0, 0.0, 1.0, 1.0, // Magenta
+ 1.0, 1.0, 0.0, 1.0, // Yellow
+ 0.0, 1.0, 1.0, 1.0, // Cyan
+ 1.0, 1.0, 1.0, 1.0, // White
+ 0.0, 0.0, 0.0, 1.0, // Black
+ 1.0, 0.0, 0.0, 1.0, // Red
+ 0.0, 1.0, 0.0, 1.0, // Green
+ 0.0, 0.0, 1.0, 1.0, // Blue
+ ];
+
+ const indices = [
+ 0, 1, 2, 0, 2, 3, // front
+ 4, 5, 6, 4, 6, 7, // back
+ 0, 4, 7, 0, 7, 1, // bottom
+ 3, 2, 6, 3, 6, 5, // top
+ 0, 3, 5, 0, 5, 4, // left
+ 1, 7, 6, 1, 6, 2, // right
+ ];
+
+ return {
+ position: createBuffer(gl, positions),
+ color: createBuffer(gl, colors),
+ indices: createIndexBuffer(gl, indices),
+ count: indices.length,
+ };
+ }
+
+ function createBuffer(gl, data) {
+ const buffer = gl.createBuffer();
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);
+ return buffer;
+ }
+
+ function createIndexBuffer(gl, data) {
+ const buffer = gl.createBuffer();
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(data), gl.STATIC_DRAW);
+ return buffer;
+ }
+
+ // Event listeners for mouse interaction
+ canvas.addEventListener('mousedown', (e) => {
+ isDragging = true;
+ lastMouseX = e.clientX;
+ lastMouseY = e.clientY;
+ });
+
+ document.addEventListener('mouseup', () => {
+ isDragging = false;
+ });
+
+ document.addEventListener('mousemove', (e) => {
+ if (!isDragging) return;
+
+ const deltaX = e.clientX - lastMouseX;
+ const deltaY = e.clientY - lastMouseY;
+
+ rotation[0] += deltaY * 0.01;
+ rotation[1] += deltaX * 0.01;
+
+ lastMouseX = e.clientX;
+ lastMouseY = e.clientY;
+ });
+
+ canvas.addEventListener('wheel', (e) => {
+ e.preventDefault();
+ zoom += e.deltaY * 0.01;
+ zoom = Math.max(-10, Math.min(-3, zoom));
+ });
+
+ // Main render loop
+ function render() {
+ resizeCanvas();
+ drawScene();
+ requestAnimationFrame(render);
+ }
+
+ function resizeCanvas() {
+ const displayWidth = canvas.clientWidth;
+ const displayHeight = canvas.clientHeight;
+
+ if (canvas.width !== displayWidth || canvas.height !== displayHeight) {
+ canvas.width = displayWidth;
+ canvas.height = displayHeight;
+ gl.viewport(0, 0, canvas.width, canvas.height);
+ }
+ }
+
+ // Initialize and start rendering
+ const shaderProgram = initShaderProgram(gl, vsSource, fsSource);
+ const buffers = initBuffers(gl);
+
+ render();
+ });