6 Copyright (c) 2019 The Khronos Group Inc.
7 Use of this source code is governed by an MIT-style license that can be
8 found in the LICENSE.txt file.
10 <link rel=
"stylesheet" type=
"text/css" href=
"../unit.css" />
11 <script type=
"application/javascript" src=
"../unit.js"></script>
12 <script type=
"application/javascript" src=
"../util.js"></script>
13 <script type=
"application/javascript">
15 var verts = [
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0];
16 var normals = [
0.0,
0.0,
1.0,
0.0,
0.0,
1.0,
0.0,
0.0,
1.0];
17 var texcoords = [
0.0,
0.0,
1.0,
0.0,
0.0,
1.0];
20 Tests.startUnit = function () {
21 var canvas = document.getElementById('gl');
22 var gl = wrapGLContext(getGLContext(canvas));
23 var prog = new Shader(gl, 'vert', 'frag');
25 var sh = prog.shader.program;
26 var v = gl.getAttribLocation(sh, 'Vertex');
27 var n = gl.getAttribLocation(sh, 'Normal');
28 var t = gl.getAttribLocation(sh, 'Tex');
29 return [gl,prog,v,n,t];
32 Tests.setup = function(gl, prog, v,n,t) {
33 assert(
0 == gl.getError());
34 return [gl, prog, v,n,t];
36 Tests.teardown = function(gl, prog, v,n,t) {
37 gl.disableVertexAttribArray(v);
38 gl.disableVertexAttribArray(n);
39 gl.disableVertexAttribArray(t);
42 Tests.endUnit = function(gl, prog, v,n,t) {
46 Tests.testDrawElementsVBO = function(gl, prog, v,n,t) {
48 {size:
3, data:Quad.vertices},
49 {elements:true, data:Quad.indices});
51 assert(gl.NO_ERROR == checkError(gl,
"vbo.draw"));
52 assertOk(function(){gl.drawElements(gl.TRIANGLES,
5, gl.UNSIGNED_SHORT,
1*
2);});
53 assertOk(function(){gl.drawElements(gl.TRIANGLES,
6, gl.UNSIGNED_SHORT,
0*
2);});
54 assertOk(function(){gl.drawElements(gl.TRIANGLES,
0, gl.UNSIGNED_SHORT,
2*
1);});
55 assertOk(function(){gl.drawElements(gl.TRIANGLES,
1, gl.UNSIGNED_SHORT,
5*
2);});
57 assert(gl.NO_ERROR == checkError(gl,
"vbo.destroy"));
60 Tests.testDrawElementsVBOMulti = function(gl, prog, v,n,t) {
61 // creates VBOs for the quad arrays, binds them with
62 // vertexAttribPointer and calls drawElements
64 {size:
3, data:Quad.vertices},
65 {size:
3, data:Quad.normals},
66 {size:
2, data:Quad.texcoords},
67 {elements:true, data:Quad.indices});
69 assert(gl.NO_ERROR == checkError(gl,
"vbo.draw"));
70 assertOk(function(){gl.drawElements(gl.TRIANGLES,
5, gl.UNSIGNED_SHORT,
1*
2);});
71 assertOk(function(){gl.drawElements(gl.TRIANGLES,
0, gl.UNSIGNED_SHORT,
2*
2);});
72 assertOk(function(){gl.drawElements(gl.TRIANGLES,
6, gl.UNSIGNED_SHORT,
0*
2);});
73 assertOk(function(){gl.drawElements(gl.TRIANGLES,
1, gl.UNSIGNED_SHORT,
5*
2);});
74 assertGLError(gl, gl.INVALID_OPERATION,
"count + offset out of range",
75 function(){gl.drawElements(gl.TRIANGLES,
1, gl.UNSIGNED_SHORT,
6*
2);});
76 assertGLError(gl, gl.INVALID_OPERATION,
"count + offset out of range 2",
77 function(){gl.drawElements(gl.TRIANGLES,
6, gl.UNSIGNED_SHORT,
1*
2);});
78 gl.bindBuffer(gl.ARRAY_BUFFER, null);
79 gl.bindBuffer(gl.ARRAY_BUFFER, vbo.vbos[
1]);
80 gl.vertexAttribPointer(n,
3, gl.FLOAT, false,
0,
0);
81 assertOk(function(){gl.drawElements(gl.TRIANGLES,
5, gl.UNSIGNED_SHORT,
1*
2);});
82 assertOk(function(){gl.drawElements(gl.TRIANGLES,
0, gl.UNSIGNED_SHORT,
2*
2);});
83 assertOk(function(){gl.drawElements(gl.TRIANGLES,
6, gl.UNSIGNED_SHORT,
0*
2);});
84 assertOk(function(){gl.drawElements(gl.TRIANGLES,
1, gl.UNSIGNED_SHORT,
5*
2);});
85 assertGLError(gl, gl.INVALID_OPERATION,
"count + offset out of range 3",
86 function(){gl.drawElements(gl.TRIANGLES,
1, gl.UNSIGNED_SHORT,
6*
2);});
87 assertGLError(gl, gl.INVALID_OPERATION,
"count + offset out of range 4",
88 function(){gl.drawElements(gl.TRIANGLES,
6, gl.UNSIGNED_SHORT,
1*
2);});
90 assert(gl.NO_ERROR == checkError(gl,
"vbo.destroy"));
95 <script id=
"vert" type=
"x-shader/x-vertex">
96 attribute vec3 Vertex;
97 attribute vec3 Normal;
100 varying vec4 texCoord0;
103 gl_Position = vec4(Vertex * Normal,
1.0);
104 texCoord0 = vec4(Tex,
0.0,
0.0) + gl_Position;
107 <script id=
"frag" type=
"x-shader/x-fragment">
108 precision mediump float;
110 varying vec4 texCoord0;
119 <style>canvas{ position:absolute; }
</style>
121 <canvas id=
"gl" width=
"1" height=
"1"></canvas>