forked from mikeseven/node-webgl
-
Notifications
You must be signed in to change notification settings - Fork 170
/
test1.js
executable file
·59 lines (48 loc) · 1.29 KB
/
test1.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
const createContext = require('../index')
const utils = require('./utils')
function main () {
// Create context
const width = 64
const height = 64
const gl = createContext(width, height)
const vertexSrc = [
'attribute vec2 a_position;',
'void main() {',
'gl_Position = vec4(a_position, 0, 1);',
'}'
].join('\n')
const fragmentSrc = [
'void main() {',
'gl_FragColor = vec4(0, 1, 0, 1); // green',
'}'
].join('\n')
// setup a GLSL program
const program = utils.createProgramFromSources(gl, [vertexSrc, fragmentSrc])
if (!program) {
return
}
gl.useProgram(program)
// look up where the vertex data needs to go.
const positionLocation = gl.getAttribLocation(program, 'a_position')
// Create a buffer and put a single clipspace rectangle in
// it (2 triangles)
const buffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
gl.bufferData(
gl.ARRAY_BUFFER,
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]),
gl.STATIC_DRAW)
gl.enableVertexAttribArray(positionLocation)
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0)
// draw
gl.drawArrays(gl.TRIANGLES, 0, 6)
utils.dumpBuffer(gl, width, height)
gl.destroy()
}
main()