- Notifications
You must be signed in to change notification settings - Fork 683
/
Copy pathwebgl-clipspace-rectangles.html
130 lines (104 loc) · 3.91 KB
/
webgl-clipspace-rectangles.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
<!-- Licensed under a BSD license. See license.html for license -->
<!DOCTYPE html>
<html>
<head>
<metacharset="utf-8">
<metaname="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>WebGL - Fundamentals</title>
<linktype="text/css" href="resources/webgl-tutorials.css" rel="stylesheet" />
</head>
<body>
<canvasid="c"></canvas>
</body>
</html>
<!--
for most samples webgl-utils only provides shader compiling/linking and
canvas resizing because why clutter the examples with code that's the same in every sample.
See https://webglfundamentals.org/webgl/lessons/webgl-boilerplate.html
and https://webglfundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html
for webgl-utils, m3, m4, and webgl-lessons-ui.
-->
<scriptsrc="resources/webgl-utils.js"></script>
<scriptid="vertex-shader-2d" type="notjs">
// an attribute will receive data from a buffer
attributevec4a_position;
// all shaders have a main function
voidmain(){
// gl_Position is a special variable a vertex shader
// is responsible for setting
gl_Position=a_position;
}
</script>
<scriptid="fragment-shader-2d" type="notjs">
// fragment shaders don't have a default precision so we need
// to pick one. mediump is a good default
precisionmediumpfloat;
voidmain(){
// gl_FragColor is a special variable a fragment shader
// is responsible for setting
gl_FragColor=vec4(1,0,0.5,1);// return redish-purple
}
</script>
<script>
"use strict";
functionmain(){
// Get A WebGL context
varcanvas=document.querySelector("#c");
vargl=canvas.getContext("webgl");
if(!gl){
return;
}
// setup GLSL program
varprogram=webglUtils.createProgramFromScripts(gl,["vertex-shader-2d","fragment-shader-2d"]);
// look up where the vertex data needs to go.
varpositionAttributeLocation=gl.getAttribLocation(program,"a_position");
// Create a buffer and put 12 clip space points in it.
// 4 rectangles, 2 triangles each, 3 vertices per triangle
varpositionBuffer=gl.createBuffer();
// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
gl.bindBuffer(gl.ARRAY_BUFFER,positionBuffer);
varpositions=[
-.8,.8,0,1,// 1st rect 1st triangle
.8,.8,0,1,
-.8,.2,0,1,
-.8,.2,0,1,// 1st rect 2nd triangle
.8,.8,0,1,
.8,.2,0,1,
-.8,-.2,0,1,// 2nd rect 1st triangle
.8,-.2,0,1,
-.8,-.8,0,1,
-.8,-.8,0,1,// 2nd rect 2nd triangle
.8,-.2,0,1,
.8,-.8,0,1,
];
gl.bufferData(gl.ARRAY_BUFFER,newFloat32Array(positions),gl.STATIC_DRAW);
// code above this line is initialization code.
// code below this line is rendering code.
webglUtils.resizeCanvasToDisplaySize(gl.canvas);
// Tell WebGL how to convert from clip space to pixels
gl.viewport(0,0,gl.canvas.width,gl.canvas.height);
// Clear the canvas
gl.clearColor(0,0,0,0);
gl.clear(gl.COLOR_BUFFER_BIT);
// Tell it to use our program (pair of shaders)
gl.useProgram(program);
// Turn on the attribute
gl.enableVertexAttribArray(positionAttributeLocation);
// Bind the position buffer.
gl.bindBuffer(gl.ARRAY_BUFFER,positionBuffer);
// Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
varsize=4;// 4 components per iteration
vartype=gl.FLOAT;// the data is 32bit floats
varnormalize=false;// don't normalize the data
varstride=0;// 0 = move forward size * sizeof(type) each iteration to get the next position
varoffset=0;// start at the beginning of the buffer
gl.vertexAttribPointer(
positionAttributeLocation,size,type,normalize,stride,offset);
// draw
varprimitiveType=gl.TRIANGLES;
varoffset=0;
varcount=4*3;// 4 triangles, 3 vertices each
gl.drawArrays(primitiveType,offset,count);
}
main();
</script>