<html>
<head>
<title>Learning WebGL — lesson 2</title>
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
<script type="text/javascript" src="../js/glMatrix.js"></script>
</head>
<body onload="webGLStart();">
<a href="http://learningwebgl.com/blog/?p=134"><<
Back to Lesson 2</a><br />
<canvas id="lesson02-canvas" style="border: none;" width="500"
height="500"></canvas>
<br/>
<a href="http://learningwebgl.com/blog/?p=134"><<
Back to Lesson 2</a><br /><script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec4 vColor;
void main(void) {
gl_FragColor = vColor;
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec4 aVertexColor;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec4 vColor;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
gl_PointSize=10.0;
vColor = aVertexColor;
}
</script><script type="text/javascript" id="mycode">
var gl; // Create WebGLRenderingContext
var shader; // the vertex or fragment shader program
var shaderProgram; //WebGL program to run shaders
var mvMatrix = mat4.create(); // model view matrix
var pMatrix = mat4.create(); // projection matrix
var triangleVertexPositionBuffer;
var triangleVertexColorBuffer;
var squareVertexPositionBuffer;
var squareVertexColorBuffer;
var lineVertexPositionBuffer;
var lineVertexColorBuffer;
var polyuVertexPositionBuffer;
var polyVertexColorBuffer;
var stripVertexPositionBuffer;
var stripVertexColorBuffer;
var pointVertexPositionBuffer;
var pointVertexColorBuffer;
var elVertexPositionBuffer;
var elVertexColorBuffer;
function initGL(canvas) {
try {
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} catch (e) {
}
if (!gl) {
alert("Could not initialise WebGL, sorry :-(");
}
}
function initShaders() {
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");
shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
gl.useProgram(shaderProgram);
shaderProgram.vertexPositionAttribute =
gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
shaderProgram.vertexColorAttribute =
gl.getAttribLocation(shaderProgram, "aVertexColor");
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
shaderProgram.pMatrixUniform =
gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform =
gl.getUniformLocation(shaderProgram, "uMVMatrix");
}
function getShader(gl, id) {
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;
}
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;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
}
function initBuffers() {
//
// Set up data for the points
//
pointVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, pointVertexPositionBuffer);
var vertices = [
2.5,0,0,
-2.5,0,0,
0,2.5,0,
0,-2.5,0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices),
gl.STATIC_DRAW);
pointVertexPositionBuffer.itemSize = 3;
pointVertexPositionBuffer.numItems = 4;
//
// now set up the color buffer for points
//
pointVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, pointVertexColorBuffer);
var colors=
[1,0,0,1,
0,1,0,1,
0,0,1,1,
1,1,0,1,
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors),
gl.STATIC_DRAW);
pointVertexColorBuffer.itemSize = 4;
pointVertexColorBuffer.numItems = 4;
//
// Set up data for the triangle
//
triangleVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
var vertices = [
0.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);
triangleVertexPositionBuffer.itemSize = 3;
triangleVertexPositionBuffer.numItems = 3;
//
// now set up the color buffer for triangle
//
triangleVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer);
var colors = [
1.0, 0.0, 0.0, 0.81,
0.0, 1.0, 0.0, 0.81,
0.0, 0.0, 1.0, 0.81
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors),
gl.STATIC_DRAW);
triangleVertexColorBuffer.itemSize = 4;
triangleVertexColorBuffer.numItems = 3;
//
// set up data for the ellipse
//
ellVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, ellVertexPositionBuffer);
vertices = [
1.5,0.,0.,1.49704,0.0627905,0.,1.48817,0.125333,0.,1.47343,0.187381,0.,1.45287,0.24869,0.,1.42658,0.309017,0.,1.39466,0.368125,0.,1.35724,0.425779,0.,1.31446,0.481754,0.,1.26649,0.535827,0.,1.21353,0.587785,0.,1.15577,0.637424,0.,1.09345,0.684547,0.,1.02682,0.728969,0.,0.956136,0.770513,0.,0.881678,0.809017,0.,0.80374,0.844328,0.,0.722631,0.876307,0.,0.638669,0.904827,0.,0.552187,0.929776,0.,0.463525,0.951057,0.,0.373035,0.968583,0.,0.281072,0.982287,0.,0.188,0.992115,0.,0.0941858,0.998027,0.,0.,1.,0.,-0.0941858,0.998027,0.,-0.188,0.992115,0.,-0.281072,0.982287,0.,-0.373035,0.968583,0.,-0.463525,0.951057,0.,-0.552187,0.929776,0.,-0.638669,0.904827,0.,-0.722631,0.876307,0.,-0.80374,0.844328,0.,-0.881678,0.809017,0.,-0.956136,0.770513,0.,-1.02682,0.728969,0.,-1.09345,0.684547,0.,-1.15577,0.637424,0.,-1.21353,0.587785,0.,-1.26649,0.535827,0.,-1.31446,0.481754,0.,-1.35724,0.425779,0.,-1.39466,0.368125,0.,-1.42658,0.309017,0.,-1.45287,0.24869,0.,-1.47343,0.187381,0.,-1.48817,0.125333,0.,-1.49704,0.0627905,0.,-1.5,0.,0.
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices),
gl.STATIC_DRAW);
ellVertexPositionBuffer.itemSize = 3;
ellVertexPositionBuffer.numItems = 51;
//
// set up color data for square
//
ellVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, ellVertexColorBuffer);
colors = [];
for (var i=0; i < 101; i++) {
colors = colors.concat([0.5, 0.5, 1.0, 1]);
}
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors),
gl.STATIC_DRAW);
ellVertexColorBuffer.itemSize = 4;
ellVertexColorBuffer.numItems = 51;
//
// Set up buffer for line
//
lineVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, lineVertexPositionBuffer);
vertices = [
-1,0,0,
1,0,0,
0,1,0,
0,-1,0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
lineVertexPositionBuffer.itemSize = 3;
lineVertexPositionBuffer.numItems =4;
//
// Set up color for the lines
//
lineVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, lineVertexColorBuffer);
var colors = [
1.0, 0.0, 0.0, 0.81,
0.0, 1.0, 0.0, 0.81,
0.0, 0.0, 1.0, 0.81,
0.5,0.5,0.5,.81
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors),
gl.STATIC_DRAW);
lineVertexColorBuffer.itemSize = 4;
lineVertexColorBuffer.numItems = 4;
//
// set up for polygon
//
polyVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, polyVertexPositionBuffer);
vertices = [0, 0, 0, 1., 0., 0., 0.707107, 0.707107, 0., 0., 1., 0., -0.707107,
0.707107, 0., -1., 0., 0., -0.707107, -0.707107, 0., 0., -1., 0.,
0.707107, -0.707107, 0., 1., 0., 0.
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices),
gl.STATIC_DRAW);
polyVertexPositionBuffer.itemSize = 3;
polyVertexPositionBuffer.numItems = 10;
//
// set up color data for polygon
//
polyVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, polyVertexColorBuffer);
colors = [];
for (var i=0; i < 10; i++) {
colors = colors.concat([0.5, 0.5, 1.0, 1]);
}
colors[0]=1;
colors[1]=0;
colors[2]=0;
colors[3]=1;
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors),
gl.STATIC_DRAW);
polyVertexColorBuffer.itemSize = 4;
polyVertexColorBuffer.numItems = 10;
//
// Set up data for the triangle strip
//
stripVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, stripVertexPositionBuffer);
var vertices = [
0.0, 0.0, 0.0,
0,1,0,
1,0,0,
2,1,0,
3.5,-1,0,
1,-3,0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices),
gl.STATIC_DRAW);
stripVertexPositionBuffer.itemSize = 3;
stripVertexPositionBuffer.numItems = 6;
//
// now set up the color buffer for strip
//
stripVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, stripVertexColorBuffer);
var colors = [
1.0, 0.0, 0.0, 0.81,
0.0, 1.0, 0.0, 0.81,
0.0, 0.0, 1.0, 0.81,
.5,.5,.5,1,
1,.5,.7,1,
.2,.5,.8,.7
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors),
gl.STATIC_DRAW);
stripVertexColorBuffer.itemSize = 4;
stripVertexColorBuffer.numItems = 6;
}
function drawScene() {
gl.viewport(0,0,gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);
mat4.identity(mvMatrix);
//
// draw points
//
mat4.translate(mvMatrix, [0,0,-7]);
gl.bindBuffer(gl.ARRAY_BUFFER, pointVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,
pointVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, pointVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute,
pointVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
setMatrixUniforms();
gl.drawArrays(gl.POINTS, 0, pointVertexPositionBuffer.numItems);
//
// draw line
//
mat4.translate(mvMatrix, [0,0,3]);
gl.bindBuffer(gl.ARRAY_BUFFER, lineVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,
lineVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, lineVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute,
lineVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
setMatrixUniforms();
gl.drawArrays(gl.LINES, 0, lineVertexPositionBuffer.numItems);
//
// draw triangle
//
mat4.translate(mvMatrix, [-1.5, 2.0, -7.0]);
gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,
triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute,
triangleVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
setMatrixUniforms();
gl.drawArrays(gl.TRIANGLES, 0, triangleVertexPositionBuffer.numItems);
//
// draw ellipse
//
mat4.translate(mvMatrix, [3.5, 0.0, 0]);
gl.bindBuffer(gl.ARRAY_BUFFER, ellVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,
ellVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, ellVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute,
ellVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
setMatrixUniforms();
gl.drawArrays(gl.LINE_LOOP, 0, ellVertexPositionBuffer.numItems);
//
// draw octagon
//
mat4.translate(mvMatrix, [0, -3.5, 0]);
gl.bindBuffer(gl.ARRAY_BUFFER, polyVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,
polyVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, polyVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute,
polyVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
setMatrixUniforms();
gl.drawArrays(gl.TRIANGLE_FAN, 0, polyVertexPositionBuffer.numItems);
//
// draw triangle strip
//
mat4.translate(mvMatrix, [-6, 0, -5]);
gl.bindBuffer(gl.ARRAY_BUFFER, stripVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,
stripVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, stripVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute,
stripVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
setMatrixUniforms();
gl.drawArrays(gl.TRIANGLE_STRIP, 0, stripVertexPositionBuffer.numItems); }
function webGLStart() {
var canvas = document.getElementById("lesson02-canvas");
initGL(canvas);
initShaders();
initBuffers();
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);
drawScene();
}
</script>
</body>
</html>