forked from skjApp/HTML5-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebGLBasic2.htm
67 lines (51 loc) · 1.83 KB
/
WebGLBasic2.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!-- Code based on http://dev.opera.com/articles/view/raw-webgl-part1-getting-started/ -->
<!DOCTYPE HTML>
<html>
<head>
<title>WebGL Basic 2</title>
</head>
<body>
<canvas id='canvas1'>
</canvas>
<script>
var canvas1 = document.getElementById('canvas1');
var webGL = canvas1.getContext('experimental-webgl');
// STEP 1 : DRAWING A VERTEX BUFFER
var vertexPosBuffer = webGL.createBuffer() ;
webGL.bindBuffer(webGL.ARRAY_BUFFER, vertexPosBuffer);
var vertices = [];
// These 3 coordinates represent points on a Cartesian plane
// These are 2D coordinates
var vertices = [-1, 1, 0, 0, 1, 1];
webGL.bufferData(webGL.ARRAY_BUFFER, new Float32Array(vertices), webGL.STATIC_DRAW);
// STEP 2 : VERTEX SHADER
var vs = 'attribute vec2 pos;' + 'void main() { gl_Position = vec4(pos, 0, 1); }';
// STEP 3 : FRAGMENT SHADER
var fs = 'precision mediump float;' + 'void main() { gl_FragColor = vec4(0,0.8,0,1); }';
// CREATING A PROGRAM
var program = createProgram(vs,fs);
webGL.useProgram(program);
program.vertexPosAttrib = webGL.getAttribLocation(program, 'pos');
webGL.enableVertexAttribArray(program.vertexPosAttrib);
webGL.vertexAttribPointer(program.vertexPosAttrib, 2, webGL.FLOAT, false, 0, 0);
webGL.drawArrays(webGL.TRIANGLES, 0, 3);
function createProgram(vstr, fstr)
{
var program = webGL.createProgram();
var vshader = createShader(vstr, webGL.VERTEX_SHADER);
var fshader = createShader(fstr, webGL.FRAGMENT_SHADER);
webGL.attachShader(program, vshader);
webGL.attachShader(program, fshader);
webGL.linkProgram(program);
return program;
}
function createShader(str, type)
{
var shader = webGL.createShader(type);
webGL.shaderSource(shader, str);
webGL.compileShader(shader);
return shader;
}
</script>
</body>
</html>