-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathbuffer_copy.html
117 lines (94 loc) · 3.59 KB
/
buffer_copy.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>WebGL 2 Samples - buffer_copy</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="info">WebGL 2 Samples - buffer_copy</div>
<p id="description">
A simple sample showing how to use copyBufferSubData.
</p>
<script id="vs" type="x-shader/x-vertex">
#version 300 es
#define POSITION_LOCATION 0
precision highp float;
precision highp int;
layout(location = POSITION_LOCATION) in vec2 pos;
void main()
{
gl_Position = vec4(pos, 0.0, 1.0);
}
</script>
<script id="fs" type="x-shader/x-fragment">
#version 300 es
precision highp float;
precision highp int;
out vec4 color;
void main()
{
color = vec4(1.0, 0.5, 0.0, 1.0);
}
</script>
<script src="utility.js"></script>
<script>
(function () {
'use strict';
// -- Init Canvas
var canvas = document.createElement('canvas');
canvas.width = Math.min(window.innerWidth, window.innerHeight);
canvas.height = canvas.width;
document.body.appendChild(canvas);
// -- Init WebGL Context
var gl = canvas.getContext('webgl2', { antialias: false });
var isWebGL2 = !!gl;
if(!isWebGL2) {
document.getElementById('info').innerHTML = 'WebGL 2 is not available. See <a href="https://www.khronos.org/webgl/wiki/Getting_a_WebGL_Implementation">How to get a WebGL 2 implementation</a>';
return;
}
// -- Init Program
var program = createProgram(gl, getShaderSource('vs'), getShaderSource('fs'));
gl.useProgram(program);
// -- Init Buffer
var vertices = new Float32Array([
-1.0, -1.0,
1.0, -1.0,
1.0, 1.0,
1.0, 1.0,
-1.0, 1.0,
-1.0, -1.0
]);
var vertexPosBufferSrc = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBufferSrc);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
gl.bindBuffer(gl.COPY_READ_BUFFER, vertexPosBufferSrc);
var vertexPosBufferDst = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBufferDst);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices.length), gl.STATIC_DRAW);
gl.bindBuffer(gl.COPY_WRITE_BUFFER, vertexPosBufferDst);
gl.copyBufferSubData(gl.COPY_READ_BUFFER, gl.COPY_WRITE_BUFFER, 0, 0, vertices.length * Float32Array.BYTES_PER_ELEMENT);
// -- Init Vertex Array
var vertexArray = gl.createVertexArray();
gl.bindVertexArray(vertexArray);
var vertexPosLocation = 0;
gl.enableVertexAttribArray(vertexPosLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBufferDst);
gl.vertexAttribPointer(vertexPosLocation, 2, gl.FLOAT, false, 0, 0);
gl.bindVertexArray(null);
// -- Render
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.bindVertexArray(vertexArray);
gl.drawArrays(gl.TRIANGLES, 0, 6);
// -- Delete WebGL resources
gl.deleteBuffer(vertexPosBufferSrc);
gl.deleteBuffer(vertexPosBufferDst);
gl.deleteProgram(program);
gl.deleteVertexArray(vertexArray);
})();
</script>
<div id="highlightedLines" style="display: none">#L77-L87</div>
</body>
</html>