在本章中,我们将以一个示例演示如何绘制可使用鼠标控件旋转的3D立方体。
示例–绘制交互式多维数据集
以下程序显示了如何使用鼠标控件旋转立方体。
HTML
x
239
239
1
2
<html>
3
<head>
4
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
6
<title>WebGL-交互式多维数据集 - web176教程网|web176.com</title>
7
</head>
8
<body>
9
<canvas width = "570" height = "570" id = "my_Canvas"></canvas>
10
11
<script>
12
/*============= Creating a canvas ======================*/
13
var canvas = document.getElementById('my_Canvas');
14
gl = canvas.getContext('experimental-webgl');
15
16
/*========== Defining and storing the geometry ==========*/
17
18
var vertices = [
19
-1,-1,-1, 1,-1,-1, 1, 1,-1, -1, 1,-1,
20
-1,-1, 1, 1,-1, 1, 1, 1, 1, -1, 1, 1,
21
-1,-1,-1, -1, 1,-1, -1, 1, 1, -1,-1, 1,
22
1,-1,-1, 1, 1,-1, 1, 1, 1, 1,-1, 1,
23
-1,-1,-1, -1,-1, 1, 1,-1, 1, 1,-1,-1,
24
-1, 1,-1, -1, 1, 1, 1, 1, 1, 1, 1,-1,
25
];
26
27
var colors = [
28
5,3,7, 5,3,7, 5,3,7, 5,3,7,
29
1,1,3, 1,1,3, 1,1,3, 1,1,3,
30
0,0,1, 0,0,1, 0,0,1, 0,0,1,
31
1,0,0, 1,0,0, 1,0,0, 1,0,0,
32
1,1,0, 1,1,0, 1,1,0, 1,1,0,
33
0,1,0, 0,1,0, 0,1,0, 0,1,0
34
];
35
36
var indices = [
37
0,1,2, 0,2,3, 4,5,6, 4,6,7,
38
8,9,10, 8,10,11, 12,13,14, 12,14,15,
39
16,17,18, 16,18,19, 20,21,22, 20,22,23
40
];
41
42
// Create and store data into vertex buffer
43
var vertex_buffer = gl.createBuffer ();
44
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
45
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
46
47
// Create and store data into color buffer
48
var color_buffer = gl.createBuffer ();
49
gl.bindBuffer(gl.ARRAY_BUFFER, color_buffer);
50
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
51
52
// Create and store data into index buffer
53
var index_buffer = gl.createBuffer ();
54
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, index_buffer);
55
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
56
57
/*=================== SHADERS =================== */
58
59
var vertCode = 'attribute vec3 position;'+
60
'uniform mat4 Pmatrix;'+
61
'uniform mat4 Vmatrix;'+
62
'uniform mat4 Mmatrix;'+
63
'attribute vec3 color;'+//the color of the point
64
'varying vec3 vColor;'+
65
'void main(void) { '+//pre-built function
66
'gl_Position = Pmatrix*Vmatrix*Mmatrix*vec4(position, 1.);'+
67
'vColor = color;'+
68
'}';
69
70
var fragCode = 'precision mediump float;'+
71
'varying vec3 vColor;'+
72
'void main(void) {'+
73
'gl_FragColor = vec4(vColor, 1.);'+
74
'}';
75
76
var vertShader = gl.createShader(gl.VERTEX_SHADER);
77
gl.shaderSource(vertShader, vertCode);
78
gl.compileShader(vertShader);
79
80
var fragShader = gl.createShader(gl.FRAGMENT_SHADER);
81
gl.shaderSource(fragShader, fragCode);
82
gl.compileShader(fragShader);
83
84
var shaderprogram = gl.createProgram();
85
gl.attachShader(shaderprogram, vertShader);
86
gl.attachShader(shaderprogram, fragShader);
87
gl.linkProgram(shaderprogram);
88
89
/*======== Associating attributes to vertex shader =====*/
90
var _Pmatrix = gl.getUniformLocation(shaderprogram, "Pmatrix");
91
var _Vmatrix = gl.getUniformLocation(shaderprogram, "Vmatrix");
92
var _Mmatrix = gl.getUniformLocation(shaderprogram, "Mmatrix");
93
94
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
95
var _position = gl.getAttribLocation(shaderprogram, "position");
96
gl.vertexAttribPointer(_position, 3, gl.FLOAT, false,0,0);
97
gl.enableVertexAttribArray(_position);
98
99
gl.bindBuffer(gl.ARRAY_BUFFER, color_buffer);
100
var _color = gl.getAttribLocation(shaderprogram, "color");
101
gl.vertexAttribPointer(_color, 3, gl.FLOAT, false,0,0) ;
102
gl.enableVertexAttribArray(_color);
103
gl.useProgram(shaderprogram);
104
105
/*==================== MATRIX ====================== */
106
107
function get_projection(angle, a, zMin, zMax) {
108
var ang = Math.tan((angle*.5)*Math.PI/180);//angle*.5
109
return [
110
0.5/ang, 0 , 0, 0,
111
0, 0.5*a/ang, 0, 0,
112
0, 0, -(zMax+zMin)/(zMax-zMin), -1,
113
0, 0, (-2*zMax*zMin)/(zMax-zMin), 0
114
];
115
}
116
117
var proj_matrix = get_projection(40, canvas.width/canvas.height, 1, 100);
118
var mo_matrix = [ 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 ];
119
var view_matrix = [ 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 ];
120
121
view_matrix[14] = view_matrix[14]-6;
122
123
/*================= Mouse events ======================*/
124
125
var AMORTIZATION = 0.95;
126
var drag = false;
127
var old_x, old_y;
128
var dX = 0, dY = 0;
129
130
var mouseDown = function(e) {
131
drag = true;
132
old_x = e.pageX, old_y = e.pageY;
133
e.preventDefault();
134
return false;
135
};
136
137
var mouseUp = function(e){
138
drag = false;
139
};
140
141
var mouseMove = function(e) {
142
if (!drag) return false;
143
dX = (e.pageX-old_x)*2*Math.PI/canvas.width,
144
dY = (e.pageY-old_y)*2*Math.PI/canvas.height;
145
THETA+= dX;
146
PHI+=dY;
147
old_x = e.pageX, old_y = e.pageY;
148
e.preventDefault();
149
};
150
151
canvas.addEventListener("mousedown", mouseDown, false);
152
canvas.addEventListener("mouseup", mouseUp, false);
153
canvas.addEventListener("mouseout", mouseUp, false);
154
canvas.addEventListener("mousemove", mouseMove, false);
155
156
/*=========================rotation================*/
157
158
function rotateX(m, angle) {
159
var c = Math.cos(angle);
160
var s = Math.sin(angle);
161
var mv1 = m[1], mv5 = m[5], mv9 = m[9];
162
163
m[1] = m[1]*c-m[2]*s;
164
m[5] = m[5]*c-m[6]*s;
165
m[9] = m[9]*c-m[10]*s;
166
167
m[2] = m[2]*c+mv1*s;
168
m[6] = m[6]*c+mv5*s;
169
m[10] = m[10]*c+mv9*s;
170
}
171
172
function rotateY(m, angle) {
173
var c = Math.cos(angle);
174
var s = Math.sin(angle);
175
var mv0 = m[0], mv4 = m[4], mv8 = m[8];
176
177
m[0] = c*m[0]+s*m[2];
178
m[4] = c*m[4]+s*m[6];
179
m[8] = c*m[8]+s*m[10];
180
181
m[2] = c*m[2]-s*mv0;
182
m[6] = c*m[6]-s*mv4;
183
m[10] = c*m[10]-s*mv8;
184
}
185
186
/*=================== Drawing =================== */
187
188
var THETA = 0,
189
PHI = 0;
190
var time_old = 0;
191
192
var animate = function(time) {
193
var dt = time-time_old;
194
195
if (!drag) {
196
dX *= AMORTIZATION, dY*=AMORTIZATION;
197
THETA+=dX, PHI+=dY;
198
}
199
200
//set model matrix to I4
201
202
mo_matrix[0] = 1, mo_matrix[1] = 0, mo_matrix[2] = 0,
203
mo_matrix[3] = 0,
204
205
mo_matrix[4] = 0, mo_matrix[5] = 1, mo_matrix[6] = 0,
206
mo_matrix[7] = 0,
207
208
mo_matrix[8] = 0, mo_matrix[9] = 0, mo_matrix[10] = 1,
209
mo_matrix[11] = 0,
210
211
mo_matrix[12] = 0, mo_matrix[13] = 0, mo_matrix[14] = 0,
212
mo_matrix[15] = 1;
213
214
rotateY(mo_matrix, THETA);
215
rotateX(mo_matrix, PHI);
216
217
time_old = time;
218
gl.enable(gl.DEPTH_TEST);
219
220
// gl.depthFunc(gl.LEQUAL);
221
222
gl.clearColor(0.5, 0.5, 0.5, 0.9);
223
gl.clearDepth(1.0);
224
gl.viewport(0.0, 0.0, canvas.width, canvas.height);
225
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
226
227
gl.uniformMatrix4fv(_Pmatrix, false, proj_matrix);
228
gl.uniformMatrix4fv(_Vmatrix, false, view_matrix);
229
gl.uniformMatrix4fv(_Mmatrix, false, mo_matrix);
230
231
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, index_buffer);
232
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT, 0);
233
234
window.requestAnimationFrame(animate);
235
}
236
animate(0);
237
</script>
238
</body>
239
</html>
阅读剩余 89%
作者:terry,如若转载,请注明出处:https://www.web176.com/webgl/778.html