Advertisement
Guest User

Untitled

a guest
Sep 18th, 2013
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 7.90 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>3D Plane Map</title>
  4. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  5. <style type="text/css">
  6.  
  7. html, body, #canvas-main {
  8.     width: 100%;
  9.     height: 100%;
  10.     margin: 0px;
  11. }
  12.  
  13. </style>
  14. <script type="text/javascript" src="scripts/glMatrix.js"></script>
  15. <script type="text/javascript" src="scripts/webgl-utils.js"></script>
  16. <script id="shader-fs" type="x-shader/x-fragment">
  17.  
  18.     precision mediump float;
  19.     varying vec2 vTextureCoord;
  20.     uniform sampler2D uSampler;
  21.  
  22.     void main(void) {
  23.         gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
  24.     }
  25.    
  26. </script>
  27. <script id="shader-vs" type="x-shader/x-vertex">
  28.  
  29.     attribute vec3 aVertexPosition;
  30.     attribute vec2 aTextureCoord;
  31.     varying vec2 vTextureCoord;
  32.  
  33.     uniform mat4 uMVMatrix;
  34.     uniform mat4 uPMatrix;
  35.  
  36.     void main(void) {
  37.         gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
  38.         vTextureCoord = aTextureCoord;
  39.     }
  40.    
  41. </script>
  42. <script type="text/javascript">
  43.  
  44.     var gl;
  45.    
  46.     function initGL(canvas) {
  47.         try {
  48.             gl = canvas.getContext("experimental-webgl");
  49.             gl.viewportWidth = canvas.width;
  50.             gl.viewportHeight = canvas.height;
  51.         } catch (e) {
  52.         }
  53.         if (!gl) {
  54.             alert("Could not initialise WebGL, sorry :-(");
  55.         }
  56.     }
  57.  
  58.     function getShader(gl, id) {
  59.    
  60.         var shaderScript = document.getElementById(id);
  61.        
  62.         if (!shaderScript) {
  63.             return null;
  64.         }
  65.  
  66.         var str = "";
  67.         var k = shaderScript.firstChild;
  68.        
  69.         while (k) {
  70.             if (k.nodeType == 3) {
  71.                 str += k.textContent;
  72.             }
  73.             k = k.nextSibling;
  74.         }
  75.  
  76.         var shader;
  77.        
  78.         if (shaderScript.type == "x-shader/x-fragment") {
  79.             shader = gl.createShader(gl.FRAGMENT_SHADER);
  80.         } else if (shaderScript.type == "x-shader/x-vertex") {
  81.             shader = gl.createShader(gl.VERTEX_SHADER);
  82.         } else {
  83.             return null;
  84.         }
  85.  
  86.         gl.shaderSource(shader, str);
  87.         gl.compileShader(shader);
  88.  
  89.         if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  90.             alert(gl.getShaderInfoLog(shader));
  91.             return null;
  92.         }
  93.  
  94.         return shader;
  95.     }
  96.  
  97.  
  98.     var shaderProgram;
  99.  
  100.     function initShaders() {
  101.    
  102.         var fragmentShader = getShader(gl, "shader-fs");
  103.         var vertexShader = getShader(gl, "shader-vs");
  104.  
  105.         shaderProgram = gl.createProgram();
  106.         gl.attachShader(shaderProgram, vertexShader);
  107.         gl.attachShader(shaderProgram, fragmentShader);
  108.         gl.linkProgram(shaderProgram);
  109.  
  110.         if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
  111.             alert("Could not initialise shaders");
  112.         }
  113.  
  114.         gl.useProgram(shaderProgram);
  115.  
  116.         shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
  117.         gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
  118.        
  119.         shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
  120.         gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
  121.  
  122.         shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
  123.         shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
  124.         shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, "uSampler");
  125.     }
  126.  
  127.     function handleLoadedTexture(texture) {
  128.    
  129.         gl.bindTexture(gl.TEXTURE_2D, texture);
  130.         gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
  131.         gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
  132.         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  133.         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  134.         gl.bindTexture(gl.TEXTURE_2D, null);
  135.     }
  136.    
  137.     var myTexture;
  138.  
  139.     function initTexture() {
  140.    
  141.         myTexture = gl.createTexture();
  142.         myTexture.image = new Image();
  143.         myTexture.image.onload = function () {
  144.        
  145.             handleLoadedTexture(myTexture)
  146.         }
  147.  
  148.         myTexture.image.src = "moscow.jpg";
  149.     }
  150.    
  151.     var mvMatrix = mat4.create();
  152.     var pMatrix = mat4.create();
  153.     var mvMatrixStack = [];
  154.  
  155.     function mvPushMatrix() {
  156.    
  157.         var copy = mat4.create();
  158.         mat4.set(mvMatrix, copy);
  159.         mvMatrixStack.push(copy);
  160.     }
  161.  
  162.     function mvPopMatrix() {
  163.    
  164.         if (mvMatrixStack.length == 0) {
  165.             throw "Invalid popMatrix!";
  166.         }
  167.        
  168.         mvMatrix = mvMatrixStack.pop();
  169.     }
  170.    
  171.     function setMatrixUniforms() {
  172.    
  173.         gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
  174.         gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
  175.     }
  176.  
  177.     function degToRad(degrees) {
  178.    
  179.         return degrees * Math.PI / 180;
  180.     }
  181.  
  182.     var squareVertexPositionBuffer;
  183.     var squareVertexTextureCoordBuffer;
  184.     var squareVertexIndexBuffer;
  185.  
  186.     function initBuffers() {
  187.    
  188.         squareVertexPositionBuffer = gl.createBuffer();
  189.         gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
  190.         vertices = [
  191.              1.0,  1.0,  0.0,
  192.             -1.0,  1.0,  0.0,
  193.              1.0, -1.0,  0.0,
  194.             -1.0, -1.0,  0.0
  195.         ];
  196.        
  197.         gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
  198.         squareVertexPositionBuffer.itemSize = 3;
  199.         squareVertexPositionBuffer.numItems = 4;
  200.        
  201.         squareVertexTextureCoordBuffer = gl.createBuffer();
  202.         gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexTextureCoordBuffer);
  203.        
  204.         var textureCoords = [
  205.              0.0, 0.0,
  206.              1.0, 0.0,
  207.              1.0, 1.0,
  208.              0.0, 1.0,
  209.         ];
  210.        
  211.         gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
  212.         squareVertexTextureCoordBuffer.itemSize = 2;
  213.         squareVertexTextureCoordBuffer.numItems = 4;
  214.     }
  215.  
  216.    
  217.     var rSquare = 0;
  218.    
  219.     function drawScene() {
  220.    
  221.         gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
  222.         gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  223.  
  224.         mat4.perspective(45, gl.viewportWidth / gl.viewportHeight / 3, 0.1, 100.0, pMatrix);
  225.         mat4.identity(mvMatrix);
  226.         mat4.translate(mvMatrix, [0.0, 0.0, -5.0]);
  227.        
  228.         mvPushMatrix();
  229.         mat4.rotate(mvMatrix, degToRad(rSquare), [1, 0, 0]);
  230.        
  231.         gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
  232.         gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
  233.        
  234.         gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexTextureCoordBuffer);
  235.         gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, squareVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);
  236.  
  237.         gl.activeTexture(gl.TEXTURE0);
  238.         gl.bindTexture(gl.TEXTURE_2D, myTexture);
  239.         gl.uniform1i(shaderProgram.samplerUniform, 0);
  240.        
  241.         setMatrixUniforms();
  242.         gl.drawArrays(gl.TRIANGLE_STRIP, 0, squareVertexPositionBuffer.numItems);
  243.         mvPopMatrix();
  244.     }
  245.    
  246.  
  247.     var lastTime = 0;
  248.  
  249.     function animate() {
  250.    
  251.         var timeNow = new Date().getTime();
  252.        
  253.         if (lastTime != 0) {
  254.             var elapsed = timeNow - lastTime;
  255.             rSquare += (75 * elapsed) / 1000.0;
  256.         }
  257.        
  258.         lastTime = timeNow;
  259.     }
  260.  
  261.     function tick() {
  262.    
  263.         requestAnimFrame(tick);
  264.         drawScene();
  265.         animate();
  266.     }
  267.    
  268.     function webGLStart() {
  269.    
  270.         var canvas = document.getElementById("canvas-main");
  271.  
  272.         initGL(canvas);
  273.         initShaders();
  274.         initBuffers();
  275.         initTexture();
  276.  
  277.         gl.clearColor(0.0, 0.0, 0.0, 1.0);
  278.         gl.enable(gl.DEPTH_TEST);
  279.         drawScene();
  280.         tick();
  281.     }
  282.  
  283. </script>
  284. </head>
  285. <body onload="webGLStart();">
  286.     <canvas id="canvas-main" style="border: none; width: 100%; height: 100%;"></canvas>
  287. </body>
  288. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement