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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| var VSHADER_SOURCE = 'attribute vec4 a_Position;\n' + 'attribute vec4 a_Color;\n' + 'attribute vec4 a_Normal;\n' + 'uniform mat4 u_MvpMatrix;\n' + 'uniform mat4 u_ModelMatrix;\n' + 'uniform mat4 u_NormalMatrix;\n' + 'uniform vec3 u_LightColor;\n' + 'uniform vec3 u_LightPosition;\n' + 'uniform vec3 u_AmbientLight;\n' + 'varying vec4 v_Color;\n' + 'void main() {\n' + ' gl_Position = u_MvpMatrix * a_Position;\n' + ' vec3 normal = normalize(vec3(u_NormalMatrix * a_Normal));\n' + ' vec4 vertexPosition = u_ModelMatrix * a_Position;\n' + ' vec3 lightDirection = normalize(u_LightPosition - vec3(vertexPosition));\n' + ' float nDotL = max(dot(lightDirection, normal), 0.0);\n' + ' vec3 diffuse = u_LightColor * vec3(a_Color) * nDotL;\n' + ' vec3 ambient = u_AmbientLight * vec3(a_Color);\n' + ' v_Color = vec4(diffuse + ambient, a_Color.a);\n' + '}\n';
var FSHADER_SOURCE= '#ifdef GL_ES\n' + 'precision mediump float;\n' + '#endif\n' + 'varying vec4 v_Color;\n' + 'void main(){\n'+ ' gl_FragColor = v_Color;\n'+ '}\n';
function main() {
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; }
gl.enable(gl.DEPTH_TEST);
var u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix'); var u_MvpMatrix = gl.getUniformLocation(gl.program, 'u_MvpMatrix'); var u_NormalMatrix = gl.getUniformLocation(gl.program, 'u_NormalMatrix'); var u_LightColor = gl.getUniformLocation(gl.program, 'u_LightColor'); var u_LightPosition = gl.getUniformLocation(gl.program, 'u_LightPosition'); var u_AmbientLight = gl.getUniformLocation(gl.program, 'u_AmbientLight'); if (!u_MvpMatrix || !u_NormalMatrix || !u_LightColor || !u_LightPosition || !u_AmbientLight) { console.log('Failed to get the storage location'); return; }
gl.uniform3f(u_LightColor, 1.0, 1.0, 1.0); gl.uniform3f(u_LightPosition, 2.3, 4.0, 3.5); gl.uniform3f(u_AmbientLight, 0.2, 0.2, 0.2);
var modelMatrix = new Matrix4(); var mvpMatrix = new Matrix4(); var normalMatrix = new Matrix4();
modelMatrix.setRotate(90, 0, 1, 0); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements);
mvpMatrix.setPerspective(30, canvas.width/canvas.height, 1 ,100); mvpMatrix.lookAt(6, 6, 14, 0, 0, 0, 0, 1, 0); mvpMatrix.multiply(modelMatrix); gl.uniformMatrix4fv(u_MvpMatrix, false, mvpMatrix.elements);
normalMatrix.setInverseOf(modelMatrix); normalMatrix.transpose(); gl.uniformMatrix4fv(u_NormalMatrix, false, normalMatrix.elements);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.drawElements(gl.TRIANGLES, n, gl.UNSIGNED_BYTE, 0); }
function initVertexBuffers(gl) { var vertices = new Float32Array([ 2.0, 2.0, 2.0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,13,14, 12,14,15, 16,17,18, 16,18,19, 20,21,22, 20,22,23 ]);
if (!initArrayBuffer(gl, vertices, 3, gl.FLOAT, 'a_Position')) return -1;
if (!initArrayBuffer(gl, colors, 3, gl.FLOAT, 'a_Color')) return -1;
if (!initArrayBuffer(gl, normals, 3, gl.FLOAT, 'a_Normal')) return -1;
var indexBuffer = gl.createBuffer(); if (!indexBuffer) { console.log('Failed to create the buffer object'); return false; }
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
return indices.length; }
function initArrayBuffer(gl, data, num, type, attribute) { var buffer = gl.createBuffer(); if (!buffer) { console.log('Failed to create the buffer object'); return false; } gl.bindBuffer(gl.ARRAY_BUFFER, buffer); gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW); var a_attribute = gl.getAttribLocation(gl.program, attribute); if (a_attribute < 0) { console.log('Failed to get the storage location of ' + attribute); return false; } gl.vertexAttribPointer(a_attribute, num, type, false, 0, 0); gl.enableVertexAttribArray(a_attribute);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
return true; }
|