var a_Position = gl.getAttribLocation(gl.program, 'a_Position'); if(a_Position < 0){ console.log("Failed to get the storage location of a_Position"); return -1; }
// 变换矩阵旋转 var VSHADER_SOURCE = //x' = x cos b - y sin b //y' = x sin b + y cosb //z' = z 'attribute vec4 a_Position;\n' + 'uniform mat4 u_xformMatrix;\n' + 'void main() {\n' + 'gl_Position = u_xformMatrix * a_Position;\n' + '}\n';
var canvas = document.getElementById("webgl"); if(!canvas){ console.log("Failed to retrieve the <canvas> element"); return; }
var gl = getWebGLContext(canvas); if(!gl){ console.log("Failed to get the rendering context for WebGL"); return; }
if(!initShaders(gl,VSHADER_SOURCE,FSHADER_SOURCE)){ console.log("Failed to initialize shaders."); return; }
//设置顶点位置 var n = initVertexBuffers(gl); if (n < 0) { console.log('Failed to set the positions of the vertices'); return; } // 为旋转矩阵创建Matrix4对象 var xformMatrix = new Matrix4(); // 将xformMatrix设置为旋转矩阵 xformMatrix.setRotate(ANGLE, 0, 0, 1);
var u_xformMatrix = gl.getUniformLocation(gl.program, 'u_xformMatrix'); if(u_xformMatrix < 0){ console.log("Failed to get the storage location of u_xformMatrix"); return; } // 将旋转矩阵传输给顶点着色器 gl.uniformMatrix4fv(u_xformMatrix, false, xformMatrix.elements);