rrUtil.js (3867B)
1 /*------------------------------------------------------------------------- 2 * drawElements Quality Program OpenGL ES Utilities 3 * ------------------------------------------------ 4 * 5 * Copyright 2014 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 */ 20 21 'use strict'; 22 goog.provide('framework.referencerenderer.rrUtil'); 23 goog.require('framework.opengl.simplereference.sglrGLContext'); 24 goog.require('framework.opengl.simplereference.sglrReferenceContext'); 25 26 goog.scope(function() { 27 28 var rrUtil = framework.referencerenderer.rrUtil; 29 var sglrGLContext = framework.opengl.simplereference.sglrGLContext; 30 var sglrReferenceContext = framework.opengl.simplereference.sglrReferenceContext; 31 32 /** 33 * @param {sglrGLContext.GLContext | WebGL2RenderingContext | sglrReferenceContext.ReferenceContext} ctx 34 * @param {number|Object} program 35 * @param {Array<number>} p0 36 * @param {Array<number>} p1 37 */ 38 rrUtil.drawQuad = function(ctx, program, p0, p1) { 39 // Vertex data. 40 var hz = (p0[2] + p1[2]) * 0.5; 41 /** @type {Array<number>} */ var position = [ 42 p0[0], p0[1], p0[2], 1.0, 43 p0[0], p1[1], hz, 1.0, 44 p1[0], p0[1], hz, 1.0, 45 p1[0], p1[1], p1[2], 1.0 46 ]; 47 /** @type {Array<number>} */ var coord = [ 48 0.0, 0.0, 49 0.0, 1.0, 50 1.0, 0.0, 51 1.0, 1.0 52 ]; 53 /** @type {Array<number>} */ var indices = [0, 1, 2, 2, 1, 3]; 54 55 var posLoc = ctx.getAttribLocation(program, 'a_position'); 56 if (posLoc == -1) 57 throw new Error('a_position attribute is not defined.'); 58 59 var coordLoc = ctx.getAttribLocation(program, 'a_coord'); 60 var vaoID; 61 var bufIDs = []; 62 63 vaoID = ctx.createVertexArray(); 64 ctx.bindVertexArray(vaoID); 65 66 bufIDs[0] = ctx.createBuffer(); 67 bufIDs[1] = ctx.createBuffer(); 68 69 ctx.useProgram(program); 70 71 if (posLoc >= 0) { 72 ctx.bindBuffer(gl.ARRAY_BUFFER, bufIDs[0]); 73 ctx.bufferData(gl.ARRAY_BUFFER, new Float32Array(position), gl.STATIC_DRAW); 74 75 ctx.enableVertexAttribArray(posLoc); 76 ctx.vertexAttribPointer(posLoc, 4, gl.FLOAT, false, 0, 0); 77 78 ctx.bindBuffer(gl.ARRAY_BUFFER, null); 79 } 80 81 if (coordLoc >= 0) { 82 ctx.bindBuffer(gl.ARRAY_BUFFER, bufIDs[1]); 83 ctx.bufferData(gl.ARRAY_BUFFER, new Float32Array(coord), gl.STATIC_DRAW); 84 85 ctx.enableVertexAttribArray(coordLoc); 86 ctx.vertexAttribPointer(coordLoc, 2, gl.FLOAT, false, 0, 0); 87 88 ctx.bindBuffer(gl.ARRAY_BUFFER, null); 89 } 90 91 { 92 var ndxID = ctx.createBuffer(); 93 94 ctx.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ndxID); 95 ctx.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW); 96 97 ctx.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0); 98 99 ctx.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null); 100 ctx.deleteBuffer(ndxID); 101 } 102 103 ctx.bindVertexArray(null); 104 ctx.deleteBuffer(bufIDs[0]); 105 ctx.deleteBuffer(bufIDs[1]); 106 ctx.deleteVertexArray(vaoID); 107 108 if (posLoc >= 0) 109 ctx.disableVertexAttribArray(posLoc); 110 111 if (coordLoc >= 0) 112 ctx.disableVertexAttribArray(coordLoc); 113 }; 114 115 });