Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <!-- saved from url=(0082)https://sorbus.if.uj.edu.pl/~pbialas/3DComputerGraphics/Tutorial/WebGL/basics.html -->
- <html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Triangle</title>
- <script type="text/javascript" src="gl-matrix-min.js"></script>
- <style>
- body {
- margin: 100px;
- }
- </style>
- <script>
- /*
- This functions check if the WebGL context gl
- did throw any error and if so displays it's number.
- Probably not neccessery as it's is dislayed in console anyway ...
- */
- function checkError(gl, msg) {
- let info = gl.getError();
- if (info !== gl.NO_ERROR) {
- console.log("Error " + info + " : " + msg);
- }
- }
- /*
- Tak samo jak powy¿sza, ale zamiast wypisywaæ
- komunikat na konsoli wyœwietla go w okienku
- dialogowym
- */
- function alertError(gl, msg) {
- let info = gl.getError();
- if (info !== gl.NO_ERROR) {
- alert("Error " + info + ": " + msg);
- }
- }
- /*
- Tu nastêpuje seria pomocniczych funkcji, które
- u³atwiaj¹ napisanie programu WebGL. W ka¿dej z nich
- pierwszy parametr to WebGL context.
- */
- /*
- Funkcja kompiluje kod zawarty w parametrze 'src'
- do³aczaj¹c go do shadera 'shader'. W przypadku b³êdów kompilacji
- wyœwietla stosowny komunikat na konsoli.
- */
- function compileShader(gl, shader, src) {
- gl.shaderSource(shader, src);
- gl.compileShader(shader);
- //Check for compilation error and display them.
- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
- console.log(gl.getShaderInfoLog(shader));
- return null;
- }
- return shader;
- }
- /*
- G³ówna funkcja tworz¹ca program cieniuj¹cy.
- ¯ród³a szaderów s¹ przekazywane za pomoca obiektu 'src'
- jako jego pola 'vertex' i 'fragment'.
- */
- function makeProgram(gl, src) {
- let vshader = gl.createShader(gl.VERTEX_SHADER);
- let fshader = gl.createShader(gl.FRAGMENT_SHADER);
- compileShader(gl, vshader, src.vertex);
- compileShader(gl, fshader, src.fragment);
- let program = gl.createProgram();
- gl.attachShader(program, vshader);
- gl.attachShader(program, fshader);
- gl.linkProgram(program);
- let info = gl.getProgramParameter(program, gl.LINK_STATUS);
- if (!info) {
- console.log("Could not link shaders\n" + info);
- }
- return program;
- }
- /*
- Function creating an ARRAY BUFFER and loading data into it.
- */
- function initBuffer(gl, data) {
- let buffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);
- gl.bindBuffer(gl.ARRAY_BUFFER, null);
- return buffer;
- }
- </script>
- </head>
- <body onload="onLoad()">
- <canvas id="canvas" width="400px" height="400px" style="border: solid thin black;"></canvas>
- <script>
- /* Those two variables contain
- the source coe for both shaders.
- */
- const vertex_src = `
- attribute vec4 aVertexPosition;
- attribute vec4 aVertexColor;
- attribute vec2 a_texcoord; // TEXTURE
- uniform mat4 uModelViewMatrix;
- uniform mat4 uProjectionMatrix;
- varying lowp vec4 vColor;
- varying vec2 v_texcoord; // TEXTURE
- void main() {
- vColor = aVertexColor;
- gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
- v_texcoord = a_texcoord; // TEXTURE
- }
- `;
- const fragment_src = `
- precision mediump float; // TEXTURE
- varying lowp vec4 vColor;
- varying vec2 v_texcoord; // TEXTURE
- // The texture.
- uniform sampler2D u_texture; // TEXTURE
- void main() {
- //gl_FragColor = vColor;
- gl_FragColor = texture2D(u_texture, v_texcoord); // TEXTURE
- }
- `;
- const PyramidCoordinates = [
- 0.0,0.0,0.0,
- 0.0,1.0,0.0,
- 1.0,0.0,0.0,
- 1.0,0.0,0.0,
- 1.0,1.0,0.0,
- 0.0,1.0,0.0,
- 0.0,0.0,0.0,
- 1.0,0.0,0.0,
- 0.5,0.5,1.0,
- 0.0,0.0,0.0,
- 0.0,1.0,0.0,
- 0.5,0.5,1.0,
- 0.0,1.0,0.0,
- 1.0,1.0,0.0,
- 0.5,0.5,1.0,
- 1.0,1.0,0.0,
- 1.0,0.0,0.0,
- 0.5,0.5,1.0];
- const VertexColors = [
- 0.5, 0.0, 0.0,
- 0.5, 0.0, 0.0,
- 0.5, 0.0, 0.0,
- 0.5, 0.0, 0.0,
- 0.5, 0.0, 0.0,
- 0.5, 0.0, 0.0,
- 0.0, 1.0, 0.0,
- 0.0, 1.0, 0.0,
- 0.0, 1.0, 0.0,
- 0.0, 0.0, 1.0,
- 0.0, 0.0, 1.0,
- 0.0, 0.0, 1.0,
- 1.0, 0.0, 0.0,
- 1.0, 0.0, 0.0,
- 1.0, 0.0, 0.0,
- 0.0, 0.0, 0.0,
- 0.0, 0.0, 0.0,
- 0.0, 0.0, 0.0];
- const textureCoordinates = [
- // Front
- 0.0, 0.0,
- 1.0, 0.0,
- 1.0, 1.0,
- 0.0, 1.0,
- // Back
- 0.0, 0.0,
- 1.0, 0.0,
- 1.0, 1.0,
- 0.0, 1.0,
- // Top
- 0.0, 0.0,
- 1.0, 0.0,
- 1.0, 1.0,
- 0.0, 1.0,
- // Bottom
- 0.0, 0.0,
- 1.0, 0.0,
- 1.0, 1.0,
- 0.0, 1.0,
- // Right
- 0.0, 0.0,
- 1.0, 0.0,
- 1.0, 1.0,
- 0.0, 1.0
- ];
- function prepareTexture(gl,texcoordLocation) {
- var buffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
- gl.enableVertexAttribArray(texcoordLocation);
- gl.vertexAttribPointer(texcoordLocation, 2, gl.FLOAT, false, 0, 0);
- gl.bufferData(
- gl.ARRAY_BUFFER,
- new Float32Array(textureCoordinates),
- gl.STATIC_DRAW);
- var texture = gl.createTexture();
- gl.bindTexture(gl.TEXTURE_2D, texture);
- // Fill the texture with a 1x1 blue pixel.
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE,
- new Uint8Array([0, 0, 255, 255]));
- // Asynchronously load an image
- var image = new Image();
- image.src = "f-texture.png";
- image.addEventListener('load', function() {
- // Now that the image has loaded make copy it to the texture.
- gl.bindTexture(gl.TEXTURE_2D, texture);
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA,gl.UNSIGNED_BYTE, image);
- gl.generateMipmap(gl.TEXTURE_2D);
- //alert("Texture loaded");
- });
- }
- function onLoad() {
- let canvas = document.getElementById("canvas");
- if (!canvas) alert("cannot access canvas");
- let gl = canvas.getContext("webgl");
- if (!gl) alert("cannot get WebGL context");
- gl.viewport(0, 0, canvas.width, canvas.height);
- let program = makeProgram(gl, {vertex: vertex_src, fragment: fragment_src});
- let colorBuffer = initBuffer(gl, VertexColors);
- let triangleVertexBuffer = initBuffer(gl, PyramidCoordinates);
- var texcoordLocation = gl.getAttribLocation(program, "a_texcoord"); //TEXTURE
- prepareTexture(gl, texcoordLocation); // TEXTURE
- // Set's the background color
- gl.clearColor(0.75, 0.75, 0.75, 1.0);
- // Enables the depth test.
- gl.enable(gl.DEPTH_TEST);
- //Clear canvas (fill with background color) and depth buffer.
- gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
- gl.useProgram(program);
- var start = null;
- function draw(timestamp)
- {
- if (!start) start = timestamp;
- var progress = timestamp - start;
- const fieldOfView = 45 * Math.PI / 180; // in radians
- const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
- const zNear = 0.1;
- const zFar = 100.0;
- const projectionMatrix = mat4.create();
- const viewMatrix = mat4.create();
- // note: glmatrix.js always has the first argument
- // as the destination to receive the result.
- mat4.perspective(projectionMatrix,
- fieldOfView,
- aspect,
- zNear,
- zFar);
- mat4.lookAt(viewMatrix, [30.0-progress/1000, 5.0, 5.0],[0.0-progress/1000, 1.0, 1.0], [0,0,1]);
- gl.uniformMatrix4fv(
- gl.getUniformLocation(program, 'uProjectionMatrix'),
- false,
- projectionMatrix);
- gl.uniformMatrix4fv(
- gl.getUniformLocation(program, 'uModelViewMatrix'),
- false,
- viewMatrix);
- // draw 100 triangles
- for (var j = 0; j < 10; j++) {
- for(var i = 0; i < 10; i++) {
- let T = mat4.create();
- mat4.translate(T,T,[2*j,2*i,0])
- mat4.mul(T,viewMatrix,T)
- gl.uniformMatrix4fv(gl.getUniformLocation(program,'uModelViewMatrix'), false , T);
- drawPyramid(gl, program, triangleVertexBuffer, colorBuffer);
- let R = mat4.create();
- let T2 = mat4.create();
- mat4.rotate(R, R, Math.PI, [2,0,0]);
- mat4.mul(R, T, R)
- mat4.translate(T2,T2,[0,-2,0])
- mat4.mul(T2,T2,R)
- gl.uniformMatrix4fv(gl.getUniformLocation(program,'uModelViewMatrix'), false , T2);
- drawPyramid(gl, program, triangleVertexBuffer, colorBuffer);
- }
- }
- if (progress < 10000) {
- window.requestAnimationFrame(draw);
- }
- }
- window.requestAnimationFrame(draw);
- gl.bindBuffer(gl.ARRAY_BUFFER, null);
- gl.disableVertexAttribArray(gl.getAttribLocation(program, 'aVertexPosition'));
- gl.disableVertexAttribArray(gl.getAttribLocation(program, 'aVertexColor'));
- }
- function drawPyramid(gl, program, triangleVertexBuffer, colorBuffer) {
- gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexBuffer);
- gl.vertexAttribPointer(gl.getAttribLocation(program, 'aVertexPosition'), 3, gl.FLOAT, false, 0, 0);
- gl.enableVertexAttribArray(gl.getAttribLocation(program, 'aVertexPosition'));
- gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
- gl.vertexAttribPointer(gl.getAttribLocation(program, 'aVertexColor'), 3, gl.FLOAT, false, 0, 0);
- gl.enableVertexAttribArray(gl.getAttribLocation(program, 'aVertexColor'));
- gl.drawArrays(gl.TRIANGLES, 0, 18);
- }
- </script>
- </body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement