<strong></strong><html>
<head>
<title>Learning WebGL
— lesson 1
</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="glMatrix-0.9.5.min.js"></script>
<script id="shader-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
void main(void) {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
//passed in from the gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute...)
attribute vec3 aVertexPosition;
//set by setMatrixUniforms()
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
}
</script>
<script type="text/javascript">
//GL Object
var gl;
function initGL(canvas) {
try {
//Initiate the webGL context
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} catch (e) {
}
if (!gl) {
alert("Could not initialise WebGL, sorry :-(");
}
}
function getShader(gl, id) {
//pass in the script
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
//create the shader object
var shader;
//assign the shader object
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
//append the source
gl.shaderSource(shader, str);
//compile the shader
gl.compileShader(shader);
//check for errors
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
//Create an object to hold the shader program
var shaderProgram;
function initShaders() {
//read in, compile, and load the shaders into javascript objects
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");
//Put a program object into the javascript shaderProgram container
shaderProgram = gl.createProgram();
//attach the compiled shaders to the shaderProgram
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
//Link the shader program
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
//designate the shader program to be active
gl.useProgram(shaderProgram);
//setup a pointer to the location of the vertex position vector
//declared by the vertex shader
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
//make the array available
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
//setup pointers to the uniform variables in the vertex shader
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
}
//Create the model view matrix
var mvMatrix = mat4.create();
//Create the projection matrix
var pMatrix = mat4.create();
//pass in the projection and modelview matrices into the vertex shader as uniforms
function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
}
//instantiate objects to hold the buffers for the geometry
var triangleVertexPositionBuffer;
var squareVertexPositionBuffer;
//fill in the buffers
function initBuffers() {
//create the triangle buffer
triangleVertexPositionBuffer = gl.createBuffer();
//bind the buffer
gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
//create the vertices for the geometry
var vertices = [
0.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0
];
//put the vertices in the buffer
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
//add an integer to the object to represent the number of dimensions for a vertex
triangleVertexPositionBuffer.itemSize = 3;
//add an integer to the object to represent the number of vertices
triangleVertexPositionBuffer.numItems = 3;
//repeat for the square
squareVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
vertices = [
1.0, 1.0, 0.0,
-1.0, 1.0, 0.0,
1.0, -1.0, 0.0,
-1.0, -1.0, 0.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
squareVertexPositionBuffer.itemSize = 3;
squareVertexPositionBuffer.numItems = 4;
}
function drawScene() {
//set the viewport
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
//clear the depth and color buffers
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
//set the perspective using the projection matrix
mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);
//set the identity matrix to the modelview matrix
mat4.identity(mvMatrix);
//translate the camera
mat4.translate(mvMatrix, [-1.5, 0.0, -7.0]);
//DRAW THE TRIANGLE
//bind the triangle buffer
gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
//put the values from the triangle buffer into the vertex position attribute variable in the vertex shader
//TODO: LOOK THIS FUNCTION UP
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
//send the modelview and projection matrices to the vertex shader
setMatrixUniforms();
//draw what was sent to the shaders
gl.drawArrays(gl.TRIANGLES, 0, triangleVertexPositionBuffer.numItems);
//repeat for the square
mat4.translate(mvMatrix, [3.0, 0.0, 0.0]);
gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
setMatrixUniforms();
gl.drawArrays(gl.TRIANGLE_STRIP, 0, squareVertexPositionBuffer.numItems);
}
//Main function to push through the WebGL code
function webGLStart() {
//Create a canvas object from the HTML5 canvas tag in the body
var canvas = document.getElementById("lesson01-canvas");
//Pass the canvas object to initialize WebGL
initGL(canvas);
//Create the shaders
initShaders();
//Create the buffers and fill in the geometry
initBuffers();
//Clear the background to black
gl.clearColor(0.0, 0.0, 0.0, 1.0);
//Occlude for depth
gl.enable(gl.DEPTH_TEST);
//Draw everything
drawScene();
}
</script>
</head>
<body onload="webGLStart();">
<canvas id="lesson01-canvas" style="border: none;" width="500" height="500"></canvas>
</body>
</html>