-
Notifications
You must be signed in to change notification settings - Fork 0
/
example2.html
183 lines (144 loc) · 6.07 KB
/
example2.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width">
<title></title>
<style> html, body, canvas { display: block; position: relative; margin: 0; padding: 0; } </style>
</head>
<body onload='init()'>
<canvas id='maincanvas' />
</body>
<script type="text/javascript" src="node_modules/gl-matrix/dist/gl-matrix-min.js"></script>
<script type="text/javascript" src="shared.js"></script>
<script type="text/javascript" src="icosahedron.js"></script>
<script type="text/glsl" id='vertShader'>
precision highp float; // Not necessary, but makes it explicit
uniform mat4 u_projectionMatrix;
uniform mat4 u_modelViewMatrix;
attribute vec3 a_position;
// attribute vec3 a_normal;
attribute vec4 a_color;
// varying vec3 v_normal;
varying vec4 v_color;
void main() {
gl_Position = u_projectionMatrix * u_modelViewMatrix * vec4(a_position, 1);
// v_normal = a_normal;
v_color = a_color;
// v_color = abs(gl_Position);
}
</script>
<script type="text/glsl" id='fragShader'>
precision highp float;
// varying vec3 v_normal;
varying vec4 v_color;
void main() {
gl_FragColor = v_color;
}
</script>
<script>
'use strict';
// Be forewarned: OpenGL LOVES global variables and state
const gl = document.getElementById('maincanvas').getContext('webgl');
const exampleShader = compileShaderProgram('vertShader', 'fragShader');
const viewMatrices = {};
const icosahedronMesh = postProcessIcoMesh(icosphere(1));
function postProcessIcoMesh(complex) {
let vertices = complex.positions;
let faceIndices = complex.cells;
let expandedPositions = [];
let normals = [];
let colors = [];
faceIndices.forEach((face, idx) => {
let v0 = vec3.clone(vertices[face[0]]);
let v1 = vec3.clone(vertices[face[1]]);
let v2 = vec3.clone(vertices[face[2]]);
expandedPositions.push(v0, v1, v2);
let c0 = randRGB();
let c1 = randRGB();
let c2 = randRGB();
// Flat color shading, since all vertices have the same color
// c0 = c1 = c2;
colors.push(c0, c1, c2);
});
let numVertexElements = 3;
let numNormalElements = 3;
let numColorElements = 4;
let verticesData = flatten2Buffer(expandedPositions, numVertexElements);
let normalsData = flatten2Buffer(normals, numNormalElements);
let colorsData = flatten2Buffer(colors, numColorElements);
return {
// Mesh information
numVertices: expandedPositions.length,
modelPosition: vec3.create(),
modelRotationMatrix: mat4.create(),
// Buffers and buffer information
verticesBuffer: prepareGLBuffer(verticesData, gl.STATIC_DRAW),
verticesBufferUnitCount: numVertexElements,
verticesBufferType: gl.FLOAT,
verticesBufferStride: verticesData.BYTES_PER_ELEMENT * numVertexElements,
verticesBufferOffset: 0,
normalsBuffer: prepareGLBuffer(normalsData, gl.STATIC_DRAW),
normalsBufferUnitCount: numNormalElements,
normalsBufferType: gl.FLOAT,
normalsBufferStride: normalsData.BYTES_PER_ELEMENT * numNormalElements,
normalsBufferOffset: 0,
colorsBuffer: prepareGLBuffer(colorsData, gl.STATIC_DRAW),
colorsBufferUnitCount: numColorElements,
colorsBufferType: gl.FLOAT,
colorsBufferStride: colorsData.BYTES_PER_ELEMENT * numColorElements,
colorsBufferOffset: 0
};
}
function setup() {
let canvasWidth = window.innerWidth;
let canvasHeight = window.innerHeight;
gl.canvas.setAttribute('width', canvasWidth);
gl.canvas.setAttribute('height', canvasHeight);
gl.viewport(0, 0, canvasWidth, canvasHeight);
gl.clearColor(0, 0, 0, 1);
gl.enable(gl.CULL_FACE);
viewMatrices.projectionMatrix = mat4.perspective(mat4.create(), deg2Rad(25), canvasWidth / canvasHeight, 0.01, 50);
viewMatrices.viewMatrix = mat4.lookAt(mat4.create(), vec3.fromValues(0, 0, 9), vec3.fromValues(0, 0, 0), vec3.fromValues(0, 1, 0));
}
function draw() {
// Animation
icosahedronMesh.modelPosition[0] = 2; //sin(0.004 * elapsedTime());
mat4.rotateY(icosahedronMesh.modelRotationMatrix, icosahedronMesh.modelRotationMatrix, -0.003 * PI);
// Drawing
gl.clear(gl.COLOR_BUFFER_BIT);
gl.useProgram(exampleShader);
let projMatLoc = gl.getUniformLocation(exampleShader, 'u_projectionMatrix');
gl.uniformMatrix4fv(projMatLoc, false, viewMatrices.projectionMatrix);
let modelMatrix = mat4.create();
mat4.multiply(modelMatrix, modelMatrix, icosahedronMesh.modelRotationMatrix);
mat4.translate(modelMatrix, modelMatrix, icosahedronMesh.modelPosition);
let modelViewMatrix = mat4.multiply(mat4.create(), viewMatrices.viewMatrix, modelMatrix);
let modelViewMatLoc = gl.getUniformLocation(exampleShader, 'u_modelViewMatrix');
gl.uniformMatrix4fv(modelViewMatLoc, false, modelViewMatrix);
let posLoc = gl.getAttribLocation(exampleShader, 'a_position');
gl.enableVertexAttribArray(posLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, icosahedronMesh.verticesBuffer);
gl.vertexAttribPointer(posLoc, icosahedronMesh.verticesBufferUnitCount, icosahedronMesh.verticesBufferType, false, icosahedronMesh.verticesBufferStride, icosahedronMesh.verticesBufferOffset);
let colorLoc = gl.getAttribLocation(exampleShader, 'a_color');
gl.enableVertexAttribArray(colorLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, icosahedronMesh.colorsBuffer);
gl.vertexAttribPointer(colorLoc, icosahedronMesh.colorsBufferUnitCount, icosahedronMesh.colorsBufferType, false, icosahedronMesh.colorsBufferStride, icosahedronMesh.colorsBufferOffset);
gl.drawArrays(gl.TRIANGLES, 0, icosahedronMesh.numVertices);
}
function prepareGLBuffer(bufferData, bufferUsage) {
let bufferId = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, bufferId);
gl.bufferData(gl.ARRAY_BUFFER, bufferData, bufferUsage);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
return bufferId;
}
function init() {
let loop = () => { draw(), requestAnimationFrame(loop); }
// Setup once, then infinite draw
setup();
loop();
}
</script>
</html>