forked from MrZJD/webgl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lesson01.js
103 lines (84 loc) · 2.69 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//Chapter 07
//Temp 01
//确定视点 观察点 上方向
//vertex shader
var VSHADER_SOURCE = `
attribute vec4 a_Position;
attribute vec4 a_Color;
uniform mat4 u_ViewMatrix;
varying vec4 v_Color;
void main() {
// gl_Position = a_Position;
gl_Position = u_ViewMatrix * a_Position;
v_Color = a_Color;
}
`;
//fragment shader
var FSHADER_SOURCE = `
precision mediump float;
varying vec4 v_Color;
void main() {
gl_FragColor = v_Color;
}
`;
//main
ready(loadWEBGL);
//主程序
function loadWEBGL () {
var canvas = document.getElementById('webgl');
if ( !canvas ) {
console.error('Failed to get canvas-element!');
return ;
}
var webgl = canvas.getContext('webgl');
if ( !webgl ) {
console.error('Failed to get canvas context: webgl!');
return ;
}
if ( !initShaders(webgl, VSHADER_SOURCE, FSHADER_SOURCE) ) {
console.error('Failed to init shaders!');
return ;
}
var n = initBuffer(webgl);
if ( n < 0 ) {
console.error('Failed to init buffer!');
return ;
}
setViewMatrix(webgl);
webgl.clearColor(0.0, 0.0, 0.0, 1.0);
webgl.clear(webgl.COLOR_BUFFER_BIT);
webgl.drawArrays(webgl.TRIANGLES, 0, n);
}
function initBuffer (webgl) {
var vertexData = new Float32Array([
//顶点坐标颜色
0.0, 0.5, -0.4, 0.4, 1.0, 0.4,
-0.5, -0.5, -0.4, 0.4, 1.0, 0.4,
0.5, -0.5, -0.4, 1.0, 0.4, 0.4,
0.5, 0.4, -0.2, 1.0, 0.4, 0.4,
-0.5, 0.4, -0.2, 1.0, 1.0, 0.4,
0, -0.6, -0.2, 1.0, 1.0, 0.4,
0.0, 0.5, 0.0, 0.4, 0.4, 1.0,
-0.5, -0.5, 0.0, 0.4, 0.4, 1.0,
0.5, -0.5, 0.0, 1.0, 0.4, 0.4
]);
var n = vertexData.length/6;
var FSIZE = vertexData.BYTES_PER_ELEMENT;
var a_Position = webgl.getAttribLocation(webgl.program, 'a_Position');
var a_Color = webgl.getAttribLocation(webgl.program, 'a_Color');
var buffer = webgl.createBuffer();
webgl.bindBuffer(webgl.ARRAY_BUFFER, buffer);
webgl.bufferData(webgl.ARRAY_BUFFER, vertexData, webgl.STATIC_DRAW);
webgl.vertexAttribPointer(a_Position, 3, webgl.FLOAT, false, FSIZE*6, 0);
webgl.vertexAttribPointer(a_Color, 3, webgl.FLOAT, false, FSIZE*6, FSIZE*3);
webgl.enableVertexAttribArray(a_Position);
webgl.enableVertexAttribArray(a_Color);
return n;
}
function setViewMatrix (webgl) {
var u_ViewMatrix = webgl.getUniformLocation(webgl.program, 'u_ViewMatrix');
//设置视点 视线 上方向
var viewMatrix = new Matrix4();
viewMatrix.setLookAt(-0.20, 0.05, 0.25, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
webgl.uniformMatrix4fv(u_ViewMatrix, false, viewMatrix.elements);
}