Professional Documents
Culture Documents
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
var vertexShaderSource =
void main() {
gl_Position = u_Ry*u_Rx*vec4(a_coords, 1.0);
v_color = a_colors;
}`;
var fragmentShaderSource =
var gl;
var a_coords_location;
var a_coords_buffer;
var a_colors_location;
var a_colors_buffer;
var u_click_location;
var bufferInd;
var u_matrix_rotateX_location;
var u_matrix_rotateY_location;
var cl = 0.1;
function draw() {
// Top face
-0.5, 0.5, -0.5,//8
-0.5, 0.5, 0.5,//9
0.5, 0.5, 0.5,//10
0.5, 0.5, -0.5,//11
// Bottom face
-0.5, -0.5, -0.5,//12
0.5, -0.5, -0.5,//13
0.5, -0.5, 0.5,//14
-0.5, -0.5, 0.5,//15
// Right face
0.5, -0.5, -0.5,//16
0.5, 0.5, -0.5,//17
0.5, 0.5, 0.5,//18
0.5, -0.5, 0.5,//19
// Left face
-0.5, -0.5, -0.5,//20
-0.5, -0.5, 0.5,//21
-0.5, 0.5, 0.5,//22
-0.5, 0.5, -0.5]);//23
gl.bindBuffer(gl.ARRAY_BUFFER, a_coords_buffer);
gl.bufferData(gl.ARRAY_BUFFER, coords, gl.STATIC_DRAW);
gl.vertexAttribPointer(a_coords_location, 3, gl.FLOAT, false, 0,
0);
gl.enableVertexAttribArray(a_coords_location);
gl.bindBuffer(gl.ARRAY_BUFFER, a_colors_buffer);
gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
gl.vertexAttribPointer(a_colors_location, 3, gl.FLOAT, false, 0,
0);
gl.enableVertexAttribArray(a_colors_location);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufferInd);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
gl.uniformMatrix4fv(u_matrix_rotateX_location, false, rotateMatX);
gl.uniformMatrix4fv(u_matrix_rotateY_location, false, rotateMatY);
gl.uniform1f(u_click_location, cl);
return prog;
}
function initGL() {
var prog = createProgram(gl, vertexShaderSource,
fragmentShaderSource);
gl.useProgram(prog);
bufferInd = gl.createBuffer();
}
function click(gl) {
gl.clearColor(0.0, 0.0, 0.0, 1); // glClear will fill the canvas
with a new color
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
cl = cl + 0.1;
console.log(cl);
gl.uniform1f(u_click_location, cl);
gl.drawElements(gl.TRIANGLES, 3 * 12, gl.UNSIGNED_BYTE, 0);
}
function init() {
var canvas = document.getElementById("webglcanvas");
gl = canvas.getContext("webgl");
initGL();
draw();
canvas.onmousedown = function (ev) {
click(gl);
};
}
init();
</script>
</html>