-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
109 lines (86 loc) · 2.56 KB
/
index.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>WebGL Demo</title>
</head>
<body>
<canvas id="glcanvas" width="640" height="480"></canvas>
<script type="text/javascript">
"use strict";
const canvas = document.querySelector("#glcanvas");
// Initialize the GL context
const gl = canvas.getContext("webgl2");
// Only continue if WebGL is available and working
console.assert(gl !== null);
// Set clear color to black, fully opaque
gl.clearColor(0.0, 0.0, 0.0, 0.2);
// Clear the color buffer with specified clear color
gl.clear(gl.COLOR_BUFFER_BIT);
// Vertex shader
const vertexShaderSource = `
attribute vec4 position;
void main() {
gl_Position = position;
}
`;
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vertexShaderSource);
gl.compileShader(vertexShader);
console.log(
"Vertex shader compile status",
gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)
);
// Fragment shader
const fragmentShaderSource = `
precision mediump float;
void main() {
gl_FragColor = vec4(1, 1, 1, 1); // reddish-purple
}
`;
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, fragmentShaderSource);
gl.compileShader(fragmentShader);
console.log(
"Fragment shader compile status",
gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)
);
// Program
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
gl.useProgram(program);
console.log(
"Program link status",
gl.getProgramParameter(program, gl.LINK_STATUS)
);
//
let positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
let positions = [
0, 0,
0, 1,
1, 0,
0, 0,
0, -0.5,
-0.5, 1
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
//
var size = 2;
var type = gl.FLOAT;
var normalize = false;
var stride = 0;
var offset = 0;
let positionAttributeLocation = gl.getAttribLocation(program, "position");
gl.enableVertexAttribArray(positionAttributeLocation);
gl.vertexAttribPointer(positionAttributeLocation, size, type, normalize, stride, offset);
//
var primitiveType = gl.TRIANGLES;
var offset = 0;
var count = positions.length;
gl.drawArrays(primitiveType, offset, count);
</script>
</body>
</html>