Skip to content

Commit b106f1f

Browse files
committed
test depth buffer
1 parent ab56afe commit b106f1f

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

test/depth-buffer.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict'
2+
3+
var tape = require('tape')
4+
var createContext = require('../index')
5+
var drawTriangle = require('./util/draw-triangle')
6+
var makeShader = require('./util/make-program')
7+
8+
tape('depth-buffer', function (t) {
9+
var width = 50
10+
var height = 50
11+
var gl = createContext(width, height)
12+
13+
var vertex_src = [
14+
'attribute vec2 position;',
15+
'uniform float depth;',
16+
'void main() { gl_Position = vec4(position,depth,1); }'
17+
].join('\n')
18+
19+
var fragment_src = [
20+
'precision mediump float;',
21+
'uniform vec4 color;',
22+
'void main() { gl_FragColor = color; }'
23+
].join('\n')
24+
25+
gl.clearColor(0, 0, 0, 0)
26+
gl.clearDepth(1)
27+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
28+
29+
gl.enable(gl.DEPTH_TEST)
30+
gl.depthFunc(gl.NOTEQUAL)
31+
32+
var program = makeShader(gl, vertex_src, fragment_src)
33+
gl.useProgram(program)
34+
gl.uniform1f(gl.getUniformLocation(program, 'depth'), 0)
35+
gl.uniform4f(gl.getUniformLocation(program, 'color'), 1, 0, 0, 1)
36+
drawTriangle(gl)
37+
gl.uniform1f(gl.getUniformLocation(program, 'depth'), 1)
38+
gl.uniform4f(gl.getUniformLocation(program, 'color'), 0, 1, 0, 1)
39+
drawTriangle(gl)
40+
41+
var pixels = new Uint8Array(width * height * 4)
42+
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
43+
for (var i = 0; i < width * height * 4; i += 4) {
44+
t.equals(pixels[i], 0, 'red')
45+
t.equals(pixels[i + 1], 255, 'green')
46+
t.equals(pixels[i + 2], 0, 'blue')
47+
t.equals(pixels[i + 3], 255, 'alpha')
48+
}
49+
50+
t.end()
51+
})

0 commit comments

Comments
 (0)