forked from MrZJD/webgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson01.js
82 lines (69 loc) · 2.22 KB
/
lesson01.js
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
////Chapter 5
//Temp 01
//多个数据的缓冲区
// 顶点着色器程序
var VSHADER_SOURCE =
`attribute vec4 a_Position;
attribute float a_PointSize;
void main() {
gl_Position = a_Position;
gl_PointSize = a_PointSize;
}`;
// 片元着色器程序
var FSHADER_SOURCE =
`void main() {
gl_FragColor = vec4(1.0, 0, 0, 1.0);
}`;
ready(() => {
var canvas = document.getElementById('webgl');
if ( !canvas ) {
console.error('Failed to retrieve the <canvas> element!');
return ;
}
var webgl = canvas.getContext('webgl');
if ( !initShaders( webgl, VSHADER_SOURCE, FSHADER_SOURCE) ) {
console.error('Failed to initialize shaders!');
}
// 获取attribute变量的地址
var a_Position = webgl.getAttribLocation(webgl.program, 'a_Position');
var a_PointSize = webgl.getAttribLocation(webgl.program, 'a_PointSize');
if ( a_Position < 0 || a_PointSize < 0 ) {
console.error('Failed to get the storage attribute location!');
return ;
}
// 获取绘制点的个数
var p_Counts = initBuffer(webgl, a_Position, a_PointSize);
if ( p_Counts < 0 ) {
console.error('Failed to init buffer!');
return ;
}
// 清空颜色缓冲区
webgl.clearColor(0, 0, 0, 0.6);
webgl.clear(webgl.COLOR_BUFFER_BIT);
webgl.drawArrays(webgl.POINTS, 0, p_Counts);
});
// 返回绘制点的个数
// -1 则报错
function initBuffer (webgl, a_Position, a_PointSize) {
// 顶点数据
var points = new Float32Array([0, 0.5, 2.0, -0.5, -0.5, 5.0, 0.5, -0.5, 10.0]);
var n = points.length/3;
var FSIZE = points.BYTES_PER_ELEMENT;
// buffer对象
var vertexBuffer = webgl.createBuffer();
if ( !vertexBuffer ) {
console.error('Failed to create buffer!');
return -1;
}
// 绑定对象到缓冲区指针(顶点数据)上
webgl.bindBuffer(webgl.ARRAY_BUFFER, vertexBuffer);
// 写入数据到缓冲区
webgl.bufferData(webgl.ARRAY_BUFFER, points, webgl.STATIC_DRAW);
// 指定attribute变量解析规则
webgl.vertexAttribPointer(a_Position, 2, webgl.FLOAT, false, FSIZE*3, 0);
webgl.vertexAttribPointer(a_PointSize, 1, webgl.FLOAT, false, FSIZE*3, FSIZE*2);
// 启用attribute变量 => 即链接缓冲区到attribute变量上
webgl.enableVertexAttribArray(a_Position);
webgl.enableVertexAttribArray(a_PointSize);
return n;
}