-
Notifications
You must be signed in to change notification settings - Fork 108
/
lesson18.html
189 lines (175 loc) · 6.15 KB
/
lesson18.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>利用四元数控制模型旋转</title>
<link rel="stylesheet" href="../css/common.css" />
</head>
<body>
<script src="../utils/webgl-helper.js"></script>
<script src="../utils/webgl-matrix.js"></script>
<script src="../utils/vector3.js"></script>
<script src="../utils/geometry.js"></script>
<script src="../utils/quaternion.js"></script>
<script src="../utils/math.js"></script>
<script src="../utils/model.js"></script>
<!-- 顶点着色器源码 -->
<script type="shader-source" id="vertexShader">
precision mediump float;
attribute vec4 a_Color;
varying vec4 v_Color;
attribute vec3 a_Position;
uniform mat4 u_Matrix;
void main(){
gl_Position = u_Matrix * vec4(a_Position,1);
v_Color = a_Color;
}
</script>
<!-- 顶点着色器源码 -->
<script type="shader-source" id="fragmentShader">
// 接收顶点坐标 (x, y)
precision mediump float;
varying vec4 v_Color;
void main(){
gl_FragColor = v_Color;
}
</script>
<canvas id="canvas"></canvas>
<div class="operation-container"></div>
<script>
var Vector3 = window.lib3d.Vector3;
var Quaternion = window.lib3d.Quaternion;
//获取canvas
var canvas = getCanvas('#canvas');
resizeCanvas(canvas);
//获取webgl绘图环境
var gl = getContext(canvas);
//创建着色器程序
var program = createProgramFromScript(
gl,
'vertexShader',
'fragmentShader'
);
//使用创建好的着色器程序
gl.useProgram(program.program);
var rate = canvas.width / canvas.height;
var cube = createCube(3, 3, 3);
cube = transformIndicesToUnIndices(cube);
var bufferInfo = createBufferInfoFromObject(gl, cube);
var objectList = new List();
var renderList = new List();
var aspect = canvas.width / canvas.height;
var fieldOfViewRadians = 60;
var projectionMatrix = matrix.perspective(
fieldOfViewRadians,
aspect,
2,
900
);
// 计算相机的位置矩阵
var cameraPosition = new Vector3(0, 0, 10);
var target = new Vector3(0, 0, 0);
var up = new Vector3(0, 1, 0);
var cameraMatrix = matrix.lookAt(cameraPosition, target, up);
var viewMatrix = matrix.inverse(cameraMatrix);
var modelMatrix = matrix.identity();
var object = new Model('cube');
object.setBufferInfo(bufferInfo);
objectList.add(object);
renderList.add({
programInfo: program,
model: object,
primitive: 'TRIANGLES',
renderType: 'drawArrays'
});
var lastProgramInfo;
var lastBufferInfo;
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.clearColor(0, 0, 0, 1);
gl.enable(gl.CULL_FACE);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
var currentMatrix = matrix.identity();
var playing = false;
gl.enable(gl.DEPTH_TEST);
function render() {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
objectList.forEach(function(object) {
var worldMatrix = matrix.multiply(viewMatrix, currentMatrix);
worldMatrix = matrix.multiply(projectionMatrix, worldMatrix);
object.uniforms.u_Matrix = worldMatrix;
});
renderList.forEach(function(object) {
var programInfo = object.programInfo;
var bufferInfo = object.model.bufferInfo;
var uniforms = object.model.uniforms;
var bindBuffers = false;
if (programInfo !== lastProgramInfo) {
lastProgramInfo = programInfo;
gl.useProgram(programInfo.program);
bindBuffers = true;
}
if (bindBuffers || bufferInfo !== lastBufferInfo) {
lastBufferInfo = bufferInfo;
setBufferInfos(gl, programInfo, bufferInfo);
}
setUniforms(programInfo, uniforms);
// 绘制
if (object.renderType === 'drawElements') {
if (bufferInfo.indices) {
gl.drawElements(
object.primitive,
bufferInfo.indices.length,
gl.UNSIGNED_SHORT,
0
);
return;
}
} else {
gl.drawArrays(gl[object.primitive], 0, bufferInfo.elementsCount);
return;
}
});
}
render();
var lastMatrix = matrix.identity();
var lastQ = new Quaternion();
var currentQ = new Quaternion();
// 判断是否支持触摸事件。
var supportTouchEvent = 'ontouchstart' in window;
var dragStartEvent = supportTouchEvent ? 'touchstart' : 'mousedown';
var dragMoveEvent = supportTouchEvent ? 'touchmove' : 'mousemove';
var dragEndEvent = supportTouchEvent ? 'touchend' : 'mouseup';
document.body.addEventListener(dragStartEvent, function(e) {
e = supportTouchEvent ? e.changedTouches[0] : e;
var px = e.pageX;
var py = e.pageY;
document.body.addEventListener(dragEndEvent, function up(e) {
Object.assign(lastQ, currentQ);
document.body.removeEventListener(dragEndEvent, up);
document.body.removeEventListener(dragMoveEvent, move);
});
function move(e) {
e = supportTouchEvent ? e.changedTouches[0] : e;
var ppx = e.pageX;
var ppy = e.pageY;
var dx = ppx - px;
var dy = ppy - py;
var l = Math.sqrt(dx * dx + dy * dy);
if (l == 0) {
return;
}
var tempQ = Quaternion.fromRotation(
{x: dy / l, y: dx / l, z: 0},
l / 2
);
currentQ = Quaternion.multiplyQuaternions(tempQ, lastQ);
currentMatrix = Quaternion.makeRotationFromQuaternion(currentQ);
render();
}
document.body.addEventListener(dragMoveEvent, move);
});
</script>
</body>
</html>