WebGL Fundamentals
Learn the core concepts and setup needed to start working with WebGL.
🚀 Start CodingWhat is WebGL?
Core Definition
- • JavaScript API for GPU-accelerated graphics
- • Works directly with GPU without plugins
- • Based on OpenGL ES 2.0/3.0 standards
Key Capabilities
- • 2D/3D rendering directly in browsers
- • Custom shader creation
- • Hardware acceleration of graphics
Getting Started with WebGL
Setup: HTML5 Canvas
<canvas id="basic-canvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById('basic-canvas');
const gl = canvas.getContext('webgl2');
if (!gl) {
alert("WebGL2 not supported in this browser!");
}
</script>
Your First WebGL Code
// Clear screen
gl.clearColor(0.1, 0.1, 0.1, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
Core Concepts
Vertex Shaders
attribute vec4 a_position;
void main() {
gl_Position = a_position;
}
Fragment Shaders
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
TheRendering Pipeline
Pipeline Overview
- 1 Define geometry in code
- 2 Vertex shader processes each vertex
- 3 Primitive assembly and rasterization
- 4 Fragment shader colors each pixel
- 5 Final output to display