-
Notifications
You must be signed in to change notification settings - Fork 0
/
example1.html
127 lines (99 loc) · 3.35 KB
/
example1.html
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width">
<title></title>
<style> html, body, canvas { display: block; position: relative; margin: 0; padding: 0; } </style>
</head>
<body onload='init()'>
<canvas id='maincanvas' />
</body>
<script type="text/javascript" src="shared.js"></script>
<script type="text/glsl" id='vertShader'>
precision highp float; // Not necessary, but makes it explicit
attribute vec4 a_position;
attribute vec4 a_color;
varying vec4 v_color;
void main() {
gl_Position = a_position;
v_color = a_color;
}
</script>
<script type="text/glsl" id='fragShader'>
precision highp float;
varying vec4 v_color;
void main() {
gl_FragColor = v_color;
}
</script>
<script>
'use strict';
// Be forewarned: OpenGL LOVES global variables and state
const gl = document.getElementById('maincanvas').getContext('webgl');
const exampleShader = compileShaderProgram('vertShader', 'fragShader');
const triangleBuffer = TrianglePositions();
const triangleColors = TriangleColors();
function setup() {
let canvasWidth = window.innerWidth;
let canvasHeight = window.innerHeight;
gl.canvas.setAttribute('width', canvasWidth);
gl.canvas.setAttribute('height', canvasHeight);
gl.viewport(0, 0, canvasWidth, canvasHeight);
gl.clearColor(0, 0, 0, 1);
gl.enable(gl.CULL_FACE);
}
function draw() {
gl.clear(gl.COLOR_BUFFER_BIT);
gl.useProgram(exampleShader);
let posLoc = gl.getAttribLocation(exampleShader, 'a_position');
gl.enableVertexAttribArray(posLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, triangleBuffer);
gl.vertexAttribPointer(posLoc, 4, gl.FLOAT, false, 0, 0);
// gl.vertexAttribPointer(posLoc, 4, gl.FLOAT, false, 4 * Float32Array.BYTES_PER_ELEMENT, 0);
let colorLoc = gl.getAttribLocation(exampleShader, 'a_color');
gl.enableVertexAttribArray(colorLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, triangleColors);
gl.vertexAttribPointer(colorLoc, 4, gl.FLOAT, false, 0, 0);
// gl.vertexAttribPointer(colorLoc, 4, gl.FLOAT, false, 4 * Float32Array.BYTES_PER_ELEMENT, 0);
gl.drawArrays(gl.TRIANGLES, 0, 3);
}
function TrianglePositions() {
let ar = gl.canvas.width / gl.canvas.height;
let bufferVertices;
bufferVertices = new Float32Array([
0, 0.5, 0, 1,
-0.5, -0.5, 0, 1,
0.5, -0.5, 0, 1,
]);
// bufferVertices = new Float32Array([
// cos(PI / 2) * 0.5, sin(PI / 2) * 0.5, 0.0, 1.0,
// cos(TWO_PI / 3 + PI / 2) * 0.5, sin(TWO_PI / 3 + PI / 2) * 0.5, 0.0, 1.0,
// cos(2 * TWO_PI / 3 + PI / 2) * 0.5, sin(2 * TWO_PI / 3 + PI / 2) * 0.5, 0.0, 1.0,
// ]);
return prepareStaticBuffer(bufferVertices);
}
function TriangleColors() {
let colorData = new Float32Array([
1, 0, 1, 1,
0, 1, 1, 1,
1, 1, 0, 1
]);
return prepareStaticBuffer(colorData);
}
function prepareStaticBuffer(bufferData) {
let bufferId = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, bufferId);
gl.bufferData(gl.ARRAY_BUFFER, bufferData, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
return bufferId;
}
function init() {
let loop = () => { draw(), requestAnimationFrame(loop); }
// Setup once, then infinite draw
setup();
loop();
}
</script>
</html>