Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL Triangle Example</title> <style> canvas { width: 400px; height: 400px; border: 1px solid black; } </style> </head> <body> <canvas id="webglCanvas"></canvas> <script> // Vertex shader program const vsSource = ` attribute vec4 aVertexPosition; void main(void) { gl_Position = aVertexPosition; } `; // Fragment shader program const fsSource = ` void main(void) { gl_FragColor = vec4(1.0, 0.5, 0.0, 1.0); // Orange color } `; // Function to create a shader, upload GLSL source code, and compile the shader function loadShader(gl, type, source) { const shader = gl.createShader(type); gl.shaderSource(shader, source); gl.compileShader(shader); if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader)); gl.deleteShader(shader); return null; } return shader; } // Function to initialize the shader program function initShaderProgram(gl, vsSource, fsSource) { const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource); const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource); const shaderProgram = gl.createProgram(); gl.attachShader(shaderProgram, vertexShader); gl.attachShader(shaderProgram, fragmentShader); gl.linkProgram(shaderProgram); if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { alert('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram)); return null; } return shaderProgram; } // Function to initialize WebGL function initWebGL() { const canvas = document.getElementById('webglCanvas'); const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl'); if (!gl) { alert('WebGL is not supported by your browser.'); return; } const shaderProgram = initShaderProgram(gl, vsSource, fsSource); const programInfo = { program: shaderProgram, attribLocations: { vertexPosition: gl.getAttribLocation(shaderProgram, 'aVertexPosition') } }; // Create a buffer for the triangle's positions. const positionBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); // Set the positions for the triangle. const positions = [ 0.0, 1.0, // Vertex 1 -1.0, -1.0, // Vertex 2 1.0, -1.0 // Vertex 3 ]; gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW); // Draw the scene function drawScene() { gl.clearColor(0.0, 0.0, 0.0, 1.0); // Clear to black, fully opaque gl.clear(gl.COLOR_BUFFER_BIT); // Tell WebGL to use our program when drawing gl.useProgram(programInfo.program); // Attach the position buffer. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); gl.vertexAttribPointer( programInfo.attribLocations.vertexPosition, 2, // Number of components per vertex attribute gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray( programInfo.attribLocations.vertexPosition); // Execute WebGL program gl.drawArrays(gl.TRIANGLES, 0, 3); } drawScene(); } // Call the initWebGL function after the document has loaded to ensure the canvas is ready. document.addEventListener("DOMContentLoaded", initWebGL); </script> </body> </html>