-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (143 loc) · 5.22 KB
/
index.js
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
'use strict'
/**
*
* INITIALIZING
*
*/
/* ==================================================
= Get canvas and set context =
================================================== */
var canvas = document.getElementById('c')
var gl = canvas.getContext('webgl')
if (!gl) {
console.log('please enable webgl')
}
/* ============================================================
= Create, Upload & Compile GLSL Source =
============================================================ */
function createShader (gl, type, source) {
// compile shader
var shader = gl.createShader(type)
gl.shaderSource(shader, source)
gl.compileShader(shader)
// check if compiled successfully
var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS)
if (success) {
return shader
}
// log and delete results on failure
console.log(gl.getShaderInfoLog(shader))
gl.deleteShader(shader)
}
var vertexShaderSource = document.getElementById('2d-vertex-shader').text
var fragmentShaderSource = document.getElementById('2d-fragment-shader').text
var vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource)
var fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource)
/* ===============================================
= Link shaders to program =
=============================================== */
function createProgram (gl, vertexShader, fragmentShader) {
// attach and link program
var program = gl.createProgram()
gl.attachShader(program, vertexShader)
gl.attachShader(program, fragmentShader)
gl.linkProgram(program)
// return if success
var success = gl.getProgramParameter(program, gl.LINK_STATUS)
if (success) return program
// log and delete results on failure
console.log(gl.getProgramInfoLog(program))
gl.deleteProgram(program)
}
var program = createProgram(gl, vertexShader, fragmentShader)
/* =============================================================
= Supply necessary data to GLSL program =
============================================================= */
// look up position of attribute in compiled program
// should be done during initialization, not in render loop
var positionAttributeLocation = gl.getAttribLocation(program, 'a_position')
var resolutionUniformLocation = gl.getUniformLocation(program, 'u_resolution')
// Create buffer for attribute to get data
var positionBuffer = gl.createBuffer()
// establish bind point. bind the position buffer
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer)
// put data into buffer (ex. three 2d points)
var positions = [
10, 20,
80, 20,
10, 30,
10, 30,
80, 20,
80, 30
]
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW)
/**
*
* RENDERING
*
*/
// Call in render loop resize(gl.canvas)
// Make sure to update clipspace after resize so clipsace still (-1 to +1)
// gl.viewport(0, 0, gl.canvas.width, gl.canvas.height)
function resize (canvas) {
// For high HD screens alter or pixel density
// If performace needs upgrade draw less px by making
// this value 1, phones and other devices will scale
// it up. less pretty, more performant.
var realToCSSPixels = window.devicePixelRatio
// Lookup the size the browser is displaying the canvas
// and alter depending on pixel density
var displayWidth = Math.floor(canvas.clientWidth * realToCSSPixels)
var displayHeight = Math.floor(canvas.clientHeight * realToCSSPixels)
// Check if the canvas is not the same size
if (canvas.width !== displayWidth ||
canvas.height !== displayHeight) {
// Make the canvas the same size
canvas.width = displayWidth
canvas.height = displayHeight
}
}
function main () {
// This is the built in version of this function
// webglUtils.resizeCanvasToDisplay(gl.canvas)
resize(gl.canvas)
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height)
// Set clear color on canvas
gl.clearColor(0, 0, 0, 0)
gl.clear(gl.COLOR_BUFFER_BIT)
// Tell WebGL what shaders to execute
gl.useProgram(program)
// set the resolution
gl.uniform2f(resolutionUniformLocation, gl.canvas.width, gl.canvas.height)
/* =====================================================
= Tell WebGL how to take buffer =
===================================================== */
// Turn attribute on
gl.enableVertexAttribArray(positionAttributeLocation)
// Then specify how to pull data out
// Bind position buffer
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer)
// Tell the attribute how to get data out of positionbuffer (ARRAY_BUFFER)
var size = 2 // 2 components per iteration
var type = gl.FLOAT // Data in 32 bit floats
var normalize = false // don't normalize the data
var stride = 0 // 0 = move forward size * sizeof(type) each iteration to get next position
var offset = 0 // start at the beginning of the buffer
gl.vertexAttribPointer(
positionAttributeLocation,
size,
type,
normalize,
stride,
offset
)
/* ================================================
= WebGL to Execute program =
================================================ */
// draw
var primitiveType = gl.TRIANGLES
var drawOffset = 0
var count = 6
gl.drawArrays(primitiveType, drawOffset, count)
}
main()