Advertisement
Grodslukarn

Drawing simple texture in WebGL

Nov 21st, 2014
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Vertex shader program
  2. var VSHADER_SOURCE =
  3.     'attribute vec3 a_Position;\n' +
  4.     'attribute vec2 a_TexCoord;\n' +               
  5.     'varying vec2 v_TexCoord;\n' +
  6.     'void main() {\n' +
  7.     ' gl_Position = vec4(a_Position, 1.0);\n' +
  8.     ' v_TexCoord = a_TexCoord;\n' +
  9.     '}\n';
  10.  
  11. // Fragment shader program
  12. var FSHADER_SOURCE =
  13.     'precision highp float;\n' +
  14.     'uniform sampler2D u_Sampler;\n' +
  15.     'varying vec2 v_TexCoord;\n' +
  16.     'void main() {\n' +
  17.     ' gl_FragColor = texture2D(u_Sampler, vec2(v_TexCoord.s, v_TexCoord.t));\n' +
  18.     '}\n';
  19.  
  20. // Global gl variable
  21. var gl;
  22.  
  23. // Global shader variables
  24. var a_Position;
  25. var a_TexCoord;
  26.  
  27. // Global buffers
  28. var verticesBuffer;
  29. var triangleFacesBuffer;
  30. var textureCoordBuffer;
  31.  
  32. // Global texture
  33. var texture;
  34.  
  35.  
  36. function main() {          
  37.     // Get the rendering context for WebGL
  38.    
  39.     initializeWebGL();
  40.     initializeShaders();
  41.     initializeBuffers();
  42.    
  43.     //   Initialize html elements and set all events
  44.    
  45.     // Image selector
  46.     var imageSel = document.getElementById("textureSelector");
  47.     imageSel.onchange = function(){loadImage(imageSel)};
  48. }
  49.  
  50. function initializeWebGL() {
  51.     // Get the rendering context for WebGL
  52.     var canvas = document.getElementById("webglCanvas");
  53.     gl = canvas.getContext("webgl");
  54.     if (!gl) {
  55.         console.log('Failed to get the rendering context for WebGL');
  56.         return;
  57.     }
  58.    
  59.     // Clear and init canvas
  60.     gl.clearColor(0.0, 0.0, 0.0, 1.0);
  61.     gl.enable(gl.DEPTH_TEST);
  62.     gl.depthFunc(gl.LEQUAL);
  63.     gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  64. }
  65.  
  66. function initializeShaders() {
  67.     // Initialize shaders through cuon-utils.js
  68.     if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
  69.         console.log("Failed to initialize shaders.");
  70.         return null;
  71.     }
  72.    
  73.     // Fetch and enable shader attributes
  74.     a_Position = gl.getAttribLocation(gl.program, "a_Position");
  75.     gl.enableVertexAttribArray(a_Position);
  76.    
  77.     a_TexCoord = gl.getAttribLocation(gl.program, "a_TexCoord");
  78.     gl.enableVertexAttribArray(a_TexCoord);
  79. }
  80.  
  81. function initializeBuffers() {
  82.     // Vertex positional data
  83.     var vertices = [
  84.         1.0,  1.0,  //Top right corner
  85.         -1.0, 1.0,  //Top left corner
  86.         1.0,  -1.0, //Bottom right corner
  87.         -1.0, -1.0  //Bottom left corner
  88.     ];
  89.  
  90.     verticesBuffer = gl.createBuffer();
  91.     gl.bindBuffer(gl.ARRAY_BUFFER, verticesBuffer);
  92.     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
  93.    
  94.    
  95.     // Texture positional data         
  96.     var textureCoords = [
  97.         1.0,  1.0,  //Top right corner
  98.         0.0,  1.0   //Top left corner
  99.         1.0,  0.0,  //Bottom right corner
  100.         0.0,  0.0,  //Bottom left corner
  101.     ];
  102.  
  103.     textureCoordBuffer  = gl.createBuffer();
  104.     gl.bindBuffer(gl.ARRAY_BUFFER, textureCoordBuffer );
  105.     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
  106.    
  107.     var triangleFaces = [
  108.                 0, 1, 2
  109.                 1, 2, 3
  110.             ];
  111.  
  112.     triangleFacesBuffer  = gl.createBuffer();
  113.     gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleFacesBuffer );
  114.     gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(triangleFaces), gl.STATIC_DRAW);
  115. }
  116.  
  117. function render() {
  118.     // Clear canvas
  119.     gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);       
  120.    
  121.     // Activate and bind texture
  122.     gl.activeTexture(gl.TEXTURE0);
  123.     gl.bindTexture(gl.TEXTURE_2D, texture);
  124.     gl.uniform1i(gl.getUniformLocation(gl.program, "u_Sampler"), 0);
  125.    
  126.     // Bind and point vertex data buffer to a_Position
  127.     gl.bindBuffer(gl.ARRAY_BUFFER, verticesBuffer);
  128.     gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
  129.    
  130.     // UPDATED
  131.     gl.bindBuffer(gl.ARRAY_BUFFER, textureCoordBuffer );
  132.     gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, 0, 0);
  133.    
  134.     // Bind triangle faces data
  135.     gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleFacesBuffer);
  136.    
  137.     // Draw elements
  138.     gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
  139. }
  140.  
  141.  
  142. // Texture functions
  143.  
  144. function loadImage(imageSelector) {
  145.     texture = gl.createTexture();
  146.     var image = new Image();
  147.     image.onload = loadTexture(gl, image);
  148.     image.src = imageSelector.value;
  149. }
  150.  
  151. function loadTexture(gl, image) {
  152.     gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
  153.     gl.activeTexture(gl.TEXTURE0);
  154.     gl.bindTexture(gl.TEXTURE_2D, texture);
  155.     gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);
  156.     gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  157.     gl.bindTexture(gl.TEXTURE_2D, null);
  158.    
  159.     render();
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement