-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathwebgl-multiple-objects-list.html
222 lines (180 loc) · 5.72 KB
/
webgl-multiple-objects-list.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!-- Licensed under a BSD license. See license.html for license -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>WebGL2 - Multiple Objects - List</title>
<link type="text/css" href="resources/webgl-tutorials.css" rel="stylesheet" />
</head>
<body>
<div class="description">
Draw multiple objects using a list
</div>
<canvas id="canvas" width="400" height="300"></canvas>
</body>
<script src="resources/webgl-utils.js"></script>
<script src="resources/webgl-lessons-helper.js"></script>
<script src="resources/3d-math.js"></script>
<script src="resources/primitives.js"></script>
<script>
"use strict";
var vs = `#version 300 es
in vec4 a_position;
in vec4 a_color;
uniform mat4 u_matrix;
out vec4 v_color;
void main() {
// Multiply the position by the matrix.
gl_Position = u_matrix * a_position;
// Pass the color to the fragment shader.
v_color = a_color;
}
`;
var fs = `#version 300 es
precision mediump float;
// Passed in from the vertex shader.
in vec4 v_color;
uniform vec4 u_colorMult;
out vec4 outColor;
void main() {
outColor = v_color * u_colorMult;
}
`;
function main() {
// Get A WebGL context
var canvas = document.getElementById("canvas");
webglLessonsHelper.setupLesson(canvas);
/** @type {WebGLRenderingContext} */
var gl = canvas.getContext("webgl2");
if (!gl) {
webglLessonsHelper.showNeedWebGL2(canvas);
return;
}
function createFlattenedVertices(gl, vertices) {
return webglUtils.createBufferInfoFromArrays(
gl,
primitives.makeRandomVertexColors(
primitives.deindexVertices(vertices),
{
vertsPerColor: 6,
rand: function(ndx, channel) {
return channel < 3 ? ((128 + Math.random() * 128) | 0) : 255;
}
})
);
};
var sphereBufferInfo = createFlattenedVertices(gl, primitives.createSphereVertices(10, 12, 6));
var cubeBufferInfo = createFlattenedVertices(gl, primitives.createCubeVertices(20));
var coneBufferInfo = createFlattenedVertices(gl, primitives.createTruncatedConeVertices(10, 0, 20, 12, 1, true, false));
// setup GLSL program
var programInfo = webglUtils.createProgramInfo(gl, [vs, fs]);
var sphereVAO = webglUtils.createVAOFromBufferInfo(gl, programInfo, sphereBufferInfo);
var cubeVAO = webglUtils.createVAOFromBufferInfo(gl, programInfo, cubeBufferInfo);
var coneVAO = webglUtils.createVAOFromBufferInfo(gl, programInfo, coneBufferInfo);
function degToRad(d) {
return d * Math.PI / 180;
}
var cameraAngleRadians = degToRad(0);
var fieldOfViewRadians = degToRad(60);
var cameraHeight = 50;
// Uniforms for each object.
var sphereUniforms = {
u_colorMult: [0.5, 1, 0.5, 1],
u_matrix: m4.identity(),
};
var cubeUniforms = {
u_colorMult: [1, 0.5, 0.5, 1],
u_matrix: m4.identity(),
};
var coneUniforms = {
u_colorMult: [0.5, 0.5, 1, 1],
u_matrix: m4.identity(),
};
var sphereTranslation = [ 0, 0, 0];
var cubeTranslation = [-40, 0, 0];
var coneTranslation = [ 40, 0, 0];
var objectsToDraw = [
{
programInfo: programInfo,
bufferInfo: sphereBufferInfo,
vertexArray: sphereVAO,
uniforms: sphereUniforms,
},
{
programInfo: programInfo,
bufferInfo: cubeBufferInfo,
vertexArray: cubeVAO,
uniforms: cubeUniforms,
},
{
programInfo: programInfo,
bufferInfo: coneBufferInfo,
vertexArray: coneVAO,
uniforms: coneUniforms,
},
];
function computeMatrix(viewProjectionMatrix, translation, xRotation, yRotation) {
var matrix = m4.translate(viewProjectionMatrix,
translation[0],
translation[1],
translation[2]);
matrix = m4.xRotate(matrix, xRotation);
return m4.yRotate(matrix, yRotation);
}
requestAnimationFrame(drawScene);
// Draw the scene.
function drawScene(time) {
time = time * 0.0005;
gl.enable(gl.CULL_FACE);
gl.enable(gl.DEPTH_TEST);
// Compute the projection matrix
var aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
var projectionMatrix =
m4.perspective(fieldOfViewRadians, aspect, 1, 2000);
// Compute the camera's matrix using look at.
var cameraPosition = [0, 0, 100];
var target = [0, 0, 0];
var up = [0, 1, 0];
var cameraMatrix = m4.lookAt(cameraPosition, target, up);
// Make a view matrix from the camera matrix.
var viewMatrix = m4.inverse(cameraMatrix);
var viewProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);
var sphereXRotation = time;
var sphereYRotation = time;
var cubeXRotation = -time;
var cubeYRotation = time;
var coneXRotation = time;
var coneYRotation = -time;
// Compute the matrices for each object.
sphereUniforms.u_matrix = computeMatrix(
viewProjectionMatrix,
sphereTranslation,
sphereXRotation,
sphereYRotation);
cubeUniforms.u_matrix = computeMatrix(
viewProjectionMatrix,
cubeTranslation,
cubeXRotation,
cubeYRotation);
coneUniforms.u_matrix = computeMatrix(
viewProjectionMatrix,
coneTranslation,
coneXRotation,
coneYRotation);
// ------ Draw the objects --------
objectsToDraw.forEach(function(object) {
var programInfo = object.programInfo;
gl.useProgram(programInfo.program);
// Setup all the needed attributes.
gl.bindVertexArray(object.vertexArray);
// Set the uniforms we just computed
webglUtils.setUniforms(programInfo, object.uniforms);
webglUtils.drawBufferInfo(gl, object.bufferInfo);
});
requestAnimationFrame(drawScene);
}
}
main();
</script>
</html>